Skip to content

Commit 2b59c6b

Browse files
committed
Added tests for the missing handlers with returned 501 (Not Implemented)
Signed-off-by: Paolo Patierno <ppatierno@live.com>
1 parent f065903 commit 2b59c6b

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

vertx-web-openapi-router/src/test/java/io/vertx/router/test/e2e/RouterBuilderTest.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
import static com.google.common.truth.Truth.assertThat;
3737
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
38+
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_IMPLEMENTED;
39+
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
3840
import static io.vertx.core.http.HttpMethod.GET;
3941
import static io.vertx.core.http.HttpMethod.POST;
4042
import static io.vertx.ext.web.openapi.router.RequestExtractor.withBodyHandler;
@@ -157,4 +159,84 @@ void testRouterWithInvalidRequest(VertxTestContext testContext) {
157159
})
158160
.onFailure(testContext::failNow);
159161
}
162+
163+
@Test
164+
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
165+
void testRouterWithNoHandlerReturns501NotImplemented(VertxTestContext testContext) {
166+
Path pathDereferencedContract = ResourceHelper.TEST_RESOURCE_PATH.resolve("v3.1").resolve("petstore.json");
167+
createServer(pathDereferencedContract, rb -> {
168+
// Intentionally do NOT add any handlers for the operations
169+
// This will trigger the default behavior of returning 501 Not Implemented
170+
return Future.succeededFuture(rb);
171+
}).compose(v -> createRequest(GET, "/v1/pets").send())
172+
.onSuccess(response -> testContext.verify(() -> {
173+
assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code());
174+
testContext.completeNow();
175+
}))
176+
.onFailure(testContext::failNow);
177+
}
178+
179+
@Test
180+
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
181+
void testRouterWithNoHandlersReturns501ForAllOperations(VertxTestContext testContext) {
182+
Checkpoint cpAllOperations = testContext.checkpoint(3);
183+
184+
Path pathDereferencedContract = ResourceHelper.TEST_RESOURCE_PATH.resolve("v3.1").resolve("petstore.json");
185+
createServer(pathDereferencedContract, rb -> Future.succeededFuture(rb))
186+
.compose(v -> createRequest(GET, "/v1/pets").send())
187+
.onSuccess(response -> testContext.verify(() -> {
188+
assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code());
189+
cpAllOperations.flag();
190+
}))
191+
.compose(v -> {
192+
JsonObject bodyJson = new JsonObject().put("id", 1).put("name", "FooBar");
193+
return createRequest(POST, "/v1/pets").sendJsonObject(bodyJson);
194+
})
195+
.onSuccess(response -> testContext.verify(() -> {
196+
assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code());
197+
cpAllOperations.flag();
198+
}))
199+
.compose(v -> createRequest(GET, "/v1/pets/123").send())
200+
.onSuccess(response -> testContext.verify(() -> {
201+
assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code());
202+
cpAllOperations.flag();
203+
}))
204+
.onFailure(testContext::failNow);
205+
}
206+
207+
@Test
208+
@Timeout(value = 2, timeUnit = TimeUnit.SECONDS)
209+
void testRouterWithPartialHandlersReturns501ForUnimplemented(VertxTestContext testContext) {
210+
Checkpoint cpAllOperations = testContext.checkpoint(3);
211+
212+
Path pathDereferencedContract = ResourceHelper.TEST_RESOURCE_PATH.resolve("v3.1").resolve("petstore.json");
213+
createServer(pathDereferencedContract, rb -> {
214+
// Add handlers for only listPets and createPets operations but intentionally do NOT add handler for showPetById
215+
rb.getRoute("listPets")
216+
.setDoSecurity(false)
217+
.addHandler(rc -> rc.response().setStatusCode(OK.code()).end("[]"));
218+
rb.getRoute("createPets")
219+
.setDoSecurity(false)
220+
.addHandler(rc -> rc.response().setStatusCode(OK.code()).end());
221+
return Future.succeededFuture(rb);
222+
}).compose(v -> createRequest(GET, "/v1/pets").send())
223+
.onSuccess(response -> testContext.verify(() -> {
224+
assertThat(response.statusCode()).isEqualTo(OK.code());
225+
cpAllOperations.flag();
226+
}))
227+
.compose(v -> {
228+
JsonObject bodyJson = new JsonObject().put("id", 1).put("name", "FooBar");
229+
return createRequest(POST, "/v1/pets").sendJsonObject(bodyJson);
230+
})
231+
.onSuccess(response -> testContext.verify(() -> {
232+
assertThat(response.statusCode()).isEqualTo(OK.code());
233+
cpAllOperations.flag();
234+
}))
235+
.compose(v -> createRequest(GET, "/v1/pets/123").send())
236+
.onSuccess(response -> testContext.verify(() -> {
237+
assertThat(response.statusCode()).isEqualTo(NOT_IMPLEMENTED.code());
238+
cpAllOperations.flag();
239+
}))
240+
.onFailure(testContext::failNow);
241+
}
160242
}

0 commit comments

Comments
 (0)