From 3565fcb2321b017c3d3907f2ee8a58d81f9230fc Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 24 Jun 2020 15:17:24 -0600 Subject: [PATCH] Wiremock conflation servers Signed-off-by: Taylor Smock --- .../MapWithAIConflationCategory.java | 32 +++++++++++++++- ...-fb2197f1-81fd-445d-bb90-b6219d187af6.json | 24 ++++++++++++ .../testutils/MapWithAITestRules.java | 38 ++++++++++++------- 3 files changed, 79 insertions(+), 15 deletions(-) create mode 100644 test/resources/wiremock/mappings/josm_mapwithai_json_conflation_serversjson-fb2197f1-81fd-445d-bb90-b6219d187af6.json diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIConflationCategory.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIConflationCategory.java index c898d61..6ff4812 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIConflationCategory.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIConflationCategory.java @@ -15,13 +15,15 @@ import org.openstreetmap.josm.tools.Logging; public class MapWithAIConflationCategory { private static final Map> CONFLATION_URLS = new EnumMap<>(MapWithAICategory.class); private static final String EMPTY_URL = ""; - protected static final String DEFAULT_CONFLATION_JSON = "https://gokaart.gitlab.io/JOSM_MapWithAI/json/conflation_servers.json"; + private static final String DEFAULT_CONFLATION_JSON = "https://gokaart.gitlab.io/JOSM_MapWithAI/json/conflation_servers.json"; + private static String conflationJson = DEFAULT_CONFLATION_JSON; static { initialize(); } static void initialize() { - try (ConflationSourceReader reader = new ConflationSourceReader(DEFAULT_CONFLATION_JSON)) { + CONFLATION_URLS.clear(); + try (ConflationSourceReader reader = new ConflationSourceReader(conflationJson)) { CONFLATION_URLS.putAll(reader.parse()); } catch (IOException e) { Logging.error(e); @@ -48,4 +50,30 @@ public class MapWithAIConflationCategory { Collection list = CONFLATION_URLS.computeIfAbsent(category, i -> new ArrayList<>(1)); list.add(url); } + + /** + * Set the URL to use to get conflation servers + * + * @param url The URL to use + */ + public static void setConflationJsonLocation(String url) { + conflationJson = url; + initialize(); + } + + /** + * Reset the conflation json location to the default location + */ + public static void resetConflationJsonLocation() { + setConflationJsonLocation(DEFAULT_CONFLATION_JSON); + } + + /** + * Get the current conflation json location + * + * @return The URL that is used to build conflation information + */ + public static String getConflationJsonLocation() { + return conflationJson; + } } diff --git a/test/resources/wiremock/mappings/josm_mapwithai_json_conflation_serversjson-fb2197f1-81fd-445d-bb90-b6219d187af6.json b/test/resources/wiremock/mappings/josm_mapwithai_json_conflation_serversjson-fb2197f1-81fd-445d-bb90-b6219d187af6.json new file mode 100644 index 0000000..9cf9636 --- /dev/null +++ b/test/resources/wiremock/mappings/josm_mapwithai_json_conflation_serversjson-fb2197f1-81fd-445d-bb90-b6219d187af6.json @@ -0,0 +1,24 @@ +{ + "id" : "fb2197f1-81fd-445d-bb90-b6219d187af6", + "name" : "josm_mapwithai_json_conflation_serversjson", + "request" : { + "url" : "/JOSM_MapWithAI/json/conflation_servers.json", + "method" : "GET" + }, + "response" : { + "status" : 200, + "body" : "{\n \"Taylor's Address Conflation Server\" : {\n \"categories\" : [\n \"addresses\"\n ],\n \"description\" : \"Originally developed for use with local datasets, it now accepts external datasets for conflation purposes. In the event of a failure, the plugin will use the original dataset.\",\n \"license\" : \"AGPL\",\n \"source\" : \"https://gitlab.com/smocktaylor/serve_osm_files/\",\n \"url\" : \"https://importdata.riverviewtechnologies.com/conflate\"\n }\n}\n", + "headers" : { + "Accept-Ranges" : "bytes", + "Cache-Control" : "max-age=600", + "Content-Type" : "application/json", + "Expires" : "Wed, 24 Jun 2020 21:26:07 UTC", + "Last-Modified" : "Wed, 24 Jun 2020 17:33:46 GMT", + "Vary" : "Origin", + "Date" : "Wed, 24 Jun 2020 21:16:07 GMT" + } + }, + "uuid" : "fb2197f1-81fd-445d-bb90-b6219d187af6", + "persistent" : true, + "insertionIndex" : 43 +} \ No newline at end of file diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java index d33787b..df775d2 100644 --- a/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/testutils/MapWithAITestRules.java @@ -23,6 +23,7 @@ import org.openstreetmap.josm.io.OsmApiInitializationException; import org.openstreetmap.josm.io.OsmTransferCanceledException; import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIDataUtils; +import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIConflationCategory; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo; import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAILayerInfo; import org.openstreetmap.josm.spi.preferences.Config; @@ -90,22 +91,15 @@ public class MapWithAITestRules extends JOSMTestRules { wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock") .extensions(new WireMockUrlTransformer()).dynamicPort()); wireMock.start(); - MapWithAIDataUtils.setPaintStyleUrl(MapWithAIDataUtils.getPaintStyleUrl() - .replace(Config.getUrls().getJOSMWebsite(), wireMock.baseUrl())); + MapWithAIDataUtils.setPaintStyleUrl(replaceUrl(wireMock, MapWithAIDataUtils.getPaintStyleUrl())); currentReleaseUrl = DataAvailability.getReleaseUrl(); - DataAvailability - .setReleaseUrl(wireMock.baseUrl() + "/gokaart/JOSM_MapWithAI/-/raw/pages/public/json/sources.json"); + DataAvailability.setReleaseUrl(replaceUrl(wireMock, DataAvailability.getReleaseUrl())); Config.getPref().put("osm-server.url", wireMock.baseUrl()); sourceSites = MapWithAILayerInfo.getImageryLayersSites(); - MapWithAILayerInfo.setImageryLayersSites(sourceSites.stream().map(t -> { - try { - URL temp = new URL(t); - return wireMock.baseUrl() + temp.getFile(); - } catch (MalformedURLException error) { - Logging.error(error); - } - return null; - }).filter(Objects::nonNull).collect(Collectors.toList())); + MapWithAILayerInfo.setImageryLayersSites(sourceSites.stream().map(t -> replaceUrl(wireMock, t)) + .filter(Objects::nonNull).collect(Collectors.toList())); + MapWithAIConflationCategory.setConflationJsonLocation( + replaceUrl(wireMock, MapWithAIConflationCategory.getConflationJsonLocation())); try { OsmApi.getOsmApi().initialize(NullProgressMonitor.INSTANCE); } catch (OsmTransferCanceledException | OsmApiInitializationException e) { @@ -135,6 +129,23 @@ public class MapWithAITestRules extends JOSMTestRules { } } + /** + * Replace URL servers with wiremock + * + * @param wireMock The wiremock to point to + * @param url The URL to fix + * @return A url that points at the wiremock server + */ + private static String replaceUrl(WireMockServer wireMock, String url) { + try { + URL temp = new URL(url); + return wireMock.baseUrl() + temp.getFile(); + } catch (MalformedURLException error) { + Logging.error(error); + } + return null; + } + @Override protected void after() throws ReflectiveOperationException { super.after(); @@ -146,6 +157,7 @@ public class MapWithAITestRules extends JOSMTestRules { DataAvailability.setReleaseUrl(currentReleaseUrl); Config.getPref().put("osm-server.url", null); MapWithAILayerInfo.setImageryLayersSites(sourceSites); + MapWithAIConflationCategory.resetConflationJsonLocation(); resetMapWithAILayerInfo(); } if (workerExceptions) {