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 28, 2022
1 parent 49b1ca9 commit 4090a7f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

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;

public final class UploadGeoJSONRequestContent {

Expand All @@ -22,15 +23,25 @@ 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) {
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 4090a7f

Please sign in to comment.