From da0af1945747f51abfe2cc2198bc88ce3aa9ce78 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Thu, 23 May 2024 08:08:51 -0300 Subject: [PATCH 01/15] fix(initialization): Basic adjustments to run the project. --- .gitignore | 0 .mvn/wrapper/MavenWrapperDownloader.java | 0 .mvn/wrapper/maven-wrapper.jar | Bin .mvn/wrapper/maven-wrapper.properties | 0 Dockerfile | 1 + README.md | 0 pom.xml | 7 +------ .../com/clickbus/challenge/ClickbusApplication.java | 0 .../challenge/controller/PlaceController.java | 8 ++++++-- .../br/com/clickbus/challenge/dto/PlaceDTO.java | 0 .../br/com/clickbus/challenge/entity/Place.java | 0 .../challenge/repository/PlaceRepository.java | 0 .../clickbus/challenge/service/PlaceService.java | 2 +- src/main/resources/application.yml | 2 ++ .../challenge/contoller/PlaceControllerTest.java | 0 .../challenge/service/PlaceServiceTest.java | 0 16 files changed, 11 insertions(+), 9 deletions(-) mode change 100644 => 100755 .gitignore mode change 100644 => 100755 .mvn/wrapper/MavenWrapperDownloader.java mode change 100644 => 100755 .mvn/wrapper/maven-wrapper.jar mode change 100644 => 100755 .mvn/wrapper/maven-wrapper.properties create mode 100755 Dockerfile mode change 100644 => 100755 README.md mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/ClickbusApplication.java mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/controller/PlaceController.java mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/entity/Place.java mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/repository/PlaceRepository.java mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/service/PlaceService.java mode change 100644 => 100755 src/main/resources/application.yml mode change 100644 => 100755 src/test/java/br/com/clickbus/challenge/contoller/PlaceControllerTest.java mode change 100644 => 100755 src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java diff --git a/.gitignore b/.gitignore old mode 100644 new mode 100755 diff --git a/.mvn/wrapper/MavenWrapperDownloader.java b/.mvn/wrapper/MavenWrapperDownloader.java old mode 100644 new mode 100755 diff --git a/.mvn/wrapper/maven-wrapper.jar b/.mvn/wrapper/maven-wrapper.jar old mode 100644 new mode 100755 diff --git a/.mvn/wrapper/maven-wrapper.properties b/.mvn/wrapper/maven-wrapper.properties old mode 100644 new mode 100755 diff --git a/Dockerfile b/Dockerfile new file mode 100755 index 0000000..85a12a3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM maven:3.3-jdk-8 \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/pom.xml b/pom.xml index cf21f4f..90bb046 100755 --- a/pom.xml +++ b/pom.xml @@ -41,15 +41,10 @@ runtime - - com.h2database - h2 - runtime - - org.projectlombok lombok + 1.18.30 true diff --git a/src/main/java/br/com/clickbus/challenge/ClickbusApplication.java b/src/main/java/br/com/clickbus/challenge/ClickbusApplication.java old mode 100644 new mode 100755 diff --git a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java old mode 100644 new mode 100755 index bbd5d5c..1dcc909 --- a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java +++ b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java @@ -24,7 +24,11 @@ @RequestMapping("places") public class PlaceController { - private PlaceService service; + private final PlaceService service; + + public PlaceController(PlaceService service) { + this.service = service; + } @PostMapping public ResponseEntity create(@RequestBody @Valid PlaceDTO dto) { @@ -35,7 +39,7 @@ public ResponseEntity create(@RequestBody @Valid PlaceDTO dto) { public ResponseEntity findById(@PathVariable Long id) { return service.findById(id) .map(place -> ResponseEntity.ok(place.convertToDTO())) - .orElseThrow(() -> new PlaceNotFoundException(HttpStatus.NOT_FOUND)); + .orElseThrow(() -> new PlaceNotFoundException("Place not found.")); } @GetMapping diff --git a/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java b/src/main/java/br/com/clickbus/challenge/dto/PlaceDTO.java old mode 100644 new mode 100755 diff --git a/src/main/java/br/com/clickbus/challenge/entity/Place.java b/src/main/java/br/com/clickbus/challenge/entity/Place.java old mode 100644 new mode 100755 diff --git a/src/main/java/br/com/clickbus/challenge/repository/PlaceRepository.java b/src/main/java/br/com/clickbus/challenge/repository/PlaceRepository.java old mode 100644 new mode 100755 diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java old mode 100644 new mode 100755 index 799d1a8..1926a2f --- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java +++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java @@ -26,7 +26,7 @@ public Optional findById(@NotNull Long id) { throw new NotImplementedException("Metodo nao implementado"); } - public Place save(@NotNull Place place) { + public Place save(Place place) { throw new NotImplementedException("Metodo nao implementado"); } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml old mode 100644 new mode 100755 index 9bd4fe5..7fe46be --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -7,6 +7,8 @@ spring: console: enabled: true path: /h2 + settings: + web-allow-others: true datasource: url: jdbc:h2:mem:testdb username: sa diff --git a/src/test/java/br/com/clickbus/challenge/contoller/PlaceControllerTest.java b/src/test/java/br/com/clickbus/challenge/contoller/PlaceControllerTest.java old mode 100644 new mode 100755 diff --git a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java old mode 100644 new mode 100755 From 32db19cbb15bcdf2771a0df07f88e2e26107a700 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Thu, 23 May 2024 08:10:06 -0300 Subject: [PATCH 02/15] feat(exceptionHandler): Custom handling exceptions with a default body. --- .../exception/GlobalExceptionHandler.java | 45 +++++++++++++++++++ .../exception/PlaceNotFoundException.java | 10 +---- .../exception/ResponseErrorBody.java | 13 ++++++ 3 files changed, 60 insertions(+), 8 deletions(-) create mode 100755 src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java mode change 100644 => 100755 src/main/java/br/com/clickbus/challenge/exception/PlaceNotFoundException.java create mode 100644 src/main/java/br/com/clickbus/challenge/exception/ResponseErrorBody.java diff --git a/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java b/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java new file mode 100755 index 0000000..ea4483e --- /dev/null +++ b/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java @@ -0,0 +1,45 @@ +package br.com.clickbus.challenge.exception; + +import org.apache.commons.lang3.NotImplementedException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +public class GlobalExceptionHandler { + private final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class); + + @ExceptionHandler(NotImplementedException.class) + public ResponseEntity handleNotImplementedException(NotImplementedException notImplementedException) { + String message = notImplementedException.getMessage().isEmpty() + ? "Resource not implemented yet." + : notImplementedException.getMessage(); + return new ResponseEntity<>(this.responseError(message, HttpStatus.NOT_IMPLEMENTED), HttpStatus.NOT_IMPLEMENTED); + } + + @ExceptionHandler(PlaceNotFoundException.class) + public ResponseEntity handlePlaceNotFoundException(PlaceNotFoundException placeNotFoundException) { + String message = placeNotFoundException.getMessage().isEmpty() + ? "Resource ID not found." + : placeNotFoundException.getMessage(); + return new ResponseEntity<>(this.responseError(message, HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND); + } + + @ExceptionHandler(Throwable.class) + public ResponseEntity handleUnexpectedException(Throwable unexpectedException) { + String message = "Unexpected server error."; + logger.error("", unexpectedException); + return new ResponseEntity<>(this.responseError(message, HttpStatus.INTERNAL_SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR); + } + + private ResponseErrorBody responseError(String message, HttpStatus statusCode){ + ResponseErrorBody responseError = new ResponseErrorBody(); + responseError.setStatus("error"); + responseError.setError(message); + responseError.setStatusCode(statusCode.value()); + return responseError; + } +} diff --git a/src/main/java/br/com/clickbus/challenge/exception/PlaceNotFoundException.java b/src/main/java/br/com/clickbus/challenge/exception/PlaceNotFoundException.java old mode 100644 new mode 100755 index d96301d..6047b9b --- a/src/main/java/br/com/clickbus/challenge/exception/PlaceNotFoundException.java +++ b/src/main/java/br/com/clickbus/challenge/exception/PlaceNotFoundException.java @@ -1,15 +1,9 @@ package br.com.clickbus.challenge.exception; -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(HttpStatus.NOT_FOUND) public class PlaceNotFoundException extends RuntimeException{ - private String message; - - public PlaceNotFoundException(HttpStatus status){ - super(status.toString()); + public PlaceNotFoundException(String message){ + super(message); } } diff --git a/src/main/java/br/com/clickbus/challenge/exception/ResponseErrorBody.java b/src/main/java/br/com/clickbus/challenge/exception/ResponseErrorBody.java new file mode 100644 index 0000000..eb8499a --- /dev/null +++ b/src/main/java/br/com/clickbus/challenge/exception/ResponseErrorBody.java @@ -0,0 +1,13 @@ +package br.com.clickbus.challenge.exception; + +import lombok.Data; + +import java.util.Date; + +@Data +public class ResponseErrorBody { + private Date timestamp = new Date(); + private String status = "error"; + private int statusCode = 400; + private String error; +} From effa0aa07514b97796abcac8e9609b43a16abd16 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Thu, 23 May 2024 23:20:40 -0300 Subject: [PATCH 03/15] feat(CRUD): Implement listing resources. --- .../challenge/controller/PlaceController.java | 16 +++++++++++++++- .../clickbus/challenge/service/PlaceService.java | 10 ++++++---- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java index 1dcc909..ff48146 100755 --- a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java +++ b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java @@ -39,7 +39,21 @@ public ResponseEntity create(@RequestBody @Valid PlaceDTO dto) { public ResponseEntity findById(@PathVariable Long id) { return service.findById(id) .map(place -> ResponseEntity.ok(place.convertToDTO())) - .orElseThrow(() -> new PlaceNotFoundException("Place not found.")); + .orElseThrow(() -> new PlaceNotFoundException(HttpStatus.NOT_FOUND.toString())); + } + + @GetMapping(name = "findByName", path = "/") + public ResponseEntity> findByName(@RequestParam(value = "name") String name) { + List places = service.findByName(name); + + if (places.isEmpty()) { + throw new PlaceNotFoundException("There are no places associated with the given name."); + } + + return ResponseEntity.ok(service.findByName(name) + .stream() + .map(Place::convertToDTO) + .collect(Collectors.toList())); } @GetMapping diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java index 1926a2f..1bd5998 100755 --- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java +++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java @@ -5,6 +5,7 @@ import br.com.clickbus.challenge.entity.Place; import br.com.clickbus.challenge.repository.PlaceRepository; import lombok.AllArgsConstructor; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.validation.constraints.NotNull; @@ -16,14 +17,15 @@ @AllArgsConstructor public class PlaceService { - private PlaceRepository repository; + @Autowired + private PlaceRepository placeRepository; public List findAll() { - throw new NotImplementedException("Metodo nao implementado"); + return placeRepository.findAll(); } public Optional findById(@NotNull Long id) { - throw new NotImplementedException("Metodo nao implementado"); + return placeRepository.findById(id); } public Place save(Place place) { @@ -31,7 +33,7 @@ public Place save(Place place) { } public List findByName(@NotNull String name) { - throw new NotImplementedException("Metodo nao implementado"); + return placeRepository.findByName(name); } public Place alter(@NotNull Place place,@NotNull PlaceDTO placeDTO) { From 613bbfc9c114e03a715369bf5eb636a8fd7e6d5e Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Thu, 23 May 2024 23:22:55 -0300 Subject: [PATCH 04/15] feat(CRUD): Implement creating resources. --- .../challenge/controller/PlaceController.java | 11 +++-------- .../challenge/exception/GlobalExceptionHandler.java | 9 +++++++++ .../com/clickbus/challenge/service/PlaceService.java | 2 +- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java index ff48146..d7e08c0 100755 --- a/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java +++ b/src/main/java/br/com/clickbus/challenge/controller/PlaceController.java @@ -6,18 +6,13 @@ import br.com.clickbus.challenge.exception.PlaceNotFoundException; import br.com.clickbus.challenge.service.PlaceService; import io.swagger.annotations.Api; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import javax.validation.Valid; +import java.util.List; +import java.util.stream.Collectors; @Api("places") @RestController diff --git a/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java b/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java index ea4483e..bcfafcf 100755 --- a/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java +++ b/src/main/java/br/com/clickbus/challenge/exception/GlobalExceptionHandler.java @@ -5,6 +5,7 @@ import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; @@ -28,6 +29,14 @@ public ResponseEntity handlePlaceNotFoundException(PlaceNotFo return new ResponseEntity<>(this.responseError(message, HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND); } + @ExceptionHandler(MethodArgumentNotValidException.class) + public ResponseEntity handleBusinessException(MethodArgumentNotValidException methodArgumentNotValidException) { + String message = methodArgumentNotValidException.getMessage().isEmpty() + ? HttpStatus.BAD_REQUEST.toString() + : methodArgumentNotValidException.getMessage(); + return new ResponseEntity<>(this.responseError(message, HttpStatus.BAD_REQUEST), HttpStatus.BAD_REQUEST); + } + @ExceptionHandler(Throwable.class) public ResponseEntity handleUnexpectedException(Throwable unexpectedException) { String message = "Unexpected server error."; diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java index 1bd5998..71edabf 100755 --- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java +++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java @@ -29,7 +29,7 @@ public Optional findById(@NotNull Long id) { } public Place save(Place place) { - throw new NotImplementedException("Metodo nao implementado"); + return placeRepository.save(place); } public List findByName(@NotNull String name) { From 77c3fdd7e453612d57e8d8944fec902a35cc373e Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Thu, 23 May 2024 23:23:38 -0300 Subject: [PATCH 05/15] feat(CRUD): Implement updating resources. --- .../java/br/com/clickbus/challenge/entity/Place.java | 10 ++++++++++ .../com/clickbus/challenge/service/PlaceService.java | 5 ++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/br/com/clickbus/challenge/entity/Place.java b/src/main/java/br/com/clickbus/challenge/entity/Place.java index ccfc550..4fd87a4 100755 --- a/src/main/java/br/com/clickbus/challenge/entity/Place.java +++ b/src/main/java/br/com/clickbus/challenge/entity/Place.java @@ -54,4 +54,14 @@ public static Place of(String name, String slug, String city, String state) { public PlaceDTO convertToDTO() { return PlaceDTO.of(this.name, this.slug, this.city, this.state); } + + public Place alter(PlaceDTO dto) { + this.name = dto.getName(); + this.slug = dto.getSlug(); + this.city = dto.getCity(); + this.state = dto.getState(); + this.updatedAt = LocalDateTime.now(); + + return this; + } } diff --git a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java index 71edabf..6f612f1 100755 --- a/src/main/java/br/com/clickbus/challenge/service/PlaceService.java +++ b/src/main/java/br/com/clickbus/challenge/service/PlaceService.java @@ -11,7 +11,6 @@ import javax.validation.constraints.NotNull; import java.util.List; import java.util.Optional; -import org.apache.commons.lang3.NotImplementedException; @Service @AllArgsConstructor @@ -36,7 +35,7 @@ public List findByName(@NotNull String name) { return placeRepository.findByName(name); } - public Place alter(@NotNull Place place,@NotNull PlaceDTO placeDTO) { - throw new NotImplementedException("Metodo nao implementado"); + public Place alter(@NotNull Place place, @NotNull PlaceDTO placeDTO) { + return placeRepository.save(place.alter(placeDTO)); } } From e5a35295039384d36514b98afaa15cca02748801 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 07:54:28 -0300 Subject: [PATCH 06/15] deploy(dockerfile): Adjust settings to generate jar file. --- Dockerfile | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 85a12a3..20a3403 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1 +1,11 @@ -FROM maven:3.3-jdk-8 \ No newline at end of file +FROM maven:3.3-jdk-8 as build +COPY src /home/app/src +COPY pom.xml /home/app +RUN mvn -f /home/app/pom.xml clean package + +# Stage 2: Create the final image +FROM openjdk:8-jre-slim +ARG JAR_FILE=/home/app/target/*.jar +COPY --from=build ${JAR_FILE} app.jar +EXPOSE 8080 +ENTRYPOINT ["java", "-jar", "/app/app.jar"] \ No newline at end of file From ea0a06aa7c999172022660f6e502afb01b843662 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 07:57:52 -0300 Subject: [PATCH 07/15] deploy(dockerfile): Rename jar file location. --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 20a3403..00e8dd1 100755 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,4 @@ FROM openjdk:8-jre-slim ARG JAR_FILE=/home/app/target/*.jar COPY --from=build ${JAR_FILE} app.jar EXPOSE 8080 -ENTRYPOINT ["java", "-jar", "/app/app.jar"] \ No newline at end of file +ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file From 3d11a8a35c51108583668f64a9ec714e94c07f65 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 10:40:22 -0300 Subject: [PATCH 08/15] deploy(dockerfile): Define a working directory. --- Dockerfile | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Dockerfile b/Dockerfile index 00e8dd1..67b8c04 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,13 @@ +# Stage 1: Build the application FROM maven:3.3-jdk-8 as build +WORKDIR /home/app COPY src /home/app/src COPY pom.xml /home/app -RUN mvn -f /home/app/pom.xml clean package +RUN mvn clean package # Stage 2: Create the final image FROM openjdk:8-jre-slim -ARG JAR_FILE=/home/app/target/*.jar -COPY --from=build ${JAR_FILE} app.jar +WORKDIR /home/app +COPY --from=build /home/app/target/*.jar app.jar EXPOSE 8080 ENTRYPOINT ["java", "-jar", "app.jar"] \ No newline at end of file From 22be4c43f698fc129870a53fe0abea3c600d6a75 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 11:11:16 -0300 Subject: [PATCH 09/15] deploy(properties): Enable debug mode to analise the deployment. --- src/main/resources/application.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 7fe46be..e0b1420 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,3 +18,7 @@ spring: hibernate.ddl-auto: update generate-ddl: true show-sql: true +logging: + level: + root: DEBUG + org.springframework: DEBUG From 42b84f44ea1325ceba21c1f296de061d05ba04fc Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 14:20:01 -0300 Subject: [PATCH 10/15] deploy(Dockerfile): Change jdk/jre images versions. --- Dockerfile | 4 ++-- pom.xml | 3 +++ src/main/resources/application.yml | 4 ---- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Dockerfile b/Dockerfile index 67b8c04..49ad763 100755 --- a/Dockerfile +++ b/Dockerfile @@ -1,12 +1,12 @@ # Stage 1: Build the application -FROM maven:3.3-jdk-8 as build +FROM maven:3.8.6-jdk-8 as build WORKDIR /home/app COPY src /home/app/src COPY pom.xml /home/app RUN mvn clean package # Stage 2: Create the final image -FROM openjdk:8-jre-slim +FROM openjdk:8-jre WORKDIR /home/app COPY --from=build /home/app/target/*.jar app.jar EXPOSE 8080 diff --git a/pom.xml b/pom.xml index 90bb046..8c603bc 100755 --- a/pom.xml +++ b/pom.xml @@ -28,11 +28,13 @@ org.springframework.boot spring-boot-starter-web + 2.2.1.RELEASE org.springframework.boot spring-boot-starter-data-jpa + 2.4.0 @@ -90,6 +92,7 @@ h2 runtime + org.apache.commons commons-lang3 diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index e0b1420..7fe46be 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,7 +18,3 @@ spring: hibernate.ddl-auto: update generate-ddl: true show-sql: true -logging: - level: - root: DEBUG - org.springframework: DEBUG From 4d06c12f18cff2cd4534431c940c63353f15e349 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 17:29:56 -0300 Subject: [PATCH 11/15] deploy(Dockerfile): Change folder structure to hide Dockerfile from autodetection. --- Procfile | 1 + docker-compose.yml | 4 +++- Dockerfile => docker/app/Dockerfile | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 Procfile mode change 100644 => 100755 docker-compose.yml rename Dockerfile => docker/app/Dockerfile (82%) diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..57b4014 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: java -jar target/clickbus-0.0.1-SNAPSHOT.jar \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml old mode 100644 new mode 100755 index a788dcf..6890a04 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,9 @@ services: container_name: clickbus_challenge_app restart: always image: maven:3.3-jdk-8 - build: . + build: + context: ./docker/app/ + dockerfile: Dockerfile working_dir: /app volumes: - ./:/app diff --git a/Dockerfile b/docker/app/Dockerfile similarity index 82% rename from Dockerfile rename to docker/app/Dockerfile index 49ad763..d655234 100755 --- a/Dockerfile +++ b/docker/app/Dockerfile @@ -1,8 +1,8 @@ # Stage 1: Build the application FROM maven:3.8.6-jdk-8 as build WORKDIR /home/app -COPY src /home/app/src -COPY pom.xml /home/app +COPY ../../src /home/app/src +COPY ../../pom.xml /home/app RUN mvn clean package # Stage 2: Create the final image From a31292db89a7abfc9aa6ca47d01bd70ed54aade0 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 17:41:53 -0300 Subject: [PATCH 12/15] update(PlaceCreation): Slugfy the slug before creating a place object. --- .../java/br/com/clickbus/challenge/entity/Place.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/br/com/clickbus/challenge/entity/Place.java b/src/main/java/br/com/clickbus/challenge/entity/Place.java index 4fd87a4..6576164 100755 --- a/src/main/java/br/com/clickbus/challenge/entity/Place.java +++ b/src/main/java/br/com/clickbus/challenge/entity/Place.java @@ -41,7 +41,7 @@ public class Place { public Place(String name, String slug, String city, String state) { this.name = name; - this.slug = slug; + this.slug = Place.slugfy(slug); this.city = city; this.state = state; this.createdAt = LocalDateTime.now(); @@ -51,13 +51,19 @@ public static Place of(String name, String slug, String city, String state) { return new Place(name, slug, city, state); } + private static String slugfy(String slug) { + return slug.toLowerCase() + .replaceAll("[^a-zA-Z0-9\\s+]", "") + .replaceAll("\\s+", "-"); + } + public PlaceDTO convertToDTO() { return PlaceDTO.of(this.name, this.slug, this.city, this.state); } public Place alter(PlaceDTO dto) { this.name = dto.getName(); - this.slug = dto.getSlug(); + this.slug = Place.slugfy(dto.getSlug()); this.city = dto.getCity(); this.state = dto.getState(); this.updatedAt = LocalDateTime.now(); From 1f84d78bf0dde9adacf6d955bdd49996626e58b5 Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 22:24:08 -0300 Subject: [PATCH 13/15] doc(readme): Describe a little about the motivation of this project. --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index c9b6851..95fafb5 100755 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +# Desafio que resolve outro desafio +- Estou participando de um bootcamp do Santander na DIO, e como desafio, +foi proposto o desenvolvimento e publicação de uma API RESTFULL. +- Encontrei o desafio da @Clickbus e achei que seria interessante +entender e melhorar um desafio de código proposto pela empresa. + # Backend Developer Challenge Esse desafio serve para avaliarmos suas habilidades em construir APIs. From 65690ea0d85a48ace44f27ec2082c6e903bef52a Mon Sep 17 00:00:00 2001 From: gabrielroot Date: Fri, 24 May 2024 22:39:27 -0300 Subject: [PATCH 14/15] test(findAll): Implement findAll test. --- .../challenge/service/PlaceServiceTest.java | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java index cf0f0a9..4adc293 100755 --- a/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java +++ b/src/test/java/br/com/clickbus/challenge/service/PlaceServiceTest.java @@ -11,7 +11,6 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -19,7 +18,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.any; import static org.mockito.Mockito.anyLong; import static org.mockito.Mockito.atLeastOnce; @@ -45,6 +43,26 @@ void setUp() { place = Place.of(NAME_PLACE, "bt", "Sao Paulo", "SP"); } + @Test + void whenFindAllNotEmpty() { + when(repository.findAll()).thenReturn(Collections.singletonList(place)); + + List places = service.findAll(); + + assertEquals(1, places.size()); + verify(repository, atLeastOnce()).findAll(); + } + + @Test + void whenFindAllEmpty() { + when(repository.findAll()).thenReturn(Collections.emptyList()); + + List places = service.findAll(); + + assertEquals(0, places.size()); + verify(repository, atLeastOnce()).findAll(); + } + @Test void whenFindByIdOk() { when(repository.findById(1L)).thenReturn(Optional.of(place)); @@ -60,7 +78,6 @@ void whenFindByIdOk() { verify(repository, atLeastOnce()).findById(anyLong()); } - @Test void whenFindByIdThenReturnEmpty() { when(repository.findById(1L)).thenReturn(Optional.empty()); From 6fcd3812997f5623fa8e36afdfa709753030ea57 Mon Sep 17 00:00:00 2001 From: Gabriel Santos Date: Fri, 24 May 2024 22:47:00 -0300 Subject: [PATCH 15/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 95fafb5..96cfa5b 100755 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ - Estou participando de um bootcamp do Santander na DIO, e como desafio, foi proposto o desenvolvimento e publicação de uma API RESTFULL. - Encontrei o desafio da @Clickbus e achei que seria interessante -entender e melhorar um desafio de código proposto pela empresa. +entender e melhorar este desafio de código proposto pela empresa. # Backend Developer Challenge