From b01b89e0d5c1f42f41875212dd2d704fe4c8c7dc Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Fri, 29 May 2020 11:16:48 -0600 Subject: [PATCH] Add tests for MapWithAIInfo and MapWithAISourceReader There is now an integration test directory, which is specifically for any tests that may call an exterior server (i.e., mapwith.ai). Signed-off-by: Taylor Smock --- .classpath | 5 ++ build.gradle | 53 ++++++++++++++----- .../io/mapwithai/MapWithAISourceReader.java | 12 ++++- .../MapWithAISourceReaderTestIT.java | 34 ++++++++++++ .../data/mapwithai/MapWithAIInfoTest.java | 42 +++++++++++++++ .../mapwithai/MapWithAISourceReaderTest.java | 52 ++++++++++++++++++ 6 files changed, 184 insertions(+), 14 deletions(-) create mode 100644 test/integration/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTestIT.java create mode 100644 test/unit/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfoTest.java create mode 100644 test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTest.java diff --git a/.classpath b/.classpath index 552d62b..43a5c6b 100644 --- a/.classpath +++ b/.classpath @@ -11,6 +11,11 @@ + + + + + diff --git a/build.gradle b/build.gradle index e2bf41e..e3586ed 100644 --- a/build.gradle +++ b/build.gradle @@ -42,6 +42,29 @@ archivesBaseName = "mapwithai" def gitlabGroup = "gokaart" def gitlabRepositoryName = "JOSM_MapWithAI" +repositories { + jcenter() + mavenCentral() +} + +sourceSets { + test { + java { + srcDirs = ["test/unit"] + } + resources { + srcDirs = ["test/data"] + } + } + intTest { + java { + srcDirs = ["test/integration"] + } + resources { + srcDirs = ["test/data"] + } + } +} def versions = [ awaitility: "4.0.2", @@ -55,10 +78,6 @@ def versions = [ findsecbugs: "1.10.1", ] -repositories { - jcenter() - mavenCentral() -} dependencies { if (!JavaVersion.current().isJava9Compatible()) { errorproneJavac("com.google.errorprone:javac:9+181-r4173-1") @@ -69,6 +88,7 @@ dependencies { testImplementation("org.junit.jupiter:junit-jupiter-api:${versions.junit}") testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:${versions.junit}") testImplementation("org.junit.vintage:junit-vintage-engine:${versions.junit}") + testCompile("org.junit.jupiter:junit-jupiter-params:${versions.junit}") testCompile("org.jmockit:jmockit:${versions.jmockit}") testImplementation("com.github.spotbugs:spotbugs-annotations:${versions.spotbugs}") testImplementation("org.openstreetmap.josm:josm-unittest:"){changing=true} @@ -76,6 +96,11 @@ dependencies { testImplementation("org.awaitility:awaitility:${versions.awaitility}") } +configurations { + intTestImplementation.extendsFrom testImplementation + intTestRuntimeOnly.extendsFrom testRuntimeOnly +} + // Add dependencies from ivy.xml def ivyModule = new XmlParser().parse(new File("$projectDir/ivy.xml")) logger.info("Dependencies from ivy.xml (added to configuration `packIntoJar`):") @@ -91,6 +116,7 @@ test { jvmArgs("-javaagent:${classpath.find { it.name.contains("jmockit") }.absolutePath}") } useJUnitPlatform() + ignoreFailures testLogging { exceptionFormat "full" events "skipped", "failed" @@ -100,16 +126,17 @@ test { } } -sourceSets { - test { - java { - srcDirs = ["test/unit"] - } - resources { - srcDirs = ["test/data"] - } - } +task integrationTest(type: Test) { + description = "Run integration tests" + group = "verification" + + testClassesDirs = sourceSets.intTest.output.classesDirs + classpath = sourceSets.intTest.runtimeClasspath + shouldRunAfter test } + +check.dependsOn integrationTest + tasks.processResources { from("$projectDir/LICENSE") from("$projectDir/README.md") diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReader.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReader.java index c1d7191..871eb67 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReader.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReader.java @@ -87,12 +87,22 @@ public class MapWithAISourceReader implements Closeable { JsonStructure struct = reader.read(); if (JsonValue.ValueType.OBJECT.equals(struct.getValueType())) { JsonObject jsonObject = struct.asJsonObject(); - entries = jsonObject.entrySet().stream().map(MapWithAISourceReader::parse).collect(Collectors.toList()); + entries = parseJson(jsonObject); } return entries; } } + /** + * Parses MapWithAI entry sources + * + * @param jsonObject The json of the data sources + * @return The parsed entries + */ + public static List parseJson(JsonObject jsonObject) { + return jsonObject.entrySet().stream().map(MapWithAISourceReader::parse).collect(Collectors.toList()); + } + private static MapWithAIInfo parse(Map.Entry entry) { String name = entry.getKey(); if (JsonValue.ValueType.OBJECT.equals(entry.getValue().getValueType())) { diff --git a/test/integration/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTestIT.java b/test/integration/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTestIT.java new file mode 100644 index 0000000..2395be0 --- /dev/null +++ b/test/integration/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTestIT.java @@ -0,0 +1,34 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.plugins.mapwithai.io.mapwithai; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.openstreetmap.josm.tools.I18n.tr; + +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + +import org.junit.Rule; +import org.junit.Test; +import org.openstreetmap.josm.plugins.mapwithai.backend.DataAvailability; +import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo; +import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo.MapWithAIType; +import org.openstreetmap.josm.testutils.JOSMTestRules; + +public class MapWithAISourceReaderTestIT { + @Rule + public JOSMTestRules rule = new JOSMTestRules().preferences().territories().projection(); + + @Test + public void testDefaultSourceIT() throws IOException { + try (MapWithAISourceReader source = new MapWithAISourceReader(DataAvailability.DEFAULT_SERVER_URL)) { + List infoList = source.parse(); + assertFalse(infoList.isEmpty()); + for (MapWithAIType type : Arrays.asList(MapWithAIType.FACEBOOK, MapWithAIType.THIRD_PARTY)) { + assertTrue(infoList.stream().filter(i -> type.equals(i.getSourceType())).count() > 0, + tr("Type {0} should have more than 0 sources", type.getTypeString())); + } + } + } +} diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfoTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfoTest.java new file mode 100644 index 0000000..ee63dd3 --- /dev/null +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/data/mapwithai/MapWithAIInfoTest.java @@ -0,0 +1,42 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.plugins.mapwithai.data.mapwithai; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.stream.Stream; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +public class MapWithAIInfoTest { + + @ParameterizedTest + @MethodSource("provideMapWithAIInfoInitializers") + public void assertInitializersWorked(MapWithAIInfo i, String name, String url, String id, + MapWithAIInfo.MapWithAIType type) { + assertEquals(name, i.getName()); + assertEquals(id, i.getId()); + assertEquals(url, i.getUrl()); + assertEquals(type, i.getSourceType()); + } + + private static Stream provideMapWithAIInfoInitializers() { + String name = "TEST"; + String url = "https://test.url"; + String id = "a;lkdjfadl;ksfj"; + String eula = ""; + MapWithAIInfo.MapWithAIType type = MapWithAIInfo.MapWithAIType.FACEBOOK; + MapWithAIInfo.MapWithAIType defaultType = MapWithAIInfo.MapWithAIType.THIRD_PARTY; + MapWithAIInfo tempInfo = new MapWithAIInfo(name, url, id); + return Stream.of(Arguments.of(new MapWithAIInfo(), null, null, null, defaultType), + Arguments.of(new MapWithAIInfo(name), name, null, null, defaultType), + Arguments.of(new MapWithAIInfo(name, url), name, url, null, defaultType), + Arguments.of(new MapWithAIInfo(name, url, id), name, url, id, defaultType), + Arguments.of(new MapWithAIInfo(name, url, type.getTypeString(), eula, id), name, url, id, type), + Arguments.of(new MapWithAIInfo(name, url, null, eula, id), name, url, id, defaultType), + Arguments.of(new MapWithAIInfo(name, url, "", eula, id), name, url, id, defaultType), + Arguments.of(new MapWithAIInfo(tempInfo), tempInfo.getName(), tempInfo.getUrl(), tempInfo.getId(), + tempInfo.getSourceType())); + } +} diff --git a/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTest.java b/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTest.java new file mode 100644 index 0000000..7fa606e --- /dev/null +++ b/test/unit/org/openstreetmap/josm/plugins/mapwithai/io/mapwithai/MapWithAISourceReaderTest.java @@ -0,0 +1,52 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.plugins.mapwithai.io.mapwithai; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import java.util.Collections; +import java.util.List; + +import javax.json.Json; +import javax.json.JsonArrayBuilder; +import javax.json.JsonObjectBuilder; +import javax.json.JsonValue; + +import org.junit.Rule; +import org.junit.Test; +import org.openstreetmap.josm.plugins.mapwithai.data.mapwithai.MapWithAIInfo; +import org.openstreetmap.josm.testutils.JOSMTestRules; + +import com.google.common.collect.ImmutableMap; + +public class MapWithAISourceReaderTest { + @Rule + public JOSMTestRules rule = new JOSMTestRules().preferences().territories().projection(); + + @Test + public void testParseSimple() { + JsonObjectBuilder builder = Json.createObjectBuilder(); + builder.add("nowhere", JsonValue.NULL); + List infoList = MapWithAISourceReader.parseJson(builder.build()); + assertEquals(1, infoList.size()); + assertEquals("nowhere", infoList.get(0).getName()); + } + + @Test + public void testParseComplex() { + JsonObjectBuilder builder = Json.createObjectBuilder(); + JsonObjectBuilder co = Json.createObjectBuilder( + ImmutableMap.of("url", "test", "license", "pd", "permission_url", "https://permission.url")); + JsonObjectBuilder coCountries = Json.createObjectBuilder(); + JsonArrayBuilder coCountriesArray = Json.createArrayBuilder(Collections.singleton("addr:housenumber")); + coCountries.add("US-CO", coCountriesArray.build()); + co.add("countries", coCountries.build()); + builder.add("Colorado", co); + List infoList = MapWithAISourceReader.parseJson(builder.build()); + assertEquals(1, infoList.size()); + MapWithAIInfo info = infoList.stream().filter(i -> "Colorado".equals(i.getName())).findFirst().orElse(null); + assertNotNull(info); + assertEquals("Colorado", info.getName()); + assertEquals("test", info.getUrl()); + } +}