Skip to content

Commit

Permalink
Update test infra
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Kwok <[email protected]>
  • Loading branch information
andy-k-improving committed Nov 27, 2024
1 parent 592368b commit 2920257
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 19 deletions.
7 changes: 3 additions & 4 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

}
Expand Down Expand Up @@ -59,7 +62,3 @@ publishing {
mavenLocal()
}
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2920257

Please sign in to comment.