diff --git a/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManager.java b/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManager.java index e00894d13d..fb8a4b4bd9 100644 --- a/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManager.java +++ b/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManager.java @@ -35,6 +35,16 @@ public ORSGraphFileManager(EngineConfig engineConfig, String hash, String hashDi this.maxNumberOfGraphBackups = Math.max(maxBak, 0); } + public void initialize() { + File vehicleGraphDir = new File(hashDirAbsPath); + if (!vehicleGraphDir.exists()) { + LOGGER.info("[%s] Creating vehicle graph directory %s".formatted(getProfileWithHash(), hashDirAbsPath)); + if (!vehicleGraphDir.mkdirs()) { + LOGGER.error("[%s] Could not create vehicle graph directory %s".formatted(getProfileWithHash(), hashDirAbsPath)); + } + } + } + public String getHash() { return hash; } diff --git a/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphManager.java b/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphManager.java index 8a336f8ab8..dc3fd156ca 100644 --- a/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphManager.java +++ b/ors-engine/src/main/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphManager.java @@ -28,6 +28,7 @@ public ORSGraphManager(EngineConfig engineConfig, String routeProfileName, Strin void initialize(EngineConfig engineConfig) { fileManager = new ORSGraphFileManager(engineConfig, hash, hashDirAbsPath, vehicleGraphDirAbsPath, routeProfileName); + fileManager.initialize(); repoManager = new ORSGraphRepoManager(engineConfig, fileManager, routeProfileName, graphsRepoGraphVersion); } diff --git a/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManagerTest.java b/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManagerTest.java index 003653f745..630ee287a8 100644 --- a/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManagerTest.java +++ b/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphFileManagerTest.java @@ -7,15 +7,18 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.api.io.CleanupMode; +import org.junit.jupiter.api.io.TempDir; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.openapitools.client.model.AssetXO; import java.io.File; import java.io.IOException; -import java.util.Arrays; -import java.util.Date; -import java.util.List; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.attribute.PosixFilePermission; +import java.util.*; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.ArgumentMatchers.any; @@ -34,7 +37,9 @@ class ORSGraphFileManagerTest { private static final String GRAPHS_COVERAGE = "planet"; private static final String GRAPHS_VERSION = "1"; private static final String VEHICLE = "car"; - private static final String LOCAL_PATH = "src/test/resources/graphs"; + private static String LOCAL_PATH = "src/test/resources/graphs"; + @TempDir(cleanup = CleanupMode.ON_SUCCESS) + Path tempDir; private static final long EARLIER_DATE = 1692373000111L; private static final long MIDDLE_DATE = 1692373000222L; private static final long LATER_DATE = 1692373000333L; @@ -48,7 +53,7 @@ void setUp() { localDir = new File(LOCAL_PATH); vehicleDirAbsPath = String.join("/", localDir.getAbsolutePath(), VEHICLE); vehicleDir = new File(vehicleDirAbsPath); - vehicleDir.mkdir(); + vehicleDir.mkdirs(); } @AfterEach @@ -83,6 +88,7 @@ void setupORSGraphManager(String hash, EngineConfig engineConfig) { 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); @@ -92,7 +98,7 @@ void setupORSGraphManager(String hash, EngineConfig engineConfig) { File setupLocalGraphDirectory(String hash, Long osmDateLocal) throws IOException { if (hash == null) return null; hashDir = new File(hashDirAbsPath); - hashDir.mkdir(); + hashDir.mkdirs(); ORSGraphInfoV1 localOrsGraphInfoV1Object = new ORSGraphInfoV1(new Date(osmDateLocal)); localGraphInfoV1File = new File(hashDir, hash + ".json"); new ObjectMapper().writeValue(localGraphInfoV1File, localOrsGraphInfoV1Object); @@ -248,5 +254,31 @@ public void deleteOldestBackups_maxNumberOfGraphBackupsIsNegative() throws IOExc assertEquals(0, backups.size()); } - + @Test + void testInitialize() throws IOException { + Path testFolder = Files.createDirectory(tempDir.resolve("noWritePermissionFolder")); + LOCAL_PATH = testFolder.toString(); + + // This code to remove write permissions is POSIX-specific (Unix-like OSes) + Set 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)); + + // Assert that the folder has no write permissions + assertFalse(testFolder.toFile().canWrite()); + assertFalse(orsGraphFileManager.hasLocalGraph()); + + // Set write permissions + perms.add(PosixFilePermission.OWNER_WRITE); + Files.setPosixFilePermissions(testFolder, perms); + + // Initialize again + orsGraphFileManager.initialize(); + assertTrue(testFolder.toFile().canWrite()); + assertTrue(orsGraphFileManager.hasLocalGraph()); + } } \ No newline at end of file diff --git a/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphRepoManagerTest.java b/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphRepoManagerTest.java index 38a1ee06e2..4468d5f1ce 100644 --- a/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphRepoManagerTest.java +++ b/ors-engine/src/test/java/org/heigit/ors/routing/graphhopper/extensions/manage/ORSGraphRepoManagerTest.java @@ -80,7 +80,7 @@ void setupORSGraphManager(String hash) { void setupLocalGraphDirectory(String hash, Long osmDateLocal) throws IOException { if (hash == null) return; hashDir = new File(hashDirAbsPath); - hashDir.mkdir(); + hashDir.mkdirs(); ORSGraphInfoV1 localOrsGraphInfoV1Object = new ORSGraphInfoV1(new Date(osmDateLocal)); localGraphInfoV1File = new File(hashDir, hash + ".json"); new ObjectMapper().writeValue(localGraphInfoV1File, localOrsGraphInfoV1Object); @@ -191,7 +191,7 @@ void filterLatestAsset() { new AssetXO().path("https://example.com/test-repo/planet/wrong/car/abc123/202301011200/abc123.json"), new AssetXO().path("https://example.com/test-repo/wrong/1/car/abc123/202301011200/abc123.ghz"), new AssetXO().path("https://example.com/test-repo/wrong/1/car/abc123/202301011200/abc123.json") - ); + ); AssetXO filtered = orsGraphRepoManager.filterLatestAsset("abc123.json", items); assertEquals("https://example.com/test-repo/planet/1/car/abc123/202301011200/abc123.json", filtered.getPath()); } @@ -267,6 +267,7 @@ public static Stream shouldDownloadGraphMethodSource() { Arguments.of(missingGraphInfo, missingGraphInfo, nonexistingFile, null, false) ); } + @Test void findLatestGraphComponent() { }