github-advanced-security[bot] commented on code in PR #816:
URL: 
https://github.com/apache/incubator-baremaps/pull/816#discussion_r1439072571


##########
baremaps-core/src/test/java/org/apache/baremaps/openstreetmap/OsmTestData.java:
##########
@@ -0,0 +1,265 @@
+/*
+ * 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.model.Entity;
+import org.apache.baremaps.openstreetmap.pbf.PbfEntityReader;
+import org.apache.baremaps.openstreetmap.store.MockDataMap;
+import org.apache.baremaps.openstreetmap.xml.XmlEntityReader;
+import org.apache.baremaps.testing.TestFiles;
+import org.apache.baremaps.utils.RoundingTransformer;
+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.geom.util.GeometryFixer;
+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 OsmTestData {
+
+  @TestFactory
+  public Stream<DynamicTest> runTests() 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()
+          .flatMap(this::createDynamicTest)
+          .toList().stream();
+    }
+  }
+
+  @NotNull
+  private Stream<DynamicTest> createDynamicTest(OsmTest osmTest) {
+    String displayNameFormat = "%s (%s): %s";
+    var xmlDisplayName =
+        String.format(displayNameFormat, osmTest.getId(), "xml", 
osmTest.getDescription());
+    var pbfDisplayName =
+        String.format(displayNameFormat, osmTest.getId(), "pbf", 
osmTest.getDescription());
+    return Stream.<DynamicTest>builder()
+        .add(DynamicTest.dynamicTest(xmlDisplayName, () -> runTest(osmTest, 
new XmlEntityReader()
+            .coordinateMap(new MockDataMap<>())
+            .referenceMap(new MockDataMap<>())
+            .geometries(true)
+            .stream(Files.newInputStream(osmTest.getOsmXml())))))
+        .add(DynamicTest.dynamicTest(pbfDisplayName, () -> runTest(osmTest, 
new PbfEntityReader()
+            .coordinateMap(new MockDataMap<>())
+            .referenceMap(new MockDataMap<>())
+            .geometries(true)
+            .stream(Files.newInputStream(osmTest.getOsmPbf())))))
+        .build();
+  }
+
+  public void runTest(OsmTest osmTest, Stream<Entity> entities) {
+    var elements = entities
+        .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);
+      }
+      if (!testGeometry.isValid()) {

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/879)



-- 
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]

Reply via email to