Skip to content

Commit

Permalink
Separate download and convert format (cases) (#26)
Browse files Browse the repository at this point in the history
Signed-off-by: Slimane AMAR <[email protected]>
  • Loading branch information
SlimaneAmar authored Apr 11, 2024
1 parent 3304091 commit 979a9c6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/main/java/com/powsybl/caseserver/CaseController.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,20 @@ public ResponseEntity<String> getCaseName(@PathVariable("caseUuid") UUID caseUui
return ResponseEntity.ok().body(caseName);
}

@GetMapping(value = "/cases/{caseUuid}")
@Operation(summary = "Download a case")
public ResponseEntity<byte[]> downloadCase(@PathVariable("caseUuid") UUID caseUuid) {
LOGGER.debug("getCase request received with parameter caseUuid = {}", caseUuid);
byte[] bytes = caseService.getCaseBytes(caseUuid).orElse(null);
if (bytes == null) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(bytes);

}

@PostMapping(value = "/cases/{caseUuid}", consumes = "application/json")
@Operation(summary = "Export a case",
requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(description = "Parameters for chosen format",
Expand All @@ -110,7 +124,7 @@ public ResponseEntity<byte[]> exportCase(
@PathVariable UUID caseUuid,
@RequestParam String format,
@RequestBody(required = false) Map<String, Object> formatParameters) throws IOException {
LOGGER.debug("getCase request received with parameter caseUuid = {}", caseUuid);
LOGGER.debug("exportCase request received with parameter caseUuid = {}", caseUuid);
return caseService.exportCase(caseUuid, format, formatParameters).map(networkInfos -> {
var headers = new HttpHeaders();
headers.setContentDisposition(
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/powsybl/caseserver/CaseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,25 @@ public List<CaseInfos> getMetadata(List<UUID> ids) {
return cases;
}

Optional<byte[]> getCaseBytes(UUID caseUuid) {
checkStorageInitialization();

Path caseFile = getCaseFile(caseUuid);
if (caseFile == null) {
return Optional.empty();
}

if (Files.exists(caseFile) && Files.isRegularFile(caseFile)) {
try {
byte[] bytes = Files.readAllBytes(caseFile);
return Optional.of(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return Optional.empty();
}

public Optional<ExportCaseInfos> exportCase(UUID caseUuid, String format, Map<String, Object> formatParameters) throws IOException {
if (!Exporter.getFormats().contains(format)) {
throw CaseException.createUnsupportedFormat(format);
Expand Down
17 changes: 14 additions & 3 deletions src/test/java/com/powsybl/caseserver/CaseControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,30 @@ public void test() throws Exception {
.andReturn();
assertThat(mvcResult.getResponse().getHeader("content-disposition")).contains("attachment;");

// retrieve a case in CGMES format
// downlaod a case
mvc.perform(get(GET_CASE_URL, firstCaseUuid))
.andExpect(status().isOk())
.andExpect(content().xml(testCaseContent))
.andReturn();

// downlaod a non existing case
mvc.perform(get(GET_CASE_URL, UUID.randomUUID()))
.andExpect(status().isNoContent())
.andReturn();

// export a case in CGMES format
mvcResult = mvc.perform(post(GET_CASE_URL, firstCaseUuid).param("format", "CGMES"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_OCTET_STREAM))
.andReturn();
assertThat(mvcResult.getResponse().getHeader("content-disposition")).contains("attachment;");

// retrieve a non-existing case
// export a non-existing case
mvc.perform(post(GET_CASE_URL, UUID.randomUUID()).param("format", "XIIDM"))
.andExpect(status().isNoContent())
.andReturn();

// retrieve a case in a non-existing format
// export a case in a non-existing format
mvc.perform(post(GET_CASE_URL, firstCaseUuid).param("format", "JPEG"))
.andExpect(status().isUnprocessableEntity());

Expand Down

0 comments on commit 979a9c6

Please sign in to comment.