From c6670d24ef8a8609ca439d3a63d8501d29f40a91 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Wed, 1 Apr 2020 20:28:53 +0200 Subject: [PATCH 01/15] hexagonal architecture - first commit --- hexagonal-architecture/HELP.md | 28 ++++++ hexagonal-architecture/pom.xml | 78 +++++++++++++++ .../hexagonal/HexagonalApplication.java | 11 +++ .../application/BookingController.java | 57 +++++++++++ .../springboot/hexagonal/domain/Flight.java | 50 ++++++++++ .../hexagonal/domain/Reservation.java | 43 +++++++++ .../hexagonal/domain/ReservationService.java | 10 ++ .../domain/ReservationServiceImpl.java | 42 ++++++++ .../infrastructure/CustomerFileService.java | 12 +++ .../infrastructure/FlightService.java | 15 +++ .../FlightServiceDummyImpl.java | 48 ++++++++++ .../customerjpa/CustomerFileJpaImpl.java | 96 +++++++++++++++++++ .../customerjpa/FlightEntity.java | 68 +++++++++++++ .../customerjpa/ReservationFile.java | 72 ++++++++++++++ .../ReservationFileRepository.java | 13 +++ .../src/main/resources/application.yml | 0 16 files changed, 643 insertions(+) create mode 100644 hexagonal-architecture/HELP.md create mode 100644 hexagonal-architecture/pom.xml create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/FlightEntity.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFile.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java create mode 100644 hexagonal-architecture/src/main/resources/application.yml diff --git a/hexagonal-architecture/HELP.md b/hexagonal-architecture/HELP.md new file mode 100644 index 000000000000..e74015dccbe6 --- /dev/null +++ b/hexagonal-architecture/HELP.md @@ -0,0 +1,28 @@ +# Getting Started + + + +Here is a simple example of Hexagonal Architecture in SpringBoot. + + + +Once started you can use following curl request + + + +Add a booking to customer with ID 1 + +curl --header "Content-Type: application/json" --request PUT --data '{"flights":[{"date": "05MAR","destination": "CDG","number": "7701","origin": "NCE"},{"date": "05MAR","destination": "LAX","number": "006","origin": "CDG"}]}' http://localhost:8080/customer/1/booking + + + +List all bookings of customer 1 + +curl http://localhost:8080/customer/1/booking + + + +Delete a specific booking of customer 1 + +curl --request DELETE "http://localhost:8080/customer/1/booking/" + diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml new file mode 100644 index 000000000000..47fb9bd68ba6 --- /dev/null +++ b/hexagonal-architecture/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + com.baeldung + hexagonal-architecture + 0.0.1-SNAPSHOT + hexagonal-architecture + + + + + + + + + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + + + + + + + org.springframework.boot + spring-boot-dependencies + 2.2.6.RELEASE + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + com.h2database + h2 + runtime + + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + 1.8 + + + diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java new file mode 100644 index 000000000000..0c1dd21d4031 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java @@ -0,0 +1,11 @@ +package com.baeldung.springboot.hexagonal; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class HexagonalApplication { + public static void main(String[] args) { + SpringApplication.run(HexagonalApplication.class, args); + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java new file mode 100644 index 000000000000..cbe27fe7dfb0 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java @@ -0,0 +1,57 @@ +package com.baeldung.springboot.hexagonal.application; + +import java.util.List; +import java.util.UUID; + +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.springboot.hexagonal.domain.Flight; +import com.baeldung.springboot.hexagonal.domain.Reservation; +import com.baeldung.springboot.hexagonal.domain.ReservationService; +import com.baeldung.springboot.hexagonal.infrastructure.customerjpa.ReservationFile; +import com.baeldung.springboot.hexagonal.infrastructure.customerjpa.ReservationFileRepository; + +@RestController +public class BookingController { + private ReservationService reservationService; + private ReservationFileRepository repo; + + public BookingController(ReservationService reservationService, ReservationFileRepository repo) { + this.reservationService = reservationService; + } + + @GetMapping("/findFlights") + public List getTravel(@RequestParam("date") String date, @RequestParam("origin") String origin, + @RequestParam("destination") String destination) { + return reservationService.identifyFLights(date, origin, destination); + } + + @PutMapping("/customer/{id}/booking") + public Reservation makeBooking(@PathVariable("id") Long customerId, @RequestBody Reservation reservation) { + return reservationService.processBooking(reservation, customerId); + } + + @GetMapping("/customer/{customerId}/booking") + public List bookings(@PathVariable("customerId") Long id) { + return reservationService.bookings(id); + } + + @DeleteMapping("/customer/{customerId}/booking/{id}") + public boolean cancelBooking(@PathVariable("customerId") Long customerId, @PathVariable("id") UUID reservationId) { + Reservation reservation = new Reservation(); + reservation.setCustomerId(customerId); + reservation.setId(reservationId); + return reservationService.cancel(reservation); + } + + @GetMapping("/booking") + public List booking() { + return repo.findAll(); + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java new file mode 100644 index 000000000000..77aa9238e2c6 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java @@ -0,0 +1,50 @@ +package com.baeldung.springboot.hexagonal.domain; + +public class Flight { + private String number; + private String origin; + private String destination; + private String date; + + public Flight() { + } + + public Flight(String number, String origin, String destination, String date) { + this.number = number; + this.origin = origin; + this.destination = destination; + this.date = date; + } + + public String getNumber() { + return number; + } + + public void setNumber(String number) { + this.number = number; + } + + public String getOrigin() { + return origin; + } + + public void setOrigin(String origin) { + this.origin = origin; + } + + public String getDestination() { + return destination; + } + + public void setDestination(String destination) { + this.destination = destination; + } + + public String getDate() { + return date; + } + + public void setDate(String date) { + this.date = date; + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java new file mode 100644 index 000000000000..a5c1533bc873 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java @@ -0,0 +1,43 @@ +package com.baeldung.springboot.hexagonal.domain; + +import java.util.List; +import java.util.UUID; + +public class Reservation { + /** + * Functional ID of the reservation + */ + private UUID id; + /** + * Customer holding the reservation + */ + private Long customerId; + /** + * List of flights in the reservation + */ + private List flights; + + public UUID getId() { + return id; + } + + public void setId(UUID id) { + this.id = id; + } + + public List getFlights() { + return flights; + } + + public void setFlights(List flights) { + this.flights = flights; + } + + public Long getCustomerId() { + return customerId; + } + + public void setCustomerId(Long customerId) { + this.customerId = customerId; + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java new file mode 100644 index 000000000000..bd5f581f98e4 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java @@ -0,0 +1,10 @@ +package com.baeldung.springboot.hexagonal.domain; + +import java.util.List; + +public interface ReservationService { + public List identifyFLights(String date, String origin, String destination); + public Reservation processBooking(Reservation reservation, Long customerId); + public List bookings(Long customerId); + public boolean cancel(Reservation file); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java new file mode 100644 index 000000000000..7c55dd417135 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java @@ -0,0 +1,42 @@ +package com.baeldung.springboot.hexagonal.domain; + +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.baeldung.springboot.hexagonal.infrastructure.CustomerFileService; +import com.baeldung.springboot.hexagonal.infrastructure.FlightService; + +@Service +public class ReservationServiceImpl implements ReservationService { + private CustomerFileService customerFileService; + private FlightService flightService; + + public ReservationServiceImpl(CustomerFileService customerFileService, FlightService flightService) { + super(); + this.customerFileService = customerFileService; + this.flightService = flightService; + } + + @Override + public List identifyFLights(String date, String origin, String destination) { + return flightService.getRoute(date, origin, destination); + } + + @Override + public Reservation processBooking(Reservation reservation, Long customer) { + reservation.setCustomerId(customer); + return customerFileService.save(reservation); + } + + @Override + public List bookings(Long customerId) { + List reservations = customerFileService.bookings(customerId); + return reservations; + } + + @Override + public boolean cancel(Reservation file) { + return customerFileService.delete(file.getCustomerId(), file.getId()); + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java new file mode 100644 index 000000000000..fdb94b38e362 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java @@ -0,0 +1,12 @@ +package com.baeldung.springboot.hexagonal.infrastructure; + +import java.util.List; +import java.util.UUID; + +import com.baeldung.springboot.hexagonal.domain.Reservation; + +public interface CustomerFileService { + public Reservation save(Reservation booking); + public List bookings(Long customerId); + public boolean delete(Long customerId, UUID booking); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java new file mode 100644 index 000000000000..7232f5d8a060 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java @@ -0,0 +1,15 @@ +package com.baeldung.springboot.hexagonal.infrastructure; + +import java.util.List; + +import com.baeldung.springboot.hexagonal.domain.Flight; + +/** + * Interface to expose functionality provided by the flight Service. + * + * @author marc + * + */ +public interface FlightService { + public List getRoute(String date, String origin, String destination); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java new file mode 100644 index 000000000000..b5bf1d879fad --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java @@ -0,0 +1,48 @@ +package com.baeldung.springboot.hexagonal.infrastructure; + +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.springframework.stereotype.Component; + +import com.baeldung.springboot.hexagonal.domain.Flight; + +/** + * Dummy implementation of the {@link FlightService} interface. This dummy class + * only knows a predefined set of routes, can be easily replaced by a HTTP call + * to a real service. + */ +@Component +public class FlightServiceDummyImpl implements FlightService { + private static final String CDG = "CDG"; + private static Map flightCodes = new HashMap<>(); + + @Override + public List getRoute(String date, String origin, String destination) { + String directFlightNumber = flightCodes.get(origin + destination); + if (directFlightNumber != null) { + return Arrays.asList(new Flight(directFlightNumber, origin, destination, date)); + } else { + String flightCode1 = flightCodes.get(origin + CDG); + String flightCode2 = flightCodes.get(CDG + destination); + if (flightCode1 != null && flightCode2 != null) { + return Arrays.asList(new Flight(flightCode1, origin, CDG, date), + new Flight(flightCode2, CDG, destination, date)); + } + ; + } + return null; + } + + static { + flightCodes.put("NCECDG", "7701"); + flightCodes.put("NCEORY", "7501"); + flightCodes.put("CDGNCE", "7711"); + flightCodes.put("CDGJFK", "005"); + flightCodes.put("JFKCDG", "015"); + flightCodes.put("CDGLAX", "006"); + flightCodes.put("LAXCDG", "016"); + } +} \ No newline at end of file diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java new file mode 100644 index 000000000000..d03a5e882e76 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java @@ -0,0 +1,96 @@ +package com.baeldung.springboot.hexagonal.infrastructure.customerjpa; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +import javax.transaction.Transactional; + +import org.springframework.stereotype.Component; + +import com.baeldung.springboot.hexagonal.domain.Flight; +import com.baeldung.springboot.hexagonal.domain.Reservation; +import com.baeldung.springboot.hexagonal.infrastructure.CustomerFileService; + +@Component +public class CustomerFileJpaImpl implements CustomerFileService { + private ReservationFileRepository repository; + + public CustomerFileJpaImpl(ReservationFileRepository repository) { + this.repository = repository; + } + + @Override + public Reservation save(Reservation booking) { + booking.setId(UUID.randomUUID()); + return toReservation(repository.save(toFile(booking))); + } + + @Override + @Transactional + public List bookings(Long customerId) { + return repository.findByCustomerId(customerId).stream() + .map(CustomerFileJpaImpl::toReservation) + .collect(Collectors.toList()); + } + + @Override + public boolean delete(Long customerId, UUID booking) { + ReservationFile file = repository.findByCustomerIdAndReservationId(customerId, booking); + if(file!=null) { + repository.delete(file); + return true; + } + return false; + } + + private static Reservation toReservation(ReservationFile file) { + Reservation reservation = new Reservation(); + reservation.setCustomerId(file.getCustomerId()); + reservation.setId(file.getReservationId()); + + for(int i=0; i flights; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public UUID getReservationId() { + return reservationId; + } + + public void setReservationId(UUID reservationId) { + this.reservationId = reservationId; + } + + public Long getCustomerId() { + return customerId; + } + + public void setCustomerId(Long customerId) { + this.customerId = customerId; + } + + public String getTravelDate() { + return travelDate; + } + + public void setTravelDate(String travelDate) { + this.travelDate = travelDate; + } + + public String getFlightNumbers() { + return flightNumbers; + } + + public void setFlightNumbers(String flightNumbers) { + this.flightNumbers = flightNumbers; + } + + public List getFlights() { + return flights; + } + + public void setFlights(List flights) { + this.flights = flights; + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java new file mode 100644 index 000000000000..095bc3502dd4 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.springboot.hexagonal.infrastructure.customerjpa; + +import java.util.List; +import java.util.UUID; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ReservationFileRepository extends JpaRepository { + public List findByCustomerId(Long customerId); + public ReservationFile findByCustomerIdAndReservationId(Long customerId, UUID reservationId); +} diff --git a/hexagonal-architecture/src/main/resources/application.yml b/hexagonal-architecture/src/main/resources/application.yml new file mode 100644 index 000000000000..e69de29bb2d1 From e27792309d01205daa829ec68144af042e5c5f7a Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Wed, 1 Apr 2020 20:44:02 +0200 Subject: [PATCH 02/15] hexagonal architecture - parent updated in pom --- hexagonal-architecture/pom.xml | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml index 47fb9bd68ba6..a874f6afde76 100644 --- a/hexagonal-architecture/pom.xml +++ b/hexagonal-architecture/pom.xml @@ -4,24 +4,17 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung hexagonal-architecture 0.0.1-SNAPSHOT hexagonal-architecture - - - - - - - org.springframework.boot - spring-boot-starter-parent - 2.2.6.RELEASE - + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../pom.xml - + From e914076d44979f6aded604af94bc26eb9ac9ebe4 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Wed, 1 Apr 2020 20:49:39 +0200 Subject: [PATCH 03/15] hexagonal architecture - cleanup and pom parent set back to springboot --- hexagonal-architecture/pom.xml | 9 +++++---- .../hexagonal/application/BookingController.java | 12 ++---------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml index a874f6afde76..636e56355eb8 100644 --- a/hexagonal-architecture/pom.xml +++ b/hexagonal-architecture/pom.xml @@ -4,15 +4,16 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 + com.baeldung hexagonal-architecture 0.0.1-SNAPSHOT hexagonal-architecture - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - ../pom.xml + org.springframework.boot + spring-boot-starter-parent + 2.2.6.RELEASE + diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java index cbe27fe7dfb0..dadd2dfaa160 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java @@ -14,20 +14,17 @@ import com.baeldung.springboot.hexagonal.domain.Flight; import com.baeldung.springboot.hexagonal.domain.Reservation; import com.baeldung.springboot.hexagonal.domain.ReservationService; -import com.baeldung.springboot.hexagonal.infrastructure.customerjpa.ReservationFile; -import com.baeldung.springboot.hexagonal.infrastructure.customerjpa.ReservationFileRepository; @RestController public class BookingController { private ReservationService reservationService; - private ReservationFileRepository repo; - public BookingController(ReservationService reservationService, ReservationFileRepository repo) { + public BookingController(ReservationService reservationService) { this.reservationService = reservationService; } @GetMapping("/findFlights") - public List getTravel(@RequestParam("date") String date, @RequestParam("origin") String origin, + public List findFlights(@RequestParam("date") String date, @RequestParam("origin") String origin, @RequestParam("destination") String destination) { return reservationService.identifyFLights(date, origin, destination); } @@ -49,9 +46,4 @@ public boolean cancelBooking(@PathVariable("customerId") Long customerId, @PathV reservation.setId(reservationId); return reservationService.cancel(reservation); } - - @GetMapping("/booking") - public List booking() { - return repo.findAll(); - } } From 0627ea5cd7774752031ddf6685004c646ae7a185 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Thu, 2 Apr 2020 22:54:25 +0200 Subject: [PATCH 04/15] hexagonal architecture - code formatting --- .../application/BookingController.java | 3 +- .../springboot/hexagonal/domain/Flight.java | 2 +- .../hexagonal/domain/Reservation.java | 9 ----- .../hexagonal/domain/ReservationService.java | 3 ++ .../infrastructure/CustomerFileService.java | 2 ++ .../infrastructure/FlightService.java | 6 ---- .../FlightServiceDummyImpl.java | 8 +---- .../customerjpa/CustomerFileJpaImpl.java | 33 +++++++------------ .../customerjpa/FlightEntity.java | 2 +- .../customerjpa/ReservationFile.java | 2 +- .../ReservationFileRepository.java | 1 + 11 files changed, 23 insertions(+), 48 deletions(-) diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java index dadd2dfaa160..71275ddbb1e4 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java @@ -24,8 +24,7 @@ public BookingController(ReservationService reservationService) { } @GetMapping("/findFlights") - public List findFlights(@RequestParam("date") String date, @RequestParam("origin") String origin, - @RequestParam("destination") String destination) { + public List findFlights(@RequestParam("date") String date, @RequestParam("origin") String origin, @RequestParam("destination") String destination) { return reservationService.identifyFLights(date, origin, destination); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java index 77aa9238e2c6..6605f7e5daa7 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java @@ -8,7 +8,7 @@ public class Flight { public Flight() { } - + public Flight(String number, String origin, String destination, String date) { this.number = number; this.origin = origin; diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java index a5c1533bc873..16ffc68da7fa 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java @@ -4,17 +4,8 @@ import java.util.UUID; public class Reservation { - /** - * Functional ID of the reservation - */ private UUID id; - /** - * Customer holding the reservation - */ private Long customerId; - /** - * List of flights in the reservation - */ private List flights; public UUID getId() { diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java index bd5f581f98e4..54fca31061ef 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java @@ -4,7 +4,10 @@ public interface ReservationService { public List identifyFLights(String date, String origin, String destination); + public Reservation processBooking(Reservation reservation, Long customerId); + public List bookings(Long customerId); + public boolean cancel(Reservation file); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java index fdb94b38e362..87a39361e720 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java @@ -7,6 +7,8 @@ public interface CustomerFileService { public Reservation save(Reservation booking); + public List bookings(Long customerId); + public boolean delete(Long customerId, UUID booking); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java index 7232f5d8a060..30b301c9eb24 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java @@ -4,12 +4,6 @@ import com.baeldung.springboot.hexagonal.domain.Flight; -/** - * Interface to expose functionality provided by the flight Service. - * - * @author marc - * - */ public interface FlightService { public List getRoute(String date, String origin, String destination); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java index b5bf1d879fad..2d866c1ae601 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java @@ -9,11 +9,6 @@ import com.baeldung.springboot.hexagonal.domain.Flight; -/** - * Dummy implementation of the {@link FlightService} interface. This dummy class - * only knows a predefined set of routes, can be easily replaced by a HTTP call - * to a real service. - */ @Component public class FlightServiceDummyImpl implements FlightService { private static final String CDG = "CDG"; @@ -28,8 +23,7 @@ public List getRoute(String date, String origin, String destination) { String flightCode1 = flightCodes.get(origin + CDG); String flightCode2 = flightCodes.get(CDG + destination); if (flightCode1 != null && flightCode2 != null) { - return Arrays.asList(new Flight(flightCode1, origin, CDG, date), - new Flight(flightCode2, CDG, destination, date)); + return Arrays.asList(new Flight(flightCode1, origin, CDG, date), new Flight(flightCode2, CDG, destination, date)); } ; } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java index d03a5e882e76..30bb6f4331d7 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java @@ -29,15 +29,13 @@ public Reservation save(Reservation booking) { @Override @Transactional public List bookings(Long customerId) { - return repository.findByCustomerId(customerId).stream() - .map(CustomerFileJpaImpl::toReservation) - .collect(Collectors.toList()); + return repository.findByCustomerId(customerId).stream().map(CustomerFileJpaImpl::toReservation).collect(Collectors.toList()); } @Override public boolean delete(Long customerId, UUID booking) { ReservationFile file = repository.findByCustomerIdAndReservationId(customerId, booking); - if(file!=null) { + if (file != null) { repository.delete(file); return true; } @@ -48,16 +46,12 @@ private static Reservation toReservation(ReservationFile file) { Reservation reservation = new Reservation(); reservation.setCustomerId(file.getCustomerId()); reservation.setId(file.getReservationId()); - - for(int i=0; i flights; public Long getId() { diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java index 095bc3502dd4..e312a5cdf439 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java @@ -9,5 +9,6 @@ @Repository public interface ReservationFileRepository extends JpaRepository { public List findByCustomerId(Long customerId); + public ReservationFile findByCustomerIdAndReservationId(Long customerId, UUID reservationId); } From 85259eb5e1a148f2d91ed0b65a796e8b835dcee6 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Wed, 8 Apr 2020 16:14:44 +0200 Subject: [PATCH 05/15] BAEL-3960 - first commit --- logging-modules/log-assertion/pom.xml | 31 ++++++++++ .../baeldung/junit/log/BusinessWorker.java | 18 ++++++ .../src/main/resources/logback.xml | 20 +++++++ .../junit/log/BusinessWorkerJunitTest.java | 50 ++++++++++++++++ .../baeldung/junit/log/MemoryAppender.java | 60 +++++++++++++++++++ .../src/test/resources/logback.xml | 20 +++++++ 6 files changed, 199 insertions(+) create mode 100644 logging-modules/log-assertion/pom.xml create mode 100644 logging-modules/log-assertion/src/main/java/com/baeldung/junit/log/BusinessWorker.java create mode 100644 logging-modules/log-assertion/src/main/resources/logback.xml create mode 100644 logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java create mode 100644 logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/MemoryAppender.java create mode 100644 logging-modules/log-assertion/src/test/resources/logback.xml diff --git a/logging-modules/log-assertion/pom.xml b/logging-modules/log-assertion/pom.xml new file mode 100644 index 000000000000..b5e1c7a16714 --- /dev/null +++ b/logging-modules/log-assertion/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + com.baeldung.junit + log-assertion + 0.0.1-SNAPSHOT + + + + + ch.qos.logback + logback-classic + 1.2.3 + + + + org.assertj + assertj-core + 3.15.0 + test + + + + + 1.8 + 1.8 + 1.8 + ${java.version} + + \ No newline at end of file diff --git a/logging-modules/log-assertion/src/main/java/com/baeldung/junit/log/BusinessWorker.java b/logging-modules/log-assertion/src/main/java/com/baeldung/junit/log/BusinessWorker.java new file mode 100644 index 000000000000..6b4024545b18 --- /dev/null +++ b/logging-modules/log-assertion/src/main/java/com/baeldung/junit/log/BusinessWorker.java @@ -0,0 +1,18 @@ +package com.baeldung.junit.log; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BusinessWorker { + + private static Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class); + + public void generateLogs(String msg) { + LOGGER.trace(msg); + LOGGER.debug(msg); + LOGGER.info(msg); + LOGGER.warn(msg); + LOGGER.error(msg); + } + +} diff --git a/logging-modules/log-assertion/src/main/resources/logback.xml b/logging-modules/log-assertion/src/main/resources/logback.xml new file mode 100644 index 000000000000..3cf16d2628b5 --- /dev/null +++ b/logging-modules/log-assertion/src/main/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + + + %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + \ No newline at end of file diff --git a/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java b/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java new file mode 100644 index 000000000000..5617c7e9ab95 --- /dev/null +++ b/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java @@ -0,0 +1,50 @@ +package com.baeldung.junit.log; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.slf4j.LoggerFactory; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.Logger; +import ch.qos.logback.classic.LoggerContext; + +public class BusinessWorkerJunitTest { + + private static MemoryAppender memoryAppender; + + private static final String LOGGER_NAME = "com.baeldung.junit.log"; + private static final String MSG = "This is a test message!!!"; + + @Before + public void setup() { + Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME); + memoryAppender = new MemoryAppender(); + memoryAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory()); + logger.setLevel(Level.DEBUG); + logger.addAppender(memoryAppender); + memoryAppender.start(); + + } + + @After + public void cleanUp() { + memoryAppender.reset(); + memoryAppender.stop(); + } + + @Test + public void test() { + BusinessWorker worker = new BusinessWorker(); + worker.generateLogs(MSG); + + // I check that I only have 4 messages (all but trace) + assertThat(memoryAppender.countEventsForLogger(LOGGER_NAME)).isEqualTo(4); + + // I look for a specific message at a specific level, and I only have 1 + assertThat(memoryAppender.search(MSG, Level.INFO).size()).isEqualTo(1); + } + +} diff --git a/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/MemoryAppender.java new file mode 100644 index 000000000000..e7e9db7a5bde --- /dev/null +++ b/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/MemoryAppender.java @@ -0,0 +1,60 @@ +package com.baeldung.junit.log; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import org.junit.Assert; + +import ch.qos.logback.classic.Level; +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.read.ListAppender; + +/** + * In memory slf4j appender
+ * Convenient appender to be able to check slf4j invocations + * + * @author M405991 + * @author m408461 + */ +public class MemoryAppender extends ListAppender { + + public void reset() { + this.list.clear(); + } + + public void checkContains(String string, Level level) { + Assert.assertTrue("Message expected to contain " + string + " with severity " + level, this.list.stream().anyMatch(event -> event.getMessage().toString().contains(string) && event.getLevel().equals(level))); + } + + public int countEventsForLogger(String loggerName) { + return (int) this.list.stream().filter(event -> event.getLoggerName().contains(loggerName)).count(); + } + + public List search(String string) { + return this.list.stream().filter(event -> event.getMessage().toString().contains(string)).collect(Collectors.toList()); + } + + public List search(String string, Level level) { + return this.list.stream().filter(event -> event.getMessage().toString().contains(string) && event.getLevel().equals(level)).collect(Collectors.toList()); + } + + public int getSize() { + return this.list.size(); + } + + public List getLoggedEvents() { + return Collections.unmodifiableList(this.list); + } + + @Override + public String toString() { + final StringBuffer stringBuffer = new StringBuffer(); + + stringBuffer.append(super.toString()); + stringBuffer.append(" "); + stringBuffer.append(this.list.toString()); + + return stringBuffer.toString(); + } +} diff --git a/logging-modules/log-assertion/src/test/resources/logback.xml b/logging-modules/log-assertion/src/test/resources/logback.xml new file mode 100644 index 000000000000..65f0486c0aac --- /dev/null +++ b/logging-modules/log-assertion/src/test/resources/logback.xml @@ -0,0 +1,20 @@ + + + + + + + %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n + + + + + + + + + + + + + \ No newline at end of file From d9a05a8689d13424ed704d963749822eb918f32c Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Wed, 8 Apr 2020 21:25:47 +0200 Subject: [PATCH 06/15] BAEL-3960: moved into testing project --- testing-modules/pom.xml | 1 + .../testing-logs}/pom.xml | 14 ++++++++++---- .../com/baeldung/junit/log/BusinessWorker.java | 0 .../testing-logs}/src/main/resources/logback.xml | 0 .../junit/log/BusinessWorkerJunitTest.java | 0 .../com/baeldung/junit/log/MemoryAppender.java | 0 .../testing-logs}/src/test/resources/logback.xml | 0 7 files changed, 11 insertions(+), 4 deletions(-) rename {logging-modules/log-assertion => testing-modules/testing-logs}/pom.xml (75%) rename {logging-modules/log-assertion => testing-modules/testing-logs}/src/main/java/com/baeldung/junit/log/BusinessWorker.java (100%) rename {logging-modules/log-assertion => testing-modules/testing-logs}/src/main/resources/logback.xml (100%) rename {logging-modules/log-assertion => testing-modules/testing-logs}/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java (100%) rename {logging-modules/log-assertion => testing-modules/testing-logs}/src/test/java/com/baeldung/junit/log/MemoryAppender.java (100%) rename {logging-modules/log-assertion => testing-modules/testing-logs}/src/test/resources/logback.xml (100%) diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 951909b36f09..1003c6f90604 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -33,6 +33,7 @@ selenium-junit-testng spring-testing test-containers + testing-logs testng junit-5-basics easymock diff --git a/logging-modules/log-assertion/pom.xml b/testing-modules/testing-logs/pom.xml similarity index 75% rename from logging-modules/log-assertion/pom.xml rename to testing-modules/testing-logs/pom.xml index b5e1c7a16714..2964694cda7a 100644 --- a/logging-modules/log-assertion/pom.xml +++ b/testing-modules/testing-logs/pom.xml @@ -2,10 +2,17 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - com.baeldung.junit - log-assertion + + testing-logs 0.0.1-SNAPSHOT + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + @@ -26,6 +33,5 @@ 1.8 1.8 1.8 - ${java.version} - \ No newline at end of file + diff --git a/logging-modules/log-assertion/src/main/java/com/baeldung/junit/log/BusinessWorker.java b/testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java similarity index 100% rename from logging-modules/log-assertion/src/main/java/com/baeldung/junit/log/BusinessWorker.java rename to testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java diff --git a/logging-modules/log-assertion/src/main/resources/logback.xml b/testing-modules/testing-logs/src/main/resources/logback.xml similarity index 100% rename from logging-modules/log-assertion/src/main/resources/logback.xml rename to testing-modules/testing-logs/src/main/resources/logback.xml diff --git a/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java similarity index 100% rename from logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java rename to testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java diff --git a/logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java similarity index 100% rename from logging-modules/log-assertion/src/test/java/com/baeldung/junit/log/MemoryAppender.java rename to testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java diff --git a/logging-modules/log-assertion/src/test/resources/logback.xml b/testing-modules/testing-logs/src/test/resources/logback.xml similarity index 100% rename from logging-modules/log-assertion/src/test/resources/logback.xml rename to testing-modules/testing-logs/src/test/resources/logback.xml From 3d2288b84ff976bc98f751eb3e2feb808aa89b65 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Fri, 10 Apr 2020 12:17:31 +0200 Subject: [PATCH 07/15] BAEL-3960: formatting --- .../baeldung/junit/log/MemoryAppender.java | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java index e7e9db7a5bde..25bb5756347c 100644 --- a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java +++ b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java @@ -13,9 +13,6 @@ /** * In memory slf4j appender
* Convenient appender to be able to check slf4j invocations - * - * @author M405991 - * @author m408461 */ public class MemoryAppender extends ListAppender { @@ -24,19 +21,29 @@ public void reset() { } public void checkContains(String string, Level level) { - Assert.assertTrue("Message expected to contain " + string + " with severity " + level, this.list.stream().anyMatch(event -> event.getMessage().toString().contains(string) && event.getLevel().equals(level))); + Assert.assertTrue("Message expected to contain " + string + " with severity " + level, + this.list.stream() + .anyMatch(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level))); } public int countEventsForLogger(String loggerName) { - return (int) this.list.stream().filter(event -> event.getLoggerName().contains(loggerName)).count(); + return (int) this.list.stream() + .filter(event -> event.getLoggerName().contains(loggerName)) + .count(); } public List search(String string) { - return this.list.stream().filter(event -> event.getMessage().toString().contains(string)).collect(Collectors.toList()); + return this.list.stream() + .filter(event -> event.getMessage().toString().contains(string)) + .collect(Collectors.toList()); } public List search(String string, Level level) { - return this.list.stream().filter(event -> event.getMessage().toString().contains(string) && event.getLevel().equals(level)).collect(Collectors.toList()); + return this.list.stream() + .filter(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level)) + .collect(Collectors.toList()); } public int getSize() { From 314f86da2ad977f8007eac9c8b2498ed587de43b Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Fri, 10 Apr 2020 17:54:39 +0200 Subject: [PATCH 08/15] switched to dedicated branch and removal of unwanted project --- hexagonal-architecture/HELP.md | 28 ------ hexagonal-architecture/pom.xml | 72 --------------- .../hexagonal/HexagonalApplication.java | 11 --- .../application/BookingController.java | 48 ---------- .../springboot/hexagonal/domain/Flight.java | 50 ----------- .../hexagonal/domain/Reservation.java | 34 -------- .../hexagonal/domain/ReservationService.java | 13 --- .../domain/ReservationServiceImpl.java | 42 --------- .../infrastructure/CustomerFileService.java | 14 --- .../infrastructure/FlightService.java | 9 -- .../FlightServiceDummyImpl.java | 42 --------- .../customerjpa/CustomerFileJpaImpl.java | 87 ------------------- .../customerjpa/FlightEntity.java | 68 --------------- .../customerjpa/ReservationFile.java | 72 --------------- .../ReservationFileRepository.java | 14 --- .../src/main/resources/application.yml | 0 16 files changed, 604 deletions(-) delete mode 100644 hexagonal-architecture/HELP.md delete mode 100644 hexagonal-architecture/pom.xml delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/FlightEntity.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFile.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java delete mode 100644 hexagonal-architecture/src/main/resources/application.yml diff --git a/hexagonal-architecture/HELP.md b/hexagonal-architecture/HELP.md deleted file mode 100644 index e74015dccbe6..000000000000 --- a/hexagonal-architecture/HELP.md +++ /dev/null @@ -1,28 +0,0 @@ -# Getting Started - - - -Here is a simple example of Hexagonal Architecture in SpringBoot. - - - -Once started you can use following curl request - - - -Add a booking to customer with ID 1 - -curl --header "Content-Type: application/json" --request PUT --data '{"flights":[{"date": "05MAR","destination": "CDG","number": "7701","origin": "NCE"},{"date": "05MAR","destination": "LAX","number": "006","origin": "CDG"}]}' http://localhost:8080/customer/1/booking - - - -List all bookings of customer 1 - -curl http://localhost:8080/customer/1/booking - - - -Delete a specific booking of customer 1 - -curl --request DELETE "http://localhost:8080/customer/1/booking/" - diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml deleted file mode 100644 index 636e56355eb8..000000000000 --- a/hexagonal-architecture/pom.xml +++ /dev/null @@ -1,72 +0,0 @@ - - - 4.0.0 - - com.baeldung - hexagonal-architecture - 0.0.1-SNAPSHOT - hexagonal-architecture - - - org.springframework.boot - spring-boot-starter-parent - 2.2.6.RELEASE - - - - - - - org.springframework.boot - spring-boot-dependencies - 2.2.6.RELEASE - pom - import - - - - - - - org.springframework.boot - spring-boot-starter-data-jpa - - - - com.h2database - h2 - runtime - - - org.springframework.boot - spring-boot-starter-test - test - - - org.junit.vintage - junit-vintage-engine - - - - - org.springframework.boot - spring-boot-starter-web - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - - - - - 1.8 - - - diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java deleted file mode 100644 index 0c1dd21d4031..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/HexagonalApplication.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.baeldung.springboot.hexagonal; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -@SpringBootApplication -public class HexagonalApplication { - public static void main(String[] args) { - SpringApplication.run(HexagonalApplication.class, args); - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java deleted file mode 100644 index 71275ddbb1e4..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/application/BookingController.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.springboot.hexagonal.application; - -import java.util.List; -import java.util.UUID; - -import org.springframework.web.bind.annotation.DeleteMapping; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PutMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.springboot.hexagonal.domain.Flight; -import com.baeldung.springboot.hexagonal.domain.Reservation; -import com.baeldung.springboot.hexagonal.domain.ReservationService; - -@RestController -public class BookingController { - private ReservationService reservationService; - - public BookingController(ReservationService reservationService) { - this.reservationService = reservationService; - } - - @GetMapping("/findFlights") - public List findFlights(@RequestParam("date") String date, @RequestParam("origin") String origin, @RequestParam("destination") String destination) { - return reservationService.identifyFLights(date, origin, destination); - } - - @PutMapping("/customer/{id}/booking") - public Reservation makeBooking(@PathVariable("id") Long customerId, @RequestBody Reservation reservation) { - return reservationService.processBooking(reservation, customerId); - } - - @GetMapping("/customer/{customerId}/booking") - public List bookings(@PathVariable("customerId") Long id) { - return reservationService.bookings(id); - } - - @DeleteMapping("/customer/{customerId}/booking/{id}") - public boolean cancelBooking(@PathVariable("customerId") Long customerId, @PathVariable("id") UUID reservationId) { - Reservation reservation = new Reservation(); - reservation.setCustomerId(customerId); - reservation.setId(reservationId); - return reservationService.cancel(reservation); - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java deleted file mode 100644 index 6605f7e5daa7..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Flight.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.springboot.hexagonal.domain; - -public class Flight { - private String number; - private String origin; - private String destination; - private String date; - - public Flight() { - } - - public Flight(String number, String origin, String destination, String date) { - this.number = number; - this.origin = origin; - this.destination = destination; - this.date = date; - } - - public String getNumber() { - return number; - } - - public void setNumber(String number) { - this.number = number; - } - - public String getOrigin() { - return origin; - } - - public void setOrigin(String origin) { - this.origin = origin; - } - - public String getDestination() { - return destination; - } - - public void setDestination(String destination) { - this.destination = destination; - } - - public String getDate() { - return date; - } - - public void setDate(String date) { - this.date = date; - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java deleted file mode 100644 index 16ffc68da7fa..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/Reservation.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.springboot.hexagonal.domain; - -import java.util.List; -import java.util.UUID; - -public class Reservation { - private UUID id; - private Long customerId; - private List flights; - - public UUID getId() { - return id; - } - - public void setId(UUID id) { - this.id = id; - } - - public List getFlights() { - return flights; - } - - public void setFlights(List flights) { - this.flights = flights; - } - - public Long getCustomerId() { - return customerId; - } - - public void setCustomerId(Long customerId) { - this.customerId = customerId; - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java deleted file mode 100644 index 54fca31061ef..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationService.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.baeldung.springboot.hexagonal.domain; - -import java.util.List; - -public interface ReservationService { - public List identifyFLights(String date, String origin, String destination); - - public Reservation processBooking(Reservation reservation, Long customerId); - - public List bookings(Long customerId); - - public boolean cancel(Reservation file); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java deleted file mode 100644 index 7c55dd417135..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/domain/ReservationServiceImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.springboot.hexagonal.domain; - -import java.util.List; - -import org.springframework.stereotype.Service; - -import com.baeldung.springboot.hexagonal.infrastructure.CustomerFileService; -import com.baeldung.springboot.hexagonal.infrastructure.FlightService; - -@Service -public class ReservationServiceImpl implements ReservationService { - private CustomerFileService customerFileService; - private FlightService flightService; - - public ReservationServiceImpl(CustomerFileService customerFileService, FlightService flightService) { - super(); - this.customerFileService = customerFileService; - this.flightService = flightService; - } - - @Override - public List identifyFLights(String date, String origin, String destination) { - return flightService.getRoute(date, origin, destination); - } - - @Override - public Reservation processBooking(Reservation reservation, Long customer) { - reservation.setCustomerId(customer); - return customerFileService.save(reservation); - } - - @Override - public List bookings(Long customerId) { - List reservations = customerFileService.bookings(customerId); - return reservations; - } - - @Override - public boolean cancel(Reservation file) { - return customerFileService.delete(file.getCustomerId(), file.getId()); - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java deleted file mode 100644 index 87a39361e720..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/CustomerFileService.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure; - -import java.util.List; -import java.util.UUID; - -import com.baeldung.springboot.hexagonal.domain.Reservation; - -public interface CustomerFileService { - public Reservation save(Reservation booking); - - public List bookings(Long customerId); - - public boolean delete(Long customerId, UUID booking); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java deleted file mode 100644 index 30b301c9eb24..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightService.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure; - -import java.util.List; - -import com.baeldung.springboot.hexagonal.domain.Flight; - -public interface FlightService { - public List getRoute(String date, String origin, String destination); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java deleted file mode 100644 index 2d866c1ae601..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/FlightServiceDummyImpl.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.springframework.stereotype.Component; - -import com.baeldung.springboot.hexagonal.domain.Flight; - -@Component -public class FlightServiceDummyImpl implements FlightService { - private static final String CDG = "CDG"; - private static Map flightCodes = new HashMap<>(); - - @Override - public List getRoute(String date, String origin, String destination) { - String directFlightNumber = flightCodes.get(origin + destination); - if (directFlightNumber != null) { - return Arrays.asList(new Flight(directFlightNumber, origin, destination, date)); - } else { - String flightCode1 = flightCodes.get(origin + CDG); - String flightCode2 = flightCodes.get(CDG + destination); - if (flightCode1 != null && flightCode2 != null) { - return Arrays.asList(new Flight(flightCode1, origin, CDG, date), new Flight(flightCode2, CDG, destination, date)); - } - ; - } - return null; - } - - static { - flightCodes.put("NCECDG", "7701"); - flightCodes.put("NCEORY", "7501"); - flightCodes.put("CDGNCE", "7711"); - flightCodes.put("CDGJFK", "005"); - flightCodes.put("JFKCDG", "015"); - flightCodes.put("CDGLAX", "006"); - flightCodes.put("LAXCDG", "016"); - } -} \ No newline at end of file diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java deleted file mode 100644 index 30bb6f4331d7..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/CustomerFileJpaImpl.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure.customerjpa; - -import java.util.List; -import java.util.UUID; -import java.util.stream.Collectors; - -import javax.transaction.Transactional; - -import org.springframework.stereotype.Component; - -import com.baeldung.springboot.hexagonal.domain.Flight; -import com.baeldung.springboot.hexagonal.domain.Reservation; -import com.baeldung.springboot.hexagonal.infrastructure.CustomerFileService; - -@Component -public class CustomerFileJpaImpl implements CustomerFileService { - private ReservationFileRepository repository; - - public CustomerFileJpaImpl(ReservationFileRepository repository) { - this.repository = repository; - } - - @Override - public Reservation save(Reservation booking) { - booking.setId(UUID.randomUUID()); - return toReservation(repository.save(toFile(booking))); - } - - @Override - @Transactional - public List bookings(Long customerId) { - return repository.findByCustomerId(customerId).stream().map(CustomerFileJpaImpl::toReservation).collect(Collectors.toList()); - } - - @Override - public boolean delete(Long customerId, UUID booking) { - ReservationFile file = repository.findByCustomerIdAndReservationId(customerId, booking); - if (file != null) { - repository.delete(file); - return true; - } - return false; - } - - private static Reservation toReservation(ReservationFile file) { - Reservation reservation = new Reservation(); - reservation.setCustomerId(file.getCustomerId()); - reservation.setId(file.getReservationId()); - - for (int i = 0; i < file.getFlights().size(); i++) - file.getFlights().get(i); - - reservation.setFlights(file.getFlights().stream().map(CustomerFileJpaImpl::toFlight).collect(Collectors.toList())); - - return reservation; - } - - private static ReservationFile toFile(Reservation booking) { - ReservationFile record = new ReservationFile(); - record.setReservationId(booking.getId()); - record.setCustomerId(booking.getCustomerId()); - record.setTravelDate(booking.getFlights().get(0).getDate()); - - record.setFlights(booking.getFlights().stream().map(CustomerFileJpaImpl::toFlightEntity).collect(Collectors.toList()) - - ); - return record; - } - - private static Flight toFlight(FlightEntity entity) { - Flight flight = new Flight(); - flight.setDate(entity.getDate()); - flight.setOrigin(entity.getOrigin()); - flight.setDestination(entity.getDestination()); - flight.setNumber(entity.getNumber()); - return flight; - } - - private static FlightEntity toFlightEntity(Flight flight) { - FlightEntity entity = new FlightEntity(); - entity.setDate(flight.getDate()); - entity.setOrigin(flight.getOrigin()); - entity.setDestination(flight.getDestination()); - entity.setNumber(flight.getNumber()); - return entity; - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/FlightEntity.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/FlightEntity.java deleted file mode 100644 index e2ec0162ef29..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/FlightEntity.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure.customerjpa; - -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; - -@Entity -public class FlightEntity { - @Id - @GeneratedValue - private Long id; - - private String number; - private String origin; - private String destination; - private String date; - - public FlightEntity() { - } - - public FlightEntity(Long id, String number, String origin, String destination, String date) { - this.id = id; - this.number = number; - this.origin = origin; - this.destination = destination; - this.date = date; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getNumber() { - return number; - } - - public void setNumber(String number) { - this.number = number; - } - - public String getOrigin() { - return origin; - } - - public void setOrigin(String origin) { - this.origin = origin; - } - - public String getDestination() { - return destination; - } - - public void setDestination(String destination) { - this.destination = destination; - } - - public String getDate() { - return date; - } - - public void setDate(String date) { - this.date = date; - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFile.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFile.java deleted file mode 100644 index 54c3ddc1c316..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFile.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure.customerjpa; - -import java.util.List; -import java.util.UUID; - -import javax.persistence.CascadeType; -import javax.persistence.Entity; -import javax.persistence.FetchType; -import javax.persistence.GeneratedValue; -import javax.persistence.Id; -import javax.persistence.OneToMany; - -@Entity -public class ReservationFile { - @Id - @GeneratedValue - private Long id; - private UUID reservationId; - private Long customerId; - private String travelDate; - private String flightNumbers; - @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) - private List flights; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public UUID getReservationId() { - return reservationId; - } - - public void setReservationId(UUID reservationId) { - this.reservationId = reservationId; - } - - public Long getCustomerId() { - return customerId; - } - - public void setCustomerId(Long customerId) { - this.customerId = customerId; - } - - public String getTravelDate() { - return travelDate; - } - - public void setTravelDate(String travelDate) { - this.travelDate = travelDate; - } - - public String getFlightNumbers() { - return flightNumbers; - } - - public void setFlightNumbers(String flightNumbers) { - this.flightNumbers = flightNumbers; - } - - public List getFlights() { - return flights; - } - - public void setFlights(List flights) { - this.flights = flights; - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java deleted file mode 100644 index e312a5cdf439..000000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/springboot/hexagonal/infrastructure/customerjpa/ReservationFileRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.springboot.hexagonal.infrastructure.customerjpa; - -import java.util.List; -import java.util.UUID; - -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface ReservationFileRepository extends JpaRepository { - public List findByCustomerId(Long customerId); - - public ReservationFile findByCustomerIdAndReservationId(Long customerId, UUID reservationId); -} diff --git a/hexagonal-architecture/src/main/resources/application.yml b/hexagonal-architecture/src/main/resources/application.yml deleted file mode 100644 index e69de29bb2d1..000000000000 From 12b1cf3856b0e5eb999588a97cb753cade48cc72 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Sun, 12 Apr 2020 08:16:10 +0200 Subject: [PATCH 09/15] BAEL-3960 code updated since article review --- .../java/com/baeldung/junit/log/MemoryAppender.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java index 25bb5756347c..b6cdad499bce 100644 --- a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java +++ b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java @@ -4,8 +4,6 @@ import java.util.List; import java.util.stream.Collectors; -import org.junit.Assert; - import ch.qos.logback.classic.Level; import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.read.ListAppender; @@ -20,11 +18,10 @@ public void reset() { this.list.clear(); } - public void checkContains(String string, Level level) { - Assert.assertTrue("Message expected to contain " + string + " with severity " + level, - this.list.stream() - .anyMatch(event -> event.getMessage().toString().contains(string) - && event.getLevel().equals(level))); + public boolean contains(String string, Level level) { + return this.list.stream() + .anyMatch(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level)); } public int countEventsForLogger(String loggerName) { From 453ff2e4e48ef4dd4a91716112eea4798a691dc7 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Sun, 12 Apr 2020 09:01:20 +0200 Subject: [PATCH 10/15] BAEL-3960 formatting --- .../junit/log/BusinessWorkerJunitTest.java | 5 ++-- .../baeldung/junit/log/MemoryAppender.java | 29 +++++-------------- .../src/test/resources/logback.xml | 6 ---- 3 files changed, 11 insertions(+), 29 deletions(-) diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java index 5617c7e9ab95..9c31e5066770 100644 --- a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java +++ b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java @@ -39,12 +39,13 @@ public void cleanUp() { public void test() { BusinessWorker worker = new BusinessWorker(); worker.generateLogs(MSG); - + // I check that I only have 4 messages (all but trace) assertThat(memoryAppender.countEventsForLogger(LOGGER_NAME)).isEqualTo(4); - // I look for a specific message at a specific level, and I only have 1 assertThat(memoryAppender.search(MSG, Level.INFO).size()).isEqualTo(1); + // I check that the entry that is not present is the trace level + assertThat(memoryAppender.contains(MSG, Level.TRACE)).isFalse(); } } diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java index b6cdad499bce..31c5c69766cc 100644 --- a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java +++ b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java @@ -13,34 +13,32 @@ * Convenient appender to be able to check slf4j invocations */ public class MemoryAppender extends ListAppender { - public void reset() { this.list.clear(); } public boolean contains(String string, Level level) { return this.list.stream() - .anyMatch(event -> event.getMessage().toString().contains(string) - && event.getLevel().equals(level)); + .anyMatch(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level)); } public int countEventsForLogger(String loggerName) { return (int) this.list.stream() - .filter(event -> event.getLoggerName().contains(loggerName)) - .count(); + .filter(event -> event.getLoggerName().contains(loggerName)).count(); } public List search(String string) { return this.list.stream() - .filter(event -> event.getMessage().toString().contains(string)) - .collect(Collectors.toList()); + .filter(event -> event.getMessage().toString().contains(string)) + .collect(Collectors.toList()); } public List search(String string, Level level) { return this.list.stream() - .filter(event -> event.getMessage().toString().contains(string) - && event.getLevel().equals(level)) - .collect(Collectors.toList()); + .filter(event -> event.getMessage().toString().contains(string) + && event.getLevel().equals(level)) + .collect(Collectors.toList()); } public int getSize() { @@ -50,15 +48,4 @@ public int getSize() { public List getLoggedEvents() { return Collections.unmodifiableList(this.list); } - - @Override - public String toString() { - final StringBuffer stringBuffer = new StringBuffer(); - - stringBuffer.append(super.toString()); - stringBuffer.append(" "); - stringBuffer.append(this.list.toString()); - - return stringBuffer.toString(); - } } diff --git a/testing-modules/testing-logs/src/test/resources/logback.xml b/testing-modules/testing-logs/src/test/resources/logback.xml index 65f0486c0aac..2fc992fc447d 100644 --- a/testing-modules/testing-logs/src/test/resources/logback.xml +++ b/testing-modules/testing-logs/src/test/resources/logback.xml @@ -1,6 +1,5 @@ - @@ -9,12 +8,7 @@ - - - - - \ No newline at end of file From ebf54df309e29b18d432f1c064d1b5d792205418 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Sun, 12 Apr 2020 09:10:47 +0200 Subject: [PATCH 11/15] BAEL-3960 PMD --- ...BusinessWorkerJunitTest.java => BusinessWorkerUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/{BusinessWorkerJunitTest.java => BusinessWorkerUnitTest.java} (97%) diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java similarity index 97% rename from testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java rename to testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java index 9c31e5066770..e2985a8496de 100644 --- a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerJunitTest.java +++ b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java @@ -11,7 +11,7 @@ import ch.qos.logback.classic.Logger; import ch.qos.logback.classic.LoggerContext; -public class BusinessWorkerJunitTest { +public class BusinessWorkerUnitTest { private static MemoryAppender memoryAppender; From 132d557234f6e139d77ce3a4c09b03ccc3b03c45 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Sun, 12 Apr 2020 09:18:08 +0200 Subject: [PATCH 12/15] BAEL-3960 personal code review --- testing-modules/testing-logs/pom.xml | 8 -------- .../main/java/com/baeldung/junit/log/BusinessWorker.java | 2 -- .../testing-logs/src/main/resources/logback.xml | 2 -- .../com/baeldung/junit/log/BusinessWorkerUnitTest.java | 3 --- 4 files changed, 15 deletions(-) diff --git a/testing-modules/testing-logs/pom.xml b/testing-modules/testing-logs/pom.xml index 2964694cda7a..1965681ff335 100644 --- a/testing-modules/testing-logs/pom.xml +++ b/testing-modules/testing-logs/pom.xml @@ -2,7 +2,6 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - testing-logs 0.0.1-SNAPSHOT @@ -14,7 +13,6 @@ - ch.qos.logback logback-classic @@ -28,10 +26,4 @@ test - - - 1.8 - 1.8 - 1.8 - diff --git a/testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java b/testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java index 6b4024545b18..86cd38824ceb 100644 --- a/testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java +++ b/testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java @@ -4,7 +4,6 @@ import org.slf4j.LoggerFactory; public class BusinessWorker { - private static Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class); public void generateLogs(String msg) { @@ -14,5 +13,4 @@ public void generateLogs(String msg) { LOGGER.warn(msg); LOGGER.error(msg); } - } diff --git a/testing-modules/testing-logs/src/main/resources/logback.xml b/testing-modules/testing-logs/src/main/resources/logback.xml index 3cf16d2628b5..deb1f12bbc9e 100644 --- a/testing-modules/testing-logs/src/main/resources/logback.xml +++ b/testing-modules/testing-logs/src/main/resources/logback.xml @@ -1,6 +1,5 @@ - @@ -12,7 +11,6 @@ - diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java index e2985a8496de..9200caf13d1f 100644 --- a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java +++ b/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java @@ -12,9 +12,7 @@ import ch.qos.logback.classic.LoggerContext; public class BusinessWorkerUnitTest { - private static MemoryAppender memoryAppender; - private static final String LOGGER_NAME = "com.baeldung.junit.log"; private static final String MSG = "This is a test message!!!"; @@ -47,5 +45,4 @@ public void test() { // I check that the entry that is not present is the trace level assertThat(memoryAppender.contains(MSG, Level.TRACE)).isFalse(); } - } From 0524b20228659ed5f0cc085cd7f92bceaf1414cf Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Sun, 12 Apr 2020 09:21:03 +0200 Subject: [PATCH 13/15] BAEL-3960 formatting --- testing-modules/testing-logs/pom.xml | 1 - testing-modules/testing-logs/src/main/resources/logback.xml | 2 -- testing-modules/testing-logs/src/test/resources/logback.xml | 1 - 3 files changed, 4 deletions(-) diff --git a/testing-modules/testing-logs/pom.xml b/testing-modules/testing-logs/pom.xml index 1965681ff335..d8667f16e3aa 100644 --- a/testing-modules/testing-logs/pom.xml +++ b/testing-modules/testing-logs/pom.xml @@ -18,7 +18,6 @@ logback-classic 1.2.3
- org.assertj assertj-core diff --git a/testing-modules/testing-logs/src/main/resources/logback.xml b/testing-modules/testing-logs/src/main/resources/logback.xml index deb1f12bbc9e..d485da62ff07 100644 --- a/testing-modules/testing-logs/src/main/resources/logback.xml +++ b/testing-modules/testing-logs/src/main/resources/logback.xml @@ -7,12 +7,10 @@ - - \ No newline at end of file diff --git a/testing-modules/testing-logs/src/test/resources/logback.xml b/testing-modules/testing-logs/src/test/resources/logback.xml index 2fc992fc447d..980ce897ff4b 100644 --- a/testing-modules/testing-logs/src/test/resources/logback.xml +++ b/testing-modules/testing-logs/src/test/resources/logback.xml @@ -7,7 +7,6 @@ - From c35f5fad0e17e62d4c578fc5e7bfb63e25412138 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Sat, 25 Apr 2020 20:36:44 +0200 Subject: [PATCH 14/15] BAEL-3960 module renamed --- testing-modules/{testing-logs => testing-assertions}/pom.xml | 2 +- .../src/main/java/com/baeldung/junit/log/BusinessWorker.java | 0 .../src/main/resources/logback.xml | 0 .../java/com/baeldung/junit/log/BusinessWorkerUnitTest.java | 0 .../src/test/java/com/baeldung/junit/log/MemoryAppender.java | 0 .../src/test/resources/logback.xml | 0 6 files changed, 1 insertion(+), 1 deletion(-) rename testing-modules/{testing-logs => testing-assertions}/pom.xml (95%) rename testing-modules/{testing-logs => testing-assertions}/src/main/java/com/baeldung/junit/log/BusinessWorker.java (100%) rename testing-modules/{testing-logs => testing-assertions}/src/main/resources/logback.xml (100%) rename testing-modules/{testing-logs => testing-assertions}/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java (100%) rename testing-modules/{testing-logs => testing-assertions}/src/test/java/com/baeldung/junit/log/MemoryAppender.java (100%) rename testing-modules/{testing-logs => testing-assertions}/src/test/resources/logback.xml (100%) diff --git a/testing-modules/testing-logs/pom.xml b/testing-modules/testing-assertions/pom.xml similarity index 95% rename from testing-modules/testing-logs/pom.xml rename to testing-modules/testing-assertions/pom.xml index d8667f16e3aa..0a7c4b0860c5 100644 --- a/testing-modules/testing-logs/pom.xml +++ b/testing-modules/testing-assertions/pom.xml @@ -2,7 +2,7 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 - testing-logs + testing-assertions 0.0.1-SNAPSHOT diff --git a/testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/junit/log/BusinessWorker.java similarity index 100% rename from testing-modules/testing-logs/src/main/java/com/baeldung/junit/log/BusinessWorker.java rename to testing-modules/testing-assertions/src/main/java/com/baeldung/junit/log/BusinessWorker.java diff --git a/testing-modules/testing-logs/src/main/resources/logback.xml b/testing-modules/testing-assertions/src/main/resources/logback.xml similarity index 100% rename from testing-modules/testing-logs/src/main/resources/logback.xml rename to testing-modules/testing-assertions/src/main/resources/logback.xml diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java similarity index 100% rename from testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java rename to testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/BusinessWorkerUnitTest.java diff --git a/testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/MemoryAppender.java similarity index 100% rename from testing-modules/testing-logs/src/test/java/com/baeldung/junit/log/MemoryAppender.java rename to testing-modules/testing-assertions/src/test/java/com/baeldung/junit/log/MemoryAppender.java diff --git a/testing-modules/testing-logs/src/test/resources/logback.xml b/testing-modules/testing-assertions/src/test/resources/logback.xml similarity index 100% rename from testing-modules/testing-logs/src/test/resources/logback.xml rename to testing-modules/testing-assertions/src/test/resources/logback.xml From 7272fdc4321fe6d1b8d320ae0898831189b737b9 Mon Sep 17 00:00:00 2001 From: Marc Guerrini Date: Mon, 27 Apr 2020 10:56:27 +0200 Subject: [PATCH 15/15] BAEL-3960 parent pom updated to match new module name --- testing-modules/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 1003c6f90604..b467b3c50355 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -33,7 +33,7 @@ selenium-junit-testng spring-testing test-containers - testing-logs + testing-assertions testng junit-5-basics easymock