Skip to content

Commit

Permalink
Add ip2geo processor integ test for failure case (opensearch-project#303
Browse files Browse the repository at this point in the history
)

Signed-off-by: Heemin Kim <[email protected]>
  • Loading branch information
heemin32 committed Jul 13, 2023
1 parent 8c331c7 commit dd3ed57
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
public final class Ip2GeoProcessor extends AbstractProcessor {
private static final Map<String, Object> DATA_EXPIRED = Map.of("error", "ip2geo_data_expired");
private static final String PROPERTY_IP = "ip";

public static final String CONFIG_FIELD = "field";
public static final String CONFIG_TARGET_FIELD = "target_field";
public static final String CONFIG_DATASOURCE = "datasource";
public static final String CONFIG_PROPERTIES = "target_field";
public static final String CONFIG_IGNORE_MISSING = "ignore_missing";
public static final String CONFIG_FIRST_ONLY = "first_only";

private final String field;
private final String targetField;
/**
Expand Down Expand Up @@ -352,12 +360,12 @@ public Ip2GeoProcessor create(
final String description,
final Map<String, Object> config
) throws IOException {
String ipField = readStringProperty(TYPE, processorTag, config, "field");
String targetField = readStringProperty(TYPE, processorTag, config, "target_field", "ip2geo");
String datasourceName = readStringProperty(TYPE, processorTag, config, "datasource");
List<String> propertyNames = readOptionalList(TYPE, processorTag, config, "properties");
boolean ignoreMissing = readBooleanProperty(TYPE, processorTag, config, "ignore_missing", false);
boolean firstOnly = readBooleanProperty(TYPE, processorTag, config, "first_only", true);
String ipField = readStringProperty(TYPE, processorTag, config, CONFIG_FIELD);
String targetField = readStringProperty(TYPE, processorTag, config, CONFIG_TARGET_FIELD, "ip2geo");
String datasourceName = readStringProperty(TYPE, processorTag, config, CONFIG_DATASOURCE);
List<String> propertyNames = readOptionalList(TYPE, processorTag, config, CONFIG_PROPERTIES);
boolean ignoreMissing = readBooleanProperty(TYPE, processorTag, config, CONFIG_IGNORE_MISSING, false);
boolean firstOnly = readBooleanProperty(TYPE, processorTag, config, CONFIG_FIRST_ONLY, true);

// Skip validation for the call by cluster applier service
if (Thread.currentThread().getName().contains(CLUSTER_UPDATE_THREAD_NAME) == false) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.geospatial.ip2geo.processor;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import lombok.SneakyThrows;

import org.opensearch.client.Response;
import org.opensearch.client.ResponseException;
import org.opensearch.geospatial.GeospatialRestTestCase;
import org.opensearch.geospatial.GeospatialTestHelper;

public class Ip2GeoProcessorIT extends GeospatialRestTestCase {

@SneakyThrows
public void testCreateIp2GeoProcessor_whenNoSuchDatasourceExist_thenFails() {
String pipelineName = GeospatialTestHelper.randomLowerCaseString();

// Run
ResponseException exception = expectThrows(
ResponseException.class,
() -> createIp2GeoProcessorPipeline(pipelineName, Collections.emptyMap())
);

// Verify
assertTrue(exception.getMessage().contains("doesn't exist"));
}

private Response createIp2GeoProcessorPipeline(final String pipelineName, final Map<String, String> properties) throws IOException {
String field = GeospatialTestHelper.randomLowerCaseString();
String datasourceName = GeospatialTestHelper.randomLowerCaseString();
Map<String, String> defaultProperties = Map.of(
Ip2GeoProcessor.CONFIG_FIELD,
field,
Ip2GeoProcessor.CONFIG_DATASOURCE,
datasourceName
);
Map<String, String> baseProperties = new HashMap<>();
baseProperties.putAll(defaultProperties);
baseProperties.putAll(properties);
Map<String, Object> processorConfig = buildProcessorConfig(Ip2GeoProcessor.TYPE, baseProperties);

return createPipeline(pipelineName, Optional.empty(), Arrays.asList(processorConfig));
}
}

0 comments on commit dd3ed57

Please sign in to comment.