Skip to content

Commit

Permalink
Create random method to generate feature
Browse files Browse the repository at this point in the history
Signed-off-by: Vijayan Balasubramanian <balasvij@amazon.com>
  • Loading branch information
VijayanB committed Jan 28, 2022
1 parent e927f9e commit 49b1ca9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@

import org.json.JSONObject;
import org.opensearch.common.Randomness;
import org.opensearch.common.collect.List;
import org.opensearch.common.geo.GeoShapeType;
import org.opensearch.geo.GeometryTestUtils;
import org.opensearch.geospatial.geojson.Feature;

import com.carrotsearch.randomizedtesting.generators.RandomPicks;

/**
* GeospatialObjectBuilder contains utility methods to generate geospatial objects
* for tests
Expand All @@ -32,13 +36,13 @@ public static JSONObject buildGeometry(String type, Object value) {
return geometry;
}

public static JSONObject getRandomGeometryPoint() {
public static JSONObject randomGeometryPoint() {
Random random = Randomness.get();
double[] point = new double[] { random.nextDouble(), random.nextDouble() };
return buildGeometry(GeoShapeType.POINT.shapeName(), point);
}

public static JSONObject getRandomGeometryLineString() {
public static JSONObject randomGeometryLineString() {
int randomTotalPoints = randomPositiveInt(MAX_POINTS);
int randomPointsDimension = randomPositiveInt(MAX_DIMENSION);
double[][] lineString = new double[randomTotalPoints][randomPointsDimension];
Expand All @@ -49,8 +53,7 @@ public static JSONObject getRandomGeometryLineString() {
}

private static double[] getRandomPoint() {
Random random = Randomness.get();
return new double[] { random.nextDouble(), random.nextDouble() };
return new double[] { GeometryTestUtils.randomLat(), GeometryTestUtils.randomLon() };
}

public static JSONObject buildGeoJSONFeature(JSONObject geometry, JSONObject properties) {
Expand All @@ -72,4 +75,12 @@ public static JSONObject buildProperties(Map<String, Object> properties) {
public static int randomPositiveInt(int bound) {
return Randomness.get().ints(MIN_POSITIVE_INTEGER_VALUE, bound).findFirst().getAsInt();
}

public static JSONObject randomGeoJSONFeature(final JSONObject properties) {
return buildGeoJSONFeature(randomGeoJSONGeometry(), properties);
}

public static JSONObject randomGeoJSONGeometry() {
return RandomPicks.randomFrom(Randomness.get(), List.of(randomGeometryLineString(), randomGeometryPoint()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@
package org.opensearch.geospatial;

import static java.util.stream.Collectors.joining;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildGeoJSONFeature;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildProperties;
import static org.opensearch.geospatial.GeospatialObjectBuilder.getRandomGeometryLineString;
import static org.opensearch.geospatial.GeospatialObjectBuilder.getRandomGeometryPoint;
import static org.opensearch.geospatial.GeospatialObjectBuilder.randomGeoJSONFeature;
import static org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONRequestContent.FIELD_DATA;

import java.io.IOException;
Expand Down Expand Up @@ -117,9 +115,9 @@ protected JSONObject buildRequestContent(String indexName, String fieldName) {
contents.put(UploadGeoJSONRequestContent.FIELD_INDEX.getPreferredName(), indexName);
contents.put(UploadGeoJSONRequestContent.FIELD_GEOSPATIAL.getPreferredName(), fieldName);
JSONArray values = new JSONArray();
values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap())));
values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap())));
values.put(buildGeoJSONFeature(getRandomGeometryLineString(), buildProperties(Collections.emptyMap())));
values.put(randomGeoJSONFeature(buildProperties(Collections.emptyMap())));
values.put(randomGeoJSONFeature(buildProperties(Collections.emptyMap())));
values.put(randomGeoJSONFeature(buildProperties(Collections.emptyMap())));
contents.put(FIELD_DATA.getPreferredName(), values);
return contents;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@

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

import static org.junit.Assert.*;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildGeoJSONFeature;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildProperties;
import static org.opensearch.geospatial.GeospatialObjectBuilder.getRandomGeometryLineString;
import static org.opensearch.geospatial.GeospatialObjectBuilder.getRandomGeometryPoint;
import static org.opensearch.geospatial.GeospatialObjectBuilder.randomGeoJSONFeature;
import static org.opensearch.geospatial.action.upload.geojson.UploadGeoJSONRequestContent.FIELD_DATA;

import java.util.Collections;
Expand All @@ -26,9 +23,9 @@ private Map<String, Object> buildRequestContent(String indexName, String fieldNa
contents.put(UploadGeoJSONRequestContent.FIELD_INDEX.getPreferredName(), indexName);
contents.put(UploadGeoJSONRequestContent.FIELD_GEOSPATIAL.getPreferredName(), fieldName);
JSONArray values = new JSONArray();
values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap())));
values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap())));
values.put(buildGeoJSONFeature(getRandomGeometryLineString(), buildProperties(Collections.emptyMap())));
values.put(randomGeoJSONFeature(buildProperties(Collections.emptyMap())));
values.put(randomGeoJSONFeature(buildProperties(Collections.emptyMap())));
values.put(randomGeoJSONFeature(buildProperties(Collections.emptyMap())));
contents.put(FIELD_DATA.getPreferredName(), values);
return contents.toMap();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
package org.opensearch.geospatial.processor;

import static org.opensearch.geospatial.GeospatialObjectBuilder.GEOMETRY_TYPE_KEY;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildGeoJSONFeature;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildProperties;
import static org.opensearch.geospatial.GeospatialObjectBuilder.getRandomGeometryLineString;
import static org.opensearch.geospatial.GeospatialObjectBuilder.randomGeoJSONFeature;
import static org.opensearch.ingest.RandomDocumentPicks.randomString;

import java.io.IOException;
Expand All @@ -23,7 +22,6 @@
import org.json.JSONObject;
import org.opensearch.client.Request;
import org.opensearch.client.Response;
import org.opensearch.common.geo.GeoShapeType;
import org.opensearch.common.settings.Settings;
import org.opensearch.geospatial.GeospatialRestTestCase;
import org.opensearch.rest.RestStatus;
Expand Down Expand Up @@ -65,7 +63,7 @@ public void testIndexGeoJSONSuccess() throws IOException {
properties.put(randomString(random()), randomString(random()));
properties.put(randomString(random()), randomString(random()));

JSONObject feature = buildGeoJSONFeature(getRandomGeometryLineString(), buildProperties(properties));
JSONObject feature = randomGeoJSONFeature(buildProperties(properties));
String requestBody = feature.toString();
Map<String, String> params = new HashMap<>();
params.put("pipeline", pipelineName);
Expand All @@ -81,7 +79,8 @@ public void testIndexGeoJSONSuccess() throws IOException {
}

Map<String, Object> geoShapeFieldValue = (Map<String, Object>) document.get(geoShapeField);
assertEquals(geoShapeFieldValue.get(GEOMETRY_TYPE_KEY), GeoShapeType.LINESTRING.shapeName());
assertNotNull(geoShapeFieldValue);
assertNotNull(geoShapeFieldValue.get(GEOMETRY_TYPE_KEY));

deletePipeline(pipelineName);
deleteIndex(indexName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

package org.opensearch.geospatial.processor;

import static org.opensearch.geospatial.GeospatialObjectBuilder.buildGeoJSONFeature;
import static org.opensearch.geospatial.GeospatialObjectBuilder.buildProperties;
import static org.opensearch.geospatial.GeospatialObjectBuilder.getRandomGeometryPoint;
import static org.opensearch.geospatial.GeospatialObjectBuilder.randomGeoJSONFeature;

import java.util.Collections;
import java.util.HashMap;
Expand All @@ -24,7 +23,7 @@ public class FeatureProcessorTests extends OpenSearchTestCase {
private Map<String, Object> buildTestFeature() {
Map<String, Object> properties = new HashMap<>();
properties.put("name", "Dinagat Islands");
return buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(properties)).toMap();
return randomGeoJSONFeature(buildProperties(properties)).toMap();
}

public void testCreateFeatureProcessor() {
Expand Down

0 comments on commit 49b1ca9

Please sign in to comment.