From 2920257e59684bdbb18e770ca4baa40aa5e2bff1 Mon Sep 17 00:00:00 2001 From: Andy Kwok Date: Wed, 27 Nov 2024 12:54:01 -0800 Subject: [PATCH] Update test infra Signed-off-by: Andy Kwok --- client/build.gradle | 7 ++--- .../action/IpEnrichmentRequest.java | 10 ++++--- ...ava => IpEnrichmentActionClientTests.java} | 8 ++++- ...est.java => IpEnrichmentRequestTests.java} | 30 +++++++++++++------ ...st.java => IpEnrichmentResponseTests.java} | 2 +- 5 files changed, 38 insertions(+), 19 deletions(-) rename client/src/test/java/org/opensearch/geospatial/action/{IpEnrichmentActionClientTest.java => IpEnrichmentActionClientTests.java} (86%) rename client/src/test/java/org/opensearch/geospatial/action/{IpEnrichmentRequestTest.java => IpEnrichmentRequestTests.java} (63%) rename client/src/test/java/org/opensearch/geospatial/action/{IpEnrichmentResponseTest.java => IpEnrichmentResponseTests.java} (94%) diff --git a/client/build.gradle b/client/build.gradle index 3b00b093..464dc0e5 100644 --- a/client/build.gradle +++ b/client/build.gradle @@ -32,6 +32,9 @@ dependencies { testImplementation "junit:junit:${versions.junit}" testImplementation "org.mockito:mockito-core:${versions.mockito}" + testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" + testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}" + testImplementation "net.bytebuddy:byte-buddy-agent:${versions.bytebuddy}" testImplementation "${group}:opensearch:${opensearch_version}" } @@ -59,7 +62,3 @@ publishing { mavenLocal() } } - -test { - useJUnitPlatform() -} \ No newline at end of file diff --git a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java index ed8a8f94..4ad92392 100644 --- a/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java +++ b/client/src/main/java/org/opensearch/geospatial/action/IpEnrichmentRequest.java @@ -52,12 +52,14 @@ public IpEnrichmentRequest(StreamInput streamInput) throws IOException { */ @Override public ActionRequestValidationException validate() { - ActionRequestValidationException errors = null; + ActionRequestValidationException errors = new ActionRequestValidationException(); if (ipString == null) { - errors = new ActionRequestValidationException(); errors.addValidationError("ip string should not be null"); } - return errors; + if (datasourceName == null) { + errors.addValidationError("DateSource should not be null"); + } + return errors.validationErrors().isEmpty() ? null : errors; } /** @@ -69,7 +71,7 @@ public ActionRequestValidationException validate() { public void writeTo(StreamOutput out) throws IOException { super.writeTo(out); out.writeString(ipString); - out.writeOptionalString(datasourceName); + out.writeString(datasourceName); } /** diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java similarity index 86% rename from client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java rename to client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java index 458b9f34..73016c18 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentActionClientTests.java @@ -13,15 +13,21 @@ import java.util.concurrent.ExecutionException; import org.junit.Assert; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.junit.MockitoJUnitRunner; +import org.opensearch.action.support.PlainActionFuture; import org.opensearch.client.node.NodeClient; import org.opensearch.common.action.ActionFuture; import org.opensearch.core.action.ActionResponse; import lombok.SneakyThrows; -public class IpEnrichmentActionClientTest { +@RunWith(MockitoJUnitRunner.class) +public class IpEnrichmentActionClientTests { @Mock private NodeClient mockNodeClient; diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java similarity index 63% rename from client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java rename to client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java index 5a2486f2..63740f9a 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentRequestTests.java @@ -11,40 +11,52 @@ /** * Test cases for IpEnrichmentRequest. */ -public class IpEnrichmentRequestTest { +public class IpEnrichmentRequestTests { /** * Test validate() against a valid record. */ @Test public void testValidateValidRequest() { + System.out.println("Test"); IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", "ValidDataSourceName"); Assert.assertNull(request.validate()); } /** - * Test validate() against a valid record, - * no error expected, because dataSourceName is optional. + * Test validate() against an invalid record, + * Expecting an error being thrown as dataSource being null. */ @Test public void testValidateNullDataSourceName() { IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null); - Assert.assertNull(request.validate()); + Assert.assertEquals(1, request.validate().validationErrors().size()); } + /** - * Test validate() against a valid record, - * no error expected, because dataSourceName is optional. + * Test validate() against an invalid record, + * Expecting an error being thrown as ipString being null. + */ + @Test + public void testValidateNullIpString() { + IpEnrichmentRequest request = new IpEnrichmentRequest(null, "dataSource"); + Assert.assertEquals(1, request.validate().validationErrors().size()); + } + + + /** + * Test validate() against an invalid record, + * Expecting an error with size in 2, because both fields are null. */ @Test public void testValidateNullIpStringAndDataSourceName() { IpEnrichmentRequest request = new IpEnrichmentRequest(null, null); - Assert.assertEquals(1, request.validate().validationErrors().size()); + Assert.assertEquals(2, request.validate().validationErrors().size()); } /** - * Test validate() against a valid record, - * no error expected, because dataSourceName is optional. + * Test fromActionRequest( ) to make sure the serialisation works. */ @Test public void testFromActionRequestOnValidRecord() { diff --git a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTests.java similarity index 94% rename from client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java rename to client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTests.java index 6bb88c9b..103b37af 100644 --- a/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTest.java +++ b/client/src/test/java/org/opensearch/geospatial/action/IpEnrichmentResponseTests.java @@ -10,7 +10,7 @@ import org.junit.Assert; import org.junit.Test; -public class IpEnrichmentResponseTest { +public class IpEnrichmentResponseTests { /** * To simulate when Response class being passed from one plugin to the other.