@Component public class ServerMockUtils { @Value("${user.name.auth}") private String userNameAuth; @Value("${password.name.auth}") private String passwordNameAuth; public MappingBuilder makeMappingBuilderSuccess(MappingBuilder mappingBuilder){ return mappingBuilder .withHeader("Accept", matching("application/json")) .withQueryParam("format", equalTo("json")) .withBasicAuth(this.userNameAuth, this.passwordNameAuth); } }
public class MockForRestControllerConfig { @Autowired private ServerMockUtils serverMockUtils; @Value("${api.url.external.server}") private String apiUrlToExternalServer; public void setupStubForProcessingRequest(int portExternalServerMock) { String addressHost = "127.0.0.1"; configureFor(addressHost, portExternalServerMock); setupResponseInCaseSuccessProcessingRequest(); } private void setupResponseInCaseSuccessProcessingRequest(){ UrlPathPattern urlPathPattern = urlPathEqualTo(this.apiUrlToExternalServer); MappingBuilder mappingBuilder = get(urlPathPattern); MappingBuilder mappingBuilderWithHeader = serverMockUtils.makeMappingBuilderSuccess(mappingBuilder); int statusOk = HttpStatus.OK.value(); ResponseDefinitionBuilder responseDefinitionBuilder = aResponse(). withStatus(statusOk) .withHeader("Content-Type", "application/json") .withBodyFile("json/json.json"); MappingBuilder responseForReturn = mappingBuilderWithHeader.willReturn(responseDefinitionBuilder); stubFor(responseForReturn); } }
public ResponseEntity<ExternalServerDto> sendRequestToExternalServer(int portExternalServerMock) { String fullUrlToExternalServer = makeFullUrlToExternalServer(portExternalServerMock); UriComponentsBuilder uriComponentsBuilder = buildUriToExternalServer(fullUrlToExternalServer); String uriWithParamsToExternalServer = uriComponentsBuilder.toUriString(); HttpHeaders requestHttpHeaders = getHeadersHttpHeaders(); HttpEntity<Object> requestHttpEntity = new HttpEntity<>(null, requestHttpHeaders); return testRestTemplate.exchange( uriWithParamsToExternalServer, HttpMethod.GET, requestHttpEntity, ExternalServerDto.class ); } private String makeFullUrlToExternalServer(int portExternalServerMock) { String prefixUrlToExternalServer = "http://127.0.0.1:"; return prefixUrlToExternalServer + portExternalServerMock + this.apiUrlToExternalServer; } private HttpHeaders getHeadersHttpHeaders() { var requestHttpHeaders = new HttpHeaders(); requestHttpHeaders.add("Accept", MediaType.APPLICATION_JSON_VALUE); addBasicAuth(requestHttpHeaders); return requestHttpHeaders; } private UriComponentsBuilder buildUriToExternalServer(String urlToExternalServer) { return UriComponentsBuilder.fromHttpUrl(urlToExternalServer) .queryParam("format", "json") } private void addBasicAuth(HttpHeaders httpHeaders) { String auth = this.userNameAuth + ":" + this.passwordNameAuth; byte[] encodedAuth = Base64.encodeBase64( auth.getBytes(StandardCharsets.US_ASCII)); String authHeader = "Basic " + new String(encodedAuth); httpHeaders.add("Authorization", authHeader); }
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-rest</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>com.jayway.jsonpath</groupId> <artifactId>json-path</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.github.tomakehurst</groupId> <artifactId>wiremock</artifactId> <version>2.27.2</version> <scope>test</scope> </dependency>