-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial code to check if this version of the plugin should not up…
…load to OSM Signed-off-by: Taylor Smock <[email protected]>
- Loading branch information
Showing
2 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
src/main/java/org/openstreetmap/josm/plugins/mapwithai/tools/BlacklistUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.mapwithai.tools; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.IOException; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonException; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonReader; | ||
import javax.json.JsonString; | ||
import javax.json.JsonStructure; | ||
import javax.json.JsonValue; | ||
|
||
import org.openstreetmap.josm.io.CachedFile; | ||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; | ||
import org.openstreetmap.josm.tools.Logging; | ||
|
||
/** | ||
* Check if this version has been blacklisted (i.e., bad data is uploaded) | ||
* | ||
* @author Taylor Smock | ||
* | ||
*/ | ||
public class BlacklistUtils { | ||
static final String DEFAULT_BLACKLIST_URL = "https://gokaart.gitlab.io/JOSM_MapWithAI/json/blacklisted_versions.json"; | ||
private static String blacklistUrl = DEFAULT_BLACKLIST_URL; | ||
|
||
private BlacklistUtils() { | ||
// Don't instantiate | ||
} | ||
|
||
/** | ||
* Check if this version is known to make bad data | ||
* | ||
* @return {@code true} if no data should be uploaded or added. Defaults to | ||
* {@code true}. | ||
*/ | ||
public static boolean isBlacklisted() { | ||
String version = MapWithAIPlugin.getVersionInfo(); | ||
try (CachedFile blacklist = new CachedFile(blacklistUrl); | ||
BufferedReader bufferedReader = blacklist.getContentReader(); | ||
JsonReader reader = Json.createReader(bufferedReader)) { | ||
JsonStructure structure = reader.read(); | ||
if (structure.getValueType() == JsonValue.ValueType.ARRAY) { | ||
JsonArray array = (JsonArray) structure; | ||
return array.stream().filter(v -> v.getValueType() == JsonValue.ValueType.STRING) | ||
.map(v -> ((JsonString) v).getString()).anyMatch(version::equals); | ||
} else if (structure.getValueType() == JsonValue.ValueType.OBJECT) { | ||
JsonObject object = (JsonObject) structure; | ||
return object.keySet().contains(version); | ||
} | ||
} catch (IOException | JsonException e) { | ||
Logging.error(e); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Set a new blacklist URL. Should only be used for testing. | ||
* | ||
* @param url The url to check. | ||
*/ | ||
static void setBlacklistUrl(String url) { | ||
blacklistUrl = url; | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
test/unit/org/openstreetmap/josm/plugins/mapwithai/tools/BlacklistUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// License: GPL. For details, see LICENSE file. | ||
package org.openstreetmap.josm.plugins.mapwithai.tools; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.noContent; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.notFound; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching; | ||
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
import org.openstreetmap.josm.plugins.mapwithai.MapWithAIPlugin; | ||
import org.openstreetmap.josm.testutils.JOSMTestRules; | ||
|
||
import com.github.tomakehurst.wiremock.WireMockServer; | ||
|
||
import mockit.Mock; | ||
import mockit.MockUp; | ||
|
||
/** | ||
* Tests for {@link BlacklistUtils} | ||
* | ||
* @author Taylor Smock | ||
* | ||
*/ | ||
class BlacklistUtilsTest { | ||
|
||
private static class MapWithAIPluginMock extends MockUp<MapWithAIPlugin> { | ||
@Mock | ||
public static String getVersionInfo() { | ||
return "1.0"; | ||
} | ||
} | ||
|
||
@RegisterExtension | ||
static JOSMTestRules rules = new JOSMTestRules(); | ||
|
||
private static WireMockServer wireMock; | ||
|
||
@BeforeAll | ||
static void setup() { | ||
wireMock = new WireMockServer(options().dynamicPort()); | ||
wireMock.start(); | ||
BlacklistUtils.setBlacklistUrl(wireMock.baseUrl() + "/JOSM_MapWithAI/json/blacklisted_versions.json"); | ||
new MapWithAIPluginMock(); | ||
} | ||
|
||
@BeforeEach | ||
void clear() { | ||
wireMock.resetMappings(); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
BlacklistUtils.setBlacklistUrl(BlacklistUtils.DEFAULT_BLACKLIST_URL); | ||
assertTrue(wireMock.findAllUnmatchedRequests().isEmpty()); | ||
wireMock.stop(); | ||
} | ||
|
||
@Test | ||
void testArrayBad() { | ||
wireMock.addStubMapping(get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")) | ||
.willReturn(aResponse().withStatus(200).withBody("[\"" + MapWithAIPlugin.getVersionInfo() + "\"]")) | ||
.build()); | ||
assertTrue(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testArrayGood() { | ||
wireMock.addStubMapping(get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")) | ||
.willReturn(aResponse().withStatus(200).withBody("[null, 0, false]")).build()); | ||
assertFalse(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testObjectBad() { | ||
wireMock.addStubMapping(get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")) | ||
.willReturn(aResponse().withStatus(200) | ||
.withBody("{ \"" + MapWithAIPlugin.getVersionInfo() + "\": \"reason here\"}")) | ||
.build()); | ||
assertTrue(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testObjectGood() { | ||
wireMock.addStubMapping(get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")) | ||
.willReturn(aResponse().withStatus(200).withBody("{ \"version\": \"reason here\"}")).build()); | ||
assertFalse(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testNullJson() { | ||
wireMock.addStubMapping(get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")) | ||
.willReturn(aResponse().withStatus(200).withBody("null")).build()); | ||
assertTrue(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testBrokenJson() { | ||
wireMock.addStubMapping(get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")).willReturn( | ||
aResponse().withStatus(200).withBody("{ \"" + MapWithAIPlugin.getVersionInfo() + "\": \"reason here\"")) | ||
.build()); | ||
assertTrue(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testNoResponse() { | ||
wireMock.addStubMapping( | ||
get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")).willReturn(noContent()).build()); | ||
assertTrue(BlacklistUtils.isBlacklisted()); | ||
} | ||
|
||
@Test | ||
void testNotFound() { | ||
wireMock.addStubMapping( | ||
get(urlMatching("/JOSM_MapWithAI/json/blacklisted_versions.json")).willReturn(notFound()).build()); | ||
assertTrue(BlacklistUtils.isBlacklisted()); | ||
} | ||
} |