-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBookingSchemaValidationTest.java
More file actions
107 lines (92 loc) · 4.16 KB
/
BookingSchemaValidationTest.java
File metadata and controls
107 lines (92 loc) · 4.16 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
package com.learning.api.tests.restfulbooker;
import com.learning.api.framework.model.AuthCredentials;
import com.learning.api.framework.model.Booking;
import com.learning.api.framework.model.CreateBookingResponse;
import com.learning.api.framework.payload.BookingPayloadBuilder;
import com.learning.api.framework.schema.RestfulBookerSchemas;
import com.learning.api.tests.base.BaseApiTest;
import io.restassured.response.Response;
import org.testng.annotations.Test;
import static io.restassured.module.jsv.JsonSchemaValidator.matchesJsonSchemaInClasspath;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Study with:
* docs/module-15-schema-validation-contract-boundaries/03-schema-validation-tests.md
*
* <p>These tests validate response shape with JSON schema. They do not replace
* business assertions; they prove that required fields and field types match
* the contract files in src/test/resources/schemas.</p>
*/
public class BookingSchemaValidationTest extends BaseApiTest {
@Test
public void bookingIdListMatchesSchema() {
bookingService.listBookingIds()
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath(RestfulBookerSchemas.BOOKING_IDS));
}
@Test
public void singleBookingMatchesSchema() {
int bookingId = 0;
try {
Booking booking = BookingPayloadBuilder.defaultBooking()
.withFirstname("Schema")
.withLastname("Single")
.withTotalprice(1501)
.withBookingDates("2026-07-10", "2026-07-12")
.withAdditionalneeds("Stable schema read")
.build();
CreateBookingResponse createdBooking = bookingService.mapCreateBookingResponse(
bookingService.createBooking(booking)
.then()
.statusCode(200)
.extract()
.response()
);
bookingId = createdBooking.getBookingId();
assertThat(bookingId).isPositive();
/*
* The test reads a booking it created itself. Reading the first id
* from the public list endpoint can be unstable because public demo
* data may be deleted between list and read calls.
*/
bookingService.readBooking(bookingId)
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath(RestfulBookerSchemas.BOOKING));
} finally {
String token = bookingService.createTokenValue(AuthCredentials.restfulBookerAdmin());
bookingService.deleteBookingBestEffort(bookingId, token);
}
}
@Test
public void createBookingResponseMatchesSchema() {
int bookingId = 0;
try {
Booking booking = BookingPayloadBuilder.defaultBooking()
.withFirstname("Schema")
.withLastname("Contract")
.withTotalprice(1515)
.withBookingDates("2026-07-01", "2026-07-05")
.withAdditionalneeds("Schema validation")
.build();
Response createResponse = bookingService.createBooking(booking)
.then()
.statusCode(200)
.body(matchesJsonSchemaInClasspath(RestfulBookerSchemas.CREATE_BOOKING_RESPONSE))
.extract()
.response();
CreateBookingResponse mappedResponse = bookingService.mapCreateBookingResponse(createResponse);
bookingId = mappedResponse.getBookingId();
/*
* This business assertion complements schema validation. The schema
* proves bookingid is a positive integer; the assertion proves this
* scenario received an id we can use for cleanup.
*/
assertThat(bookingId).isPositive();
} finally {
String token = bookingService.createTokenValue(AuthCredentials.restfulBookerAdmin());
bookingService.deleteBookingBestEffort(bookingId, token);
}
}
}