Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/main/java/org/opensearch/geospatial/GeospatialParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -77,18 +76,18 @@ public static Map<String, Object> convertToMap(BytesReference content) {
* getFeatures will return features from given map input. This function abstracts the logic to parse given input and returns
* list of Features if exists in Map format.
* @param geoJSON given input which may contain GeoJSON Object
* @return Returns an Optional with the List of Features as map if input is GeoJSON,
* else, returns an empty Optional instance.
* @return Returns List of Features as map if input is GeoJSON,
* else, returns an empty list.
*/
public static Optional<List<Map<String, Object>>> getFeatures(final Map<String, Object> geoJSON) {
public static List<Map<String, Object>> getFeatures(final Map<String, Object> geoJSON) {
final String type = extractValueAsString(geoJSON, TYPE_KEY);
Objects.requireNonNull(type, TYPE_KEY + " cannot be null");
if (Feature.TYPE.equalsIgnoreCase(type)) {
return Optional.of(List.of(geoJSON));
return List.of(geoJSON);
}
if (FeatureCollection.TYPE.equalsIgnoreCase(type)) {
return Optional.of(Collections.unmodifiableList(FeatureCollection.create(geoJSON).getFeatures()));
return Collections.unmodifiableList(FeatureCollection.create(geoJSON).getFeatures());
}
return Optional.empty();
return List.of();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ private Optional<BulkRequestBuilder> prepareContentRequest(UploadGeoJSONRequestC
.stream()
.map(GeospatialParser::toStringObjectMap)
.map(GeospatialParser::getFeatures)
.filter(Optional::isPresent)
.map(Optional::get)
.flatMap(List::stream)
.map(this::createIndexRequestBuilder)
.map(indexRequestBuilder -> indexRequestBuilder.setIndex(content.getIndexName()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.json.JSONArray;
import org.json.JSONObject;
Expand Down Expand Up @@ -77,10 +76,10 @@ public void testConvertToMap() {

public void testGetFeaturesWithGeoJSONFeature() {
Map<String, Object> geoJSON = GeospatialObjectBuilder.randomGeoJSONFeature(new JSONObject()).toMap();
Optional<List<Map<String, Object>>> features = GeospatialParser.getFeatures(geoJSON);
assertTrue(features.isPresent());
assertEquals(1, features.get().size());
assertEquals(features.get().get(0), geoJSON);
List<Map<String, Object>> features = GeospatialParser.getFeatures(geoJSON);
assertFalse(features.isEmpty());
assertEquals(1, features.size());
assertEquals(features.get(0), geoJSON);
}

public void testGetFeaturesWithGeoJSONFeatureCollection() {
Expand All @@ -90,16 +89,16 @@ public void testGetFeaturesWithGeoJSONFeatureCollection() {
features.put(GeospatialObjectBuilder.randomGeoJSONFeature(new JSONObject()));

JSONObject collection = GeospatialObjectBuilder.buildGeoJSONFeatureCollection(features);
Optional<List<Map<String, Object>>> featureList = GeospatialParser.getFeatures(collection.toMap());
assertTrue(featureList.isPresent());
assertEquals(featureList.get().size(), features.length());
List<Map<String, Object>> featureList = GeospatialParser.getFeatures(collection.toMap());
assertFalse(featureList.isEmpty());
assertEquals(featureList.size(), features.length());
}

public void testGetFeaturesWithUnSupportedType() {
Map<String, Object> geoJSON = new HashMap<>();
geoJSON.put(Feature.TYPE_KEY, "invalid-type");
Optional<List<Map<String, Object>>> features = GeospatialParser.getFeatures(geoJSON);
assertFalse(features.isPresent());
List<Map<String, Object>> features = GeospatialParser.getFeatures(geoJSON);
assertTrue(features.isEmpty());
}

}