Skip to content

Commit

Permalink
Update code style
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 23, 2024
1 parent abde0e5 commit 8671e74
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package org.opensearch.geospatial.ip2geo.action;

import lombok.extern.log4j.Log4j2;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.opensearch.action.ActionRequest;
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
Expand All @@ -21,20 +24,17 @@
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import lombok.extern.log4j.Log4j2;

/**
* Transport action to convert provided IP address String into GeoLocation data.
*/
@Log4j2
public class IpEnrichmentTransportAction extends HandledTransportAction<ActionRequest,
ActionResponse> {
public class IpEnrichmentTransportAction extends HandledTransportAction<ActionRequest, ActionResponse> {

private final Ip2GeoCachedDao ip2GeoCachedDao;

// private final String defaultDataSourceName;
// private final String defaultDataSourceName;

private final DatasourceDao datasourceDao;

Expand All @@ -46,16 +46,16 @@ public class IpEnrichmentTransportAction extends HandledTransportAction<ActionRe
*/
@Inject
public IpEnrichmentTransportAction(
TransportService transportService,
ActionFilters actionFilters,
Ip2GeoCachedDao cachedDao,
DatasourceDao datasourceDao) {
TransportService transportService,
ActionFilters actionFilters,
Ip2GeoCachedDao cachedDao,
DatasourceDao datasourceDao
) {
super(IpEnrichmentAction.NAME, transportService, actionFilters, IpEnrichmentRequest::new);
this.ip2GeoCachedDao = cachedDao;
this.datasourceDao = datasourceDao;
}


/**
* Overridden method to extract IP String from IpEnrichmentRequest object and return the enrichment result
* in the form of IpEnrichmentResponse which contains the GeoLocation data for given IP String.
Expand All @@ -68,13 +68,11 @@ protected void doExecute(Task task, ActionRequest request, ActionListener<Action
IpEnrichmentRequest enrichmentRequest = IpEnrichmentRequest.fromActionRequest(request);
String ipString = enrichmentRequest.getIpString();
String defaultDataSourceName = getDefaultDataSourceName();
if (enrichmentRequest.getDatasourceName() == null &&
defaultDataSourceName == null) {
if (enrichmentRequest.getDatasourceName() == null && defaultDataSourceName == null) {
log.error("No data source available, IpEnrichmentTransportAction aborted.");
listener.onFailure(new IllegalArgumentException());
} else {
String dataSourceName = Optional.ofNullable(enrichmentRequest.getDatasourceName())
.orElse(defaultDataSourceName);
String dataSourceName = Optional.ofNullable(enrichmentRequest.getDatasourceName()).orElse(defaultDataSourceName);
String indexName = ip2GeoCachedDao.getIndexName(dataSourceName);
Map<String, Object> geoLocationData = ip2GeoCachedDao.getGeoData(indexName, ipString);
log.debug("GeoSpatial IP lookup on IP: [{}], and result [{}]", ipString, geoLocationData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import org.opensearch.env.Environment;
import org.opensearch.env.NodeEnvironment;
import org.opensearch.geospatial.action.IpEnrichmentAction;
import org.opensearch.geospatial.ip2geo.action.IpEnrichmentTransportAction;
import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONAction;
import org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONTransportAction;
import org.opensearch.geospatial.index.mapper.xypoint.XYPointFieldMapper;
Expand All @@ -43,6 +42,7 @@
import org.opensearch.geospatial.ip2geo.action.DeleteDatasourceTransportAction;
import org.opensearch.geospatial.ip2geo.action.GetDatasourceAction;
import org.opensearch.geospatial.ip2geo.action.GetDatasourceTransportAction;
import org.opensearch.geospatial.ip2geo.action.IpEnrichmentTransportAction;
import org.opensearch.geospatial.ip2geo.action.PutDatasourceAction;
import org.opensearch.geospatial.ip2geo.action.PutDatasourceTransportAction;
import org.opensearch.geospatial.ip2geo.action.RestDeleteDatasourceHandler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@

package org.opensearch.geospatial.ip2geo.action;

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

import java.util.Collections;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
Expand All @@ -16,15 +25,6 @@
import org.opensearch.geospatial.ip2geo.jobscheduler.Datasource;
import org.opensearch.tasks.Task;

import java.util.Collections;
import java.util.List;

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

public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase {

private IpEnrichmentTransportAction action;
Expand All @@ -40,11 +40,9 @@ public class IpEnrichmentTransportActionTests extends Ip2GeoTestCase {

@Before
public void init() {
action = new IpEnrichmentTransportAction(
transportService, actionFilters, ip2GeoCachedDao, datasourceDao);
action = new IpEnrichmentTransportAction(transportService, actionFilters, ip2GeoCachedDao, datasourceDao);
}


/**
* When dataSource is provided.
*/
Expand All @@ -71,19 +69,15 @@ public void testDoExecute_WithDefaultDataSource() {
verify(listener, times(1)).onResponse(any(IpEnrichmentResponse.class));
}


/**
* No alternative dataSource, exception being thrown to indicate this.
*/
@Test
public void testDoExecute_WithNoAlternativeDataSource() {
IpEnrichmentRequest request = new IpEnrichmentRequest(
"192.168.1.1", null);
IpEnrichmentRequest request = new IpEnrichmentRequest("192.168.1.1", null);
action.doExecute(task, request, listener);

verify(listener, times(1))
.onFailure(any(IllegalArgumentException.class));
verify(listener, times(1)).onFailure(any(IllegalArgumentException.class));
}


}
}

0 comments on commit 8671e74

Please sign in to comment.