diff --git a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java index 506c802..95d1b67 100644 --- a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java +++ b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java @@ -36,6 +36,7 @@ public FilesystemBoxRepository(AppProperties appProperties, HashService hashServ @Override public List getBoxes() { return Arrays.stream(boxHome.listFiles(File::isDirectory)) + .filter(this::containsValidBoxFile) .map(File::getName) .sorted() .collect(Collectors.toList()); @@ -82,8 +83,9 @@ private Map> groupBoxFilesByVersion(File boxDir) { private boolean validateFilename(File boxFile) { String filename = boxFile.getName(); - List parsedFilename = Arrays.asList(filename.split("_")); - if (parsedFilename.size() != 3) { + File parentDir = boxFile.getParentFile(); + + if (!filename.matches(parentDir.getName() + "_(\\d+)_(\\w+)\\.box")) { LOG.warn("box file [{}] has wrong name. must be in format ${name}_${version}_${provider}.box", filename); return false; } @@ -132,4 +134,9 @@ private BoxProvider createBoxProviderFromFile(File file) { hashService.getChecksum(file.getAbsolutePath()) ); } + + private boolean containsValidBoxFile(File file) { + File[] files = file.listFiles(this::validateFilename); + return files.length > 0; + } } diff --git a/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java b/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java index 1c8022c..4198b99 100644 --- a/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java +++ b/src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java @@ -19,6 +19,7 @@ import java.util.Optional; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @SpringBootTest @@ -168,7 +169,8 @@ public void givenValidRepositoryWithBoxes_whenIndex_thenGetValidBoxes() { BoxRepository boxRepository = new FilesystemBoxRepository(testAppProperties, new NoopHashService()); List boxes = boxRepository.getBoxes(); - assertTrue(boxes.containsAll(Arrays.asList("f25", "f26", "f27", "f28", "f29"))); + assertTrue(boxes.containsAll(Arrays.asList("f25", "f26", "f28", "f29"))); + assertFalse(boxes.containsAll(Arrays.asList("f27"))); } private String composePath(String boxName, String version, String provider) {