Commit 09f05433 authored by Kristel Meikas's avatar Kristel Meikas
Browse files

TEIS-2113: created REST endpoint for testing signing file as a stream

parent 919fb67c
package ee.sm.ti.teis.commongateway.signing.dto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FileDto {
private String fileName;
private byte[] content;
private String contentType;
}
package ee.sm.ti.teis.commongateway.signing.request;
import ee.sm.ti.teis.AbstractDTO;
import ee.sm.ti.teis.ErrorDTO;
import ee.sm.ti.teis.commongateway.signing.dto.FileDto;
public class SignFileStreamRequest extends AbstractDTO<FileDto, ErrorDTO> {
public static final String ROUTING_KEY = "api.SignFileStreamRequest";
@Override
public String routingKey() {
return ROUTING_KEY;
}
}
\ No newline at end of file
package ee.sm.ti.teis.commongateway.signing.response;
import ee.sm.ti.teis.AbstractDTO;
import ee.sm.ti.teis.ErrorDTO;
import ee.sm.ti.teis.commongateway.signing.dto.FileDto;
public class SignFileStreamResponse extends AbstractDTO<FileDto, ErrorDTO> {
public static final String ROUTING_KEY = "api.SignFileStreamResponse";
@Override
public String routingKey() {
return ROUTING_KEY;
}
}
......@@ -3,16 +3,29 @@ package ee.sm.ti.teis.commongateway.signing;
import com.fasterxml.jackson.core.type.TypeReference;
import ee.sm.ti.teis.commongateway.CommonApiGatewayTestBase;
import ee.sm.ti.teis.commongateway.file.FileMetadataDto;
import ee.sm.ti.teis.commongateway.signing.dto.FileDto;
import ee.sm.ti.teis.commongateway.signing.request.SignExistingFileRequest;
import ee.sm.ti.teis.commongateway.signing.request.SignFileStreamRequest;
import ee.sm.ti.teis.commongateway.signing.response.SignFileStreamResponse;
import ee.sm.ti.teis.gatewaycommon.MockMvcUtil;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
import org.junit.jupiter.api.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.security.test.context.support.WithMockUser;
import java.io.UnsupportedEncodingException;
import static java.util.UUID.randomUUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.util.MimeTypeUtils.APPLICATION_OCTET_STREAM_VALUE;
@WithMockUser
public class SigningTestControllerTest extends CommonApiGatewayTestBase {
......@@ -22,6 +35,13 @@ public class SigningTestControllerTest extends CommonApiGatewayTestBase {
@Autowired
MockMvcUtil mockMvcUtil;
@Autowired
SigningTestController signingTestController;
@Autowired
SigningProcessingService signingProcessingService;
@MockBean
RabbitTemplate gwRabbitTemplate;
@Test
void signFile_success() {
when(requestProcessingService.sendAndReceive(any(SignExistingFileRequest.class)))
......@@ -33,4 +53,29 @@ public class SigningTestControllerTest extends CommonApiGatewayTestBase {
verify(requestProcessingService).sendAndReceive(any(SignExistingFileRequest.class));
}
@Test
void signFileStream_success() throws UnsupportedEncodingException {
SignFileStreamResponse response = new SignFileStreamResponse();
String fileName = "test-file.asice";
String content = "test-content";
String contentType = "application/pdf";
FileDto payload = FileDto.builder()
.fileName(fileName)
.contentType(APPLICATION_OCTET_STREAM_VALUE)
.content(content.getBytes())
.build();
response.setPayload(payload, new RequestMetaDTO());
when(gwRabbitTemplate.convertSendAndReceive(anyString(), any(SignFileStreamRequest.class)))
.thenReturn(response);
MockHttpServletResponse httpResponse = new MockHttpServletResponse();
signingTestController.signFileStream(fileName, contentType, new MockHttpServletRequest(), httpResponse,
new RequestMetaDTO());
assertThat(httpResponse.getContentAsString()).isEqualTo(content);
}
}
package ee.sm.ti.teis.commongateway.signing;
import ee.sm.ti.teis.commongateway.signing.dto.FileDto;
import ee.sm.ti.teis.commongateway.signing.request.SignFileStreamRequest;
import ee.sm.ti.teis.commongateway.signing.response.SignFileStreamResponse;
import ee.sm.ti.teis.exceptions.TeisResourceNotFoundException;
import ee.sm.ti.teis.gatewaycommon.error.NoResponseFromRabbitException;
import lombok.RequiredArgsConstructor;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@Service
@RequiredArgsConstructor
public class SigningProcessingService {
private final RabbitTemplate gwRabbitTemplate;
public void signFileStream(SignFileStreamRequest request, HttpServletResponse response) {
FileDto signedFileStream = getSignedFileStream(request);
response.addHeader("Content-disposition", "attachment;filename=" + signedFileStream.getFileName());
response.setContentType(signedFileStream.getContentType());
try (ByteArrayInputStream docStream = new ByteArrayInputStream(signedFileStream.getContent())) {
IOUtils.copy(docStream, response.getOutputStream());
response.flushBuffer();
} catch (IOException e) {
throw new TeisResourceNotFoundException("File stream processing failed.");
}
}
private FileDto getSignedFileStream(SignFileStreamRequest request) {
SignFileStreamResponse response = (SignFileStreamResponse) gwRabbitTemplate.convertSendAndReceive(request.routingKey(), request);
if (response != null) {
return response.processResponse();
}
throw new NoResponseFromRabbitException(request.getRequestMetaDTO().getRequestId());
}
}
package ee.sm.ti.teis.commongateway.signing;
import ee.sm.ti.teis.commongateway.file.FileMetadataDto;
import ee.sm.ti.teis.commongateway.signing.dto.FileDto;
import ee.sm.ti.teis.commongateway.signing.dto.SignFileDto;
import ee.sm.ti.teis.commongateway.signing.request.SignExistingFileRequest;
import ee.sm.ti.teis.commongateway.signing.request.SignFileStreamRequest;
import ee.sm.ti.teis.exceptions.TeisIllegalArgumentException;
import ee.sm.ti.teis.gatewaycommon.controller.TeisBaseController;
import ee.sm.ti.teis.gatewaycommon.rabbit.RequestProcessingService;
import ee.sm.ti.teis.servicerequest.RequestMetaDTO;
......@@ -11,7 +14,9 @@ import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
import static org.apache.commons.lang3.StringUtils.isEmpty;
......@@ -23,6 +28,7 @@ import static org.apache.commons.lang3.StringUtils.isEmpty;
public class SigningTestController extends TeisBaseController {
private final RequestProcessingService service;
private final SigningProcessingService processingService;
@ApiOperation(value = "${teis.api.signings.sign-file-by-id}", response = FileMetadataDto.class)
@PostMapping("sign-file/{fileId}")
......@@ -34,4 +40,24 @@ public class SigningTestController extends TeisBaseController {
return (FileMetadataDto) service.sendAndReceive(request);
}
@ApiOperation(value = "${teis.api.signings.sign-file-stream}")
@PostMapping("sign-file-stream")
public void signFileStream(@RequestParam String fileName, @RequestParam String contentType,
HttpServletRequest servletRequest, HttpServletResponse response, RequestMetaDTO requestMetaDTO) {
SignFileStreamRequest request = new SignFileStreamRequest();
try {
FileDto payload = FileDto.builder()
.fileName(fileName)
.content(servletRequest.getInputStream().readAllBytes())
.contentType(contentType)
.build();
request.setPayload(payload, requestMetaDTO);
} catch (IOException e) {
throw new TeisIllegalArgumentException("Can't read input file");
}
processingService.signFileStream(request, response);
}
}
......@@ -115,6 +115,7 @@ teis.api.payments.patch-claim: Teenus võimaldab tasumise nõude (nt sunniraha t
teis.api.payments.delete-claim: Teenus võimaldab tasumise nõude (nt sunniraha tasumise nõude) kustutada.
teis.api.signings.sign-file-by-id: Teenus allkirjastab olemasoleva faili.
teis.api.signings.sign-file-stream: Teenus allkirjastab päringuga saadetud faili.
teis.api.parameters.get: Teenus, mis tagastab süsteemi erinevate parameetrite väärtused.
teis.api.parameters.get-admin: Tegemist on API-ga, mis tagastab süsteemi parameetrid väärtuste haldamise kasutajaliidese jaoks.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment