|
| 1 | +package de.komoot.photon.api; |
| 2 | + |
| 3 | +import com.vividsolutions.jts.geom.Coordinate; |
| 4 | +import com.vividsolutions.jts.geom.Point; |
| 5 | + |
| 6 | +import de.komoot.photon.App; |
| 7 | +import de.komoot.photon.ESBaseTester; |
| 8 | +import de.komoot.photon.Importer; |
| 9 | +import de.komoot.photon.PhotonDoc; |
| 10 | +import org.json.JSONArray; |
| 11 | +import org.json.JSONObject; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import java.io.BufferedReader; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.net.HttpURLConnection; |
| 18 | +import java.net.URL; |
| 19 | +import java.util.*; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.*; |
| 23 | +import static spark.Spark.*; |
| 24 | + |
| 25 | +public class ApiLanguagesTest extends ESBaseTester { |
| 26 | + |
| 27 | + private static final int LISTEN_PORT = 30234; |
| 28 | + |
| 29 | + @AfterEach |
| 30 | + public void shutdown() { |
| 31 | + stop(); |
| 32 | + awaitStop(); |
| 33 | + } |
| 34 | + |
| 35 | + protected PhotonDoc createDoc(int id, String key, String value, String... names) { |
| 36 | + Point location = FACTORY.createPoint(new Coordinate(1.0, 2.34)); |
| 37 | + PhotonDoc doc = new PhotonDoc(id, "W", id, key, value).centroid(location); |
| 38 | + |
| 39 | + Map<String, String> nameMap = new HashMap<>(); |
| 40 | + |
| 41 | + for (int i = 0; i < names.length - 1; i += 2) { |
| 42 | + nameMap.put(names[i], names[i + 1]); |
| 43 | + } |
| 44 | + |
| 45 | + doc.names(nameMap); |
| 46 | + |
| 47 | + return doc; |
| 48 | + } |
| 49 | + |
| 50 | + private void importPlaces(String... languages) throws Exception { |
| 51 | + setUpES(dataDirectory, languages); |
| 52 | + Importer instance = makeImporterWithLanguages(languages); |
| 53 | + instance.add(createDoc(1000, "place", "city", |
| 54 | + "name:en", "thething", "name:fr", "letruc", "name:ch", "dasding")); |
| 55 | + instance.add(createDoc(1001, "place", "town", |
| 56 | + "name:ch", "thething", "name:fr", "letruc", "name:en", "dasding")); |
| 57 | + instance.finish(); |
| 58 | + refresh(); |
| 59 | + } |
| 60 | + |
| 61 | + private void startAPI(String languages) throws Exception { |
| 62 | + List<String> params = new ArrayList<>(Arrays.asList("-cluster", TEST_CLUSTER_NAME, |
| 63 | + "-listen-port", Integer.toString(LISTEN_PORT), |
| 64 | + "-transport-addresses", "127.0.0.1", |
| 65 | + "-cors-any")); |
| 66 | + |
| 67 | + if (languages.length() > 0) { |
| 68 | + params.add("-languages"); |
| 69 | + params.add(languages); |
| 70 | + } |
| 71 | + |
| 72 | + App.main(params.toArray(new String[0])); |
| 73 | + awaitInitialization(); |
| 74 | + } |
| 75 | + |
| 76 | + private JSONArray query(String q) throws Exception { |
| 77 | + HttpURLConnection connection = |
| 78 | + (HttpURLConnection) new URL("http://127.0.0.1:" + port() + "/api?" + q).openConnection(); |
| 79 | + JSONObject json = new JSONObject( |
| 80 | + new BufferedReader(new InputStreamReader(connection.getInputStream())).lines().collect(Collectors.joining("\n"))); |
| 81 | + |
| 82 | + return json.getJSONArray("features"); |
| 83 | + } |
| 84 | + |
| 85 | + private int getOsmId(JSONArray results, int idx) { |
| 86 | + return results.getJSONObject(idx).getJSONObject("properties").getInt("osm_id"); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testOnlyImportSelectedLanguages() throws Exception { |
| 91 | + importPlaces("en"); |
| 92 | + startAPI(""); |
| 93 | + |
| 94 | + JSONArray results = query("q=thething"); |
| 95 | + assertEquals(1, results.length()); |
| 96 | + assertEquals(1000, getOsmId(results, 0)); |
| 97 | + |
| 98 | + assertEquals(0, query("q=letruc").length()); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testUseImportLanguagesWhenNoOtherIsGiven() throws Exception { |
| 103 | + importPlaces("en", "fr", "ch"); |
| 104 | + startAPI(""); |
| 105 | + |
| 106 | + JSONArray results = query("q=thething"); |
| 107 | + assertEquals(2, results.length()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testUseCommandLineLangauges() throws Exception { |
| 112 | + importPlaces("en", "fr", "ch"); |
| 113 | + startAPI("en,fr"); |
| 114 | + |
| 115 | + JSONArray results = query("q=thething"); |
| 116 | + assertEquals(1, results.length()); |
| 117 | + } |
| 118 | + |
| 119 | +} |
0 commit comments