private static void setDefaultVersionBehaviour( VersionRouter router, DefaultVersionBehaviour versionBehaviour) { switch (versionBehaviour) { case LATEST: router.setVersioningToDefaultToLatest(); break; case OLDEST: router.setVersioningToDefaultToOldest(); break; case NONE: router.setVersioningBehaviourToNone(); break; } }
@BeforeClass public void setUp() { router = new VersionRouter(); usersHandlerOne = mock(RequestHandler.class); usersHandlerTwo = mock(RequestHandler.class); usersHandlerThree = mock(RequestHandler.class); groupsHandlerOne = mock(RequestHandler.class); groupsHandlerTwo = mock(RequestHandler.class); groupsHandlerThree = mock(RequestHandler.class); router .addRoute(RoutingMode.EQUALS, "/users") .addVersion("1.0", usersHandlerOne) .addVersion("1.5", usersHandlerTwo) .addVersion("2.1", usersHandlerThree); router .addRoute(RoutingMode.EQUALS, "/groups") .addVersion("1.0", groupsHandlerOne) .addVersion("1.5", groupsHandlerTwo) .addVersion("2.1", groupsHandlerThree); }
@Test(dataProvider = "dataWithoutVersionHeader") @SuppressWarnings("unchecked") public void shouldRouteActionRequestsUsingDefaultVersionBehaviour( String resource, DefaultVersionBehaviour versionBehaviour, boolean expectException, RequestHandler provider) { // Given AcceptAPIVersionContext apiVersionContext = new AcceptAPIVersionContext( new RootContext(), "PROTOCOL_NAME", AcceptAPIVersion.newBuilder().withDefaultProtocolVersion("1.0").build()); ServerContext context = new ServerContext(apiVersionContext); ActionRequest request = Requests.newActionRequest(resource, "ACTION_ID").setContent(json(object())); ResultHandler<JsonValue> handler = mock(ResultHandler.class); setDefaultVersionBehaviour(router, versionBehaviour); // When router.handleAction(context, request, handler); // Then if (expectException) { ArgumentCaptor<ResourceException> exceptionCaptor = ArgumentCaptor.forClass(ResourceException.class); verify(handler).handleError(exceptionCaptor.capture()); assertThat(exceptionCaptor.getValue()).isInstanceOf(BadRequestException.class); verifyZeroInteractions( usersHandlerOne, usersHandlerTwo, usersHandlerThree, groupsHandlerOne, groupsHandlerTwo, groupsHandlerThree); } else { verify(provider) .handleAction( Matchers.<ServerContext>anyObject(), Matchers.<ActionRequest>anyObject(), eq(handler)); } }
@Test(dataProvider = "dataWithVersionHeader") @SuppressWarnings("unchecked") public void shouldRouteDeleteVersionRequests( String resource, String requestedVersion, boolean expectException, RequestHandler provider) { // Given AcceptAPIVersionContext apiVersionContext = new AcceptAPIVersionContext( new RootContext(), "PROTOCOL_NAME", AcceptAPIVersion.newBuilder() .withDefaultProtocolVersion("1.0") .withDefaultResourceVersion(requestedVersion) .build()); ServerContext context = new ServerContext(apiVersionContext); DeleteRequest request = Requests.newDeleteRequest(resource); ResultHandler<Resource> handler = mock(ResultHandler.class); // When router.handleDelete(context, request, handler); // Then if (expectException) { ArgumentCaptor<ResourceException> exceptionCaptor = ArgumentCaptor.forClass(ResourceException.class); verify(handler).handleError(exceptionCaptor.capture()); assertThat(exceptionCaptor.getValue()).isInstanceOf(NotFoundException.class); verifyZeroInteractions( usersHandlerOne, usersHandlerTwo, usersHandlerThree, groupsHandlerOne, groupsHandlerTwo, groupsHandlerThree); } else { verify(provider) .handleDelete( Matchers.<ServerContext>anyObject(), Matchers.<DeleteRequest>anyObject(), eq(handler)); } }