Skip to content

Commit

Permalink
Spotless
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 26, 2024
1 parent 9725a41 commit 00dd378
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 60 deletions.
14 changes: 13 additions & 1 deletion client/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

//apply plugin: 'java'
apply plugin: 'opensearch.build'
apply plugin: 'io.freefair.lombok'
apply plugin: "com.diffplug.spotless"

allprojects {
group = opensearch_group
Expand Down Expand Up @@ -36,9 +36,21 @@ dependencies {

}



licenseFile = "LICENSE.txt"
noticeFile = "NOTICE.txt"

spotless {
java {
removeUnusedImports()
importOrder 'java', 'javax', 'org', 'com'
eclipse().configFile rootProject.file('formatterConfig.xml')
trimTrailingWhitespace()
endWithNewline()
}
}

publishing {
publications {
mavenJava(MavenPublication) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

package org.opensearch.geospatial.action;

import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import java.util.Map;

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.Optional;
import java.util.concurrent.ExecutionException;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;

/**
* Facade to provide GeoLocation enrichment for other plugins.
Expand All @@ -22,7 +21,7 @@
@AllArgsConstructor
public class IpEnrichmentActionClient {

NodeClient nodeClient;
final private NodeClient nodeClient;

/**
* IpEnrichment with default datasource.
Expand All @@ -39,10 +38,12 @@ public Map<String, Object> getGeoLocationData (String ipString) {
* @param datasourceName datasourceName in String form.
* @return A map instance which contain GeoLocation data for the given Ip address.
*/
public Map<String, Object> getGeoLocationData (String ipString, String datasourceName) {
public Map<String, Object> getGeoLocationData(String ipString, String datasourceName) {
// Composite the request object.
ActionFuture<ActionResponse> responseActionFuture = nodeClient.execute(
IpEnrichmentAction.INSTANCE, new IpEnrichmentRequest(ipString, datasourceName));
IpEnrichmentAction.INSTANCE,
new IpEnrichmentRequest(ipString, datasourceName)
);
// Send out the request and process the response.
try {
ActionResponse genericActionResponse = responseActionFuture.get();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,22 @@

package org.opensearch.geospatial.action;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.ActionRequestValidationException;
import org.opensearch.core.common.io.stream.InputStreamStreamInput;
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

/**
* Wrapper for the IP 2 GeoLocation action request.
Expand All @@ -41,7 +42,7 @@ public class IpEnrichmentRequest extends ActionRequest {
public IpEnrichmentRequest(StreamInput streamInput) throws IOException {
super(streamInput);
ipString = streamInput.readString();
datasourceName= streamInput.readOptionalString();
datasourceName = streamInput.readOptionalString();
log.trace("Constructing IP Enrichment request with values: [{}, {}]", ipString, datasourceName);
}

Expand Down Expand Up @@ -83,11 +84,9 @@ public static IpEnrichmentRequest fromActionRequest(ActionRequest actionRequest)
}

// Or else convert it
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
actionRequest.writeTo(osso);
try (StreamInput input =
new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
return new IpEnrichmentRequest(input);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@

package org.opensearch.geospatial.action;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;

import org.opensearch.core.action.ActionResponse;
import org.opensearch.core.common.io.stream.InputStreamStreamInput;
import org.opensearch.core.common.io.stream.OutputStreamStreamOutput;
import org.opensearch.core.common.io.stream.StreamInput;
import org.opensearch.core.common.io.stream.StreamOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.log4j.Log4j2;

/**
* Wrapper class to encapsulate the IP enrichment result for IpEnrichmentTransportAction.
Expand Down Expand Up @@ -70,11 +70,9 @@ public static IpEnrichmentResponse fromActionResponse(ActionResponse actionRespo
}

// Or else convert it
try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStreamStreamOutput osso = new OutputStreamStreamOutput(baos)) {
actionResponse.writeTo(osso);
try (StreamInput input =
new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
try (StreamInput input = new InputStreamStreamInput(new ByteArrayInputStream(baos.toByteArray()))) {
return new IpEnrichmentResponse(input);
}
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@

package org.opensearch.geospatial.action;

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

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

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mock;
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;

import lombok.SneakyThrows;

public class IpEnrichmentActionClientTest {

Expand Down Expand Up @@ -52,4 +52,4 @@ public void testWithException() {
IpEnrichmentActionClient ipClient = new IpEnrichmentActionClient(mockNodeClient);
Assert.assertNull(ipClient.getGeoLocationData(dummyIpString));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package org.opensearch.geospatial.action;


import org.junit.Assert;
import org.junit.Test;

Expand All @@ -19,8 +18,7 @@ public class IpEnrichmentRequestTest {
*/
@Test
public void testValidateValidRequest() {
IpEnrichmentRequest request = new IpEnrichmentRequest(
"192.168.1.1", "ValidDataSourceName");
IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", "ValidDataSourceName");
Assert.assertNull(request.validate());
}

Expand All @@ -30,8 +28,7 @@ public void testValidateValidRequest() {
*/
@Test
public void testValidateNullDataSourceName() {
IpEnrichmentRequest request = new IpEnrichmentRequest(
"192.168.1.1", null);
IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null);
Assert.assertNull(request.validate());
}

Expand All @@ -41,12 +38,10 @@ public void testValidateNullDataSourceName() {
*/
@Test
public void testValidateNullIpStringAndDataSourceName() {
IpEnrichmentRequest request = new IpEnrichmentRequest(
null, null);
IpEnrichmentRequest request = new IpEnrichmentRequest(null, null);
Assert.assertEquals(1, request.validate().validationErrors().size());
}


/**
* Test validate() against a valid record,
* no error expected, because dataSourceName is optional.
Expand All @@ -55,13 +50,12 @@ public void testValidateNullIpStringAndDataSourceName() {
public void testFromActionRequestOnValidRecord() {
String ipString = "192.168.1.1";
String dsName = "demo";
IpEnrichmentRequest request = new IpEnrichmentRequest(
ipString, dsName);
IpEnrichmentRequest request = new IpEnrichmentRequest(ipString, dsName);

IpEnrichmentRequest requestAfterStream = IpEnrichmentRequest.fromActionRequest(request);

Assert.assertEquals(request.getIpString(), requestAfterStream.getIpString());
Assert.assertEquals(request.getDatasourceName(), requestAfterStream.getDatasourceName());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@

package org.opensearch.geospatial.action;

import java.util.Map;

import org.junit.Assert;
import org.junit.Test;

import java.util.Map;

public class IpEnrichmentResponseTest {

/**
Expand All @@ -23,4 +23,4 @@ public void testFromActionResponseWithValidPayload() {
IpEnrichmentResponse castedResponse = IpEnrichmentResponse.fromActionResponse(response);
Assert.assertEquals(response.getGeoLocationData(), castedResponse.getGeoLocationData());
}
}
}

0 comments on commit 00dd378

Please sign in to comment.