Skip to content

Commit

Permalink
Add initial code to check if this version of the plugin should not up…
Browse files Browse the repository at this point in the history
…load to OSM

Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Jan 13, 2021
1 parent 7986d7e commit 20f1388
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 0 deletions.
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;
}
}
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());
}
}

0 comments on commit 20f1388

Please sign in to comment.