Add a wiremock response transformer

This ensures that any URL's in the response are also mocked (or fails
the test).

I also fixed a listener bug for MapWithAILayerInfo.

Signed-off-by: Taylor Smock <taylor.smock@kaart.com>
pull/1/head
Taylor Smock 2020-06-22 11:12:23 -06:00
rodzic ab34c3843e
commit 69d69e8df3
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 625F6A74A3E4311A
4 zmienionych plików z 104 dodań i 8 usunięć

Wyświetl plik

@ -0,0 +1,60 @@
// License: GPL. For details, see LICENSE file.
package org.openstreetmap.josm.plugins.mapwithai.backend.commands.conflation;
import static org.openstreetmap.josm.tools.I18n.tr;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.openstreetmap.josm.command.Command;
import org.openstreetmap.josm.data.osm.Node;
import org.openstreetmap.josm.data.osm.OsmPrimitive;
import org.openstreetmap.josm.data.osm.Relation;
import org.openstreetmap.josm.data.osm.Way;
/**
* This is similar to the ReplaceGeometryUtils.buildUpgradeNodeCommand method
*
* @author Taylor Smock
*
*/
public class MergeBuildingNodeCommand {
/**
* Upgrade a node to a way or multipolygon
*
* @param subjectNode node to be replaced
* @param referenceObject object with greater spatial quality
*/
public static Command buildUpgradeNodeCommand(Node subjectNode, OsmPrimitive referenceObject) {
boolean keepNode = !subjectNode.isNew();
if (keepNode) {
getNewOrNoTagNode(referenceObject);
}
return null;
}
private static Node getNewOrNoTagNode(OsmPrimitive referenceObject) {
List<Node> nodes;
if (referenceObject instanceof Way) {
nodes = ((Way) referenceObject).getNodes();
} else if (referenceObject instanceof Relation) {
nodes = ((Relation) referenceObject).getMemberPrimitives().stream().flatMap(o -> {
if (o instanceof Way) {
return ((Way) o).getNodes().stream();
} else if (o instanceof Node) {
return Stream.of((Node) o);
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
} else if (referenceObject instanceof Node) {
nodes = Collections.singletonList((Node) referenceObject);
} else {
throw new IllegalArgumentException(tr("Unknown OsmPrimitive type"));
}
return nodes.stream().filter(OsmPrimitive::isNew).findAny()
.orElse(nodes.stream().filter(p -> !p.isTagged()).findAny().orElse(nodes.get(0)));
}
}

Wyświetl plik

@ -142,7 +142,7 @@ public class MapWithAILayerInfo {
}
Collections.sort(layers);
}
loadDefaults(false, MainApplication.worker, fastFail, null);
loadDefaults(false, MainApplication.worker, fastFail, listener);
}
/**

Wyświetl plik

@ -31,6 +31,11 @@ import org.openstreetmap.josm.testutils.JOSMTestRules;
import org.openstreetmap.josm.tools.Logging;
import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.FileSource;
import com.github.tomakehurst.wiremock.extension.Parameters;
import com.github.tomakehurst.wiremock.extension.ResponseTransformer;
import com.github.tomakehurst.wiremock.http.Request;
import com.github.tomakehurst.wiremock.http.Response;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import mockit.integration.TestRunnerDecorator;
@ -79,8 +84,10 @@ public class MapWithAITestRules extends JOSMTestRules {
super.before();
Logging.getLogger().setFilter(record -> record.getLevel().intValue() >= Level.WARNING.intValue()
|| record.getSourceClassName().startsWith("org.openstreetmap.josm.plugins.mapwithai"));
if (wiremock) {
wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock"));
wireMock = new WireMockServer(options().usingFilesUnderDirectory("test/resources/wiremock")
.extensions(new WireMockUrlTransformer()).dynamicPort());
wireMock.start();
resetMapWithAILayerInfo();
setupMapWithAILayerInfo(wireMock);
@ -100,6 +107,8 @@ public class MapWithAITestRules extends JOSMTestRules {
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList()));
MapWithAILayerInfo.getInstance().getLayers()
.forEach(l -> l.setUrl(l.getUrl().replaceAll("https?:\\/\\/.*?\\/", wireMock.baseUrl() + "/")));
try {
OsmApi.getOsmApi().initialize(NullProgressMonitor.INSTANCE);
} catch (OsmTransferCanceledException | OsmApiInitializationException e) {
@ -110,10 +119,6 @@ public class MapWithAITestRules extends JOSMTestRules {
AtomicBoolean finished = new AtomicBoolean();
MapWithAILayerInfo.getInstance().load(false, () -> finished.set(true));
Awaitility.await().atMost(Durations.TEN_SECONDS).until(finished::get);
if (wiremock) {
MapWithAILayerInfo.getInstance().getLayers()
.forEach(l -> l.setUrl(l.getUrl().replaceAll("https?:\\/\\/.*?\\/", wireMock.baseUrl() + "/")));
}
}
if (workerExceptions) {
currentExceptionHandler = Thread.getDefaultUncaughtExceptionHandler();
@ -169,4 +174,28 @@ public class MapWithAITestRules extends JOSMTestRules {
public WireMockServer getWireMock() {
return this.wireMock;
}
/**
* Replace URL's with the wiremock URL
*
* @author Taylor Smock
*/
private class WireMockUrlTransformer extends ResponseTransformer {
@Override
public String getName() {
return "Convert urls in responses to wiremock url";
}
@Override
public Response transform(Request request, Response response, FileSource files, Parameters parameters) {
if (wireMock != null) {
String origBody = response.getBodyAsString();
String newBody = origBody.replaceAll("https?:\\/\\/.*?\\/", wireMock.baseUrl() + "/");
return Response.Builder.like(response).but().body(newBody).build();
}
return response;
}
}
}