From 335bc7175fe3e5afdc088db5838932cae3b7f277 Mon Sep 17 00:00:00 2001 From: D3ryk Date: Sat, 29 Jul 2017 11:38:46 +0200 Subject: [PATCH 1/2] Bugfix implementation + modification of unit test --- .../cz/sparko/boxitory/service/FilesystemBoxRepository.java | 6 ++++++ .../boxitory/service/FilesystemBoxRepositoryTest.java | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java index 506c802..67b2014 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()); @@ -132,4 +133,9 @@ private BoxProvider createBoxProviderFromFile(File file) { hashService.getChecksum(file.getAbsolutePath()) ); } + + private boolean containsValidBoxFile(File file) { + File[] files = file.listFiles((dir, name) -> name.matches(dir.getName() + "_(\\d+)_(\\w+)\\.box")); + 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) { From 3144f3019a8edf296768a51f0db4e386d6164d0b Mon Sep 17 00:00:00 2001 From: D3ryk Date: Fri, 4 Aug 2017 10:06:25 +0200 Subject: [PATCH 2/2] Improving of method validateFilename + calling it from different places --- .../sparko/boxitory/service/FilesystemBoxRepository.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java index 67b2014..95d1b67 100644 --- a/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java +++ b/src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java @@ -83,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; } @@ -135,7 +136,7 @@ private BoxProvider createBoxProviderFromFile(File file) { } private boolean containsValidBoxFile(File file) { - File[] files = file.listFiles((dir, name) -> name.matches(dir.getName() + "_(\\d+)_(\\w+)\\.box")); + File[] files = file.listFiles(this::validateFilename); return files.length > 0; } }