generated from digitalservicebund/java-application-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from digitalservicebund/prepare_egvp_client
Prepare egvp client
- Loading branch information
Showing
10 changed files
with
215 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/de/bund/digitalservice/a2j/config/EgvpClientConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package de.bund.digitalservice.a2j.config; | ||
|
||
import de.bund.digitalservice.a2j.service.egvp.EgvpClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.client.DefaultResponseErrorHandler; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Configuration | ||
public class EgvpClientConfig { | ||
|
||
@Bean | ||
public EgvpClient egvpClient(@Value("egvp.client.baseUri") String baseUri) { | ||
RestTemplate restClient = | ||
new RestTemplateBuilder() | ||
.errorHandler(new DefaultResponseErrorHandler()) | ||
.rootUri(baseUri) | ||
.build(); | ||
|
||
return new EgvpClient(restClient); | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
src/main/java/de/bund/digitalservice/a2j/service/egvp/DTO/GetVersionResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package de.bund.digitalservice.a2j.service.egvp.DTO; | ||
|
||
public record GetVersionResponse(String version) {} |
4 changes: 4 additions & 0 deletions
4
src/main/java/de/bund/digitalservice/a2j/service/egvp/DTO/MessageDeliveryStatusResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/de/bund/digitalservice/a2j/service/egvp/DTO/ResponseError.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package de.bund.digitalservice.a2j.service.egvp.DTO; | ||
|
||
public record ResponseError(String responseCode, String errorDetail, String errorDescription) {} |
8 changes: 8 additions & 0 deletions
8
src/main/java/de/bund/digitalservice/a2j/service/egvp/DTO/SendMessageRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
3 changes: 3 additions & 0 deletions
3
src/main/java/de/bund/digitalservice/a2j/service/egvp/DTO/SendMessageResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} |
57 changes: 57 additions & 0 deletions
57
src/main/java/de/bund/digitalservice/a2j/service/egvp/EgvpClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package de.bund.digitalservice.a2j.service.egvp; | ||
|
||
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 { | ||
private final RestTemplate client; | ||
|
||
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()); | ||
} | ||
} | ||
|
||
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()); | ||
} | ||
} | ||
|
||
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()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/de/bund/digitalservice/a2j/service/egvp/EgvpClientException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package de.bund.digitalservice.a2j.service.egvp; | ||
|
||
public class EgvpClientException extends RuntimeException { | ||
public EgvpClientException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
src/test/java/de/bund/digitalservice/a2j/integration/EgvpClientIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package de.bund.digitalservice.a2j.integration; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
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.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; | ||
|
||
// only extend with SpringExtension to avoid loading the whole Application context | ||
@ExtendWith(SpringExtension.class) | ||
@Tag("integration") | ||
public class EgvpClientIntegrationTest { | ||
|
||
private EgvpClient client; | ||
|
||
private MockRestServiceServer mockServer; | ||
|
||
@BeforeEach | ||
void setup() { | ||
RestTemplate restTemplate = new RestTemplateBuilder().rootUri("http://localhost:8088").build(); | ||
this.client = new EgvpClient(restTemplate); | ||
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 | ||
.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("ZERO_SIZE_ATTACHMENT", ex.getMessage()); | ||
} | ||
} |