From 79c1a6ffe997148c0a941951fe33ef83a6c740f9 Mon Sep 17 00:00:00 2001 From: Vijayan Balasubramanian Date: Fri, 28 Jan 2022 11:33:54 -0800 Subject: [PATCH] Refactor getRandomGeometryLineString Signed-off-by: Vijayan Balasubramanian --- .../geospatial/GeospatialObjectBuilder.java | 13 +++++++++++-- .../geospatial/GeospatialRestTestCase.java | 9 +-------- .../geojson/UploadGeoJSONRequestContentTests.java | 12 +----------- .../geospatial/processor/FeatureProcessorIT.java | 15 +-------------- 4 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/test/java/org/opensearch/geospatial/GeospatialObjectBuilder.java b/src/test/java/org/opensearch/geospatial/GeospatialObjectBuilder.java index b58fbfae..392e9324 100644 --- a/src/test/java/org/opensearch/geospatial/GeospatialObjectBuilder.java +++ b/src/test/java/org/opensearch/geospatial/GeospatialObjectBuilder.java @@ -21,6 +21,9 @@ public class GeospatialObjectBuilder { public static final String GEOMETRY_TYPE_KEY = "type"; public static final String GEOMETRY_COORDINATES_KEY = "coordinates"; + public static final int MIN_POSITIVE_INTEGER_VALUE = 1; + public static final int MAX_POINTS = 10; + public static final int MAX_DIMENSION = 4; public static JSONObject buildGeometry(String type, Object value) { JSONObject geometry = new JSONObject(); @@ -35,8 +38,10 @@ public static JSONObject getRandomGeometryPoint() { return buildGeometry(GeoShapeType.POINT.shapeName(), point); } - public static JSONObject getRandomGeometryLineString(int totalPoints, int dimension) { - double[][] lineString = new double[totalPoints][dimension]; + public static JSONObject getRandomGeometryLineString() { + int randomTotalPoints = randomPositiveInt(MAX_POINTS); + int randomPointsDimension = randomPositiveInt(MAX_DIMENSION); + double[][] lineString = new double[randomTotalPoints][randomPointsDimension]; for (int i = 0; i < lineString.length; i++) { lineString[i] = getRandomPoint(); } @@ -63,4 +68,8 @@ public static JSONObject buildProperties(Map properties) { } return propertiesObject; } + + public static int randomPositiveInt(int bound) { + return Randomness.get().ints(MIN_POSITIVE_INTEGER_VALUE, bound).findFirst().getAsInt(); + } } diff --git a/src/test/java/org/opensearch/geospatial/GeospatialRestTestCase.java b/src/test/java/org/opensearch/geospatial/GeospatialRestTestCase.java index 5b064c70..c82ae6d7 100644 --- a/src/test/java/org/opensearch/geospatial/GeospatialRestTestCase.java +++ b/src/test/java/org/opensearch/geospatial/GeospatialRestTestCase.java @@ -36,10 +36,6 @@ public abstract class GeospatialRestTestCase extends OpenSearchRestTestCase { - public static final int MIN_POSITIVE_VALUE = 1; - public static final int MAX_POINTS = 10; - public static final int MAX_DIMENSION = 4; - public static final String SOURCE = "_source"; public static final String DOC = "_doc"; public static final String URL_DELIMITER = "/"; @@ -125,10 +121,7 @@ protected JSONObject buildRequestContent(String indexName, String fieldName) { values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap()))); values.put( buildGeoJSONFeature( - getRandomGeometryLineString( - randomIntBetween(MIN_POSITIVE_VALUE, MAX_POINTS), - randomIntBetween(MIN_POSITIVE_VALUE, MAX_DIMENSION) - ), + getRandomGeometryLineString(), buildProperties(Collections.emptyMap()) ) ); diff --git a/src/test/java/org/opensearch/geospatial/action/upload/geojson/UploadGeoJSONRequestContentTests.java b/src/test/java/org/opensearch/geospatial/action/upload/geojson/UploadGeoJSONRequestContentTests.java index 860bfb68..8d2d3e76 100644 --- a/src/test/java/org/opensearch/geospatial/action/upload/geojson/UploadGeoJSONRequestContentTests.java +++ b/src/test/java/org/opensearch/geospatial/action/upload/geojson/UploadGeoJSONRequestContentTests.java @@ -22,8 +22,6 @@ public class UploadGeoJSONRequestContentTests extends OpenSearchTestCase { public static final int MIN_POSITIVE_VALUE = 1; - public static final int MAX_POINTS = 10; - public static final int MAX_DIMENSION = 4; private Map buildRequestContent(String indexName, String fieldName) { JSONObject contents = new JSONObject(); @@ -32,15 +30,7 @@ private Map buildRequestContent(String indexName, String fieldNa JSONArray values = new JSONArray(); values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap()))); values.put(buildGeoJSONFeature(getRandomGeometryPoint(), buildProperties(Collections.emptyMap()))); - values.put( - buildGeoJSONFeature( - getRandomGeometryLineString( - randomIntBetween(MIN_POSITIVE_VALUE, MAX_POINTS), - randomIntBetween(MIN_POSITIVE_VALUE, MAX_DIMENSION) - ), - buildProperties(Collections.emptyMap()) - ) - ); + values.put(buildGeoJSONFeature(getRandomGeometryLineString(), buildProperties(Collections.emptyMap()))); contents.put(FIELD_DATA.getPreferredName(), values); return contents.toMap(); } diff --git a/src/test/java/org/opensearch/geospatial/processor/FeatureProcessorIT.java b/src/test/java/org/opensearch/geospatial/processor/FeatureProcessorIT.java index ebe62042..f5b0afa3 100644 --- a/src/test/java/org/opensearch/geospatial/processor/FeatureProcessorIT.java +++ b/src/test/java/org/opensearch/geospatial/processor/FeatureProcessorIT.java @@ -30,19 +30,6 @@ public class FeatureProcessorIT extends GeospatialRestTestCase { - public static final int LINESTRING_TOTAL_POINTS = 4; - public static final int LINESTRING_POINT_DIMENSION = 2; - - private double[][] randomDoubleArray(int row, int col) { - double[][] randomArray = new double[row][col]; - for (int i = 0; i < row; i++) { - for (int j = 0; j < col; j++) { - randomArray[i][j] = randomDouble(); - } - } - return randomArray; - } - public void testProcessorAvailable() throws IOException { String nodeIngestURL = String.join("/", "_nodes", "ingest"); String endpoint = nodeIngestURL + "?filter_path=nodes.*.ingest.processors&pretty"; @@ -79,7 +66,7 @@ public void testIndexGeoJSONSuccess() throws IOException { properties.put(randomString(random()), randomString(random())); JSONObject feature = buildGeoJSONFeature( - getRandomGeometryLineString(LINESTRING_TOTAL_POINTS, LINESTRING_POINT_DIMENSION), + getRandomGeometryLineString(), buildProperties(properties) ); String requestBody = feature.toString();