Skip to content

Commit

Permalink
Merge pull request #36 from digitalservicebund/prepare_egvp_client
Browse files Browse the repository at this point in the history
deduplicate error handling, catch server errors
  • Loading branch information
ekl176 authored Jan 6, 2025
2 parents b053821 + e34ebc2 commit 720b921
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.bund.digitalservice.a2j.service.egvp.DTO.*;
import java.util.Objects;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.HttpServerErrorException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

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

public GetVersionResponse getVersion() throws EgvpClientException {
try {
return this.client.getForEntity("/getVersion", GetVersionResponse.class).getBody();
} catch (HttpClientErrorException e) {
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());
}
return invoke(
() ->
this.client
.postForEntity("/sendMessage", request, SendMessageResponse.class)
.getBody());
}

public MessageDeliveryStatusResponse checkMessageStatus(String customId) {
public MessageDeliveryStatusResponse checkMessageStatus(String customId)
throws EgvpClientException {
return invoke(
() ->
this.client.getForObject(
"/getMessageDeliveryStatus/{customId}",
MessageDeliveryStatusResponse.class,
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());
}
interface Operation<T> {
T execute() throws RestClientException;
}

private EgvpClientException parseException(HttpClientErrorException e) {
ResponseError re = e.getResponseBodyAs(ResponseError.class);
if (Objects.isNull(re)) {
return new EgvpClientException(e.getMessage());
private <T> T invoke(Operation<T> operation) throws EgvpClientException {
try {
return operation.execute();
} catch (HttpClientErrorException clientErrorException) {
ResponseError re = clientErrorException.getResponseBodyAs(ResponseError.class);
if (Objects.isNull(re)) {
throw new EgvpClientException(clientErrorException.getMessage(), clientErrorException);
}
throw new EgvpClientException(re.responseCode(), clientErrorException);
} catch (HttpServerErrorException serverErrorException) {
throw new EgvpClientException(
serverErrorException.getStatusCode().toString(), serverErrorException);
}
return new EgvpClientException(re.responseCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package de.bund.digitalservice.a2j.service.egvp;

public class EgvpClientException extends RuntimeException {
public EgvpClientException(String message) {
super(message);
public EgvpClientException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

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.DTO.SendMessageResponse;
import de.bund.digitalservice.a2j.service.egvp.EgvpClient;
import de.bund.digitalservice.a2j.service.egvp.EgvpClientException;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -39,22 +39,6 @@ void setup() {
this.mockServer = MockRestServiceServer.createServer(restTemplate);
}

@Test
void getVersionsuccess() {
mockServer
.expect(requestTo("http://localhost:8088/getVersion"))
.andRespond(
withSuccess(
"""
{
"version":"6.0.1"
}
""",
MediaType.APPLICATION_JSON));

assertEquals(new GetVersionResponse("6.0.1"), client.getVersion());
}

@Test
void checkMessageStatus() {
mockServer
Expand All @@ -77,7 +61,30 @@ void checkMessageStatus() {
}

@Test
void sendMessageFailure() throws JsonProcessingException {
void sendMessage() 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(
withSuccess()
.body(
"""
{
"customId":"ABCDE12345"
}
""")
.contentType(MediaType.APPLICATION_JSON));

assertEquals(new SendMessageResponse("ABCDE12345"), client.sendMessage(request));
}

@Test
void sendMessageClientFailure() throws JsonProcessingException {
SendMessageRequest request =
new SendMessageRequest(
"receiverId", "mailbox", "subject", "attachmentFilepath", "xjustizFilepath");
Expand All @@ -100,4 +107,21 @@ void sendMessageFailure() throws JsonProcessingException {

assertEquals("ZERO_SIZE_ATTACHMENT", ex.getMessage());
}

@Test
void sendMessageServerFailure() 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(withServerError());
EgvpClientException ex =
Assertions.assertThrows(EgvpClientException.class, () -> client.sendMessage(request));

assertEquals("500 INTERNAL_SERVER_ERROR", ex.getMessage());
}
}

0 comments on commit 720b921

Please sign in to comment.