Skip to content

Commit

Permalink
Spotless check
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 Jan 10, 2025
1 parent c4e80ce commit 15177d4
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class IPFunctions {
public void register(BuiltinFunctionRepository repository) {
repository.register(cidrmatch());
repository.register(geoIp());

}

private DefaultFunctionResolver cidrmatch() {
Expand All @@ -40,9 +39,14 @@ private DefaultFunctionResolver cidrmatch() {

private DefaultFunctionResolver geoIp() {
return define(
BuiltinFunctionName.GEOIP.getName(),
impl(nullMissingHandling(IPFunctions::exprGeoIp), BOOLEAN, STRING, STRING),
impl(nullMissingHandling(IPFunctions::exprGeoIpWithOption), BOOLEAN, STRING, STRING, STRING));
BuiltinFunctionName.GEOIP.getName(),
impl(nullMissingHandling(IPFunctions::exprGeoIp), BOOLEAN, STRING, STRING),
impl(
nullMissingHandling(IPFunctions::exprGeoIpWithOption),
BOOLEAN,
STRING,
STRING,
STRING));
}

/**
Expand All @@ -67,12 +71,12 @@ private ExprValue exprCidrMatch(ExprValue addressExprValue, ExprValue rangeExprV
: ExprValueUtils.LITERAL_TRUE;
}

private ExprValue exprGeoIpWithOption(ExprValue dataSource, ExprValue ipAddress, ExprValue option) {
private ExprValue exprGeoIpWithOption(
ExprValue dataSource, ExprValue ipAddress, ExprValue option) {
return ExprValueUtils.booleanValue(true);
}

private ExprValue exprGeoIp(ExprValue dataSource, ExprValue ipAddress) {
return ExprValueUtils.booleanValue(true);
}

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

package org.opensearch.sql.expression.ip;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.opensearch.sql.data.type.ExprCoreType.STRING;

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.sql.data.model.ExprValue;
import org.opensearch.sql.data.model.ExprValueUtils;
import org.opensearch.sql.exception.SemanticCheckException;
import org.opensearch.sql.expression.DSL;
import org.opensearch.sql.expression.Expression;
import org.opensearch.sql.expression.FunctionExpression;
import org.opensearch.sql.expression.env.Environment;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_FALSE;
import static org.opensearch.sql.data.model.ExprValueUtils.LITERAL_TRUE;
import static org.opensearch.sql.data.type.ExprCoreType.STRING;

@ExtendWith(MockitoExtension.class)
public class GeoIPFunctionTest {

Expand All @@ -36,10 +29,9 @@ public void geoIpDefaultImplementation() {
UnsupportedOperationException exception =
assertThrows(
UnsupportedOperationException.class,
() -> DSL.geoip(
DSL.literal("HARDCODED_DATASOURCE_NAME"),
DSL.ref("ip_address", STRING)).valueOf(env));
assertTrue(
exception.getMessage().matches(".*no default implementation available"));
() ->
DSL.geoip(DSL.literal("HARDCODED_DATASOURCE_NAME"), DSL.ref("ip_address", STRING))
.valueOf(env));
assertTrue(exception.getMessage().matches(".*no default implementation available"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,19 @@

package org.opensearch.sql.geo;

import static org.opensearch.sql.legacy.TestUtils.getResponseBody;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_GEOIP;
import static org.opensearch.sql.util.MatcherUtils.columnName;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyColumn;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;

import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import lombok.SneakyThrows;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.json.JSONObject;
Expand All @@ -21,31 +34,15 @@
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.sql.ppl.PPLIntegTestCase;

import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import static org.opensearch.sql.legacy.TestUtils.getResponseBody;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_GEOIP;
import static org.opensearch.sql.util.MatcherUtils.columnName;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyColumn;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;

/**
* IP enrichment PPL request with OpenSearch Geo-sptial plugin
*/
/** IP enrichment PPL request with OpenSearch Geo-sptial plugin */
public class PplIpEnrichmentIT extends PPLIntegTestCase {

private static boolean initialized = false;

private static Map<String, Object> MANIFEST_LOCATION = Map.of(
private static Map<String, Object> MANIFEST_LOCATION =
Map.of(
"endpoint",
"https://raw.githubusercontent.com/opensearch-project/geospatial/main/src/test/resources/ip2geo/server/city/manifest.json"
);
"https://raw.githubusercontent.com/opensearch-project/geospatial/main/src/test/resources/ip2geo/server/city/manifest.json");

private static String DATASOURCE_NAME = "dummycityindex";

Expand All @@ -70,7 +67,6 @@ protected void init() throws Exception {
waitForDatasourceToBeAvailable(DATASOURCE_NAME, Duration.ofSeconds(10));
}


@Test
public void testGeoPluginInstallation() throws IOException {

Expand All @@ -86,25 +82,30 @@ public void testGeoPluginInstallation() throws IOException {
@SneakyThrows
@Test
public void testGeoIpEnrichment() {
JSONObject resultGeoIp = executeQuery(
String.format("search source=%s | eval enrichmentResult = geoip(\\\"%s\\\",%s)",
TEST_INDEX_GEOIP, "dummycityindex", "ip"));
JSONObject resultGeoIp =
executeQuery(
String.format(
"search source=%s | eval enrichmentResult = geoip(\\\"%s\\\",%s)",
TEST_INDEX_GEOIP, "dummycityindex", "ip"));

verifyColumn(resultGeoIp, columnName("name"), columnName("ip"), columnName("enrichmentResult"));
verifyDataRows(resultGeoIp,
rows("Test user - USA", "10.1.1.1", Map.of("country", "USA", "city", "Seattle")),
rows("Test user - Canada", "127.1.1.1", Map.of("country", "Canada", "city", "Vancouver")));

verifyDataRows(
resultGeoIp,
rows("Test user - USA", "10.1.1.1", Map.of("country", "USA", "city", "Seattle")),
rows("Test user - Canada", "127.1.1.1", Map.of("country", "Canada", "city", "Vancouver")));
}

/**
* Helper method to send a PUT request to create a dummy dataSource with provided endpoint for integration test.
* Helper method to send a PUT request to create a dummy dataSource with provided endpoint for
* integration test.
*
* @param name Name of the dataSource
* @param properties Request payload in Json format
* @return Response for the create dataSource request.
* @throws IOException In case of network failure
*/
private Response createDatasource(final String name, Map<String, Object> properties) throws IOException {
private Response createDatasource(final String name, Map<String, Object> properties)
throws IOException {
XContentBuilder builder = XContentFactory.jsonBuilder().startObject();
for (Map.Entry<String, Object> config : properties.entrySet()) {
builder.field(config.getKey(), config.getValue());
Expand All @@ -117,37 +118,40 @@ private Response createDatasource(final String name, Map<String, Object> propert

/**
* Helper method check the status of dataSource creation within the specific timeframe.
*
* @param name The name of the dataSource to assert
* @param timeout The timeout value in seconds
* @throws Exception Exception
*/
private void waitForDatasourceToBeAvailable(final String name, final Duration timeout) throws Exception {
private void waitForDatasourceToBeAvailable(final String name, final Duration timeout)
throws Exception {
Instant start = Instant.now();
while (!"AVAILABLE".equals(getDatasourceState(name))) {
if (Duration.between(start, Instant.now()).compareTo(timeout) > 0) {
throw new RuntimeException(
String.format(
Locale.ROOT,
"Datasource state didn't change to %s after %d seconds",
"AVAILABLE",
timeout.toSeconds()
)
);
String.format(
Locale.ROOT,
"Datasource state didn't change to %s after %d seconds",
"AVAILABLE",
timeout.toSeconds()));
}
Thread.sleep(1000);
}
}

/**
* Helper method to fetch the DataSource creation status via REST client.
*
* @param name dataSource name
* @return Status in String
* @throws Exception IO.
*/
private String getDatasourceState(final String name) throws Exception {
Request request = new Request("GET", GEO_SPATIAL_DATASOURCE_PATH + name);
Response response = client().performRequest(request);
var responseInMap = createParser(XContentType.JSON.xContent(), EntityUtils.toString(response.getEntity())).map();
var responseInMap =
createParser(XContentType.JSON.xContent(), EntityUtils.toString(response.getEntity()))
.map();
var datasources = (List<Map<String, Object>>) responseInMap.get("datasources");
return (String) datasources.get(0).get("state");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,10 @@ public enum Index {
getUnexpandedObjectIndexMapping(),
"src/test/resources/unexpanded_objects.json"),
GEOIP(
TestsConstants.TEST_INDEX_GEOIP,
"geoip",
getGeoIpIndexMapping(),
"src/test/resources/geoip.json"),
TestsConstants.TEST_INDEX_GEOIP,
"geoip",
getGeoIpIndexMapping(),
"src/test/resources/geoip.json"),
BANK(
TestsConstants.TEST_INDEX_BANK,
"account",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,21 @@

package org.opensearch.sql.security;

import static org.opensearch.sql.legacy.TestUtils.getResponseBody;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_ACCOUNT;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_BANK;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_DOG;
import static org.opensearch.sql.legacy.TestsConstants.TEST_INDEX_GEOIP;
import static org.opensearch.sql.plugin.rest.RestPPLQueryAction.QUERY_API_ENDPOINT;
import static org.opensearch.sql.util.MatcherUtils.columnName;
import static org.opensearch.sql.util.MatcherUtils.rows;
import static org.opensearch.sql.util.MatcherUtils.verifyColumn;
import static org.opensearch.sql.util.MatcherUtils.verifyDataRows;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import lombok.SneakyThrows;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opensearch.client.Request;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.sql.ppl.PPLIntegTestCase;

/** Cross Cluster Search tests to be executed with security plugin. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ private Map<String, ExprValue> eval(Environment<Expression, ExprValue> env) {
return evalResultMap;
}

private ExprValue fetchIpEnrichment(List<Expression> arguments, Environment<Expression, ExprValue> env) {
private ExprValue fetchIpEnrichment(
List<Expression> arguments, Environment<Expression, ExprValue> env) {
final Set<String> PERMITTED_OPTIONS =
Set.of(
"country_iso_code",
Expand Down
Loading

0 comments on commit 15177d4

Please sign in to comment.