Update code to be more compact
This commit is contained in:
parent
ac30817a23
commit
c5b651fca0
@ -15,4 +15,8 @@ public class BookingPersistenceAdapter implements BookingPersistencePort {
|
|||||||
public boolean persist(Booking booking) {
|
public boolean persist(Booking booking) {
|
||||||
return bookingRepository.save(booking);
|
return bookingRepository.save(booking);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean updateStatus(String bookingId, String status) {
|
||||||
|
return bookingRepository.updateStatus(bookingId, status);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package com.baeldung.hexagonal.adapter;
|
|||||||
|
|
||||||
import com.baeldung.hexagonal.port.BookingServicePort;
|
import com.baeldung.hexagonal.port.BookingServicePort;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
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.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.*;
|
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
|
@RestController
|
||||||
public class RestAPIEndpointAdapter {
|
public class RestAPIEndpointAdapter {
|
||||||
@ -25,14 +26,7 @@ public class RestAPIEndpointAdapter {
|
|||||||
@PostMapping(path = "/booking")
|
@PostMapping(path = "/booking")
|
||||||
public ResponseEntity<BookingResponse> createBooking(@RequestBody BookingRequest request) {
|
public ResponseEntity<BookingResponse> createBooking(@RequestBody BookingRequest request) {
|
||||||
BookingResponse response = bookingServicePort.book(request);
|
BookingResponse response = bookingServicePort.book(request);
|
||||||
|
return new ResponseEntity<>(response,
|
||||||
if (response.getStatusCode() == SEAT_NOT_AVAILABLE
|
response.getStatusCode() == SUCCESS ? CREATED : FAILED_DEPENDENCY);
|
||||||
|| response.getStatusCode() == PAYMENT_FAILED){
|
|
||||||
return new ResponseEntity<BookingResponse>(response, HttpStatus.PRECONDITION_FAILED);
|
|
||||||
} else if (response.getStatusCode() == UNKNOWN_ERROR) {
|
|
||||||
return new ResponseEntity<BookingResponse>(response, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new ResponseEntity<BookingResponse>(response, HttpStatus.CREATED);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,29 +3,28 @@ package com.baeldung.hexagonal.adapter;
|
|||||||
import com.baeldung.hexagonal.external.service.TheatreService;
|
import com.baeldung.hexagonal.external.service.TheatreService;
|
||||||
import com.baeldung.hexagonal.port.TheatreServicePort;
|
import com.baeldung.hexagonal.port.TheatreServicePort;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Set;
|
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
|
@Component
|
||||||
public class TheatreServiceAdapter implements TheatreServicePort {
|
public class TheatreServiceAdapter implements TheatreServicePort {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TheatreService theatreService;
|
private TheatreService theatreService;
|
||||||
|
|
||||||
public Optional<String> reserveSeats(String theatreId, String movieShowId, Set<String> seats) {
|
public Optional<String> reserveSeats(String movieShowId, Set<String> seats) {
|
||||||
ResponseEntity<TheatreService.Reservation> response = theatreService.postReservation(theatreId, movieShowId, seats);
|
ResponseEntity<Reservation> response = theatreService.postReservation(movieShowId, seats);
|
||||||
if (response.getStatusCode() == HttpStatus.CREATED) {
|
return response.getStatusCode() == CREATED ? Optional.of(response.getBody().getId()): Optional.empty();
|
||||||
return Optional.of(response.getBody().getId());
|
|
||||||
}
|
|
||||||
return Optional.empty();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean releaseSeats(String resrevationId) {
|
public boolean releaseSeats(String resrevationId) {
|
||||||
ResponseEntity<?> response = theatreService.deleteReservation(resrevationId);
|
return theatreService.deleteReservation(resrevationId).getStatusCode() == NO_CONTENT;
|
||||||
return response.getStatusCode() == HttpStatus.NO_CONTENT;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,21 +3,17 @@ package com.baeldung.hexagonal.adapter;
|
|||||||
import com.baeldung.hexagonal.external.service.CustomerWalletService;
|
import com.baeldung.hexagonal.external.service.CustomerWalletService;
|
||||||
import com.baeldung.hexagonal.port.WalletServicePort;
|
import com.baeldung.hexagonal.port.WalletServicePort;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpStatus;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import static org.springframework.http.HttpStatus.CREATED;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class WalletServiceAdapter implements WalletServicePort {
|
public class WalletServiceAdapter implements WalletServicePort {
|
||||||
|
|
||||||
private final CustomerWalletService customerWalletService;
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public WalletServiceAdapter(CustomerWalletService customerWalletService) {
|
private CustomerWalletService customerWalletService;
|
||||||
this.customerWalletService = customerWalletService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean debit(String customerId, Double amount) {
|
public boolean debit(String customerId, Double amount) {
|
||||||
HttpStatus response = customerWalletService.postDebit(customerId, amount);
|
return customerWalletService.postDebit(customerId, amount) == CREATED;
|
||||||
return response == HttpStatus.CREATED;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,39 +1,46 @@
|
|||||||
package com.baeldung.hexagonal.domain;
|
package com.baeldung.hexagonal.domain;
|
||||||
|
|
||||||
|
import com.baeldung.hexagonal.port.BookingServicePort;
|
||||||
|
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
public class Booking {
|
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 bookingId;
|
||||||
private String movieShowId;
|
private String movieShowId;
|
||||||
private String theatreId;
|
|
||||||
private String customerId;
|
private String customerId;
|
||||||
private Set<String> seats;
|
private Set<String> seats;
|
||||||
private Double amount;
|
private Double amount;
|
||||||
private Status status;
|
private String status;
|
||||||
|
|
||||||
public enum Status {
|
|
||||||
INITIAL, SUCCESS, FAILURE
|
|
||||||
}
|
|
||||||
|
|
||||||
public Booking(
|
public Booking(
|
||||||
String bookingId, String movieShowId, String theatreId, String customerId, Set<String> seats, Double amount, Status status) {
|
String bookingId, String movieShowId, String customerId, Set<String> seats, Double amount, String status) {
|
||||||
this.bookingId = bookingId;
|
this.bookingId = bookingId;
|
||||||
this.movieShowId = movieShowId;
|
this.movieShowId = movieShowId;
|
||||||
this.theatreId = theatreId;
|
|
||||||
this.customerId = customerId;
|
this.customerId = customerId;
|
||||||
this.seats = seats;
|
this.seats = seats;
|
||||||
this.amount = amount;
|
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() {
|
public String getMovieShowId() {
|
||||||
return movieShowId;
|
return movieShowId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTheatreId() {
|
|
||||||
return theatreId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getSeats() {
|
public Set<String> getSeats() {
|
||||||
return seats;
|
return seats;
|
||||||
}
|
}
|
||||||
@ -50,7 +57,7 @@ public class Booking {
|
|||||||
return amount;
|
return amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Status getStatus() {
|
public String getStatus() {
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,10 +69,6 @@ public class Booking {
|
|||||||
this.movieShowId = movieShowId;
|
this.movieShowId = movieShowId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setTheatreId(String theatreId) {
|
|
||||||
this.theatreId = theatreId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setCustomerId(String customerId) {
|
public void setCustomerId(String customerId) {
|
||||||
this.customerId = customerId;
|
this.customerId = customerId;
|
||||||
}
|
}
|
||||||
@ -78,7 +81,7 @@ public class Booking {
|
|||||||
this.amount = amount;
|
this.amount = amount;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setStatus(Status status) {
|
public void setStatus(String status) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,12 +10,12 @@ import java.util.UUID;
|
|||||||
@Service
|
@Service
|
||||||
public class MockTheatreService implements TheatreService {
|
public class MockTheatreService implements TheatreService {
|
||||||
|
|
||||||
public ResponseEntity<Reservation> postReservation(String theatreId, String movieShowId, Set<String> seats) {
|
public ResponseEntity<Reservation> postReservation(String movieShowId, Set<String> seats) {
|
||||||
return new ResponseEntity<Reservation>(
|
return new ResponseEntity<>(
|
||||||
new Reservation(UUID.randomUUID().toString()), HttpStatus.CREATED);
|
new Reservation(UUID.randomUUID().toString()), HttpStatus.CREATED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResponseEntity<?> deleteReservation(String reservationId) {
|
public ResponseEntity<Void> deleteReservation(String reservationId) {
|
||||||
return new ResponseEntity(HttpStatus.NO_CONTENT);
|
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,9 +6,9 @@ import java.util.Set;
|
|||||||
|
|
||||||
public interface TheatreService {
|
public interface TheatreService {
|
||||||
|
|
||||||
ResponseEntity<Reservation> postReservation(String theatreId, String movieShowId, Set<String> seats);
|
ResponseEntity<Reservation> postReservation(String movieShowId, Set<String> seats);
|
||||||
|
|
||||||
ResponseEntity<?> deleteReservation(String reservationId);
|
ResponseEntity<Void> deleteReservation(String reservationId);
|
||||||
|
|
||||||
class Reservation {
|
class Reservation {
|
||||||
private final String id;
|
private final String id;
|
||||||
|
@ -4,4 +4,5 @@ import com.baeldung.hexagonal.domain.Booking;
|
|||||||
|
|
||||||
public interface BookingPersistencePort {
|
public interface BookingPersistencePort {
|
||||||
boolean persist(Booking booking);
|
boolean persist(Booking booking);
|
||||||
|
public boolean updateStatus(String bookingId, String status);
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,6 @@ public interface BookingServicePort {
|
|||||||
class BookingRequest {
|
class BookingRequest {
|
||||||
private String movieShowId;
|
private String movieShowId;
|
||||||
private String customerId;
|
private String customerId;
|
||||||
private String theatreId;
|
|
||||||
private Set<String> seats;
|
private Set<String> seats;
|
||||||
private Double amount;
|
private Double amount;
|
||||||
|
|
||||||
@ -29,14 +28,6 @@ public interface BookingServicePort {
|
|||||||
this.customerId = customerId;
|
this.customerId = customerId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getTheatreId() {
|
|
||||||
return theatreId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTheatreId(String theatreId) {
|
|
||||||
this.theatreId = theatreId;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Set<String> getSeats() {
|
public Set<String> getSeats() {
|
||||||
return seats;
|
return seats;
|
||||||
}
|
}
|
||||||
@ -61,6 +52,12 @@ public interface BookingServicePort {
|
|||||||
public static final int UNKNOWN_ERROR = 3;
|
public static final int UNKNOWN_ERROR = 3;
|
||||||
|
|
||||||
private final int statusCode;
|
private final int statusCode;
|
||||||
|
private String bookingId;
|
||||||
|
|
||||||
|
public BookingResponse(String bookingId, int statusCode) {
|
||||||
|
this.bookingId = bookingId;
|
||||||
|
this.statusCode = statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
public BookingResponse(int statusCode) {
|
public BookingResponse(int statusCode) {
|
||||||
this.statusCode = statusCode;
|
this.statusCode = statusCode;
|
||||||
@ -69,5 +66,8 @@ public interface BookingServicePort {
|
|||||||
public int getStatusCode() {
|
public int getStatusCode() {
|
||||||
return statusCode;
|
return statusCode;
|
||||||
}
|
}
|
||||||
|
public String getBookingId() {
|
||||||
|
return bookingId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,6 @@ import java.util.Optional;
|
|||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
public interface TheatreServicePort {
|
public interface TheatreServicePort {
|
||||||
Optional<String> reserveSeats(String theatreId, String movieShowId, Set<String> seats);
|
Optional<String> reserveSeats(String movieShowId, Set<String> seats);
|
||||||
boolean releaseSeats(String resrevationId);
|
boolean releaseSeats(String resrevationId);
|
||||||
}
|
}
|
||||||
|
@ -5,4 +5,5 @@ import com.baeldung.hexagonal.domain.Booking;
|
|||||||
public interface BookingRepository {
|
public interface BookingRepository {
|
||||||
|
|
||||||
boolean save(Booking booking);
|
boolean save(Booking booking);
|
||||||
|
boolean updateStatus(String bookingId, String status);
|
||||||
}
|
}
|
||||||
|
@ -8,4 +8,9 @@ public class MockBookingRepository implements BookingRepository {
|
|||||||
public boolean save(Booking booking) {
|
public boolean save(Booking booking) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean updateStatus(String bookingId, String status) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,8 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.Optional;
|
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.*;
|
import static com.baeldung.hexagonal.port.BookingServicePort.BookingResponse.*;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@ -29,39 +30,28 @@ public class BookTicketUseCase implements BookingServicePort {
|
|||||||
|
|
||||||
public BookingResponse book(BookingRequest request) {
|
public BookingResponse book(BookingRequest request) {
|
||||||
|
|
||||||
Booking booking = new Booking(
|
Booking booking = new Booking(request);
|
||||||
UUID.randomUUID().toString(),
|
|
||||||
request.getMovieShowId(),
|
|
||||||
request.getTheatreId(),
|
|
||||||
request.getCustomerId(),
|
|
||||||
request.getSeats(),
|
|
||||||
request.getAmount(),
|
|
||||||
Booking.Status.INITIAL);
|
|
||||||
|
|
||||||
if (!bookingPersistencePort.persist(booking)) {
|
if (!bookingPersistencePort.persist(booking)) {
|
||||||
return new BookingResponse(UNKNOWN_ERROR);
|
return new BookingResponse(UNKNOWN_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
Optional<String> reservationIdOptional = theatreServicePort.reserveSeats(
|
String bookingId = booking.getBookingId();
|
||||||
booking.getTheatreId(),
|
|
||||||
booking.getMovieShowId(),
|
Optional<String> reservationIdOptional = theatreServicePort.reserveSeats(booking.getMovieShowId(), booking.getSeats());
|
||||||
booking.getSeats());
|
|
||||||
if (!reservationIdOptional.isPresent()) {
|
if (!reservationIdOptional.isPresent()) {
|
||||||
booking.setStatus(Booking.Status.FAILURE);
|
bookingPersistencePort.updateStatus(bookingId, STATUS_FAILURE);
|
||||||
bookingPersistencePort.persist(booking);
|
return new BookingResponse(bookingId, SEAT_NOT_AVAILABLE);
|
||||||
return new BookingResponse(SEAT_NOT_AVAILABLE);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!walletServicePort.debit(booking.getCustomerId(), booking.getAmount())) {
|
if (!walletServicePort.debit(booking.getCustomerId(), booking.getAmount())) {
|
||||||
reservationIdOptional.ifPresent(reservationId -> theatreServicePort.releaseSeats(reservationId));
|
reservationIdOptional.ifPresent(reservationId -> theatreServicePort.releaseSeats(reservationId));
|
||||||
booking.setStatus(Booking.Status.FAILURE);
|
bookingPersistencePort.updateStatus(bookingId, STATUS_FAILURE);
|
||||||
bookingPersistencePort.persist(booking);
|
return new BookingResponse(bookingId, PAYMENT_FAILED);
|
||||||
return new BookingResponse(PAYMENT_FAILED);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
booking.setStatus(Booking.Status.SUCCESS);
|
bookingPersistencePort.updateStatus(bookingId, STATUS_SUCCESS);
|
||||||
bookingPersistencePort.persist(booking);
|
|
||||||
|
|
||||||
return new BookingResponse(SUCCESS);
|
return new BookingResponse(bookingId, SUCCESS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,7 +30,6 @@ class RestAPIEndpointAdapterUnitTest {
|
|||||||
|
|
||||||
private BookingServicePort.BookingRequest getBookingRequest() {
|
private BookingServicePort.BookingRequest getBookingRequest() {
|
||||||
BookingServicePort.BookingRequest request = new BookingServicePort.BookingRequest();
|
BookingServicePort.BookingRequest request = new BookingServicePort.BookingRequest();
|
||||||
request.setTheatreId("theatre-id");
|
|
||||||
request.setMovieShowId("movie-show-id");
|
request.setMovieShowId("movie-show-id");
|
||||||
request.setCustomerId("customer-id");
|
request.setCustomerId("customer-id");
|
||||||
request.setSeats(new HashSet<>(Arrays.asList("A1", "A2")));
|
request.setSeats(new HashSet<>(Arrays.asList("A1", "A2")));
|
||||||
@ -47,7 +46,7 @@ class RestAPIEndpointAdapterUnitTest {
|
|||||||
|
|
||||||
verify(bookingServicePort).book(any(BookingRequest.class));
|
verify(bookingServicePort).book(any(BookingRequest.class));
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode());
|
assertEquals(HttpStatus.FAILED_DEPENDENCY, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -59,7 +58,7 @@ class RestAPIEndpointAdapterUnitTest {
|
|||||||
|
|
||||||
verify(bookingServicePort).book(any(BookingRequest.class));
|
verify(bookingServicePort).book(any(BookingRequest.class));
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
assertEquals(HttpStatus.PRECONDITION_FAILED, response.getStatusCode());
|
assertEquals(HttpStatus.FAILED_DEPENDENCY, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -71,7 +70,7 @@ class RestAPIEndpointAdapterUnitTest {
|
|||||||
|
|
||||||
verify(bookingServicePort).book(any(BookingRequest.class));
|
verify(bookingServicePort).book(any(BookingRequest.class));
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
assertEquals(HttpStatus.PRECONDITION_FAILED, response.getStatusCode());
|
assertEquals(HttpStatus.FAILED_DEPENDENCY, response.getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -34,7 +34,6 @@ class BookTicketUseCaseUnitTest {
|
|||||||
|
|
||||||
private BookingServicePort.BookingRequest getBookingRequest() {
|
private BookingServicePort.BookingRequest getBookingRequest() {
|
||||||
BookingServicePort.BookingRequest request = new BookingServicePort.BookingRequest();
|
BookingServicePort.BookingRequest request = new BookingServicePort.BookingRequest();
|
||||||
request.setTheatreId("theatre-id");
|
|
||||||
request.setMovieShowId("movie-show-id");
|
request.setMovieShowId("movie-show-id");
|
||||||
request.setCustomerId("customer-id");
|
request.setCustomerId("customer-id");
|
||||||
request.setSeats(new HashSet<>(Arrays.asList("A1", "A2")));
|
request.setSeats(new HashSet<>(Arrays.asList("A1", "A2")));
|
||||||
@ -42,21 +41,10 @@ class BookTicketUseCaseUnitTest {
|
|||||||
return request;
|
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
|
@Test
|
||||||
void whenErrorInInitialPersistence_thenReturnUnknownError() {
|
void whenErrorInInitialPersistence_thenReturnUnknownError() {
|
||||||
BookingServicePort.BookingRequest request = getBookingRequest();
|
BookingServicePort.BookingRequest request = getBookingRequest();
|
||||||
Booking booking = getBooking(request);
|
Booking booking = new Booking(request);
|
||||||
when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(false);
|
when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(false);
|
||||||
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
||||||
|
|
||||||
@ -68,14 +56,15 @@ class BookTicketUseCaseUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
void whenErrorInReserveSeats_thenReturnSeatNotAvailable() {
|
void whenErrorInReserveSeats_thenReturnSeatNotAvailable() {
|
||||||
BookingServicePort.BookingRequest request = getBookingRequest();
|
BookingServicePort.BookingRequest request = getBookingRequest();
|
||||||
Booking booking = getBooking(request);
|
Booking booking = new Booking(request);
|
||||||
when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(true);
|
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());
|
.thenReturn(Optional.empty());
|
||||||
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
||||||
|
|
||||||
verify(bookingPersistencePort, times(2)).persist(any(Booking.class));
|
verify(bookingPersistencePort).persist(any(Booking.class));
|
||||||
verify(theatreServicePort).reserveSeats(any(String.class), any(String.class), any(HashSet.class));
|
verify(bookingPersistencePort).updateStatus(any(String.class), any(String.class));
|
||||||
|
verify(theatreServicePort).reserveSeats(any(String.class), any(HashSet.class));
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
assertEquals(BookingServicePort.BookingResponse.SEAT_NOT_AVAILABLE, response.getStatusCode());
|
assertEquals(BookingServicePort.BookingResponse.SEAT_NOT_AVAILABLE, response.getStatusCode());
|
||||||
}
|
}
|
||||||
@ -83,16 +72,17 @@ class BookTicketUseCaseUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
void whenErrorInWalletDebit_thenReturnPaymentFailed() {
|
void whenErrorInWalletDebit_thenReturnPaymentFailed() {
|
||||||
BookingServicePort.BookingRequest request = getBookingRequest();
|
BookingServicePort.BookingRequest request = getBookingRequest();
|
||||||
Booking booking = getBooking(request);
|
Booking booking = new Booking(request);
|
||||||
when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(true);
|
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"));
|
.thenReturn(Optional.of("reservation-id"));
|
||||||
when(walletServicePort.debit(booking.getCustomerId(), booking.getAmount()))
|
when(walletServicePort.debit(booking.getCustomerId(), booking.getAmount()))
|
||||||
.thenReturn(false);
|
.thenReturn(false);
|
||||||
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
||||||
|
|
||||||
verify(bookingPersistencePort, times(2)).persist(any(Booking.class));
|
verify(bookingPersistencePort).persist(any(Booking.class));
|
||||||
verify(theatreServicePort).reserveSeats(any(String.class), any(String.class), any(HashSet.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(walletServicePort).debit(any(String.class), any(Double.class));
|
||||||
verify(theatreServicePort).releaseSeats(any(String.class));
|
verify(theatreServicePort).releaseSeats(any(String.class));
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
@ -102,16 +92,17 @@ class BookTicketUseCaseUnitTest {
|
|||||||
@Test
|
@Test
|
||||||
void whenNoErrorInAnyPorts_thenReturnSuccess() {
|
void whenNoErrorInAnyPorts_thenReturnSuccess() {
|
||||||
BookingServicePort.BookingRequest request = getBookingRequest();
|
BookingServicePort.BookingRequest request = getBookingRequest();
|
||||||
Booking booking = getBooking(request);
|
Booking booking = new Booking(request);
|
||||||
when(bookingPersistencePort.persist(any(Booking.class))).thenReturn(true);
|
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"));
|
.thenReturn(Optional.of("reservation-id"));
|
||||||
when(walletServicePort.debit(booking.getCustomerId(), booking.getAmount()))
|
when(walletServicePort.debit(booking.getCustomerId(), booking.getAmount()))
|
||||||
.thenReturn(true);
|
.thenReturn(true);
|
||||||
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
BookingServicePort.BookingResponse response = bookTicketUseCase.book(request);
|
||||||
|
|
||||||
verify(bookingPersistencePort, times(2)).persist(any(Booking.class));
|
verify(bookingPersistencePort).persist(any(Booking.class));
|
||||||
verify(theatreServicePort).reserveSeats(any(String.class), any(String.class), any(HashSet.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(walletServicePort).debit(any(String.class), any(Double.class));
|
||||||
assertNotNull(response);
|
assertNotNull(response);
|
||||||
assertEquals(BookingServicePort.BookingResponse.SUCCESS, response.getStatusCode());
|
assertEquals(BookingServicePort.BookingResponse.SUCCESS, response.getStatusCode());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user