Skip to content

Commit 1a1acae

Browse files
committed
Move invalidFileInCaseDirectoryShouldBeIgnored to FsCaseControllerTest
Signed-off-by: Etienne Homer <etiennehomer@gmail.com>
1 parent 6a41f57 commit 1a1acae

File tree

2 files changed

+46
-35
lines changed

2 files changed

+46
-35
lines changed

src/test/java/com/powsybl/caseserver/service/AbstractCaseControllerTest.java

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
*/
77
package com.powsybl.caseserver.service;
88

9-
import com.fasterxml.jackson.core.type.TypeReference;
109
import com.fasterxml.jackson.databind.ObjectMapper;
1110
import com.google.common.io.ByteStreams;
1211
import com.powsybl.caseserver.ContextConfigurationWithTestChannel;
@@ -57,7 +56,7 @@
5756
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK)
5857
@ContextConfigurationWithTestChannel
5958
abstract class AbstractCaseControllerTest {
60-
private static final String TEST_CASE = "testCase.xiidm";
59+
public static final String TEST_CASE = "testCase.xiidm";
6160
private static final String TEST_TAR_CASE = "tarCase.tar";
6261
private static final String TEST_CASE_FORMAT = "XIIDM";
6362
private static final String NOT_A_NETWORK = "notANetwork.txt";
@@ -80,11 +79,11 @@ abstract class AbstractCaseControllerTest {
8079
OutputDestination outputDestination;
8180

8281
@Autowired
83-
private ObjectMapper mapper;
82+
public ObjectMapper mapper;
8483

8584
FileSystem fileSystem;
8685

87-
private final String caseImportDestination = "case.import.destination";
86+
public final String caseImportDestination = "case.import.destination";
8887

8988
@AfterEach
9089
public void tearDown() throws Exception {
@@ -93,7 +92,7 @@ public void tearDown() throws Exception {
9392
TestUtils.assertQueuesEmptyThenClear(destinations, outputDestination);
9493
}
9594

96-
private void createStorageDir() throws IOException {
95+
void createStorageDir() throws IOException {
9796
Path path = fileSystem.getPath(caseService.getRootDirectory());
9897
if (!Files.exists(path)) {
9998
Files.createDirectories(path);
@@ -467,7 +466,7 @@ void testDuplicateNonIndexedCase() throws Exception {
467466
assertFalse(caseMetadataRepository.findById(duplicateCaseUuid).get().isIndexed());
468467
}
469468

470-
private UUID importCase(String testCase, Boolean withExpiration) throws Exception {
469+
UUID importCase(String testCase, Boolean withExpiration) throws Exception {
471470
String importedCase;
472471
if (withExpiration) {
473472
importedCase = mvc.perform(multipart("/v1/cases")
@@ -721,35 +720,6 @@ private static String getDateSearchTerm(String entsoeFormatDate) {
721720
return "date:\"" + utcFormattedDate + "\"";
722721
}
723722

724-
@Test
725-
void invalidFileInCaseDirectoryShouldBeIgnored() throws Exception {
726-
createStorageDir();
727-
728-
// add a random file in the storage, not stored in a UUID named directory
729-
Path filePath = fileSystem.getPath(caseService.getRootDirectory()).resolve("randomFile.txt");
730-
Files.createFile(filePath);
731-
732-
// add a case file in a UUID named directory but no metadata in the database
733-
Path casePath = fileSystem.getPath(caseService.getRootDirectory()).resolve(UUID.randomUUID().toString());
734-
Files.createDirectory(casePath);
735-
Files.createFile(casePath.resolve(TEST_CASE));
736-
737-
importCase(TEST_CASE, false);
738-
739-
MvcResult mvcResult = mvc.perform(get("/v1/cases"))
740-
.andExpect(status().isOk())
741-
.andReturn();
742-
String resultAsString = mvcResult.getResponse().getContentAsString();
743-
List<CaseInfos> caseInfos = mapper.readValue(resultAsString, new TypeReference<>() { });
744-
assertEquals(1, caseInfos.size());
745-
assertEquals(TEST_CASE, caseInfos.get(0).getName());
746-
747-
Files.delete(filePath);
748-
mvc.perform(delete("/v1/cases"))
749-
.andExpect(status().isOk());
750-
assertNotNull(outputDestination.receive(1000, caseImportDestination));
751-
}
752-
753723
@Test
754724
void testGetCaseBaseName() throws Exception {
755725
MvcResult mvcResult = mvc.perform(get("/v1/cases/caseBaseName?caseName=case.xml"))

src/test/java/com/powsybl/caseserver/service/FsCaseControllerTest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@
66
*/
77
package com.powsybl.caseserver.service;
88

9+
import com.fasterxml.jackson.core.type.TypeReference;
910
import com.google.common.jimfs.Configuration;
1011
import com.google.common.jimfs.Jimfs;
1112

13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertNotNull;
1215
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
16+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
1317
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
1418

19+
import com.powsybl.caseserver.dto.CaseInfos;
1520
import com.powsybl.computation.ComputationManager;
1621
import org.junit.jupiter.api.BeforeEach;
1722
import org.junit.jupiter.api.Test;
1823
import org.mockito.Mockito;
1924
import org.springframework.beans.factory.annotation.Autowired;
2025
import org.springframework.test.context.TestPropertySource;
26+
import org.springframework.test.web.servlet.MvcResult;
27+
28+
import java.nio.file.Files;
29+
import java.nio.file.Path;
30+
import java.util.List;
31+
import java.util.UUID;
2132

2233
/**
2334
* @author Ghazwa Rehili <ghazwa.rehili at rte-france.com>
@@ -44,4 +55,34 @@ void testStorageNotCreated() throws Exception {
4455
mvc.perform(delete("/v1/cases")).andExpect(status().isUnprocessableEntity());
4556
}
4657

58+
@Test
59+
void invalidFileInCaseDirectoryShouldBeIgnored() throws Exception {
60+
createStorageDir();
61+
62+
// add a random file in the storage, not stored in a UUID named directory
63+
Path filePath = fileSystem.getPath(caseService.getRootDirectory()).resolve("randomFile.txt");
64+
Files.createFile(filePath);
65+
66+
// add a case file in a UUID named directory but no metadata in the database
67+
Path casePath = fileSystem.getPath(caseService.getRootDirectory()).resolve(UUID.randomUUID().toString());
68+
Files.createDirectory(casePath);
69+
Files.write(casePath.resolve(TEST_CASE), AbstractCaseControllerTest.class.getResourceAsStream("/" + TEST_CASE).readAllBytes());
70+
71+
// import case properly
72+
importCase(TEST_CASE, false);
73+
74+
MvcResult mvcResult = mvc.perform(get("/v1/cases"))
75+
.andExpect(status().isOk())
76+
.andReturn();
77+
String resultAsString = mvcResult.getResponse().getContentAsString();
78+
List<CaseInfos> caseInfos = mapper.readValue(resultAsString, new TypeReference<>() { });
79+
assertEquals(1, caseInfos.size());
80+
assertEquals(TEST_CASE, caseInfos.get(0).getName());
81+
82+
Files.delete(filePath);
83+
mvc.perform(delete("/v1/cases"))
84+
.andExpect(status().isOk());
85+
assertNotNull(outputDestination.receive(1000, caseImportDestination));
86+
}
87+
4788
}

0 commit comments

Comments
 (0)