Skip to content

Commit

Permalink
Handle exception when getCases processes corrupted cases (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: BOUHOURS Antoine <[email protected]>
  • Loading branch information
antoinebhs authored Apr 24, 2024
1 parent 979a9c6 commit 0831fff
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/powsybl/caseserver/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,23 @@ String getFormat(Path caseFile) {
public List<CaseInfos> getCases(Path directory) {
try (Stream<Path> walk = Files.walk(directory)) {
return walk.filter(Files::isRegularFile)
.map(file -> createInfos(file.getFileName().toString(), UUID.fromString(file.getParent().getFileName().toString()), getFormat(file)))
.collect(Collectors.toList());
.map(this::getCaseInfos)
.filter(Objects::nonNull)
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private CaseInfos getCaseInfos(Path file) {
try {
return createInfos(file.getFileName().toString(), UUID.fromString(file.getParent().getFileName().toString()), getFormat(file));
} catch (Exception e) {
LOGGER.error("Error processing file {}: {}", file.getFileName(), e.getMessage(), e);
return null;
}
}

public String getCaseName(UUID caseUuid) {
Path file = getCaseFile(caseUuid);
if (file == null) {
Expand Down
26 changes: 26 additions & 0 deletions src/test/java/com/powsybl/caseserver/CaseControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.powsybl.caseserver;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.io.ByteStreams;
import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
Expand Down Expand Up @@ -42,6 +44,7 @@
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -94,6 +97,9 @@ public class CaseControllerTest {
@Autowired
private OutputDestination outputDestination;

@Autowired
private ObjectMapper mapper;

@Value("${case-store-directory}")
private String rootDirectory;

Expand Down Expand Up @@ -684,4 +690,24 @@ private String getDateSearchTerm(String entsoeFormatDate) {
String utcFormattedDate = EntsoeFileNameParser.parseDateTime(entsoeFormatDate).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
return "date:\"" + utcFormattedDate + "\"";
}

@Test
public void invalidFileInCaseDirectoryShouldBeIgnored() throws Exception {
createStorageDir();
Path filePath = fileSystem.getPath(rootDirectory).resolve("randomFile.txt");
Files.createFile(filePath);
importCase(TEST_CASE, false);

MvcResult mvcResult = mvc.perform(get("/v1/cases"))
.andExpect(status().isOk())
.andReturn();
String resultAsString = mvcResult.getResponse().getContentAsString();
List<CaseInfos> caseInfos = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(1, caseInfos.size());
assertEquals("testCase.xiidm", caseInfos.get(0).getName());

Files.delete(filePath);
mvc.perform(delete("/v1/cases"))
.andExpect(status().isOk());
}
}

0 comments on commit 0831fff

Please sign in to comment.