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 <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-05-29 11:16:48 -06:00
rodzic 4c15c59df5
commit b01b89e0d5
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
6 zmienionych plików z 184 dodań i 14 usunięć

Wyświetl plik

@ -11,6 +11,11 @@
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="build/bintest" path="test/integration">
<attributes>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry combineaccessrules="false" kind="src" path="/JOSM"/>
<classpathentry combineaccessrules="false" kind="src" path="/JOSM-utilsplugin2"/>

Wyświetl plik

@ -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")

Wyświetl plik

@ -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<MapWithAIInfo> parseJson(JsonObject jsonObject) {
return jsonObject.entrySet().stream().map(MapWithAISourceReader::parse).collect(Collectors.toList());
}
private static MapWithAIInfo parse(Map.Entry<String, JsonValue> entry) {
String name = entry.getKey();
if (JsonValue.ValueType.OBJECT.equals(entry.getValue().getValueType())) {

Wyświetl plik

@ -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<MapWithAIInfo> 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()));
}
}
}
}

Wyświetl plik

@ -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<Arguments> 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()));
}
}

Wyświetl plik

@ -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<MapWithAIInfo> 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<MapWithAIInfo> 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());
}
}