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.
- Loading branch information
Showing
7 changed files
with
125 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) {} |
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) {} |
29 changes: 29 additions & 0 deletions
29
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,29 @@ | ||
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 java.util.Objects; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
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, HttpClientErrorException { | ||
try { | ||
return this.client.getForEntity("/getVersion", GetVersionResponse.class).getBody(); | ||
} catch (HttpClientErrorException e) { | ||
ResponseError error = e.getResponseBodyAs(ResponseError.class); | ||
|
||
if (Objects.isNull(error)) { | ||
throw e; | ||
} else { | ||
throw new EgvpClientException(error.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
56 changes: 56 additions & 0 deletions
56
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,56 @@ | ||
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 de.bund.digitalservice.a2j.service.egvp.DTO.GetVersionResponse; | ||
import de.bund.digitalservice.a2j.service.egvp.EgvpClient; | ||
import de.bund.digitalservice.a2j.service.egvp.EgvpClientException; | ||
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.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.web.client.MockRestServiceServer; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@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 getVersion() { | ||
mockServer | ||
.expect(requestTo("http://localhost:8088/getVersion")) | ||
.andRespond( | ||
withSuccess( | ||
""" | ||
{ | ||
"version":"6.0.1" | ||
} | ||
""", | ||
MediaType.APPLICATION_JSON)); | ||
GetVersionResponse res = null; | ||
|
||
try { | ||
res = client.getVersion(); | ||
} catch (EgvpClientException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
assertEquals(new GetVersionResponse("6.0.1"), res); | ||
} | ||
} |