Skip to content

Commit

Permalink
test error parsing
Browse files Browse the repository at this point in the history
test checking status of message in transit
  • Loading branch information
ekl176 committed Jan 6, 2025
1 parent 40deae0 commit 96a763d
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package de.bund.digitalservice.a2j.service.egvp.DTO;

public record MessageDeliveryStatusResponse(
String messageId, Boolean delivered, String pendingReason, String path) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package de.bund.digitalservice.a2j.service.egvp.DTO;

public record SendMessageRequest(
String receiverId,
String bundIdMailbox,
String subject,
String attachmentFile,
String xJustizFile) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package de.bund.digitalservice.a2j.service.egvp.DTO;

public record SendMessageResponse(String customId) {}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package de.bund.digitalservice.a2j.service.egvp;

import de.bund.digitalservice.a2j.service.egvp.DTO.GetVersionResponse;
import de.bund.digitalservice.a2j.service.egvp.DTO.ResponseError;
import de.bund.digitalservice.a2j.service.egvp.DTO.*;
import java.util.Objects;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

public class EgvpClient {
Expand All @@ -13,17 +13,45 @@ public EgvpClient(RestTemplate client) {
this.client = client;
}

public GetVersionResponse getVersion() throws EgvpClientException, HttpClientErrorException {
public GetVersionResponse getVersion() throws EgvpClientException {
try {
return this.client.getForEntity("/getVersion", GetVersionResponse.class).getBody();
} catch (HttpClientErrorException e) {
ResponseError error = e.getResponseBodyAs(ResponseError.class);
throw parseException(e);
} catch (RestClientException e) {
throw new EgvpClientException(e.getMessage());
}
}

public SendMessageResponse sendMessage(SendMessageRequest request) throws EgvpClientException {
try {
return this.client
.postForEntity("/sendMessage", request, SendMessageResponse.class)
.getBody();
} catch (HttpClientErrorException e) {
throw parseException(e);
} catch (RestClientException e) {
throw new EgvpClientException(e.getMessage());
}
}

public MessageDeliveryStatusResponse checkMessageStatus(String customId) {

try {
return this.client.getForObject(
"/getMessageDeliveryStatus/{customId}", MessageDeliveryStatusResponse.class, customId);
} catch (HttpClientErrorException e) {
throw parseException(e);
} catch (RestClientException e) {
throw new EgvpClientException(e.getMessage());
}
}

if (Objects.isNull(error)) {
throw e;
} else {
throw new EgvpClientException(error.responseCode());
}
private EgvpClientException parseException(HttpClientErrorException e) {
ResponseError re = e.getResponseBodyAs(ResponseError.class);
if (Objects.isNull(re)) {
return new EgvpClientException(e.getMessage());
}
return new EgvpClientException(re.responseCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package de.bund.digitalservice.a2j.integration;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.*;
import static org.springframework.test.web.client.response.MockRestResponseCreators.*;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import de.bund.digitalservice.a2j.service.egvp.DTO.GetVersionResponse;
import de.bund.digitalservice.a2j.service.egvp.DTO.MessageDeliveryStatusResponse;
import de.bund.digitalservice.a2j.service.egvp.DTO.SendMessageRequest;
import de.bund.digitalservice.a2j.service.egvp.EgvpClient;
import de.bund.digitalservice.a2j.service.egvp.EgvpClientException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestTemplate;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
// only extend with SpringExtension to avoid loading the whole Application context
@ExtendWith(SpringExtension.class)
@Tag("integration")
public class EgvpClientIntegrationTest {

Expand All @@ -32,7 +40,7 @@ void setup() {
}

@Test
void getVersion() {
void getVersionsuccess() {
mockServer
.expect(requestTo("http://localhost:8088/getVersion"))
.andRespond(
Expand All @@ -43,14 +51,53 @@ void getVersion() {
}
""",
MediaType.APPLICATION_JSON));
GetVersionResponse res = null;

try {
res = client.getVersion();
} catch (EgvpClientException e) {
throw new RuntimeException(e);
}
assertEquals(new GetVersionResponse("6.0.1"), client.getVersion());
}

@Test
void checkMessageStatus() {
mockServer
.expect(requestTo("http://localhost:8088/getMessageDeliveryStatus/12345"))
.andRespond(
withSuccess(
"""
{
"messageId":"MSG_ID_123",
"delivered":true,
"pendingReason":"",
"path": "path_to_file.pdf"
}
""",
MediaType.APPLICATION_JSON));
MessageDeliveryStatusResponse expectedResponse =
new MessageDeliveryStatusResponse("MSG_ID_123", true, "", "path_to_file.pdf");

assertEquals(expectedResponse, client.checkMessageStatus("12345"));
}

@Test
void sendMessageFailure() throws JsonProcessingException {
SendMessageRequest request =
new SendMessageRequest(
"receiverId", "mailbox", "subject", "attachmentFilepath", "xjustizFilepath");

mockServer
.expect(requestTo("http://localhost:8088/sendMessage"))
.andExpect(method(HttpMethod.POST))
.andExpect(content().json(new ObjectMapper().writeValueAsString(request)))
.andRespond(
withBadRequest()
.body(
"""
{
"responseCode":"ZERO_SIZE_ATTACHMENT"
}
""")
.contentType(MediaType.APPLICATION_JSON));
EgvpClientException ex =
Assertions.assertThrows(EgvpClientException.class, () -> client.sendMessage(request));

assertEquals(new GetVersionResponse("6.0.1"), res);
assertEquals("ZERO_SIZE_ATTACHMENT", ex.getMessage());
}
}

0 comments on commit 96a763d

Please sign in to comment.