1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.osm;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertTrue;
|
---|
5 |
|
---|
6 | import java.io.IOException;
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.net.URL;
|
---|
9 | import java.util.ArrayList;
|
---|
10 | import java.util.List;
|
---|
11 |
|
---|
12 | import javax.json.Json;
|
---|
13 | import javax.json.JsonObject;
|
---|
14 | import javax.json.JsonReader;
|
---|
15 | import javax.json.JsonValue;
|
---|
16 |
|
---|
17 | import org.junit.BeforeClass;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.JOSMFixture;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
|
---|
22 | import org.openstreetmap.josm.data.validation.tests.TagChecker;
|
---|
23 | import org.openstreetmap.josm.gui.mappaint.mapcss.parsergen.ParseException;
|
---|
24 | import org.openstreetmap.josm.gui.tagging.presets.TaggingPresets;
|
---|
25 | import org.openstreetmap.josm.tools.HttpClient;
|
---|
26 | import org.xml.sax.SAXException;
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Various integration tests with Taginfo.
|
---|
30 | */
|
---|
31 | public class TaginfoTestIT {
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Setup test.
|
---|
35 | */
|
---|
36 | @BeforeClass
|
---|
37 | public static void setUp() {
|
---|
38 | JOSMFixture.createFunctionalTestFixture().init();
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Checks that popular tags are known (i.e included in internal presets, or deprecated, or explicitely ignored)
|
---|
43 | * @throws SAXException if any XML parsing error occurs
|
---|
44 | * @throws IOException if any I/O error occurs
|
---|
45 | * @throws ParseException if any MapCSS parsing error occurs
|
---|
46 | */
|
---|
47 | @Test
|
---|
48 | public void testCheckPopularTags() throws SAXException, IOException, ParseException {
|
---|
49 | TaggingPresets.readFromPreferences();
|
---|
50 | new TagChecker().initialize();
|
---|
51 | MapCSSTagChecker mapCssTagChecker = new MapCSSTagChecker();
|
---|
52 | mapCssTagChecker.addMapCSS("resource://data/validator/deprecated.mapcss");
|
---|
53 |
|
---|
54 | List<String> errors = new ArrayList<>();
|
---|
55 | try (InputStream in = HttpClient.create(new URL("https://taginfo.openstreetmap.org/api/4/tags/popular")).connect().getContent();
|
---|
56 | JsonReader reader = Json.createReader(in)) {
|
---|
57 | for (JsonValue item : reader.readObject().getJsonArray("data")) {
|
---|
58 | JsonObject obj = (JsonObject) item;
|
---|
59 | // Only consider tags with wiki pages
|
---|
60 | if (obj.getInt("in_wiki") == 1) {
|
---|
61 | String key = obj.getString("key");
|
---|
62 | String value = obj.getString("value");
|
---|
63 | System.out.print("Checking "+key+"="+value+" ... ");
|
---|
64 | boolean ok = true;
|
---|
65 | // Check if tag is in internal presets
|
---|
66 | if (!TagChecker.isTagInPresets(key, value)) {
|
---|
67 | // If not, check if we have either a deprecated mapcss test for it
|
---|
68 | Node n = new Node(LatLon.NORTH_POLE);
|
---|
69 | Way w = new Way();
|
---|
70 | Relation r = new Relation();
|
---|
71 | n.put(key, value);
|
---|
72 | w.put(key, value);
|
---|
73 | r.put(key, value);
|
---|
74 | new DataSet(n, w, r);
|
---|
75 | if (mapCssTagChecker.getErrorsForPrimitive(n, false).isEmpty()
|
---|
76 | && mapCssTagChecker.getErrorsForPrimitive(w, false).isEmpty()
|
---|
77 | && mapCssTagChecker.getErrorsForPrimitive(r, false).isEmpty()) {
|
---|
78 | // Or a legacy tagchecker ignore rule
|
---|
79 | if (!TagChecker.isTagIgnored(key, value)) {
|
---|
80 | ok = !errors.add(key +"="+ value + " - " + obj.getInt("count_all"));
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|
84 | System.out.println(ok ? "OK" : "KO");
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | for (String error : errors) {
|
---|
89 | System.err.println(error);
|
---|
90 | }
|
---|
91 | assertTrue(errors.toString(), errors.isEmpty());
|
---|
92 | }
|
---|
93 | }
|
---|