From ea6bc132cf5f3fad87469de1dc2cd82db3b64bfd Mon Sep 17 00:00:00 2001 From: Marvin Bechtold Date: Mon, 30 May 2022 07:57:17 +0200 Subject: [PATCH] Fix/tosca app api (#215) --- .../ToscaApplicationController.java | 13 +++++++++--- .../ToscaApplicationControllerTest.java | 21 +++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/org.planqk.atlas.web/src/main/java/org/planqk/atlas/web/controller/ToscaApplicationController.java b/org.planqk.atlas.web/src/main/java/org/planqk/atlas/web/controller/ToscaApplicationController.java index b4c053c9..dc092987 100644 --- a/org.planqk.atlas.web/src/main/java/org/planqk/atlas/web/controller/ToscaApplicationController.java +++ b/org.planqk.atlas.web/src/main/java/org/planqk/atlas/web/controller/ToscaApplicationController.java @@ -19,8 +19,12 @@ package org.planqk.atlas.web.controller; +import java.util.Map; import java.util.UUID; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.type.MapType; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; @@ -60,6 +64,8 @@ public class ToscaApplicationController { private final ToscaApplicationService toscaApplicationService; + private ObjectMapper mapper; + @Operation(responses = { @ApiResponse(responseCode = "200") }, description = "Retrieve all TOSCA applications.") @@ -70,15 +76,16 @@ public ResponseEntity> getApplications( return ResponseEntity.ok(ModelMapperUtils.convertPage(toscaApplicationService.findAll(listParameters.getPageable()), ToscaApplicationDto.class)); } - @Operation(responses = { @ApiResponse(responseCode = "201"), @ApiResponse(responseCode = "400", description = "Bad Request. Invalid request body."), }, description = "Create a TOSCA Application.") @PostMapping() public ResponseEntity createApplication(@RequestPart("file") MultipartFile file, - @RequestPart("name") String name) { - final ToscaApplication savedToscaApplication = this.toscaApplicationService.createFromFile(file, name); + @RequestPart("payload") String payload) throws JsonProcessingException { + final MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class); + final Map payloadMap = mapper.readValue(payload, mapType); + final ToscaApplication savedToscaApplication = this.toscaApplicationService.createFromFile(file, payloadMap.get("name").toString()); return new ResponseEntity<>(ModelMapperUtils.convert(savedToscaApplication, ToscaApplicationDto.class), HttpStatus.CREATED); } diff --git a/org.planqk.atlas.web/src/test/java/org/planqk/atlas/web/controller/ToscaApplicationControllerTest.java b/org.planqk.atlas.web/src/test/java/org/planqk/atlas/web/controller/ToscaApplicationControllerTest.java index 601da3f5..518bb372 100644 --- a/org.planqk.atlas.web/src/test/java/org/planqk/atlas/web/controller/ToscaApplicationControllerTest.java +++ b/org.planqk.atlas.web/src/test/java/org/planqk/atlas/web/controller/ToscaApplicationControllerTest.java @@ -50,6 +50,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doThrow; @@ -90,14 +91,13 @@ class ToscaApplicationControllerTest { public void createApplication_returnCreated() { UUID uuid = UUID.randomUUID(); - String name = "Test Name"; + String name = "Test-Name"; + String nameJson = String.format("{\"name\":\"%s\"}", name); var returnedResource = new ToscaApplication(); returnedResource.setName(name); returnedResource.setId(uuid); - doReturn(returnedResource).when(toscaApplicationService).createFromFile(any(), any()); - MockMultipartFile file = new MockMultipartFile( "file", @@ -105,15 +105,18 @@ public void createApplication_returnCreated() { MediaType.TEXT_PLAIN_VALUE, "Hello, World!".getBytes() ); - MockPart namePart = new MockPart("name", "name", name.getBytes()); + + doReturn(returnedResource).when(toscaApplicationService).createFromFile(eq(file), eq(name)); + + var url = linkBuilderService.urlStringTo( + methodOn(ToscaApplicationController.class).createApplication(null, null) + ); + + MockPart namePart = new MockPart("payload", "payload", nameJson.getBytes()); namePart.getHeaders().setContentType(MediaType.TEXT_PLAIN); mockMvc.perform( - multipart( - linkBuilderService.urlStringTo( - methodOn(ToscaApplicationController.class).createApplication(null, null) - ) - ).file(file).part(namePart) + multipart(url).file(file).part(namePart) ).andExpect(status().isCreated()) .andExpect(jsonPath("$.id").value(uuid.toString())) .andExpect(jsonPath("$.name").value(name));