Implement the hexagonal architecture.
Booking pallets have been sent to the application. We now want to compute the sum of all the exchanged pallets.
The two boundaries that we can already detect are the following:
- Provided use case: sum the bookings
- Needed to fulfill the request: have all the bookings
- Create
booking
package (new package for booking management) - Create
PalletBooking
class with two fieldsString partner
Long quantity
- Create the
booking.port.in
package - Create the
ForCalculatingBookingSum
interface - Add the method
Long computeSum()
- Create the
BookingManager
class in thebooking
package - Add the
implements ForCalculatingBookingSum
toBookingManager
- Implement the method with a fake body
- Create the
BookingManagerTest
- Create a first test named
sums
- Test that the method
computeSum
of theBookingManager
returns a specific value
Example:
BookingManager bookingManager = new BookingManager();
Long sum = bookingManager.computeSum();
Assertions.assertThat(sum).isEqualTo(8L);
- Create the
booking.port.out
package - Create the
ForGettingBookings
interface - Add the method
Collection<PalletBooking> getBookings();
- Go in the
BookingManager
- Create the field
private final ForGettingBookings bookingsRepository;
- Create the constructor with the parameter
- Adapt the test to make it compile
- Create a
ForGettingBookings
manual mock without Mockito (new Class in the test for instance) - Make the mock return the values needed to make the test green
- Code the rule in the
BookingManager
- Create the
booking.adapter.out
package - Create the
FileBookingRepository
that implementsForGettingBookings
- Implement the method that reads the file
- Create the csv file (here it will be located in
C:\data\bookings.csv
)
FIRST PARTNER,5
SECOND PARTNER,8
THIRD PARTNER,10
@Override
public Collection<PalletBooking> getBookings() {
return loadPalletBookings();
}
private Collection<PalletBooking> loadPalletBookings() throws RuntimeException {
try {
List<String> fileContent = Files.readAllLines(Path.of("c:\\data\\bookings.csv"));
return fileContent.stream()
.map(line -> new PalletBooking(line.split(",")[0], Long.parseLong(line.split(",")[1])))
.collect(Collectors.toList());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
- Create the
booking.adapter.in
package - Create the
BookingRestController
- Add
@RestController
and@RequestMapping("booking")
at the top of the file - Create a method that do the bookingSum
- Add
Example:
@GetMapping
public ResponseEntity<Long> sumBookings() {
return ResponseEntity.ok(forCalculatingBookingSum.computeSum());
}
- Create the constructor without parameter
- Instanciate manually the
BookingManager
- Add the
@Service
on the top of theBookingManager
- Add the
@Repository
on the top of theFileBookingRepository
- Add the
BookingManager
parameter to theBookingRestController