From c5b651fca0334a707e3cd8cd443b52ec35b0011e Mon Sep 17 00:00:00 2001 From: Swapan Pramanick Date: Fri, 9 Apr 2021 20:31:53 +0200 Subject: [PATCH] Update code to be more compact --- .../adapter/BookingPersistenceAdapter.java | 4 ++ .../adapter/RestAPIEndpointAdapter.java | 16 +++----- .../adapter/TheatreServiceAdapter.java | 17 ++++---- .../adapter/WalletServiceAdapter.java | 12 ++---- .../baeldung/hexagonal/domain/Booking.java | 41 ++++++++++--------- .../external/service/MockTheatreService.java | 8 ++-- .../external/service/TheatreService.java | 4 +- .../port/BookingPersistencePort.java | 1 + .../hexagonal/port/BookingServicePort.java | 18 ++++---- .../hexagonal/port/TheatreServicePort.java | 2 +- .../repository/BookingRepository.java | 1 + .../repository/MockBookingRepository.java | 5 +++ .../hexagonal/usecase/BookTicketUseCase.java | 34 ++++++--------- .../RestAPIEndpointAdapterUnitTest.java | 7 ++-- .../usecase/BookTicketUseCaseUnitTest.java | 41 ++++++++----------- 15 files changed, 97 insertions(+), 114 deletions(-) diff --git a/ddd/src/main/java/com/baeldung/hexagonal/adapter/BookingPersistenceAdapter.java b/ddd/src/main/java/com/baeldung/hexagonal/adapter/BookingPersistenceAdapter.java index e6d4dbbe36..1b191bde86 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/adapter/BookingPersistenceAdapter.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/adapter/BookingPersistenceAdapter.java @@ -15,4 +15,8 @@ public class BookingPersistenceAdapter implements BookingPersistencePort { public boolean persist(Booking booking) { return bookingRepository.save(booking); } + + public boolean updateStatus(String bookingId, String status) { + return bookingRepository.updateStatus(bookingId, status); + } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapter.java b/ddd/src/main/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapter.java index 7c35f302c0..3e1b1fc90f 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapter.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapter.java @@ -2,7 +2,6 @@ package com.baeldung.hexagonal.adapter; import com.baeldung.hexagonal.port.BookingServicePort; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -10,7 +9,9 @@ import org.springframework.web.bind.annotation.RestController; import static com.baeldung.hexagonal.port.BookingServicePort.BookingRequest; import static com.baeldung.hexagonal.port.BookingServicePort.BookingResponse; -import static com.baeldung.hexagonal.port.BookingServicePort.BookingResponse.*; +import static com.baeldung.hexagonal.port.BookingServicePort.BookingResponse.SUCCESS; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.FAILED_DEPENDENCY; @RestController public class RestAPIEndpointAdapter { @@ -25,14 +26,7 @@ public class RestAPIEndpointAdapter { @PostMapping(path = "/booking") public ResponseEntity createBooking(@RequestBody BookingRequest request) { BookingResponse response = bookingServicePort.book(request); - - if (response.getStatusCode() == SEAT_NOT_AVAILABLE - || response.getStatusCode() == PAYMENT_FAILED){ - return new ResponseEntity(response, HttpStatus.PRECONDITION_FAILED); - } else if (response.getStatusCode() == UNKNOWN_ERROR) { - return new ResponseEntity(response, HttpStatus.INTERNAL_SERVER_ERROR); - } - - return new ResponseEntity(response, HttpStatus.CREATED); + return new ResponseEntity<>(response, + response.getStatusCode() == SUCCESS ? CREATED : FAILED_DEPENDENCY); } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/adapter/TheatreServiceAdapter.java b/ddd/src/main/java/com/baeldung/hexagonal/adapter/TheatreServiceAdapter.java index d644f5eee0..fa19483b13 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/adapter/TheatreServiceAdapter.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/adapter/TheatreServiceAdapter.java @@ -3,29 +3,28 @@ package com.baeldung.hexagonal.adapter; import com.baeldung.hexagonal.external.service.TheatreService; import com.baeldung.hexagonal.port.TheatreServicePort; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import java.util.Optional; import java.util.Set; +import static com.baeldung.hexagonal.external.service.TheatreService.Reservation; +import static org.springframework.http.HttpStatus.CREATED; +import static org.springframework.http.HttpStatus.NO_CONTENT; + @Component public class TheatreServiceAdapter implements TheatreServicePort { @Autowired private TheatreService theatreService; - public Optional reserveSeats(String theatreId, String movieShowId, Set seats) { - ResponseEntity response = theatreService.postReservation(theatreId, movieShowId, seats); - if (response.getStatusCode() == HttpStatus.CREATED) { - return Optional.of(response.getBody().getId()); - } - return Optional.empty(); + public Optional reserveSeats(String movieShowId, Set seats) { + ResponseEntity response = theatreService.postReservation(movieShowId, seats); + return response.getStatusCode() == CREATED ? Optional.of(response.getBody().getId()): Optional.empty(); } public boolean releaseSeats(String resrevationId) { - ResponseEntity response = theatreService.deleteReservation(resrevationId); - return response.getStatusCode() == HttpStatus.NO_CONTENT; + return theatreService.deleteReservation(resrevationId).getStatusCode() == NO_CONTENT; } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/adapter/WalletServiceAdapter.java b/ddd/src/main/java/com/baeldung/hexagonal/adapter/WalletServiceAdapter.java index 930b9c1cf6..1d8db867b7 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/adapter/WalletServiceAdapter.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/adapter/WalletServiceAdapter.java @@ -3,21 +3,17 @@ package com.baeldung.hexagonal.adapter; import com.baeldung.hexagonal.external.service.CustomerWalletService; import com.baeldung.hexagonal.port.WalletServicePort; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; +import static org.springframework.http.HttpStatus.CREATED; + @Component public class WalletServiceAdapter implements WalletServicePort { - private final CustomerWalletService customerWalletService; - @Autowired - public WalletServiceAdapter(CustomerWalletService customerWalletService) { - this.customerWalletService = customerWalletService; - } + private CustomerWalletService customerWalletService; public boolean debit(String customerId, Double amount) { - HttpStatus response = customerWalletService.postDebit(customerId, amount); - return response == HttpStatus.CREATED; + return customerWalletService.postDebit(customerId, amount) == CREATED; } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/domain/Booking.java b/ddd/src/main/java/com/baeldung/hexagonal/domain/Booking.java index 34e3f3b81b..ed9b7281f2 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/domain/Booking.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/domain/Booking.java @@ -1,39 +1,46 @@ package com.baeldung.hexagonal.domain; +import com.baeldung.hexagonal.port.BookingServicePort; + import java.util.Set; +import java.util.UUID; public class Booking { + + public static final String STATUS_INITIAL = "INITIAL"; + public static final String STATUS_SUCCESS = "SUCCESS"; + public static final String STATUS_FAILURE = "FAILED"; + private String bookingId; private String movieShowId; - private String theatreId; private String customerId; private Set seats; private Double amount; - private Status status; - - public enum Status { - INITIAL, SUCCESS, FAILURE - } + private String status; public Booking( - String bookingId, String movieShowId, String theatreId, String customerId, Set seats, Double amount, Status status) { + String bookingId, String movieShowId, String customerId, Set seats, Double amount, String status) { this.bookingId = bookingId; this.movieShowId = movieShowId; - this.theatreId = theatreId; this.customerId = customerId; this.seats = seats; this.amount = amount; - this.status = Status.INITIAL; + this.status = status; + } + + public Booking(BookingServicePort.BookingRequest request) { + this.bookingId = UUID.randomUUID().toString(); + this.movieShowId = request.getMovieShowId(); + this.customerId = request.getCustomerId(); + this.seats = request.getSeats(); + this.amount = request.getAmount(); + this.status = STATUS_INITIAL; } public String getMovieShowId() { return movieShowId; } - public String getTheatreId() { - return theatreId; - } - public Set getSeats() { return seats; } @@ -50,7 +57,7 @@ public class Booking { return amount; } - public Status getStatus() { + public String getStatus() { return status; } @@ -62,10 +69,6 @@ public class Booking { this.movieShowId = movieShowId; } - public void setTheatreId(String theatreId) { - this.theatreId = theatreId; - } - public void setCustomerId(String customerId) { this.customerId = customerId; } @@ -78,7 +81,7 @@ public class Booking { this.amount = amount; } - public void setStatus(Status status) { + public void setStatus(String status) { this.status = status; } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/external/service/MockTheatreService.java b/ddd/src/main/java/com/baeldung/hexagonal/external/service/MockTheatreService.java index 333cbee889..eb018e69b2 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/external/service/MockTheatreService.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/external/service/MockTheatreService.java @@ -10,12 +10,12 @@ import java.util.UUID; @Service public class MockTheatreService implements TheatreService { - public ResponseEntity postReservation(String theatreId, String movieShowId, Set seats) { - return new ResponseEntity( + public ResponseEntity postReservation(String movieShowId, Set seats) { + return new ResponseEntity<>( new Reservation(UUID.randomUUID().toString()), HttpStatus.CREATED); } - public ResponseEntity deleteReservation(String reservationId) { - return new ResponseEntity(HttpStatus.NO_CONTENT); + public ResponseEntity deleteReservation(String reservationId) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/external/service/TheatreService.java b/ddd/src/main/java/com/baeldung/hexagonal/external/service/TheatreService.java index 48f0c5e15d..8107bfb418 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/external/service/TheatreService.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/external/service/TheatreService.java @@ -6,9 +6,9 @@ import java.util.Set; public interface TheatreService { - ResponseEntity postReservation(String theatreId, String movieShowId, Set seats); + ResponseEntity postReservation(String movieShowId, Set seats); - ResponseEntity deleteReservation(String reservationId); + ResponseEntity deleteReservation(String reservationId); class Reservation { private final String id; diff --git a/ddd/src/main/java/com/baeldung/hexagonal/port/BookingPersistencePort.java b/ddd/src/main/java/com/baeldung/hexagonal/port/BookingPersistencePort.java index 0deffb42e0..c1d1d73630 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/port/BookingPersistencePort.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/port/BookingPersistencePort.java @@ -4,4 +4,5 @@ import com.baeldung.hexagonal.domain.Booking; public interface BookingPersistencePort { boolean persist(Booking booking); + public boolean updateStatus(String bookingId, String status); } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/port/BookingServicePort.java b/ddd/src/main/java/com/baeldung/hexagonal/port/BookingServicePort.java index 3b444df4f6..12710a6014 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/port/BookingServicePort.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/port/BookingServicePort.java @@ -9,7 +9,6 @@ public interface BookingServicePort { class BookingRequest { private String movieShowId; private String customerId; - private String theatreId; private Set seats; private Double amount; @@ -29,14 +28,6 @@ public interface BookingServicePort { this.customerId = customerId; } - public String getTheatreId() { - return theatreId; - } - - public void setTheatreId(String theatreId) { - this.theatreId = theatreId; - } - public Set getSeats() { return seats; } @@ -61,6 +52,12 @@ public interface BookingServicePort { public static final int UNKNOWN_ERROR = 3; private final int statusCode; + private String bookingId; + + public BookingResponse(String bookingId, int statusCode) { + this.bookingId = bookingId; + this.statusCode = statusCode; + } public BookingResponse(int statusCode) { this.statusCode = statusCode; @@ -69,5 +66,8 @@ public interface BookingServicePort { public int getStatusCode() { return statusCode; } + public String getBookingId() { + return bookingId; + } } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/port/TheatreServicePort.java b/ddd/src/main/java/com/baeldung/hexagonal/port/TheatreServicePort.java index dac517c724..8e5fab8b46 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/port/TheatreServicePort.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/port/TheatreServicePort.java @@ -4,6 +4,6 @@ import java.util.Optional; import java.util.Set; public interface TheatreServicePort { - Optional reserveSeats(String theatreId, String movieShowId, Set seats); + Optional reserveSeats(String movieShowId, Set seats); boolean releaseSeats(String resrevationId); } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/repository/BookingRepository.java b/ddd/src/main/java/com/baeldung/hexagonal/repository/BookingRepository.java index c56ffe1308..7dd20290ba 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/repository/BookingRepository.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/repository/BookingRepository.java @@ -5,4 +5,5 @@ import com.baeldung.hexagonal.domain.Booking; public interface BookingRepository { boolean save(Booking booking); + boolean updateStatus(String bookingId, String status); } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/repository/MockBookingRepository.java b/ddd/src/main/java/com/baeldung/hexagonal/repository/MockBookingRepository.java index d378cff0af..1738e606d0 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/repository/MockBookingRepository.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/repository/MockBookingRepository.java @@ -8,4 +8,9 @@ public class MockBookingRepository implements BookingRepository { public boolean save(Booking booking) { return true; } + + @Override + public boolean updateStatus(String bookingId, String status) { + return true; + } } diff --git a/ddd/src/main/java/com/baeldung/hexagonal/usecase/BookTicketUseCase.java b/ddd/src/main/java/com/baeldung/hexagonal/usecase/BookTicketUseCase.java index 7d9db9724e..8d3112bd37 100644 --- a/ddd/src/main/java/com/baeldung/hexagonal/usecase/BookTicketUseCase.java +++ b/ddd/src/main/java/com/baeldung/hexagonal/usecase/BookTicketUseCase.java @@ -9,8 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.Optional; -import java.util.UUID; +import static com.baeldung.hexagonal.domain.Booking.STATUS_FAILURE; +import static com.baeldung.hexagonal.domain.Booking.STATUS_SUCCESS; import static com.baeldung.hexagonal.port.BookingServicePort.BookingResponse.*; @Component @@ -29,39 +30,28 @@ public class BookTicketUseCase implements BookingServicePort { public BookingResponse book(BookingRequest request) { - Booking booking = new Booking( - UUID.randomUUID().toString(), - request.getMovieShowId(), - request.getTheatreId(), - request.getCustomerId(), - request.getSeats(), - request.getAmount(), - Booking.Status.INITIAL); + Booking booking = new Booking(request); if (!bookingPersistencePort.persist(booking)) { return new BookingResponse(UNKNOWN_ERROR); } - Optional reservationIdOptional = theatreServicePort.reserveSeats( - booking.getTheatreId(), - booking.getMovieShowId(), - booking.getSeats()); + String bookingId = booking.getBookingId(); + + Optional reservationIdOptional = theatreServicePort.reserveSeats(booking.getMovieShowId(), booking.getSeats()); if (!reservationIdOptional.isPresent()) { - booking.setStatus(Booking.Status.FAILURE); - bookingPersistencePort.persist(booking); - return new BookingResponse(SEAT_NOT_AVAILABLE); + bookingPersistencePort.updateStatus(bookingId, STATUS_FAILURE); + return new BookingResponse(bookingId, SEAT_NOT_AVAILABLE); } if (!walletServicePort.debit(booking.getCustomerId(), booking.getAmount())) { reservationIdOptional.ifPresent(reservationId -> theatreServicePort.releaseSeats(reservationId)); - booking.setStatus(Booking.Status.FAILURE); - bookingPersistencePort.persist(booking); - return new BookingResponse(PAYMENT_FAILED); + bookingPersistencePort.updateStatus(bookingId, STATUS_FAILURE); + return new BookingResponse(bookingId, PAYMENT_FAILED); } - booking.setStatus(Booking.Status.SUCCESS); - bookingPersistencePort.persist(booking); + bookingPersistencePort.updateStatus(bookingId, STATUS_SUCCESS); - return new BookingResponse(SUCCESS); + return new BookingResponse(bookingId, SUCCESS); } } diff --git a/ddd/src/test/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapterUnitTest.java b/ddd/src/test/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapterUnitTest.java index 76d853fdda..cb22f5f37a 100644 --- a/ddd/src/test/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapterUnitTest.java +++ b/ddd/src/test/java/com/baeldung/hexagonal/adapter/RestAPIEndpointAdapterUnitTest.java @@ -30,7 +30,6 @@ class RestAPIEndpointAdapterUnitTest { private BookingServicePort.BookingRequest getBookingRequest() { BookingServicePort.BookingRequest request = new BookingServicePort.BookingRequest(); - request.setTheatreId("theatre-id"); request.setMovieShowId("movie-show-id"); request.setCustomerId("customer-id"); request.setSeats(new HashSet<>(Arrays.asList("A1", "A2"))); @@ -47,7 +46,7 @@ class RestAPIEndpointAdapterUnitTest { verify(bookingServicePort).book(any(BookingRequest.class)); assertNotNull(response); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); + assertEquals(HttpStatus.FAILED_DEPENDENCY, response.getStatusCode()); } @Test @@ -59,7 +58,7 @@ class RestAPIEndpointAdapterUnitTest { verify(bookingServicePort).book(any(BookingRequest.class)); assertNotNull(response); - assertEquals(HttpStatus.PRECONDITION_FAILED, response.getStatusCode()); + assertEquals(HttpStatus.FAILED_DEPENDENCY, response.getStatusCode()); } @Test @@ -71,7 +70,7 @@ class RestAPIEndpointAdapterUnitTest { verify(bookingServicePort).book(any(BookingRequest.class)); assertNotNull(response); - assertEquals(HttpStatus.PRECONDITION_FAILED, response.getStatusCode()); + assertEquals(HttpStatus.FAILED_DEPENDENCY, response.getStatusCode()); } @Test diff --git a/ddd/src/test/java/com/baeldung/hexagonal/usecase/BookTicketUseCaseUnitTest.java b/ddd/src/test/java/com/baeldung/hexagonal/usecase/BookTicketUseCaseUnitTest.java index eec2b65609..ffb4246f79 100644 --- a/ddd/src/test/java/com/baeldung/hexagonal/usecase/BookTicketUseCaseUnitTest.java +++ b/ddd/src/test/java/com/baeldung/hexagonal/usecase/BookTicketUseCaseUnitTest.java @@ -34,7 +34,6 @@ class BookTicketUseCaseUnitTest { private BookingServicePort.BookingRequest getBookingRequest() { BookingServicePort.BookingRequest request = new BookingServicePort.BookingRequest(); - request.setTheatreId("theatre-id"); request.setMovieShowId("movie-show-id"); request.setCustomerId("customer-id"); request.setSeats(new HashSet<>(Arrays.asList("A1", "A2"))); @@ -42,21 +41,10 @@ class BookTicketUseCaseUnitTest { return request; } - private Booking getBooking(BookingServicePort.BookingRequest request) { - return new Booking( - "booking-id", - request.getMovieShowId(), - request.getTheatreId(), - request.getCustomerId(), - request.getSeats(), - request.getAmount(), - Booking.Status.INITIAL); - } - @Test void whenErrorInInitialPersistence_thenReturnUnknownError() { BookingServicePort.BookingRequest request = getBookingRequest(); - Booking booking = getBooking(request); + Booking booking = new Booking(request); when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(false); BookingServicePort.BookingResponse response = bookTicketUseCase.book(request); @@ -68,14 +56,15 @@ class BookTicketUseCaseUnitTest { @Test void whenErrorInReserveSeats_thenReturnSeatNotAvailable() { BookingServicePort.BookingRequest request = getBookingRequest(); - Booking booking = getBooking(request); + Booking booking = new Booking(request); when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(true); - when(theatreServicePort.reserveSeats(booking.getTheatreId(), booking.getMovieShowId(), booking.getSeats())) + when(theatreServicePort.reserveSeats(booking.getMovieShowId(), booking.getSeats())) .thenReturn(Optional.empty()); BookingServicePort.BookingResponse response = bookTicketUseCase.book(request); - verify(bookingPersistencePort, times(2)).persist(any(Booking.class)); - verify(theatreServicePort).reserveSeats(any(String.class), any(String.class), any(HashSet.class)); + verify(bookingPersistencePort).persist(any(Booking.class)); + verify(bookingPersistencePort).updateStatus(any(String.class), any(String.class)); + verify(theatreServicePort).reserveSeats(any(String.class), any(HashSet.class)); assertNotNull(response); assertEquals(BookingServicePort.BookingResponse.SEAT_NOT_AVAILABLE, response.getStatusCode()); } @@ -83,16 +72,17 @@ class BookTicketUseCaseUnitTest { @Test void whenErrorInWalletDebit_thenReturnPaymentFailed() { BookingServicePort.BookingRequest request = getBookingRequest(); - Booking booking = getBooking(request); + Booking booking = new Booking(request); when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(true); - when(theatreServicePort.reserveSeats(booking.getTheatreId(), booking.getMovieShowId(), booking.getSeats())) + when(theatreServicePort.reserveSeats(booking.getMovieShowId(), booking.getSeats())) .thenReturn(Optional.of("reservation-id")); when(walletServicePort.debit(booking.getCustomerId(), booking.getAmount())) .thenReturn(false); BookingServicePort.BookingResponse response = bookTicketUseCase.book(request); - verify(bookingPersistencePort, times(2)).persist(any(Booking.class)); - verify(theatreServicePort).reserveSeats(any(String.class), any(String.class), any(HashSet.class)); + verify(bookingPersistencePort).persist(any(Booking.class)); + verify(bookingPersistencePort).updateStatus(any(String.class), any(String.class)); + verify(theatreServicePort).reserveSeats(any(String.class), any(HashSet.class)); verify(walletServicePort).debit(any(String.class), any(Double.class)); verify(theatreServicePort).releaseSeats(any(String.class)); assertNotNull(response); @@ -102,16 +92,17 @@ class BookTicketUseCaseUnitTest { @Test void whenNoErrorInAnyPorts_thenReturnSuccess() { BookingServicePort.BookingRequest request = getBookingRequest(); - Booking booking = getBooking(request); + Booking booking = new Booking(request); when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(true); - when(theatreServicePort.reserveSeats(booking.getTheatreId(), booking.getMovieShowId(), booking.getSeats())) + when(theatreServicePort.reserveSeats(booking.getMovieShowId(), booking.getSeats())) .thenReturn(Optional.of("reservation-id")); when(walletServicePort.debit(booking.getCustomerId(), booking.getAmount())) .thenReturn(true); BookingServicePort.BookingResponse response = bookTicketUseCase.book(request); - verify(bookingPersistencePort, times(2)).persist(any(Booking.class)); - verify(theatreServicePort).reserveSeats(any(String.class), any(String.class), any(HashSet.class)); + verify(bookingPersistencePort).persist(any(Booking.class)); + verify(bookingPersistencePort).updateStatus(any(String.class), any(String.class)); + verify(theatreServicePort).reserveSeats( any(String.class), any(HashSet.class)); verify(walletServicePort).debit(any(String.class), any(Double.class)); assertNotNull(response); assertEquals(BookingServicePort.BookingResponse.SUCCESS, response.getStatusCode());