Skip to content

Commit

Permalink
Add test-cases
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 22, 2024
1 parent cb60df6 commit b4181aa
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 20 deletions.
3 changes: 3 additions & 0 deletions client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ dependencies {

testImplementation 'org.junit.jupiter:junit-jupiter:5.11.3'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.3'
testImplementation 'org.mockito:mockito-core:5.14.2'
testImplementation 'org.mockito:mockito-junit-jupiter:5.14.2'

testImplementation "${group}:opensearch:${opensearch_version}"

}

publishing {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.geospatial.action;

import lombok.SneakyThrows;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opensearch.client.node.NodeClient;
import org.opensearch.common.action.ActionFuture;
import org.opensearch.core.action.ActionResponse;

import java.util.Map;
import java.util.concurrent.ExecutionException;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
class IpEnrichmentActionClientTest {

// Test with happy path

// Test with exception path

@Mock
private NodeClient mockNodeClient;

@Mock
private ActionFuture<ActionResponse> mockResult;

@SneakyThrows
@Test
void testWithValidResponse() {
Map<String, Object> dummyPayload = Map.of("k1", "v1");
String dummyIpString = "192.168.1.1";
when(mockResult.get()).thenReturn(new IpEnrichmentResponse(dummyPayload));
when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult);

IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient);
Map<String, Object> actualPayload = ipClient.getGeoLocationData(dummyIpString);
Assertions.assertEquals(dummyPayload, actualPayload);
}

@SneakyThrows
@Test
void testWithException() {
Map<String, Object> dummyPayload = Map.of("k1", "v1");
String dummyIpString = "192.168.1.1";
when(mockResult.get()).thenThrow(new ExecutionException(new Throwable()));
when(mockNodeClient.execute(eq(IpEnrichmentAction.INSTANCE), any())).thenReturn(mockResult);

IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient);
Assertions.assertNull(ipClient.getGeoLocationData(dummyIpString));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/**
* Test cases for IpEnrichmentRequest.
*/
public class IpEnrichmentRequestTest {


// Test Validate
// Success
// Missing IP String
// Missing DataSource is ok


/**
* Test validate() against a valid record.
*/
Expand Down Expand Up @@ -51,20 +47,21 @@ public void testValidateNullIpStringAndDataSourceName() {
}


// Test StreamInput
// Successful case
// Junk input
// Partial missing output


// Test WriteTo
// Write a valid record
// Write with some junk data
/**
* Test validate() against a valid record,
* no error expected, because dataSourceName is optional.
*/
@Test
public void testFromActionRequestOnValidRecord() {
String ipString = "192.168.1.1";
String dsName = "demo";
IpEnrichmentRequest request = new IpEnrichmentRequest(
ipString, dsName);

IpEnrichmentRequest requestAfterStream = IpEnrichmentRequest.fromActionRequest(request);

// Test FromActionRequest
// Test valid record
// Test non IPEnrichment request
// Test with some junk.
Assertions.assertEquals(request.getIpString(), requestAfterStream.getIpString());
Assertions.assertEquals(request.getDatasourceName(), requestAfterStream.getDatasourceName());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.geospatial.action;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Map;

class IpEnrichmentResponseTest {

/**
* To simulate when Response class being passed from one plugin to the other.
*/
@Test
void testFromActionResponseWithValidPayload() {

Map<String, Object> payload = Map.of("k1", "v1");
IpEnrichmentResponse response = new IpEnrichmentResponse(payload);
IpEnrichmentResponse castedResponse = IpEnrichmentResponse.fromActionResponse(response);
Assertions.assertEquals(response.getGeoLocationData(), castedResponse.getGeoLocationData());
}
}

0 comments on commit b4181aa

Please sign in to comment.