Skip to content

Commit

Permalink
Refactor getRandomGeometryLineString
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 c1e4734 commit 79c1a6f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -63,4 +68,8 @@ public static JSONObject buildProperties(Map<String, Object> properties) {
}
return propertiesObject;
}

public static int randomPositiveInt(int bound) {
return Randomness.get().ints(MIN_POSITIVE_INTEGER_VALUE, bound).findFirst().getAsInt();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/";
Expand Down Expand Up @@ -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())
)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> buildRequestContent(String indexName, String fieldName) {
JSONObject contents = new JSONObject();
Expand All @@ -32,15 +30,7 @@ private Map<String, Object> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 79c1a6f

Please sign in to comment.