-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReserveRoomApiOpTest.kt
More file actions
241 lines (203 loc) · 9.5 KB
/
Copy pathReserveRoomApiOpTest.kt
File metadata and controls
241 lines (203 loc) · 9.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package pro.azhidkov.mariotte.cases.app.reservations
import io.kotest.inspectors.forExactly
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeInstanceOf
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.api.RepeatedTest
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpStatus
import pro.azhidkov.mariotte.app.reservations.NoAvailableRoomsException
import pro.azhidkov.mariotte.assertions.shouldMatch
import pro.azhidkov.mariotte.cases.app.platform.annotations.MariotteApiTest
import pro.azhidkov.mariotte.domain.rooms.RoomType
import pro.azhidkov.mariotte.domain.reservations.ReservationPeriod
import pro.azhidkov.mariotte.fixtures.clients.HttpClientFactory
import pro.azhidkov.mariotte.fixtures.clients.apis.HttpOutcome
import pro.azhidkov.mariotte.fixtures.clients.apis.only
import pro.azhidkov.mariotte.fixtures.clients.apis.with
import pro.azhidkov.mariotte.fixtures.object_mothers.*
import pro.azhidkov.mariotte.fixtures.presets.HotelsFixturePresets
import pro.azhidkov.mariotte.fixtures.presets.NewHotelFixture
import pro.azhidkov.mariotte.platform.spring.test.concurrent.executeSimultaneously
import java.time.LocalDate
import java.time.Period
/**
* Тест-кейсы на операцию бронирования номера.
*
* Метки в коде
* 1. Этот тест будет мигать, если его запускать в районе 23:59:59, но во имя простоты демо, на это можно пойти
*/
@MariotteApiTest
@DisplayName("Операция API - Бронирование номера")
class ReserveRoomApiOpTest(
@param:Autowired private val hotelsFixturePresets: HotelsFixturePresets,
@param:Autowired private val httpClientFactory: HttpClientFactory
) {
@Test
fun `должна возвращать идентификатор брони, по которому можно получить детали брони`() {
// Given
val roomType = RoomType.LUX
val reserveRoomRequest =
ReservationsObjectMother.aRoomReservationRequest(
hotelId = HotelsObjectMother.theHotel.ref.id,
roomType = roomType
)
val guest = httpClientFactory.aGuest
// When
val reservationSuccess = guest.reservations.reserveRoom(reserveRoomRequest)
// And when
val reservation = guest.reservations.getReservation(reservationSuccess.reservationId)
// Then
reservation shouldMatch reserveRoomRequest
}
@Test
fun `должна не позволять бронировать номера по несуществующему идентификатору отеля`() {
// Given
val notExistingHotelId = faker.randomUuid()
val roomReservationRequest = ReservationsObjectMother.aRoomReservationRequest(hotelId = notExistingHotelId)
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.reserveRoomForError(roomReservationRequest)
// Then
errorResponse.status shouldBe HttpStatus.NOT_FOUND.value()
errorResponse.type?.path shouldBe "hotel-not-found"
}
@Test
fun `должна не позволять бронировать номера типа, не представленного в отеле`() {
// Given
val absentRoomType = RoomType.SEMI_LUX
val roomReservationRequest =
ReservationsObjectMother.aRoomReservationRequest(
hotelId = HotelsObjectMother.theHotel.ref.id,
roomType = absentRoomType
)
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.reserveRoomForError(roomReservationRequest)
// Then
errorResponse.status shouldBe HttpStatus.NOT_FOUND.value()
errorResponse.type?.path shouldBe "room-type-not-found"
}
@Test
fun `должна возвращать 404 при запросе деталей несуществующей брони`() {
// Given
val reservationId = faker.randomUuid()
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.getReservationForError(reservationId)
// Then
errorResponse.status shouldBe HttpStatus.NOT_FOUND.value()
errorResponse.type?.path shouldBe "reservation-not-found"
}
@Test
fun `должна не позволять бронировать номер, если в отеле нет свободных номеров заданного типа на весь запрошенный период`() {
// Given
val luxRoomType = RoomType.LUX
val hotelWithSingleLux = hotelsFixturePresets
.insertFixture(NewHotelFixture.ofHotelWithSingleRoom(luxRoomType))
.hotel
val reservationFrom = nearFutureDate(LocalDate.now())
val period = ReservationPeriod(Period.ofDays(1))
val roomReservationRequest =
ReservationsObjectMother.aRoomReservationRequest(
hotelId = hotelWithSingleLux.id,
luxRoomType,
from = reservationFrom,
period = period
)
val guest = httpClientFactory.aGuest
// When
guest.reservations.reserveRoom(roomReservationRequest)
// And when
val errorResponse =
guest.reservations.reserveRoomForError(roomReservationRequest)
// Then
errorResponse.status shouldBe HttpStatus.CONFLICT.value()
errorResponse.type?.path shouldBe "no-available-rooms"
}
@Test
fun `должна возвращать 400 ошибку при запросе с пустым телом`() {
// Given
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.reserveRoomForError("")
// Then
errorResponse.status shouldBe HttpStatus.BAD_REQUEST.value()
}
@Test
fun `должна возвращать 400 ошибку при запросе без обязательного поля`() {
// Given
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.reserveRoomForError(
ReservationsObjectMother.aRoomReservationRequestJson(
email = null
)
)
// Then
errorResponse.status shouldBe HttpStatus.BAD_REQUEST.value()
}
@Test
fun `должна возвращать 400 ошибку при запросе с неизвестным идентификатором типа номера`() {
// Given
val notExistingRoomTypeId = 3
val guest = httpClientFactory.aGuest
// When
val errorResponse =
guest.reservations.reserveRoomForError(ReservationsObjectMother.aRoomReservationRequestJson(roomTypeId = notExistingRoomTypeId))
// Then
errorResponse.status shouldBe HttpStatus.BAD_REQUEST.value()
}
@Test
fun `должна не позволять выполнять бронь начинающуюся ранее, чем завтра`() {
// Given
val today = LocalDate.now()
val reserveRoomRequest = ReservationsObjectMother.aRoomReservationRequest(from = today)
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.reserveRoomForError(reserveRoomRequest)
// Then
errorResponse.status shouldBe HttpStatus.UNPROCESSABLE_ENTITY.value()
errorResponse.type?.path shouldBe "reservation-dates-in-past"
}
@RepeatedTest(value = 10, failureThreshold = 1)
fun `должна исключать овербукинг при конкурентных запросах на бронирование`() {
// Given
val roomType = RoomType.LUX
val capacity = HotelsObjectMother.theHotel.capacity.getValue(roomType)
val requests = capacity * 2
val reserveRoomRequest = ReservationsObjectMother.aRoomReservationRequest(
hotelId = HotelsObjectMother.theHotel.ref.id,
roomType = roomType,
from = nearFutureDate(LocalDate.now()),
)
val guest = httpClientFactory.aGuest
// When
val reservationResults = executeSimultaneously(requests) {
guest.reservations.reserveRoomForOutcome(
roomReservationRq = reserveRoomRequest,
expectedErrors = only(HttpStatus.CONFLICT with NoAvailableRoomsException.CODE)
)
}
// Then
reservationResults.forExactly(capacity) { it.shouldBeInstanceOf<HttpOutcome.Success<*>>() }
reservationResults.forExactly(requests - capacity) { it.shouldBeInstanceOf<HttpOutcome.ExpectedError<*>>() }
}
@Test
fun `должна не позволять выполнять резервацию при запросе с длительностью менее 1 дня`() {
// Given
val reservationFrom = nearFutureDate(LocalDate.now())
val reservationPeriod = Period.ZERO
val bodyJson = ReservationsObjectMother.aRoomReservationRequestJson(
email = "",
from = reservationFrom,
period = reservationPeriod
)
val guest = httpClientFactory.aGuest
// When
val errorResponse = guest.reservations.reserveRoomForError(bodyJson)
// Then
errorResponse.status shouldBe HttpStatus.BAD_REQUEST.value()
}
}