Skip to content

Commit 8dce89f

Browse files
authored
Merge pull request #14 from D3ryk/validVMsOnIndexPage
Bugfix implementation + modification of unit test
2 parents cf79176 + 3144f30 commit 8dce89f

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/main/java/cz/sparko/boxitory/service/FilesystemBoxRepository.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public FilesystemBoxRepository(AppProperties appProperties, HashService hashServ
3636
@Override
3737
public List<String> getBoxes() {
3838
return Arrays.stream(boxHome.listFiles(File::isDirectory))
39+
.filter(this::containsValidBoxFile)
3940
.map(File::getName)
4041
.sorted()
4142
.collect(Collectors.toList());
@@ -82,8 +83,9 @@ private Map<String, List<File>> groupBoxFilesByVersion(File boxDir) {
8283

8384
private boolean validateFilename(File boxFile) {
8485
String filename = boxFile.getName();
85-
List<String> parsedFilename = Arrays.asList(filename.split("_"));
86-
if (parsedFilename.size() != 3) {
86+
File parentDir = boxFile.getParentFile();
87+
88+
if (!filename.matches(parentDir.getName() + "_(\\d+)_(\\w+)\\.box")) {
8789
LOG.warn("box file [{}] has wrong name. must be in format ${name}_${version}_${provider}.box", filename);
8890
return false;
8991
}
@@ -132,4 +134,9 @@ private BoxProvider createBoxProviderFromFile(File file) {
132134
hashService.getChecksum(file.getAbsolutePath())
133135
);
134136
}
137+
138+
private boolean containsValidBoxFile(File file) {
139+
File[] files = file.listFiles(this::validateFilename);
140+
return files.length > 0;
141+
}
135142
}

src/test/java/cz/sparko/boxitory/service/FilesystemBoxRepositoryTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.Optional;
2020

2121
import static org.testng.Assert.assertEquals;
22+
import static org.testng.Assert.assertFalse;
2223
import static org.testng.Assert.assertTrue;
2324

2425
@SpringBootTest
@@ -168,7 +169,8 @@ public void givenValidRepositoryWithBoxes_whenIndex_thenGetValidBoxes() {
168169
BoxRepository boxRepository = new FilesystemBoxRepository(testAppProperties, new NoopHashService());
169170

170171
List<String> boxes = boxRepository.getBoxes();
171-
assertTrue(boxes.containsAll(Arrays.asList("f25", "f26", "f27", "f28", "f29")));
172+
assertTrue(boxes.containsAll(Arrays.asList("f25", "f26", "f28", "f29")));
173+
assertFalse(boxes.containsAll(Arrays.asList("f27")));
172174
}
173175

174176
private String composePath(String boxName, String version, String provider) {

0 commit comments

Comments
 (0)