From 1867c781625111a68e6fbc34829576013503e6ee Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 15 Jan 2020 12:02:13 -0700 Subject: [PATCH] Add test for conflation keys, don't autoremove (yet) Signed-off-by: Taylor Smock --- .../plugins/mapwithai/MapWithAIPlugin.java | 5 ++ .../tests/ConnectingNodeInformationTest.java | 81 +++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/validation/tests/ConnectingNodeInformationTest.java diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java index dc90f29..93f1ff7 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/MapWithAIPlugin.java @@ -35,6 +35,7 @@ import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIObject; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIRemoteControl; import org.openstreetmap.josm.plugins.mapwithai.backend.MapWithAIUploadHook; import org.openstreetmap.josm.plugins.mapwithai.backend.MergeDuplicateWaysAction; +import org.openstreetmap.josm.plugins.mapwithai.data.validation.tests.ConnectingNodeInformationTest; import org.openstreetmap.josm.plugins.mapwithai.data.validation.tests.RoutingIslandsTest; import org.openstreetmap.josm.plugins.mapwithai.frontend.MapWithAIDownloadReader; import org.openstreetmap.josm.spi.preferences.Config; @@ -83,6 +84,9 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable { if (!OsmValidator.getAllAvailableTestClasses().contains(RoutingIslandsTest.class)) { OsmValidator.addTest(RoutingIslandsTest.class); } + if (!OsmValidator.getAllAvailableTestClasses().contains(ConnectingNodeInformationTest.class)) { + OsmValidator.addTest(ConnectingNodeInformationTest.class); + } if (!Config.getPref().getKeySet().contains(PAINTSTYLE_PREEXISTS)) { Config.getPref().putBoolean(PAINTSTYLE_PREEXISTS, MapWithAIDataUtils.checkIfMapWithAIPaintStyleExists()); @@ -161,6 +165,7 @@ public final class MapWithAIPlugin extends Plugin implements Destroyable { destroyables.forEach(Destroyable::destroy); DownloadDialog.removeDownloadSource(mapWithAIDownloadReader); OsmValidator.removeTest(RoutingIslandsTest.class); + OsmValidator.removeTest(ConnectingNodeInformationTest.class); DownloadListener.destroyAll(); } } diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/validation/tests/ConnectingNodeInformationTest.java b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/validation/tests/ConnectingNodeInformationTest.java new file mode 100644 index 0000000..43af072 --- /dev/null +++ b/src/main/java/org/openstreetmap/josm/plugins/mapwithai/data/validation/tests/ConnectingNodeInformationTest.java @@ -0,0 +1,81 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.plugins.mapwithai.data.validation.tests; + +import static org.openstreetmap.josm.tools.I18n.tr; + +import java.lang.reflect.InvocationTargetException; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.stream.Collectors; + +import org.openstreetmap.josm.command.ChangePropertyCommand; +import org.openstreetmap.josm.data.osm.DataSet; +import org.openstreetmap.josm.data.osm.DownloadPolicy; +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.UploadPolicy; +import org.openstreetmap.josm.data.osm.Way; +import org.openstreetmap.josm.data.validation.Severity; +import org.openstreetmap.josm.data.validation.Test; +import org.openstreetmap.josm.data.validation.TestError; +import org.openstreetmap.josm.gui.progress.ProgressMonitor; +import org.openstreetmap.josm.plugins.mapwithai.commands.CreateConnectionsCommand; +import org.openstreetmap.josm.tools.Logging; + +/** + * Ensure that no conflation keys remain + * + * @author Taylor Smock + */ +public class ConnectingNodeInformationTest extends Test { + private static final int ERROR_CODE = 827277536; + Map badTags; + + public ConnectingNodeInformationTest() { + super(tr("Left over conflation information (MapWithAI)"), + tr("Checks conflation keys that should not exist in OpenStreetMap.")); + } + + @Override + public void startTest(ProgressMonitor monitor) { + super.startTest(monitor); + badTags = new HashMap<>(); + CreateConnectionsCommand.getConflationCommands().forEach(clazz -> { + try { + badTags.put(clazz.getConstructor(DataSet.class).newInstance(new DataSet()).getKey(), null); + } catch (InstantiationException | IllegalAccessException | IllegalArgumentException + | InvocationTargetException | NoSuchMethodException | SecurityException e) { + Logging.error(e); + } + }); + } + + @Override + public void visit(Relation relation) { + checkTags(relation); + } + + @Override + public void visit(Way way) { + checkTags(way); + } + + @Override + public void visit(Node node) { + checkTags(node); + } + + private void checkTags(OsmPrimitive prim) { + DataSet ds = prim.getDataSet(); + if (!UploadPolicy.BLOCKED.equals(ds.getUploadPolicy()) && !DownloadPolicy.BLOCKED.equals(ds.getDownloadPolicy()) + && prim.hasKey(badTags.keySet().toArray(new String[0]))) { + errors.add(TestError.builder(this, Severity.ERROR, ERROR_CODE).primitives(prim) + .message(tr("Don''t leave conflation keys in the data {0}", + badTags.keySet().stream().filter(prim::hasKey).collect(Collectors.toList()))) + .fix(() -> new ChangePropertyCommand(Collections.singleton(prim), badTags)).build()); + } + } + +} \ No newline at end of file