From 0831ffffa1d845d86229020c28d83ea421938081 Mon Sep 17 00:00:00 2001 From: Antoine Bouhours Date: Wed, 24 Apr 2024 11:35:25 +0200 Subject: [PATCH] Handle exception when getCases processes corrupted cases (#29) Signed-off-by: BOUHOURS Antoine --- .../com/powsybl/caseserver/CaseService.java | 14 ++++++++-- .../caseserver/CaseControllerTest.java | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/powsybl/caseserver/CaseService.java b/src/main/java/com/powsybl/caseserver/CaseService.java index f3d75a2..d051201 100644 --- a/src/main/java/com/powsybl/caseserver/CaseService.java +++ b/src/main/java/com/powsybl/caseserver/CaseService.java @@ -111,13 +111,23 @@ String getFormat(Path caseFile) { public List getCases(Path directory) { try (Stream 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) { diff --git a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java index 52ed016..b5f703e 100644 --- a/src/test/java/com/powsybl/caseserver/CaseControllerTest.java +++ b/src/test/java/com/powsybl/caseserver/CaseControllerTest.java @@ -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; @@ -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; @@ -94,6 +97,9 @@ public class CaseControllerTest { @Autowired private OutputDestination outputDestination; + @Autowired + private ObjectMapper mapper; + @Value("${case-store-directory}") private String rootDirectory; @@ -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 = 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()); + } }