Skip to content

Instantly share code, notes, and snippets.

View eliasnogueira's full-sized avatar
🇧🇷

Elias Nogueira eliasnogueira

🇧🇷
View GitHub Profile
@eliasnogueira
eliasnogueira / docker-compose.yml
Created November 25, 2022 16:00
[JavaAdvent] Docker compose file to copy the necessary Wiremock files and build the Docker image
version: '3.9'
services:
wiremock:
build:
context: ./
dockerfile: Dockerfile
image: wiremock-custom
container_name: wiremock-credit-restriction-api
ports:
@eliasnogueira
eliasnogueira / Dockerfile
Last active November 25, 2022 15:57
[JavaAdvent] Example of Dockerfile for Wiremock
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
@eliasnogueira
eliasnogueira / restrictions_response_200.json
Created November 24, 2022 19:38
[JavaAdvent] Response for the Restriction API
{
"message": "CPF 97093236014 has a restriction"
}
@eliasnogueira
eliasnogueira / simulation_with_restriction_response.json
Created November 24, 2022 19:32
[JavaAdvent] Response body for the Simulation with a Restriction
{
"message": "CPF has a restriciton"
}
@eliasnogueira
eliasnogueira / simulation_with_restriction.json
Created November 24, 2022 15:51
[JavaAdvent] Simulation with a restriction
{
"name" : "Simulation request containing a restriction",
"request" : {
"url" : "/api/v1/simulations/",
"method" : "POST",
"bodyPatterns" : [
{
"equalToJson" : {
"cpf": "97093236014"
}
@eliasnogueira
eliasnogueira / restricitons_complete.json
Created November 24, 2022 15:50
[JavaAdvent] Restriction response
{
"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" : {
@eliasnogueira
eliasnogueira / post.sh
Last active November 24, 2022 15:45
[JavaAdvent] Post example, trying to create a simulation where the CPF has a restriction
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,
@eliasnogueira
eliasnogueira / SimulationsController.java
Created November 24, 2022 15:42
[JavaAdvent] Method that connects to the Restriction API and return the appropriate result
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());
}
@eliasnogueira
eliasnogueira / SimulationController.java
Last active November 24, 2022 15:36
[JavaAdvent] Simulation controller example for the POST method
@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();