Skip to content
Snippets Groups Projects
Commit 6961e3e4 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-6949 CR Kommentare

parent 2d3b5796
Branches
No related tags found
No related merge requests found
......@@ -38,6 +38,7 @@ import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import de.ozgcloud.antragsraum.WebConfiguration;
import de.ozgcloud.antragsraum.common.RestClientUtils;
......@@ -70,15 +71,20 @@ class FileRestClient {
request.setNachrichtId(fileIdentificationData.nachrichtId());
request.setSamlToken(AuthenticationHelper.getSamlToken());
return restClient
.post()
.uri(FIND_FILE_METADATA_API)
.header(X_GRPC_ADDRESS, RestClientUtils.sanitizeAddress(address))
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(AntragraumGrpcGetAttachmentMetadataResponse.class).getBody();
try {
return restClient
.post()
.uri(FIND_FILE_METADATA_API)
.header(X_GRPC_ADDRESS, RestClientUtils.sanitizeAddress(address))
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.accept(MediaType.APPLICATION_JSON)
.retrieve()
.toEntity(AntragraumGrpcGetAttachmentMetadataResponse.class).getBody();
} catch (RestClientException e) {
LOG.error("Error loading file metadata. {}", e.getMessage(), e);
throw e;
}
}
void uploadFile(final OzgUploadFile uploadFile, final String address, final CompletableFuture<String> fileIdFuture) {
......@@ -114,16 +120,21 @@ class FileRestClient {
request.setNachrichtId(fileIdentificationData.nachrichtId());
request.setSamlToken(AuthenticationHelper.getSamlToken());
var fileContent = Optional.ofNullable(restClient
.post()
.uri(GET_FILE_CONTENT_API, fileIdentificationData.fileId())
.header(X_GRPC_ADDRESS, RestClientUtils.sanitizeAddress(address))
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.accept(MediaType.APPLICATION_OCTET_STREAM)
.retrieve().toEntity(Resource.class).getBody());
fileContent.ifPresent(file -> processFile(out, file));
try {
var fileContent = Optional.ofNullable(restClient
.post()
.uri(GET_FILE_CONTENT_API, fileIdentificationData.fileId())
.header(X_GRPC_ADDRESS, RestClientUtils.sanitizeAddress(address))
.contentType(MediaType.APPLICATION_JSON)
.body(request)
.accept(MediaType.APPLICATION_OCTET_STREAM)
.retrieve().toEntity(Resource.class).getBody());
fileContent.ifPresent(file -> processFile(out, file));
} catch (RestClientException e) {
LOG.error("Error downloading file. {}", e.getMessage(), e);
throw e;
}
}
private static void processFile(final OutputStream out, Resource fileContent) {
......
......@@ -39,10 +39,12 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.boot.test.web.client.MockServerRestClientCustomizer;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
......@@ -78,25 +80,33 @@ class FileRestClientTest {
@Nested
class TestGetFileMetadata {
private static final String FILE_METADATA_URI = ADDRESS + FIND_FILE_METADATA_API;
@BeforeEach
void setup() throws JsonProcessingException {
server.expect(requestTo(FILE_METADATA_URI)).andRespond(
withSuccess(objectMapper.writeValueAsString(AntragraumGrpcGetAttachmentMetadataResponseTestFactory.create()),
MediaType.APPLICATION_JSON));
}
private static final String FILE_METADATA_URI = ADDRESS + FIND_FILE_METADATA_API;
@Test
void shouldLoadFileMetadata() {
void shouldLoadFileMetadata() throws JsonProcessingException {
try (var authenticationHelper = mockStatic(AuthenticationHelper.class)) {
authenticationHelper.when(AuthenticationHelper::getSamlToken).thenReturn(UserTestFactory.SAML_TOKEN);
server.expect(requestTo(FILE_METADATA_URI)).andRespond(
withSuccess(objectMapper.writeValueAsString(AntragraumGrpcGetAttachmentMetadataResponseTestFactory.create()),
MediaType.APPLICATION_JSON));
var fileMetadata = fileRestClient.getFileMetadata(FILE_IDENTIFICATION_DATA, ADDRESS + FIND_FILE_METADATA_API);
assertThat(fileMetadata).isNotNull();
}
}
@Test
void shouldThrowRestClientException() {
try (var authenticationHelper = mockStatic(AuthenticationHelper.class)) {
authenticationHelper.when(AuthenticationHelper::getSamlToken).thenReturn(UserTestFactory.SAML_TOKEN);
server.expect(requestTo(FILE_METADATA_URI)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
assertThatExceptionOfType(RestClientException.class).isThrownBy(
() -> fileRestClient.getFileMetadata(FILE_IDENTIFICATION_DATA, ADDRESS + GET_FILE_CONTENT_API));
}
}
}
@Nested
......@@ -127,18 +137,15 @@ class FileRestClientTest {
@Nested
class TestDownloadFileContent {
private static final String FILE_CONTENT_URI = ADDRESS + GET_FILE_CONTENT_API;
@BeforeEach
void setup() throws IOException {
server.expect(requestTo(FILE_CONTENT_URI))
.andRespond(withSuccess(OzgUploadFileTestFactory.MULTIPART_FILE.getBytes(), MediaType.APPLICATION_OCTET_STREAM));
}
private static final String FILE_CONTENT_URI = ADDRESS + GET_FILE_CONTENT_API;
@Test
void shouldDownloadFileContent() throws IOException {
try (var authenticationHelper = mockStatic(AuthenticationHelper.class)) {
authenticationHelper.when(AuthenticationHelper::getSamlToken).thenReturn(UserTestFactory.SAML_TOKEN);
server.expect(requestTo(FILE_CONTENT_URI))
.andRespond(withSuccess(OzgUploadFileTestFactory.MULTIPART_FILE.getBytes(), MediaType.APPLICATION_OCTET_STREAM));
var out = new ByteArrayOutputStream();
fileRestClient.downloadFileContent(FILE_IDENTIFICATION_DATA, ADDRESS + GET_FILE_CONTENT_API, out);
......@@ -149,9 +156,11 @@ class FileRestClientTest {
}
@Test
void shouldDownloadFileContentIoException() {
void shouldThrowTechnicalException() throws IOException {
try (var authenticationHelper = mockStatic(AuthenticationHelper.class)) {
authenticationHelper.when(AuthenticationHelper::getSamlToken).thenReturn(UserTestFactory.SAML_TOKEN);
server.expect(requestTo(FILE_CONTENT_URI))
.andRespond(withSuccess(OzgUploadFileTestFactory.MULTIPART_FILE.getBytes(), MediaType.APPLICATION_OCTET_STREAM));
var out = mock(ByteArrayOutputStream.class);
doThrow(IOException.class).when(out).write(any(byte[].class), anyInt(), anyInt());
......@@ -160,5 +169,17 @@ class FileRestClientTest {
() -> fileRestClient.downloadFileContent(FILE_IDENTIFICATION_DATA, ADDRESS + GET_FILE_CONTENT_API, out));
}
}
@Test
void shouldThrowRestClientException() {
try (var authenticationHelper = mockStatic(AuthenticationHelper.class)) {
authenticationHelper.when(AuthenticationHelper::getSamlToken).thenReturn(UserTestFactory.SAML_TOKEN);
server.expect(requestTo(FILE_CONTENT_URI)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
assertThatExceptionOfType(RestClientException.class).isThrownBy(
() -> fileRestClient.downloadFileContent(FILE_IDENTIFICATION_DATA, ADDRESS + GET_FILE_CONTENT_API,
mock(ByteArrayOutputStream.class)));
}
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment