Skip to content

Commit

Permalink
Validate user input for Feature
Browse files Browse the repository at this point in the history
check whether input has valid index name, geospatial field
and GeoJSON data.

Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed Jan 29, 2022
1 parent 49b1ca9 commit 7e43e65
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/main/java/org/opensearch/geospatial/GeospatialParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public static Map<String, Object> toStringObjectMap(final Object input) {
/**
* User inputs are usually deserialized into Map. extractValueAsString will help caller to
* extract value from the Map and cast it to string with validation.
* @param input User input of type Map
* @param key property we would like to extract value of
* @param input User input of type Map, cannot be null
* @param key property we would like to extract value of, cannot be null
* @return null if key doesn't exist, value as String if it exists, throw exception otherwise
*/
public static String extractValueAsString(final Map<String, Object> input, final String key) {
Objects.requireNonNull(key);
Objects.requireNonNull(input);
Objects.requireNonNull(key, "parameter 'key' cannot be null");
Objects.requireNonNull(input, "parameter 'input' cannot be null");
Object value = input.get(key);
if (value == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@

package org.opensearch.geospatial.action.upload.geojson;

import static java.util.Objects.requireNonNull;
import static org.opensearch.geospatial.GeospatialParser.extractValueAsString;

import java.util.Map;
import java.util.Objects;

import org.opensearch.common.ParseField;
import org.opensearch.common.Strings;

/**
* UploadGeoJSONRequestContent is the Data model for UploadGeoJSONRequest's body
*/
public final class UploadGeoJSONRequestContent {

public static final ParseField FIELD_INDEX = new ParseField("index", new String[0]);
Expand All @@ -22,15 +26,26 @@ public final class UploadGeoJSONRequestContent {
private final Object data;

private UploadGeoJSONRequestContent(String indexName, String geospatialFieldName, Object data) {
this.indexName = requireNonNull(indexName);
this.geospatialFieldName = requireNonNull(geospatialFieldName);
this.data = requireNonNull(data);
this.indexName = indexName;
this.geospatialFieldName = geospatialFieldName;
this.data = data;
}

public static UploadGeoJSONRequestContent create(Map<String, Object> input) {
Objects.requireNonNull(input, "input cannot be null");
String index = extractValueAsString(input, FIELD_INDEX.getPreferredName());
if (!Strings.hasText(index)) {
throw new IllegalArgumentException("field [ " + FIELD_INDEX.getPreferredName() + " ] cannot be empty");
}
String geospatialField = extractValueAsString(input, FIELD_GEOSPATIAL.getPreferredName());
return new UploadGeoJSONRequestContent(index, geospatialField, input.get(FIELD_DATA.getPreferredName()));
if (!Strings.hasText(geospatialField)) {
throw new IllegalArgumentException("field [ " + FIELD_GEOSPATIAL.getPreferredName() + " ] cannot be empty");
}
Object geoJSONData = Objects.requireNonNull(
input.get(FIELD_DATA.getPreferredName()),
"field [ " + FIELD_DATA.getPreferredName() + " ] cannot be empty"
);
return new UploadGeoJSONRequestContent(index, geospatialField, geoJSONData);
}

public final String getIndexName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,20 @@ public void testCreate() {
assertEquals(indexName, content.getIndexName());
assertEquals(contents.get(FIELD_DATA.getPreferredName()), content.getData());
}

public void testCreateEmptyIndexName() {
IllegalArgumentException invalidIndexName = assertThrows(
IllegalArgumentException.class,
() -> UploadGeoJSONRequestContent.create(buildRequestContent("", "location"))
);
assertTrue(invalidIndexName.getMessage().contains("cannot be empty"));
}

public void testCreateEmptyGeospatialFieldName() {
IllegalArgumentException invalidIndexName = assertThrows(
IllegalArgumentException.class,
() -> UploadGeoJSONRequestContent.create(buildRequestContent("some-index", ""))
);
assertTrue(invalidIndexName.getMessage().contains("cannot be empty"));
}
}

0 comments on commit 7e43e65

Please sign in to comment.