|
11 | 11 | import com.expediagroup.sdk.rapid.models.Property;
|
12 | 12 | import com.expediagroup.sdk.rapid.models.PropertyAvailability;
|
13 | 13 | import com.expediagroup.sdk.rapid.models.RoomPriceCheck;
|
14 |
| -import org.slf4j.Logger; |
15 |
| -import org.slf4j.LoggerFactory; |
16 |
| - |
17 | 14 | import java.util.Arrays;
|
18 | 15 | import java.util.List;
|
19 | 16 | import java.util.concurrent.CompletableFuture;
|
| 17 | +import org.slf4j.Logger; |
| 18 | +import org.slf4j.LoggerFactory; |
20 | 19 |
|
| 20 | +/** |
| 21 | + * Scenario for booking a single room asynchronously. |
| 22 | + */ |
21 | 23 | public class AsyncSingleRoomBookScenario implements RapidScenario {
|
22 | 24 |
|
23 |
| - private static final Logger logger = LoggerFactory.getLogger(AsyncSingleRoomBookScenario.class); |
24 |
| - private ShopService shopService = new ShopService(); |
25 |
| - private RapidPartnerSalesProfile rapidPartnerSalesProfile; |
26 |
| - |
27 |
| - @Override |
28 |
| - public void setProfile(RapidPartnerSalesProfile rapidPartnerSalesProfile) { |
29 |
| - this.rapidPartnerSalesProfile = rapidPartnerSalesProfile; |
| 25 | + private static final Logger logger = LoggerFactory.getLogger(AsyncSingleRoomBookScenario.class); |
| 26 | + private ShopService shopService = new ShopService(); |
| 27 | + private RapidPartnerSalesProfile rapidPartnerSalesProfile; |
| 28 | + |
| 29 | + /** |
| 30 | + * Sets the sales profile for the scenario. |
| 31 | + * |
| 32 | + * @param rapidPartnerSalesProfile the sales profile to set |
| 33 | + */ |
| 34 | + @Override |
| 35 | + public void setProfile(RapidPartnerSalesProfile rapidPartnerSalesProfile) { |
| 36 | + this.rapidPartnerSalesProfile = rapidPartnerSalesProfile; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Runs the scenario to book a single room asynchronously. |
| 41 | + */ |
| 42 | + @Override |
| 43 | + public void run() { |
| 44 | + |
| 45 | + logger.info( |
| 46 | + "Running Book Single Room Scenario using the default profile in asynchronous manner..."); |
| 47 | + |
| 48 | + // Shopping for properties |
| 49 | + logger.info("Async call - Getting property availability for test property: {}", |
| 50 | + Constants.TEST_PROPERTY_ID); |
| 51 | + |
| 52 | + shopService.asyncGetSingleRoomPropertiesAvailability(this.rapidPartnerSalesProfile) |
| 53 | + .thenApply(Response::getData) |
| 54 | + .thenCompose(this::checkRoomPrices) |
| 55 | + .thenCompose(this::bookRoom) |
| 56 | + .thenCompose(this::getItinerary) |
| 57 | + .thenAccept(itinerary -> { |
| 58 | + if (itinerary == null) { |
| 59 | + throw new IllegalStateException("Itinerary is null"); |
| 60 | + } |
| 61 | + logger.info("Itinerary: {}", itinerary.getItineraryId()); |
| 62 | + }); |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Retrieves the itinerary for the booking. |
| 68 | + * |
| 69 | + * @param itineraryCreation the itinerary creation details |
| 70 | + * @return a CompletableFuture with the itinerary |
| 71 | + */ |
| 72 | + private CompletableFuture<Itinerary> getItinerary(ItineraryCreation itineraryCreation) { |
| 73 | + if (itineraryCreation == null) { |
| 74 | + throw new IllegalStateException("ItineraryCreation is null"); |
30 | 75 | }
|
31 | 76 |
|
32 |
| - @Override |
33 |
| - public void run() { |
34 |
| - |
35 |
| - logger.info("Running Book Single Room Scenario using the default profile in asynchronous manner..."); |
36 |
| - |
37 |
| - // Shopping for properties |
38 |
| - logger.info("Async call - Getting property availability for test property: {}", Constants.TEST_PROPERTY_ID); |
39 |
| - |
40 |
| - shopService.asyncGetSingleRoomPropertiesAvailability(this.rapidPartnerSalesProfile) |
41 |
| - .thenApply(Response::getData) |
42 |
| - .thenCompose(this::checkRoomPrices) |
43 |
| - .thenCompose(this::bookRoom) |
44 |
| - .thenCompose(this::getItinerary) |
45 |
| - .thenAccept(itinerary -> { |
46 |
| - if (itinerary == null) { |
47 |
| - throw new IllegalStateException("Itinerary is null"); |
48 |
| - } |
49 |
| - logger.info("Itinerary: {}", itinerary.getItineraryId()); |
50 |
| - }); |
51 |
| - |
| 77 | + logger.info("Booking Success. Itinerary id: {}", itineraryCreation.getItineraryId()); |
| 78 | + |
| 79 | + // Manage booking |
| 80 | + logger.info("Getting itinerary by itinerary id..."); |
| 81 | + BookService bookService = new BookService(); |
| 82 | + return bookService.asyncGetReservation(itineraryCreation) |
| 83 | + .thenApply(response -> response.getData()); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Books a room based on the room price check. |
| 88 | + * |
| 89 | + * @param roomPriceCheck the room price check details |
| 90 | + * @return a CompletableFuture with the itinerary creation details |
| 91 | + */ |
| 92 | + private CompletableFuture<ItineraryCreation> bookRoom(RoomPriceCheck roomPriceCheck) { |
| 93 | + if (roomPriceCheck == null) { |
| 94 | + throw new IllegalStateException("Room Price Check is null"); |
52 | 95 | }
|
53 | 96 |
|
54 |
| - private CompletableFuture<Itinerary> getItinerary(ItineraryCreation itineraryCreation) { |
55 |
| - if (itineraryCreation == null) { |
56 |
| - throw new IllegalStateException("ItineraryCreation is null"); |
57 |
| - } |
58 |
| - |
59 |
| - logger.info("Booking Success. Itinerary id: {}", itineraryCreation.getItineraryId()); |
60 |
| - |
61 |
| - // Manage booking |
62 |
| - logger.info("Getting itinerary by itinerary id..."); |
63 |
| - BookService bookService = new BookService(); |
64 |
| - return bookService.asyncGetReservation(itineraryCreation) |
65 |
| - .thenApply(response -> response.getData()); |
| 97 | + logger.info("Room Price Check: {}", roomPriceCheck.getStatus()); |
| 98 | + |
| 99 | + // Booking a single room in the property |
| 100 | + logger.info("Booking a room in test property: {}...", Constants.TEST_PROPERTY_ID); |
| 101 | + |
| 102 | + BookService bookService = new BookService(); |
| 103 | + return bookService.asyncCreateBooking(roomPriceCheck, Arrays.asList("2")) |
| 104 | + .thenApply(response -> response.getData()); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Checks the room prices for the available properties. |
| 109 | + * |
| 110 | + * @param propertyAvailabilityList the list of available properties |
| 111 | + * @return a CompletableFuture with the room price check details |
| 112 | + */ |
| 113 | + private CompletableFuture<RoomPriceCheck> checkRoomPrices( |
| 114 | + List<Property> propertyAvailabilityList) { |
| 115 | + if (propertyAvailabilityList == null || propertyAvailabilityList.isEmpty()) { |
| 116 | + throw new IllegalStateException("No property availability found for the test property."); |
66 | 117 | }
|
67 | 118 |
|
68 |
| - private CompletableFuture<ItineraryCreation> bookRoom(RoomPriceCheck roomPriceCheck) { |
69 |
| - if (roomPriceCheck == null) { |
70 |
| - throw new IllegalStateException("Room Price Check is null"); |
71 |
| - } |
72 |
| - |
73 |
| - logger.info("Room Price Check: {}", roomPriceCheck.getStatus()); |
| 119 | + logger.info("Property Availability: {}", propertyAvailabilityList.get(0).getStatus()); |
74 | 120 |
|
75 |
| - // Booking a single room in the property |
76 |
| - logger.info("Booking a room in test property: {}...", Constants.TEST_PROPERTY_ID); |
| 121 | + // Checking room prices for the property |
| 122 | + logger.info("Checking room prices for the property: {}...", Constants.TEST_PROPERTY_ID); |
| 123 | + Property property = propertyAvailabilityList.get(0); |
77 | 124 |
|
78 |
| - BookService bookService = new BookService(); |
79 |
| - return bookService.asyncCreateBooking(roomPriceCheck, Arrays.asList("2")) |
80 |
| - .thenApply(response -> response.getData()); |
| 125 | + if (!(property instanceof PropertyAvailability)) { |
| 126 | + throw new IllegalStateException("Property is not an instance of PropertyAvailability"); |
81 | 127 | }
|
82 | 128 |
|
83 |
| - private CompletableFuture<RoomPriceCheck> checkRoomPrices(List<Property> propertyAvailabilityList) { |
84 |
| - if (propertyAvailabilityList == null || propertyAvailabilityList.isEmpty()) { |
85 |
| - throw new IllegalStateException("No property availability found for the test property."); |
86 |
| - } |
| 129 | + PropertyAvailability propertyAvailability = (PropertyAvailability) property; |
| 130 | + return shopService.asyncCheckRoomPrices(propertyAvailability, 0, 0) |
| 131 | + .thenApply(response -> response.getData()); |
87 | 132 |
|
88 |
| - logger.info("Property Availability: {}", propertyAvailabilityList.get(0).getStatus()); |
89 |
| - |
90 |
| - // Checking room prices for the property |
91 |
| - logger.info("Checking room prices for the property: {}...", Constants.TEST_PROPERTY_ID); |
92 |
| - Property property = propertyAvailabilityList.get(0); |
93 |
| - |
94 |
| - if (!(property instanceof PropertyAvailability)) { |
95 |
| - throw new IllegalStateException("Property is not an instance of PropertyAvailability"); |
96 |
| - } |
97 |
| - |
98 |
| - PropertyAvailability propertyAvailability = (PropertyAvailability) property; |
99 |
| - return shopService.asyncCheckRoomPrices(propertyAvailability, 0, 0) |
100 |
| - .thenApply(response -> response.getData()); |
101 |
| - |
102 |
| - } |
| 133 | + } |
103 | 134 | }
|
0 commit comments