github-advanced-security[bot] commented on code in PR #816: URL: https://github.com/apache/incubator-baremaps/pull/816#discussion_r1432687122
########## baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CleanDuplicatePoints.java: ########## @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap.function; + +import java.util.ArrayList; +import java.util.List; +import org.locationtech.jts.geom.*; + +public class CleanDuplicatePoints { + + public static Coordinate[] removeDuplicatePoints(Coordinate[] coord) { + List uniqueCoords = new ArrayList(); + Coordinate lastPt = null; + for (int i = 0; i < coord.length; i++) { + if (lastPt == null || !lastPt.equals(coord[i])) { + lastPt = coord[i]; + uniqueCoords.add(new Coordinate(lastPt)); + } + } + return (Coordinate[]) uniqueCoords.toArray(new Coordinate[0]); + } + + private GeometryFactory fact; + + public CleanDuplicatePoints() {} + + public Geometry clean(Geometry g) { + fact = g.getFactory(); + if (g.isEmpty()) { + return g; + } + if (g instanceof Point) { + return g; + } else if (g instanceof MultiPoint) { + return g; + } + // LineString also handles LinearRings + else if (g instanceof LinearRing) { + return clean((LinearRing) g); + } else if (g instanceof LineString) { + return clean((LineString) g); + } else if (g instanceof Polygon) { + return clean((Polygon) g); + } else if (g instanceof MultiLineString) { + return clean((MultiLineString) g); + } else if (g instanceof MultiPolygon) { + return clean((MultiPolygon) g); + } else if (g instanceof GeometryCollection) { + return clean((GeometryCollection) g); + } else { + throw new UnsupportedOperationException(g.getClass().getName()); + } + } + + private LinearRing clean(LinearRing g) { + Coordinate[] coords = removeDuplicatePoints(g.getCoordinates()); + return fact.createLinearRing(coords); + } + + private LineString clean(LineString g) { + Coordinate[] coords = removeDuplicatePoints(g.getCoordinates()); + return fact.createLineString(coords); + } + + private Polygon clean(Polygon poly) { + Coordinate[] shellCoords = removeDuplicatePoints(poly.getExteriorRing().getCoordinates()); + LinearRing shell = fact.createLinearRing(shellCoords); + List holes = new ArrayList(); + for (int i = 0; i < poly.getNumInteriorRing(); i++) { + Coordinate[] holeCoords = removeDuplicatePoints(poly.getInteriorRingN(i).getCoordinates()); + holes.add(fact.createLinearRing(holeCoords)); + } + return fact.createPolygon(shell, GeometryFactory.toLinearRingArray(holes)); + } + + private MultiPolygon clean(MultiPolygon g) { + List polys = new ArrayList(); + for (int i = 0; i < g.getNumGeometries(); i++) { + Polygon poly = (Polygon) g.getGeometryN(i); + polys.add(clean(poly)); + } + return fact.createMultiPolygon(GeometryFactory.toPolygonArray(polys)); + } + + private MultiLineString clean(MultiLineString g) { + List lines = new ArrayList(); + for (int i = 0; i < g.getNumGeometries(); i++) { + LineString line = (LineString) g.getGeometryN(i); + lines.add(clean(line)); + } + return fact.createMultiLineString(GeometryFactory.toLineStringArray(lines)); + } + + private GeometryCollection clean(GeometryCollection g) { Review Comment: ## Confusing overloading of methods Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](1), since dispatch depends on static types. Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](2), since dispatch depends on static types. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/864) ########## baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CleanDuplicatePoints.java: ########## @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap.function; + +import java.util.ArrayList; +import java.util.List; +import org.locationtech.jts.geom.*; + +public class CleanDuplicatePoints { + + public static Coordinate[] removeDuplicatePoints(Coordinate[] coord) { + List uniqueCoords = new ArrayList(); + Coordinate lastPt = null; + for (int i = 0; i < coord.length; i++) { + if (lastPt == null || !lastPt.equals(coord[i])) { + lastPt = coord[i]; + uniqueCoords.add(new Coordinate(lastPt)); + } + } + return (Coordinate[]) uniqueCoords.toArray(new Coordinate[0]); + } + + private GeometryFactory fact; + + public CleanDuplicatePoints() {} + + public Geometry clean(Geometry g) { + fact = g.getFactory(); + if (g.isEmpty()) { + return g; + } + if (g instanceof Point) { + return g; + } else if (g instanceof MultiPoint) { + return g; + } + // LineString also handles LinearRings + else if (g instanceof LinearRing) { + return clean((LinearRing) g); + } else if (g instanceof LineString) { + return clean((LineString) g); + } else if (g instanceof Polygon) { + return clean((Polygon) g); + } else if (g instanceof MultiLineString) { + return clean((MultiLineString) g); + } else if (g instanceof MultiPolygon) { + return clean((MultiPolygon) g); + } else if (g instanceof GeometryCollection) { + return clean((GeometryCollection) g); + } else { + throw new UnsupportedOperationException(g.getClass().getName()); + } + } + + private LinearRing clean(LinearRing g) { + Coordinate[] coords = removeDuplicatePoints(g.getCoordinates()); + return fact.createLinearRing(coords); + } + + private LineString clean(LineString g) { Review Comment: ## Confusing overloading of methods Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](1), since dispatch depends on static types. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/865) ########## baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CleanDuplicatePoints.java: ########## @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap.function; + +import java.util.ArrayList; +import java.util.List; +import org.locationtech.jts.geom.*; + +public class CleanDuplicatePoints { + + public static Coordinate[] removeDuplicatePoints(Coordinate[] coord) { + List uniqueCoords = new ArrayList(); + Coordinate lastPt = null; + for (int i = 0; i < coord.length; i++) { + if (lastPt == null || !lastPt.equals(coord[i])) { + lastPt = coord[i]; + uniqueCoords.add(new Coordinate(lastPt)); + } + } + return (Coordinate[]) uniqueCoords.toArray(new Coordinate[0]); + } + + private GeometryFactory fact; + + public CleanDuplicatePoints() {} + + public Geometry clean(Geometry g) { Review Comment: ## Confusing overloading of methods Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](1), since dispatch depends on static types. Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](2), since dispatch depends on static types. Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](3), since dispatch depends on static types. Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](4), since dispatch depends on static types. Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](5), since dispatch depends on static types. Method CleanDuplicatePoints.clean(..) could be confused with overloaded method [clean](6), since dispatch depends on static types. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/863) ########## baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestRunner.java: ########## @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; +import org.apache.baremaps.openstreetmap.model.Element; +import org.apache.baremaps.openstreetmap.store.MockDataMap; +import org.apache.baremaps.openstreetmap.xml.XmlEntityReader; +import org.apache.baremaps.testing.TestFiles; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.MultiPolygon; +import org.locationtech.jts.io.WKTReader; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.DeserializationFeature; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; + + +public class OsmTestRunner { + + @TestFactory + public Stream<DynamicTest> runTestsFromFiles() throws IOException { + var directory = TestFiles.resolve("osm-testdata"); + try (var files = Files.walk(directory)) { + return files.filter(f -> f.endsWith("test.json")) + .map(OsmTest::new) + .filter(OsmTest::isValid) + .sorted() + .map(this::createDynamicTest) + .toList().stream(); + } + } + + @NotNull + private DynamicTest createDynamicTest(OsmTest testFile) { + var displayName = String.format("%s: %s", testFile.getId(), testFile.getDescription()); + return DynamicTest.dynamicTest(displayName, () -> runTest(testFile)); + } + + public void runTest(OsmTest osmTest) { + try { + var elements = new XmlEntityReader() + .coordinateMap(new MockDataMap<>()) + .referenceMap(new MockDataMap<>()) + .geometries(true) + .stream(Files.newInputStream(osmTest.getOsmXml())) + .filter(e -> e instanceof Element) + .map(e -> (Element) e) + .toList(); + + for (var element : elements) { + // Each element should have a geometry + var id = element.getId(); + var fileGeometry = element.getGeometry(); + assertNotNull(fileGeometry); + + // Prepare the test geometry + var testWkt = osmTest.getWkts().get(id); + Geometry testGeometry = null; + try { + testGeometry = new WKTReader().read(testWkt); + } catch (Exception e) { + // ignore + } + if (testGeometry instanceof LineString lineString + && lineString.isClosed()) { + testGeometry = + testGeometry.getFactory().createPolygon(lineString.getCoordinateSequence()); + } + if (testGeometry instanceof MultiPolygon multiPolygon + && multiPolygon.getNumGeometries() == 1) { + testGeometry = multiPolygon.getGeometryN(0); + } + + // The test geometry and the file geometry should be equal + assertTrue(String.format("The geometries are not equal\nExpected:\n%s\nActual:\n%s", + testGeometry, fileGeometry), testGeometry.norm().equalsExact(fileGeometry.norm())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private class OsmTest implements Comparable<OsmTest> { + + private static final ObjectMapper objectMapper = + new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + + private final Path jsonFile; + + private final JsonNode testJson; + + public OsmTest(Path jsonFile) { + this.jsonFile = jsonFile; + try { + this.testJson = objectMapper.readValue(jsonFile.toFile(), JsonNode.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public Long getId() { + return testJson.get("test_id").asLong(); + } + + public String getDescription() { + return testJson.get("description").asText(); + } + + public Path getJsonFile() { + return jsonFile; + } + + public Path getDirectory() { + return jsonFile.getParent(); + } + + public boolean isValid() { + try { + return Files.readString(getDirectory().resolve("result")).trim().equals("valid"); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + public JsonNode getTestJson() { + return testJson; + } + + public Map<Long, String> getWkts() { + var wkts = new HashMap<Long, String>(); + wkts.putAll(getNodes()); + wkts.putAll(getWays()); + wkts.putAll(getRelations()); + wkts.putAll(getAreas()); + return wkts; + } + + public Map<Long, String> getNodes() { + var wkts = new HashMap<Long, String>(); + extractWktFile("nodes.wkt", wkts); + return wkts; + } + + public Map<Long, String> getWays() { + var wkts = new HashMap<Long, String>(); + extractWktFile("ways.wkt", wkts); + return wkts; + } + + public Map<Long, String> getRelations() { + var wkts = new HashMap<Long, String>(); + extractWktFile("relations.wkt", wkts); + return wkts; + } + + public Map<Long, String> getAreas() { + var wkts = new HashMap<Long, String>(); + if (testJson.has("areas")) { + var areas = testJson.get("areas"); + extractAreas(areas, "default", wkts); + extractAreas(areas, "location", wkts); + extractAreas(areas, "fixed", wkts); + extractAreas(areas, "fix", wkts); + } + return wkts; + } + + private void extractAreas(JsonNode areas, String name, Map<Long, String> wkts) { + if (areas.has(name)) { + var defaultAreas = areas.get(name); + if (defaultAreas.isArray()) { + for (var area : defaultAreas) { + if (area.has("wkt")) { + var wkt = area.get("wkt").asText(); + var id = area.get("from_id").asLong(); + wkts.put(id, wkt); + } + } + } + } + } + + private void extractWktFile(String wktFile, Map<Long, String> wkts) { + parseWkt(getDirectory().resolve(wktFile), wkts); + } + + private void parseWkt(Path path, Map<Long, String> geometries) { + if (!Files.exists(path)) { + return; + } + try { + Files.lines(path).forEach(line -> { + var needle = line.indexOf(' '); + var id = Long.parseLong(line.substring(0, needle).strip()); Review Comment: ## Missing catch of NumberFormatException Potential uncaught 'java.lang.NumberFormatException'. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/866) ########## baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestRunner.java: ########## @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; +import org.apache.baremaps.openstreetmap.model.Element; +import org.apache.baremaps.openstreetmap.store.MockDataMap; +import org.apache.baremaps.openstreetmap.xml.XmlEntityReader; +import org.apache.baremaps.testing.TestFiles; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.MultiPolygon; +import org.locationtech.jts.io.WKTReader; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.DeserializationFeature; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; + + +public class OsmTestRunner { + + @TestFactory + public Stream<DynamicTest> runTestsFromFiles() throws IOException { + var directory = TestFiles.resolve("osm-testdata"); + try (var files = Files.walk(directory)) { + return files.filter(f -> f.endsWith("test.json")) + .map(OsmTest::new) + .filter(OsmTest::isValid) + .sorted() + .map(this::createDynamicTest) + .toList().stream(); + } + } + + @NotNull + private DynamicTest createDynamicTest(OsmTest testFile) { + var displayName = String.format("%s: %s", testFile.getId(), testFile.getDescription()); + return DynamicTest.dynamicTest(displayName, () -> runTest(testFile)); + } + + public void runTest(OsmTest osmTest) { + try { + var elements = new XmlEntityReader() + .coordinateMap(new MockDataMap<>()) + .referenceMap(new MockDataMap<>()) + .geometries(true) + .stream(Files.newInputStream(osmTest.getOsmXml())) + .filter(e -> e instanceof Element) + .map(e -> (Element) e) + .toList(); + + for (var element : elements) { + // Each element should have a geometry + var id = element.getId(); + var fileGeometry = element.getGeometry(); + assertNotNull(fileGeometry); + + // Prepare the test geometry + var testWkt = osmTest.getWkts().get(id); + Geometry testGeometry = null; + try { + testGeometry = new WKTReader().read(testWkt); + } catch (Exception e) { + // ignore + } + if (testGeometry instanceof LineString lineString + && lineString.isClosed()) { + testGeometry = + testGeometry.getFactory().createPolygon(lineString.getCoordinateSequence()); + } + if (testGeometry instanceof MultiPolygon multiPolygon + && multiPolygon.getNumGeometries() == 1) { + testGeometry = multiPolygon.getGeometryN(0); + } + + // The test geometry and the file geometry should be equal + assertTrue(String.format("The geometries are not equal\nExpected:\n%s\nActual:\n%s", + testGeometry, fileGeometry), testGeometry.norm().equalsExact(fileGeometry.norm())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private class OsmTest implements Comparable<OsmTest> { Review Comment: ## Inconsistent compareTo This class declares [compareTo](1) but inherits equals; the two could be inconsistent. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/870) ########## baremaps-core/src/main/java/org/apache/baremaps/openstreetmap/function/CleanDuplicatePoints.java: ########## @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap.function; + +import java.util.ArrayList; +import java.util.List; +import org.locationtech.jts.geom.*; + +public class CleanDuplicatePoints { + + public static Coordinate[] removeDuplicatePoints(Coordinate[] coord) { + List uniqueCoords = new ArrayList(); + Coordinate lastPt = null; + for (int i = 0; i < coord.length; i++) { + if (lastPt == null || !lastPt.equals(coord[i])) { + lastPt = coord[i]; + uniqueCoords.add(new Coordinate(lastPt)); + } + } + return (Coordinate[]) uniqueCoords.toArray(new Coordinate[0]); + } + + private GeometryFactory fact; + + public CleanDuplicatePoints() {} + + public Geometry clean(Geometry g) { + fact = g.getFactory(); + if (g.isEmpty()) { + return g; + } + if (g instanceof Point) { Review Comment: ## Chain of 'instanceof' tests This if block performs a chain of 8 type tests - consider alternatives, e.g. polymorphism or the visitor pattern. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/868) ########## baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestRunner.java: ########## @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; +import org.apache.baremaps.openstreetmap.model.Element; +import org.apache.baremaps.openstreetmap.store.MockDataMap; +import org.apache.baremaps.openstreetmap.xml.XmlEntityReader; +import org.apache.baremaps.testing.TestFiles; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.MultiPolygon; +import org.locationtech.jts.io.WKTReader; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.DeserializationFeature; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; + + +public class OsmTestRunner { + + @TestFactory + public Stream<DynamicTest> runTestsFromFiles() throws IOException { + var directory = TestFiles.resolve("osm-testdata"); + try (var files = Files.walk(directory)) { + return files.filter(f -> f.endsWith("test.json")) + .map(OsmTest::new) + .filter(OsmTest::isValid) + .sorted() + .map(this::createDynamicTest) + .toList().stream(); + } + } + + @NotNull + private DynamicTest createDynamicTest(OsmTest testFile) { + var displayName = String.format("%s: %s", testFile.getId(), testFile.getDescription()); + return DynamicTest.dynamicTest(displayName, () -> runTest(testFile)); + } + + public void runTest(OsmTest osmTest) { + try { + var elements = new XmlEntityReader() + .coordinateMap(new MockDataMap<>()) + .referenceMap(new MockDataMap<>()) + .geometries(true) + .stream(Files.newInputStream(osmTest.getOsmXml())) + .filter(e -> e instanceof Element) + .map(e -> (Element) e) + .toList(); + + for (var element : elements) { + // Each element should have a geometry + var id = element.getId(); + var fileGeometry = element.getGeometry(); + assertNotNull(fileGeometry); + + // Prepare the test geometry + var testWkt = osmTest.getWkts().get(id); + Geometry testGeometry = null; + try { + testGeometry = new WKTReader().read(testWkt); + } catch (Exception e) { + // ignore + } + if (testGeometry instanceof LineString lineString + && lineString.isClosed()) { + testGeometry = + testGeometry.getFactory().createPolygon(lineString.getCoordinateSequence()); + } + if (testGeometry instanceof MultiPolygon multiPolygon + && multiPolygon.getNumGeometries() == 1) { + testGeometry = multiPolygon.getGeometryN(0); + } + + // The test geometry and the file geometry should be equal + assertTrue(String.format("The geometries are not equal\nExpected:\n%s\nActual:\n%s", + testGeometry, fileGeometry), testGeometry.norm().equalsExact(fileGeometry.norm())); Review Comment: ## Dereferenced variable may be null Variable [testGeometry](1) may be null at this access because of [this](2) assignment. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/869) ########## baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestRunner.java: ########## @@ -0,0 +1,243 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.baremaps.openstreetmap; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Stream; +import org.apache.baremaps.openstreetmap.model.Element; +import org.apache.baremaps.openstreetmap.store.MockDataMap; +import org.apache.baremaps.openstreetmap.xml.XmlEntityReader; +import org.apache.baremaps.testing.TestFiles; +import org.jetbrains.annotations.NotNull; +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.TestFactory; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.MultiPolygon; +import org.locationtech.jts.io.WKTReader; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.DeserializationFeature; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.JsonNode; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; + + +public class OsmTestRunner { + + @TestFactory + public Stream<DynamicTest> runTestsFromFiles() throws IOException { + var directory = TestFiles.resolve("osm-testdata"); + try (var files = Files.walk(directory)) { + return files.filter(f -> f.endsWith("test.json")) + .map(OsmTest::new) + .filter(OsmTest::isValid) + .sorted() + .map(this::createDynamicTest) + .toList().stream(); + } + } + + @NotNull + private DynamicTest createDynamicTest(OsmTest testFile) { + var displayName = String.format("%s: %s", testFile.getId(), testFile.getDescription()); + return DynamicTest.dynamicTest(displayName, () -> runTest(testFile)); + } + + public void runTest(OsmTest osmTest) { + try { + var elements = new XmlEntityReader() + .coordinateMap(new MockDataMap<>()) + .referenceMap(new MockDataMap<>()) + .geometries(true) + .stream(Files.newInputStream(osmTest.getOsmXml())) + .filter(e -> e instanceof Element) + .map(e -> (Element) e) + .toList(); + + for (var element : elements) { + // Each element should have a geometry + var id = element.getId(); + var fileGeometry = element.getGeometry(); + assertNotNull(fileGeometry); + + // Prepare the test geometry + var testWkt = osmTest.getWkts().get(id); + Geometry testGeometry = null; + try { + testGeometry = new WKTReader().read(testWkt); + } catch (Exception e) { + // ignore + } + if (testGeometry instanceof LineString lineString + && lineString.isClosed()) { + testGeometry = + testGeometry.getFactory().createPolygon(lineString.getCoordinateSequence()); + } + if (testGeometry instanceof MultiPolygon multiPolygon + && multiPolygon.getNumGeometries() == 1) { + testGeometry = multiPolygon.getGeometryN(0); + } + + // The test geometry and the file geometry should be equal + assertTrue(String.format("The geometries are not equal\nExpected:\n%s\nActual:\n%s", + testGeometry, fileGeometry), testGeometry.norm().equalsExact(fileGeometry.norm())); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private class OsmTest implements Comparable<OsmTest> { Review Comment: ## Inner class could be static OsmTest should be made static, since the enclosing instance is not used. [Show more details](https://github.com/apache/incubator-baremaps/security/code-scanning/867) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
