Skip to content

Commit

Permalink
Fix/tosca app api (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-be authored May 30, 2022
1 parent 6570a32 commit ea6bc13
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -60,6 +64,8 @@ public class ToscaApplicationController {

private final ToscaApplicationService toscaApplicationService;

private ObjectMapper mapper;

@Operation(responses = {
@ApiResponse(responseCode = "200")
}, description = "Retrieve all TOSCA applications.")
Expand All @@ -70,15 +76,16 @@ public ResponseEntity<Page<ToscaApplicationDto>> 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<ToscaApplicationDto> 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<String, Object> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -90,30 +91,32 @@ 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",
"hello.txt",
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));
Expand Down

0 comments on commit ea6bc13

Please sign in to comment.