Skip to content

Commit

Permalink
Merge pull request #40 from powsybl/jamal-khey/fix-duplication-on-non…
Browse files Browse the repository at this point in the history
…-indexed-cases

fix a bug affecting duplication of non indexed cases
  • Loading branch information
jamal-khey authored Aug 22, 2024
2 parents b9d7b67 + 7b716f1 commit 80f06cb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/main/java/com/powsybl/caseserver/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.zip.ZipOutputStream;

import static com.powsybl.caseserver.CaseException.createDirectoryNotFound;
import static com.powsybl.caseserver.dto.CaseInfos.*;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
Expand Down Expand Up @@ -114,7 +115,7 @@ public List<CaseInfos> getCases(Path directory) {

private CaseInfos getCaseInfos(Path file) {
try {
return createInfos(file.getFileName().toString(), UUID.fromString(file.getParent().getFileName().toString()), getFormat(file));
return createInfos(file, UUID.fromString(file.getParent().getFileName().toString()));
} catch (Exception e) {
LOGGER.error("Error processing file {}: {}", file.getFileName(), e.getMessage(), e);
return null;
Expand Down Expand Up @@ -230,12 +231,14 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) {
newCaseFile = newCaseUuidDirectory.resolve(existingCaseFile.getFileName());
Files.copy(existingCaseFile, newCaseFile, StandardCopyOption.COPY_ATTRIBUTES);

CaseInfos existingCaseInfos = caseInfosService.getCaseInfosByUuid(sourceCaseUuid.toString()).orElseThrow();
CaseInfos caseInfos = createInfos(existingCaseInfos.getName(), newCaseUuid, existingCaseInfos.getFormat());
caseInfosService.addCaseInfos(caseInfos);

CaseMetadataEntity existingCase = getCaseMetaDataEntity(sourceCaseUuid);
CaseInfos caseInfos = createInfos(newCaseFile, newCaseUuid);
if (existingCase.isIndexed()) {
caseInfosService.addCaseInfos(caseInfos);
}

createCaseMetadataEntity(newCaseUuid, withExpiration, existingCase.isIndexed());

sendImportMessage(caseInfos.createMessage());
return newCaseUuid;

Expand All @@ -244,6 +247,10 @@ UUID duplicateCase(UUID sourceCaseUuid, boolean withExpiration) {
}
}

private CaseInfos createInfos(Path caseFile, UUID caseUuid) {
return createInfos(caseFile.getFileName().toString(), caseUuid, getFormat(caseFile));
}

private CaseMetadataEntity getCaseMetaDataEntity(UUID caseUuid) {
return caseMetadataRepository.findById(caseUuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "case " + caseUuid + " not found"));
}
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/powsybl/caseserver/CaseControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,28 @@ public void test() throws Exception {
assertTrue(response.contains("\"format\":\"XIIDM\""));
}

@Test
public void testDuplicateNonIndexedCase() throws Exception {
// create the storage dir
createStorageDir();

// import IIDM test case
String caseUuid = mvc.perform(multipart("/v1/cases")
.file(createMockMultipartFile(TEST_CASE))
.param("withExpiration", "false")
.param("withIndexation", "false"))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
assertNotNull(outputDestination.receive(1000, caseImportDestination));
//duplicate an existing case
String duplicateCaseStr = mvc.perform(post("/v1/cases?duplicateFrom=" + caseUuid.substring(1, caseUuid.length() - 1)))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
UUID duplicateCaseUuid = UUID.fromString(duplicateCaseStr.substring(1, duplicateCaseStr.length() - 1));
assertNotNull(outputDestination.receive(1000, caseImportDestination));
assertFalse(caseMetadataRepository.findById(duplicateCaseUuid).get().isIndexed());
}

private UUID importCase(String testCase, Boolean withExpiration) throws Exception {
String importedCase;
if (withExpiration) {
Expand Down

0 comments on commit 80f06cb

Please sign in to comment.