Skip to content

Commit

Permalink
fix: Repair the extractSourceFileElements function and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelsJP committed Oct 20, 2023
1 parent 26dd739 commit b02154d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.heigit.ors.util.StringUtility;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import static org.heigit.ors.api.ORSEnvironmentPostProcessor.ORS_CONFIG_LOCATION_ENV;
Expand Down Expand Up @@ -102,22 +104,27 @@ record SourceFileElements(String repoBaseUrlString, String repoName, String loca
}

SourceFileElements extractSourceFileElements(String sourceFilePropertyValue) {
String repoBaseUrlString = null;
String repoName = null;
String localOsmFilePath = "";
String accessScheme = null, repoBaseUrlString = null, repoName = null;
sourceFilePropertyValue = sourceFilePropertyValue.trim();
try {
new URL(sourceFilePropertyValue);
LOGGER.debug("Configuration property 'source_file' contains a URL, using value as URL for a graphs repository");
sourceFilePropertyValue = sourceFilePropertyValue.trim().replaceAll("/$", "");
String[] urlElements = sourceFilePropertyValue.split("/");
URI uri = new URI(sourceFilePropertyValue);
accessScheme = uri.getScheme();
repoBaseUrlString = uri.getHost();
if (accessScheme == null || repoBaseUrlString == null) {
throw new URISyntaxException(sourceFilePropertyValue, "URI does not contain a valid schema or host");
}
repoBaseUrlString = accessScheme + "://" + repoBaseUrlString;

repoName = urlElements[urlElements.length - 1];
repoBaseUrlString = sourceFilePropertyValue.replaceAll("/%s$".formatted(repoName), "");
} catch (MalformedURLException e) {
String[] pathElements = uri.getPath().split("/");
if (pathElements.length == 0) {
throw new URISyntaxException(sourceFilePropertyValue, "URI does not contain a path");
}
repoName = pathElements[pathElements.length - 1];
} catch (URISyntaxException e) {
LOGGER.debug("Configuration property 'source_file' does not contain a URL, using value as local osm file path");
localOsmFilePath = sourceFilePropertyValue;
return new SourceFileElements(repoBaseUrlString, repoName, sourceFilePropertyValue);
}
return new SourceFileElements(repoBaseUrlString, repoName, localOsmFilePath);
return new SourceFileElements(repoBaseUrlString, repoName, "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,16 @@ void setUp(){
}
@ParameterizedTest
@CsvSource(value = {
"https://example.com/test-repo/planet, https://example.com, test-repo, planet",
"https://example.com/test-repo/planet/, https://example.com, test-repo, planet",
"' https://example.com/test-repo/planet ', https://example.com, test-repo, planet",
"' https://example.com/test-repo/planet/ ', https://example.com, test-repo, planet",
"' http://example.com/test-repo/planet/ ', http://example.com, test-repo, planet"
"https://example.com/foo1/foo2/foo3/test-repo, https://example.com, test-repo",
"https://example.com/test-repo/, https://example.com, test-repo",
"https://example.com/test-repo, https://example.com, test-repo",
"' https://example.com/test-repo ', https://example.com, test-repo",
"https://example.com/test-repos/, https://example.com, test-repos",
"http://example.com/test-repo/, http://example.com, test-repo"
})
void extractSourceFileElements_withURL(String url, String expectedBaseUrl, String expectedRepoName, String expectedCoverage){
void extractSourceFileElements_withURL(String url, String expectedBaseUrl, String expectedRepoName){
ORSInitContextListener.SourceFileElements elements = orsInitContextListener.extractSourceFileElements(url);
assertEquals(expectedBaseUrl, elements.repoBaseUrlString());
assertEquals(expectedRepoName, elements.repoName());
// TODO fix this function
// assertEquals(expectedCoverage, elements.repoCoverage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.nio.file.attribute.PosixFilePermission;
import java.util.*;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -62,16 +61,6 @@ void deleteFiles() throws IOException {
FileUtils.deleteDirectory(vehicleDir);
}

void setupORSGraphManager(String hash) {
EngineConfig engineConfig = EngineConfig.EngineConfigBuilder.init()
.setGraphsRepoUrl(GRAPHS_REPO_BASE_URL)
.setGraphsRepoName(GRAPHS_REPO_NAME)
.setGraphsExtent(GRAPHS_COVERAGE)
.setMaxNumberOfGraphBackups(3)
.buildWithAppConfigOverride();
setupORSGraphManager(hash, engineConfig);
}

private File createBackupDirectory(String hash, String dateString) throws IOException {
File oldBackupDir = setupLocalGraphDirectory(hash, EARLIER_DATE);
oldBackupDir.renameTo(new File(oldBackupDir.getAbsolutePath()+"_"+dateString));
Expand All @@ -83,6 +72,16 @@ private void assertCorrectBackupDir(File backupDir, String hash) {
assertTrue(new File(backupDir, hash+".json").exists());
}

void setupORSGraphManager(String hash) {
EngineConfig engineConfig = EngineConfig.EngineConfigBuilder.init()
.setGraphsRepoUrl(GRAPHS_REPO_BASE_URL)
.setGraphsRepoName(GRAPHS_REPO_NAME)
.setGraphsExtent(GRAPHS_COVERAGE)
.setMaxNumberOfGraphBackups(3)
.buildWithAppConfigOverride();
setupORSGraphManager(hash, engineConfig);
}

void setupORSGraphManager(String hash, EngineConfig engineConfig) {
File localDir = TEMP_DIR.toFile();
vehicleDirAbsPath = String.join("/", localDir.getAbsolutePath(), VEHICLE);
Expand All @@ -96,6 +95,26 @@ void setupORSGraphManager(String hash, EngineConfig engineConfig) {
orsGraphRepoManager.setFileManager(orsGraphFileManager);
}

void setupORSGraphManager(String hash, String customGraphFolder) {
EngineConfig engineConfig = EngineConfig.EngineConfigBuilder.init()
.setGraphsRepoUrl(GRAPHS_REPO_BASE_URL)
.setGraphsRepoName(GRAPHS_REPO_NAME)
.setGraphsExtent(GRAPHS_COVERAGE)
.setMaxNumberOfGraphBackups(3)
.buildWithAppConfigOverride();
Path localDir = Path.of(customGraphFolder);

vehicleDirAbsPath = String.join("/", localDir.toAbsolutePath().toString(), VEHICLE);
hashDirAbsPath = String.join("/", vehicleDirAbsPath, hash);

orsGraphFileManager = new ORSGraphFileManager(engineConfig, hash, hashDirAbsPath, vehicleDirAbsPath, VEHICLE);
orsGraphFileManager.initialize();
orsGraphRepoManager.initialize(engineConfig);
orsGraphRepoManager.setGraphsRepoGraphVersion(GRAPHS_VERSION);
orsGraphRepoManager.setRouteProfileName(VEHICLE);
orsGraphRepoManager.setFileManager(orsGraphFileManager);
}

File setupLocalGraphDirectory(String hash, Long osmDateLocal) throws IOException {
if (hash == null) return null;
hashDir = new File(hashDirAbsPath);
Expand Down Expand Up @@ -257,17 +276,16 @@ public void deleteOldestBackups_maxNumberOfGraphBackupsIsNegative() throws IOExc

@Test
void testInitialize() throws IOException {
Path testFolder = Files.createDirectory(tempDir.resolve("noWritePermissionFolder"));
LOCAL_PATH = testFolder.toString();
Path testFolder = Files.createDirectory(TEMP_DIR.resolve("noWritePermissionFolder"));

// This code to remove write permissions is POSIX-specific (Unix-like OSes)
Set<PosixFilePermission> perms = new HashSet<>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_EXECUTE);
Files.setPosixFilePermissions(testFolder, perms);

setupORSGraphManager("foo");
assertTrue(orsGraphFileManager.getVehicleGraphDirAbsPath().contains(LOCAL_PATH));
setupORSGraphManager("foo", testFolder.toString());
assertTrue(orsGraphFileManager.getVehicleGraphDirAbsPath().contains(testFolder.toString()));

// Assert that the folder has no write permissions
assertFalse(testFolder.toFile().canWrite());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Date;
Expand Down Expand Up @@ -207,7 +208,7 @@ void shouldDownloadGraph(GraphInfo remoteGraphInfo, GraphInfo localGraphInfo, Fi
assertEquals(expected, orsGraphRepoManager.shouldDownloadGraph(remoteGraphInfo, localGraphInfo, persistedDownloadFile, persistedRemoteGraphInfo));
}

public static Stream<Arguments> shouldDownloadGraphMethodSource() {
public static Stream<Arguments> shouldDownloadGraphMethodSource() throws IOException {
ORSGraphInfoV1 earlierOrsGraphInfoV1 = new ORSGraphInfoV1(new Date(EARLIER_DATE));
ORSGraphInfoV1 middleOrsGraphInfoV1 = new ORSGraphInfoV1(new Date(MIDDLE_DATE));
ORSGraphInfoV1 laterOrsGraphInfoV1 = new ORSGraphInfoV1(new Date(LATER_DATE));
Expand All @@ -216,9 +217,10 @@ public static Stream<Arguments> shouldDownloadGraphMethodSource() {
GraphInfo existingGraphInfoMiddle = new GraphInfo().withPersistedInfo(middleOrsGraphInfoV1);
GraphInfo existingGraphInfoLater = new GraphInfo().withPersistedInfo(laterOrsGraphInfoV1);

File resourcesDir = TEMP_DIR.toFile().getParentFile();
File nonexistingFile = new File(resourcesDir, "missing.ghz");
File existingFile = new File(resourcesDir, "some.ghz");
Path resourcesDir = Files.createTempDirectory(TEMP_DIR, "ghz");

File nonexistingFile = new File(resourcesDir.toAbsolutePath().toFile(), "missing.ghz");
File existingFile = Files.createTempFile(resourcesDir, "some", ".ghz").toFile();

return Stream.of(
Arguments.of(existingGraphInfoMiddle, existingGraphInfoEarlier, existingFile, earlierOrsGraphInfoV1, true),
Expand Down

0 comments on commit b02154d

Please sign in to comment.