Skip to content

Commit b97d2e1

Browse files
committed
Add exercise and answer for response templating using path parameter
1 parent a9e51c3 commit b97d2e1

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

src/test/java/answers/WireMockAnswers4Test.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,25 @@ public void setupStubExercise401() {
5656

5757
public void setupStubExercise402() {
5858

59+
/************************************************
60+
* Create a stub that listens at path /customers/<customerId>
61+
* (use urlPathMatching() to do so, valid customer IDs are exactly
62+
* five digits long, i.e., matching [0-9]{5}), and responds to all
63+
* GET requests with HTTP status code 200 and a response body containing
64+
* the text 'Hello, customer <customerId>',
65+
* where <customerId> is the value of the path parameter
66+
* extracted from the endpoint
67+
************************************************/
68+
69+
wiremock.stubFor(get(urlPathMatching("/customers/([0-9]{5})"))
70+
.willReturn(aResponse()
71+
.withStatus(200)
72+
.withBody("Hello, customer {{request.pathSegments.[1]}}")
73+
));
74+
}
75+
76+
public void setupStubExercise403() {
77+
5978
/************************************************
6079
* Create a stub that listens at path /echo-loan-amount
6180
* and responds to all POST requests with HTTP
@@ -92,22 +111,51 @@ public void testExercise401() {
92111
body(org.hamcrest.Matchers.equalTo("Listening on port 9876"));
93112
}
94113

114+
@ParameterizedTest(name = "User ID {0} is echoed in the response correctly")
115+
@CsvSource({
116+
"12212",
117+
"12323",
118+
"98765"
119+
})
120+
public void testExercise402(int customerId) {
121+
122+
/***
123+
* Use this test to test your implementation of exercise 402
124+
*/
125+
126+
setupStubExercise402();
127+
128+
given().
129+
spec(requestSpec).
130+
and().
131+
pathParam("customerId", customerId).
132+
when().
133+
get("/customers/{customerId}").
134+
then().
135+
assertThat().
136+
statusCode(200).
137+
and().
138+
body(org.hamcrest.Matchers.equalTo(
139+
String.format("Hello, customer %d", customerId)
140+
));
141+
}
142+
95143
@ParameterizedTest(name = "Loan amount {0} is echoed in the response correctly")
96144
@CsvSource({
97145
"1000",
98146
"1500",
99147
"3000"
100148
})
101-
public void testExercise402(int loanAmount) {
149+
public void testExercise403(int loanAmount) {
102150

103151
/***
104-
* Use this test to test your implementation of exercise 402
152+
* Use this test to test your implementation of exercise 403
105153
*/
106154

107155
LoanDetails loanDetails = new LoanDetails(loanAmount, 100, "pending");
108156
LoanRequest loanRequest = new LoanRequest(12212, loanDetails);
109157

110-
setupStubExercise402();
158+
setupStubExercise403();
111159

112160
given().
113161
spec(requestSpec).

src/test/java/exercises/WireMockExercises4Test.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ public void setupStubExercise401() {
5151

5252
public void setupStubExercise402() {
5353

54+
/************************************************
55+
* Create a stub that listens at path /customers/<customerId>
56+
* (use urlPathMatching() to do so, valid customer IDs are exactly
57+
* five digits long, i.e., matching [0-9]{5}), and responds to all
58+
* GET requests with HTTP status code 200 and a response body containing
59+
* the text 'Hello, customer <customerId>',
60+
* where <customerId> is the value of the path parameter
61+
* extracted from the endpoint
62+
************************************************/
63+
64+
}
65+
66+
public void setupStubExercise403() {
67+
5468
/************************************************
5569
* Create a stub that listens at path /echo-loan-amount
5670
* and responds to all POST requests with HTTP
@@ -82,22 +96,51 @@ public void testExercise401() {
8296
body(org.hamcrest.Matchers.equalTo("Listening on port 9876"));
8397
}
8498

99+
@ParameterizedTest(name = "User ID {0} is echoed in the response correctly")
100+
@CsvSource({
101+
"12212",
102+
"12323",
103+
"98765"
104+
})
105+
public void testExercise402(int customerId) {
106+
107+
/***
108+
* Use this test to test your implementation of exercise 402
109+
*/
110+
111+
setupStubExercise402();
112+
113+
given().
114+
spec(requestSpec).
115+
and().
116+
pathParam("customerId", customerId).
117+
when().
118+
get("/customers/{customerId}").
119+
then().
120+
assertThat().
121+
statusCode(200).
122+
and().
123+
body(org.hamcrest.Matchers.equalTo(
124+
String.format("Hello, customer %d", customerId)
125+
));
126+
}
127+
85128
@ParameterizedTest(name = "Loan amount {0} is echoed in the response correctly")
86129
@CsvSource({
87130
"1000",
88131
"1500",
89132
"3000"
90133
})
91-
public void testExercise402(int loanAmount) {
134+
public void testExercise403(int loanAmount) {
92135

93136
/***
94-
* Use this test to test your implementation of exercise 402
137+
* Use this test to test your implementation of exercise 403
95138
*/
96139

97140
LoanDetails loanDetails = new LoanDetails(loanAmount, 100, "pending");
98141
LoanRequest loanRequest = new LoanRequest(12212, loanDetails);
99142

100-
setupStubExercise402();
143+
setupStubExercise403();
101144

102145
given().
103146
spec(requestSpec).

0 commit comments

Comments
 (0)