From 9bbf637d3145028c19037f2e1f9d82b8f82c4aa5 Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Mon, 24 Feb 2020 15:56:39 -0700 Subject: [PATCH] More testing Signed-off-by: Taylor Smock --- .../mapwithai/backend/DataAvailability.java | 3 +- .../mapwithai/backend/GetDataRunnable.java | 8 ++--- .../backend/MapWithAIPreferenceHelper.java | 2 +- .../backend/MapWithAIAvailabilityTest.java | 35 +++++++++++++++++++ .../testutils/OpenBrowserMocker.java | 1 + 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java index 33f7bd0..50f1254 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/DataAvailability.java @@ -191,7 +191,8 @@ public class DataAvailability { * @return the unique instance */ public static DataAvailability getInstance() { - if (InstanceHelper.instance == null || COUNTRIES.isEmpty()) { + if (InstanceHelper.instance == null || COUNTRIES.isEmpty() + || MapWithAIPreferenceHelper.getMapWithAIUrl().isEmpty()) { InstanceHelper.instance = new DataAvailability(); } return InstanceHelper.instance; diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnable.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnable.java index e379a29..4691d0a 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnable.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/GetDataRunnable.java @@ -300,12 +300,8 @@ public class GetDataRunnable extends RecursiveTask { } private static boolean distanceCheck(Node nearNode, Node node, Double distance) { - try { - return nearNode.getCoor().greatCircleDistance(node.getCoor()) < distance; - } catch (Exception e) { - Logging.error(e); - return false; - } + return nearNode == null || node == null ? false + : nearNode.getCoor().greatCircleDistance(node.getCoor()) < distance; } private static boolean keyCheck(Node nearNode, Node node) { diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java index 237a593..a4489ba 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIPreferenceHelper.java @@ -180,7 +180,7 @@ public final class MapWithAIPreferenceHelper { * @return true if the configuration changed */ public static boolean setMapWithAIURLs(List> urls) { - final List> setUrls = urls; + final List> setUrls = urls.isEmpty() ? new ArrayList<>() : urls; if (urls.isEmpty()) { final TreeMap defaultAPIMap = new TreeMap<>(); defaultAPIMap.put(URL_STRING, DEFAULT_MAPWITHAI_API); diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java index 398414d..ee55655 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/backend/MapWithAIAvailabilityTest.java @@ -3,9 +3,13 @@ package org.openstreetmap.josm.plugins.mapwithai.backend; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static org.awaitility.Awaitility.await; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.util.Collections; + import org.awaitility.Durations; import org.junit.After; import org.junit.Before; @@ -75,4 +79,35 @@ public class MapWithAIAvailabilityTest { assertFalse(DataAvailability.getDataTypes(new LatLon(19.433333, -99.133333)).getOrDefault("building", false), "Mexico does not yet have building data"); } + + @Test + public void testNoURLs() { + MapWithAIPreferenceHelper.setMapWithAIURLs(Collections.emptyList()); + DataAvailability.getInstance(); + testgetDataLatLon(); + MapWithAIPreferenceHelper.setMapWithAIURLs(Collections.emptyList()); + DataAvailability.getInstance(); + testHasDataLatLon(); + MapWithAIPreferenceHelper.setMapWithAIURLs(Collections.emptyList()); + DataAvailability.getInstance(); + testHasDataBBox(); + } + + @Test + public void testGetPrivacyUrls() { + assertFalse(DataAvailability.getPrivacyPolicy().isEmpty()); + } + + @Test + public void testGetTOSUrls() { + assertFalse(DataAvailability.getTermsOfUse().isEmpty()); + } + + @Test + public void testDefaultUrlImplementations() { + DataAvailability instance = DataAvailability.getInstance(); + assertNull(instance.getUrl()); + assertEquals("", instance.getPrivacyPolicyUrl()); + assertEquals("", instance.getTermsOfUseUrl()); + } } diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/OpenBrowserMocker.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/OpenBrowserMocker.java index 48ba9b1..b0d1576 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/OpenBrowserMocker.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/OpenBrowserMocker.java @@ -11,6 +11,7 @@ import org.openstreetmap.josm.tools.OpenBrowser; import mockit.Mock; import mockit.MockUp; +/** TODO Remove if #18766 is merged */ public class OpenBrowserMocker extends MockUp { @Mock public static String displayUrl(URI uri) {