- Project: https://github.com/architects4j/devoxx-be-2022
- Step by step guide: https://architects4j.github.io/devoxx-be-2022/
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
version: '3.9' | |
services: | |
wiremock: | |
build: | |
context: ./ | |
dockerfile: Dockerfile | |
image: wiremock-custom | |
container_name: wiremock-credit-restriction-api | |
ports: |
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
FROM anapsix/alpine-java:8 | |
RUN apk add --update curl && \ | |
rm -rf /var/cache/apk/* | |
ENV WM_PACKAGE wiremock-jre8-standalone | |
ARG WM_VERSION=2.35.0 | |
RUN mkdir /$WM_PACKAGE | |
WORKDIR /$WM_PACKAGE |
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
{ | |
"message": "CPF 97093236014 has a restriction" | |
} |
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
{ | |
"message": "CPF has a restriciton" | |
} |
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
{ | |
"name" : "Simulation request containing a restriction", | |
"request" : { | |
"url" : "/api/v1/simulations/", | |
"method" : "POST", | |
"bodyPatterns" : [ | |
{ | |
"equalToJson" : { | |
"cpf": "97093236014" | |
} |
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
{ | |
"name" : "Restriction request for a CPF with restriction", | |
"request" : { | |
"url" : "/api/v1/restrictions/97093236014", | |
"method" : "GET" | |
}, | |
"response" : { | |
"status" : 200, | |
"bodyFileName" : "restrictions_response_200.json", | |
"headers" : { |
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
curl -X 'POST' \ | |
'http://localhost:8089/api/v1/simulations/' \ | |
-H 'accept: */*' \ | |
-H 'Content-Type: application/json' \ | |
-d '{ | |
"cpf": "97093236014", | |
"email": "john.doe@gmail.com", | |
"name": "John Doe", | |
"installments": 3, | |
"insurance": true, |
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
private void checkForRestriction(String cpf) throws SimulationException { | |
RestTemplate template = new RestTemplateBuilder().errorHandler(new RestTemplateErrorHandler()).build(); | |
String restrictionsEndpoint = String.format("%s:%s%s", env.getProperty("restrictions.host"), | |
env.getProperty("restrictions.port"), env.getProperty("restrictions.path")); | |
var response = template.getForObject(restrictionsEndpoint, MessageDto.class, cpf); | |
if (response != null) throw new ResponseStatusException(HttpStatus.FORBIDDEN, response.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
@PostMapping("/") | |
@ResponseStatus(HttpStatus.CREATED) | |
public ResponseEntity<Simulation> newSimulation(@Valid @RequestBody SimulationDto simulation) { | |
checkForRestriction(simulation.getCpf()); | |
Simulation createdSimulation = repository.save(new ModelMapper().map(simulation, Simulation.class)); | |
URI location = ServletUriComponentsBuilder.fromCurrentRequest().path("/{cpf}"). | |
buildAndExpand(createdSimulation.getCpf()).toUri(); | |
return ResponseEntity.created(location).build(); |