Test profile upload

This commit is contained in:
Manuel Fuhr 2022-10-29 20:33:47 +02:00
parent 06f0315cf4
commit 556555b8ae

View file

@ -4,31 +4,42 @@ package btools.server;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
public class RouteServerTest {
private static final String host = "localhost";
private static final String port = "17777";
private static final String baseUrl = "http://" + host + ":" + port + "/";
@ClassRule
public static TemporaryFolder profileDir = new TemporaryFolder();
@BeforeClass
public static void setupServer() throws IOException, InterruptedException {
File workingDir = new File(".").getCanonicalFile();
File segmentDir = new File(workingDir, "../brouter-map-creator/build/resources/test/tmp/segments");
File profileDir = new File(workingDir, "../misc/profiles2");
File customProfileDir = workingDir;
File profileSourceDir = new File(workingDir, "../misc/profiles2");
// Copy required files to temporary dir because profile upload will create files
Files.copy(Paths.get(profileSourceDir.getAbsolutePath(), "lookups.dat"), Paths.get(profileDir.getRoot().getAbsolutePath(), "lookups.dat"));
Files.copy(Paths.get(profileSourceDir.getAbsolutePath(), "trekking.brf"), Paths.get(profileDir.getRoot().getAbsolutePath(), "trekking.brf"));
String customProfileDir = "custom";
Runnable runnable = () -> {
try {
RouteServer.main(new String[]{segmentDir.getAbsolutePath(), profileDir.getAbsolutePath(), customProfileDir.getAbsolutePath(), port, "1"});
RouteServer.main(new String[]{segmentDir.getAbsolutePath(), profileDir.getRoot().getAbsolutePath(), customProfileDir, port, "1"});
} catch (Exception e) {
e.printStackTrace();
}
@ -81,6 +92,60 @@ public class RouteServerTest {
Assert.assertEquals("1902", geoJson.query("/features/0/properties/track-length"));
}
@Test
public void uploadValidProfile() throws IOException {
URL requestUrl = new URL(baseUrl + "brouter/profile");
HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
String dummyProfile = "---context:global # following code refers to global config\n" +
"\n" +
"# this prevents suppression of unused tags, so they are visibly in the data tab\n" +
"assign processUnusedTags = true\n" +
"assign validForFoot = true\n" +
"\n" +
"---context:way # following code refers to way-tags\n" +
"\n" +
"assign costfactor\n" +
" switch and highway= not route=ferry 100000 1\n" +
"\n" +
"---context:node # following code refers to node tags\n" +
"assign initialcost = 0\n";
try (OutputStream outputStream = httpConnection.getOutputStream()) {
outputStream.write(dummyProfile.getBytes(StandardCharsets.UTF_8));
}
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpConnection.getResponseCode());
InputStream inputStream = httpConnection.getInputStream();
String response = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
JSONObject jsonResponse = new JSONObject(response);
Assert.assertTrue(jsonResponse.query("/profileid").toString().startsWith("custom_"));
Assert.assertFalse(jsonResponse.has("error"));
}
@Test
public void uploadInvalidProfile() throws IOException {
URL requestUrl = new URL(baseUrl + "brouter/profile");
HttpURLConnection httpConnection = (HttpURLConnection) requestUrl.openConnection();
httpConnection.setRequestMethod("POST");
httpConnection.setDoOutput(true);
String invalidProfile = "";
try (OutputStream outputStream = httpConnection.getOutputStream()) {
outputStream.write(invalidProfile.getBytes(StandardCharsets.UTF_8));
}
// It would be better if RouteServer would return "400 Bad Request"
Assert.assertEquals(HttpURLConnection.HTTP_OK, httpConnection.getResponseCode());
InputStream inputStream = httpConnection.getInputStream();
String response = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
JSONObject jsonResponse = new JSONObject(response);
// Returns profileid, but profile isn't valid
Assert.assertTrue(jsonResponse.query("/profileid").toString().startsWith("custom_"));
Assert.assertTrue(jsonResponse.has("error"));
}
@Test
public void robots() throws IOException {
URL requestUrl = new URL(baseUrl + "robots.txt");