Skip to content

Commit 40d7453

Browse files
committed
use languages from database as default
When starting Photon without the language parameter, the languages that were originally configured with the database should be used. Fixes #691.
1 parent 2395ab4 commit 40d7453

File tree

3 files changed

+133
-2
lines changed

3 files changed

+133
-2
lines changed

src/main/java/de/komoot/photon/App.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private static void startApi(CommandLineArgs args, Server server) {
144144
// Get database properties and ensure that the version is compatible.
145145
DatabaseProperties dbProperties = new DatabaseProperties();
146146
server.loadFromDatabase(dbProperties);
147-
if (args.getLanguages().length > 0) {
147+
if (args.getLanguages(false).length > 0) {
148148
dbProperties.restrictLanguages(args.getLanguages());
149149
}
150150

src/main/java/de/komoot/photon/CommandLineArgs.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public class CommandLineArgs {
2727
private boolean nominatimUpdate = false;
2828

2929
@Parameter(names = "-languages", description = "languages nominatim importer should import and use at run-time, comma separated (default is 'en,fr,de,it')", converter = StringArrayConverter.class)
30-
private String[] languages = new String[]{"en", "de", "fr", "it"};
30+
private String[] languages = new String[]{};
3131

3232
@Parameter(names = "-default-language", description = "language to return results in when no explicit language is choosen by the user")
3333
private String defaultLanguage = "default";
@@ -79,5 +79,17 @@ public class CommandLineArgs {
7979

8080
@Parameter(names = "-h", description = "show help / usage")
8181
private boolean usage = false;
82+
83+
public String[] getLanguages(boolean useDefaultIfEmpty) {
84+
if (useDefaultIfEmpty && languages.length == 0) {
85+
return new String[]{"en", "de", "fr", "it"};
86+
}
87+
88+
return languages;
89+
}
90+
91+
public String[] getLanguages() {
92+
return getLanguages(true);
93+
}
8294
}
8395

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

Comments
 (0)