Skip to content

Commit

Permalink
Make casename an optional request param
Browse files Browse the repository at this point in the history
When not given or empty, use filename as case name

Signed-off-by: EL-MHARI Charaf <charaf.elmhari@rte-france.com>
  • Loading branch information
celmhari committed Sep 18, 2024
1 parent 595f0a7 commit e900bf2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import org.gridsuite.caseimport.server.dto.ImportedCase;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
Expand All @@ -19,6 +20,8 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

/**
Expand All @@ -36,14 +39,14 @@ public CaseImportController(CaseImportService caseImportService) {
this.caseImportService = caseImportService;
}

@PostMapping(value = "/cases/{caseName}", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
@PostMapping(value = "/cases", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = APPLICATION_JSON_VALUE)
@Operation(summary = "Import a case in the parametrized directory")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The case is imported"),
@ApiResponse(responseCode = "400", description = "Invalid case file"),
@ApiResponse(responseCode = "422", description = "File with wrong extension"),
@ApiResponse(responseCode = "201", description = "Case created successfully")})
public ResponseEntity<ImportedCase> importCase(@Parameter(description = "case file") @RequestPart("caseFile") MultipartFile caseFile,
@Parameter(description = "name of the case") @PathVariable String caseName,
@Parameter(description = "name of the case") @NotBlank Optional<String> caseName,
@Parameter(description = "origin of case file") @RequestParam(defaultValue = "default", required = false) String caseFileSource,
@RequestHeader("userId") String userId) {
ImportedCase importedCase = caseImportService.importCaseInDirectory(caseFile, caseName, caseFileSource, userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;
import java.util.UUID;

import static org.gridsuite.caseimport.server.CaseImportException.Type.UNKNOWN_CASE_SOURCE;
Expand Down Expand Up @@ -44,9 +45,10 @@ private String getTargetDirectory(String caseOrigin) {
return targetDirectory;
}

ImportedCase importCaseInDirectory(MultipartFile caseFile, String caseName, String caseOrigin, String userId) {
ImportedCase importCaseInDirectory(MultipartFile caseFile, Optional<String> caseNameParam, String caseOrigin, String userId) {
String targetDirectory = getTargetDirectory(caseOrigin);
UUID caseUuid = caseService.importCase(caseFile);
String caseName = caseNameParam.filter(s -> !s.isEmpty()).orElse(caseFile.getOriginalFilename());
var caseElementAttributes = new ElementAttributes(caseUuid, caseName, CASE, new AccessRightsAttributes(false), userId, 0L, null);
directoryService.createElementInDirectory(caseElementAttributes, targetDirectory, userId);
ImportedCase importedCase = new ImportedCase();
Expand Down
34 changes: 28 additions & 6 deletions src/test/java/org/gridsuite/caseimport/server/CaseImportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public void testImportCase() throws Exception {
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);

mockMvc.perform(multipart("/v1/cases/testCase").file(mockFile)
mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
.contentType(MediaType.MULTIPART_FORM_DATA)
)
Expand All @@ -94,7 +94,7 @@ public void testImportCaseWithBadRequestError() throws Exception {
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE_WITH_ERRORS, "text/xml", is);

mockMvc.perform(multipart("/v1/cases/testCase_with_errors").file(mockFile)
mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
.contentType(MediaType.MULTIPART_FORM_DATA)
)
Expand All @@ -109,7 +109,7 @@ public void testImportCaseWithUnprocessableEntityError() throws Exception {
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_INCORRECT_FILE, "text/xml", is);

mockMvc.perform(multipart("/v1/cases/incorrectFile").file(mockFile)
mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
.contentType(MediaType.MULTIPART_FORM_DATA)
)
Expand All @@ -124,7 +124,7 @@ public void testImportCaseWithInvalidOrigin() throws Exception {
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);

mockMvc.perform(multipart("/v1/cases/testCase").file(mockFile)
mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
.contentType(MediaType.MULTIPART_FORM_DATA)
.param("caseFileSource", INVALID_CASE_ORIGIN)
Expand All @@ -135,19 +135,41 @@ public void testImportCaseWithInvalidOrigin() throws Exception {

@Test
public void testImportCaseWithValidOrigin() throws Exception {
final String caseName = "testCase";
wireMockUtils.stubImportCase(TEST_FILE);
wireMockUtils.stubAddDirectoryElement(CASE_ORIGIN_1_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);

mockMvc.perform(multipart("/v1/cases/testCase").file(mockFile)
mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
.contentType(MediaType.MULTIPART_FORM_DATA)
.param("caseFileSource", CASE_ORIGIN_1)
.param("caseName", caseName)
)
.andExpectAll(status().isCreated(),
jsonPath("caseName").value("testCase"),
jsonPath("caseName").value(caseName),
jsonPath("parentDirectory").value(CASE_ORIGIN_1_DIRECTORY));
}
}

@Test
public void testGivenEmptyCaseNameUseFilename() throws Exception {
wireMockUtils.stubImportCase(TEST_FILE);
wireMockUtils.stubAddDirectoryElement(CASE_ORIGIN_1_DIRECTORY);
try (InputStream is = new FileInputStream(ResourceUtils.getFile("classpath:" + TEST_FILE))) {
MockMultipartFile mockFile = new MockMultipartFile("caseFile", TEST_FILE, "text/xml", is);

mockMvc.perform(multipart("/v1/cases").file(mockFile)
.header("userId", USER1)
.contentType(MediaType.MULTIPART_FORM_DATA)
.param("caseFileSource", CASE_ORIGIN_1)
.param("caseName", "")
)
.andExpectAll(status().isCreated(),
jsonPath("caseName").value(TEST_FILE),
jsonPath("parentDirectory").value(CASE_ORIGIN_1_DIRECTORY));
}

}
}

0 comments on commit e900bf2

Please sign in to comment.