Skip to content

Commit

Permalink
fix(graphbuilder): Add a graph folder creation routine if the base fo… (
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelsJP authored Oct 19, 2023
2 parents b919ccd + e02ec99 commit 6475359
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
1 change: 0 additions & 1 deletion .github/workflows/run_maven_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
run_tests:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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<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));

// 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -267,6 +267,7 @@ public static Stream<Arguments> shouldDownloadGraphMethodSource() {
Arguments.of(missingGraphInfo, missingGraphInfo, nonexistingFile, null, false)
);
}

@Test
void findLatestGraphComponent() {
}
Expand Down

0 comments on commit 6475359

Please sign in to comment.