PathAnnotationRoutesParser implements RoutesParser { public List<Route> rulesFor(ResourceClass resource) { Class<?> baseType = resource.getType(); return registerRulesFor(baseType); } PathAnnotationRoutesParser(Router router); List<Route> rulesFor(ResourceClass resource);  }
@Test(expected = IllegalArgumentException.class) public void addsAPrefixToMethodsWhenTheControllerHasMoreThanOneAnnotatedPath() throws Exception { parser.rulesFor(new DefaultResourceClass(MoreThanOnePathAnnotatedController.class)); }
@Test public void addsAPrefixToMethodsWhenTheControllerAndTheMethodAreAnnotatedWithRelativePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(PathAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/relativePath"); assertThat(route, canHandle(PathAnnotatedController.class, "withRelativePath")); }
@Test public void addsAPrefixToMethodsWhenTheControllerEndsWithSlashAndTheMethodAreAnnotatedWithRelativePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(EndSlashAnnotatedController.class)); Route route = getRouteMatching(routes, "/endSlash/relativePath"); assertThat(route, canHandle(EndSlashAnnotatedController.class, "withRelativePath")); }
@Test public void addsAPrefixToMethodsWhenTheControllerEndsWithSlashAndTheMethodAreAnnotatedWithAbsolutePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(EndSlashAnnotatedController.class)); Route route = getRouteMatching(routes, "/endSlash/absolutePath"); assertThat(route, canHandle(EndSlashAnnotatedController.class, "withAbsolutePath")); }
@Test public void addsAPrefixToMethodsWhenTheControllerEndsWithSlashAndTheMethodAreAnnotatedWithEmptyPath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(EndSlashAnnotatedController.class)); Route route = getRouteMatching(routes, "/endSlash/"); assertThat(route, canHandle(EndSlashAnnotatedController.class, "withEmptyPath")); }
@Test public void addsAPrefixToMethodsWhenTheControllerAndTheMethodAreAnnotatedWithAbsolutePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(PathAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/absolutePath"); assertThat(route, canHandle(PathAnnotatedController.class, "withAbsolutePath")); }
@Test public void addsAPrefixToMethodsWhenTheControllerAndTheMethodAreAnnotatedWithEmptyPath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(PathAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix"); assertThat(route, canHandle(PathAnnotatedController.class, "withEmptyPath")); }
@Test public void addsAPrefixToMethodsWhenTheControllerIsAnnotatedWithPath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(PathAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/withoutPath"); assertThat(route, canHandle(PathAnnotatedController.class, "withoutPath")); }
@Test public void findsTheCorrectAnnotatedMethodIfThereIsNoWebMethodAnnotationPresent() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients"); assertThat(route, canHandle(ClientsController.class, "list")); }
@Test public void suportsTheDefaultNameForANonAnnotatedMethod() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/add"); assertThat(route, canHandle(ClientsController.class, "add")); }
@Test public void ignoresTheControllerSuffixForANonAnnotatedMethod() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/add"); assertThat(route, canHandle(ClientsController.class, "add")); }
@Test public void addsASlashWhenUserForgotIt() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/noSlash"); assertThat(route, canHandle(ClientsController.class, "noSlash")); }
@Test public void matchesWhenUsingAWildcard() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/move/second/child"); assertThat(route, canHandle(ClientsController.class, "move")); }
@Test public void dontRegisterRouteIfMethodIsNotPublic() { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/protectMe"); assertNull(route); }
@Test public void dontRegisterRouteIfMethodIsStatic() { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/staticMe"); assertNull(route); }
@Test(expected=IllegalArgumentException.class) public void shouldThrowExceptionIfPathAnnotationHasEmptyArray() throws Exception { parser.rulesFor(new DefaultResourceClass(NoPath.class)); }
@Test public void shouldFindNonAnnotatedNonStaticPublicMethodWithComponentNameInVariableCamelCaseConventionAsURI() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/add"); assertThat(route, canHandle(ClientsController.class, "add")); }
@Test public void shouldFindSeveralPathsForMethodWithManyValue() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/path1"); assertThat(route, canHandle(ClientsController.class, "manyPaths")); Route route2 = getRouteMatching(routes, "/path2"); assertThat(route2, canHandle(ClientsController.class, "manyPaths")); }
@Test public void shouldNotMatchIfAResourceHasTheWrongWebMethod() throws SecurityException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/remove"); assertThat(route.allowedMethods(), not(contains(HttpMethod.POST))); }
@Test public void shouldAcceptAResultWithASpecificWebMethod() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/head"); assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.HEAD))); }
@Test public void shouldAcceptAResultWithOptionsWebMethod() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/options"); assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.OPTIONS))); }
@Test public void shouldAcceptAResultWithPatchWebMethod() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(ClientsController.class)); Route route = getRouteMatching(routes, "/clients/update"); assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.PATCH))); }
@Test public void findsInheritedMethodsWithDefaultNames() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(NiceClients.class)); Route route = getRouteMatching(routes, "/niceClients/toInherit"); assertTrue(route.canHandle(NiceClients.class, ClientsController.class.getDeclaredMethod("toInherit"))); }
@Test public void supportMethodOverriding() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(NiceClients.class)); Route route = getRouteMatching(routes, "/niceClients/add"); assertThat(route, canHandle(NiceClients.class, "add")); }
@Test public void supportTypeHttpMethodAnnotation() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(AnnotatedController.class)); Route route = getRouteMatching(routes, "/annotated/test"); assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.POST))); }
@Test public void supportOverrideTypeHttpMethodAnnotation() throws SecurityException, NoSuchMethodException { List<Route> routes = parser.rulesFor(new DefaultResourceClass(AnnotatedController.class)); Route route = getRouteMatching(routes, "/annotated/overridden"); assertThat(route.allowedMethods(), is(EnumSet.of(HttpMethod.GET))); }
@Test public void addsAPrefixToMethodsWhenTheGetControllerAndTheMethodAreAnnotatedWithRelativePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(GetAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/relativePath"); assertThat(route, canHandle(GetAnnotatedController.class, "withRelativePath")); }
@Test public void priorityForGetAnnotationShouldBeDefault() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(GetAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/relativePath"); assertThat(route.getPriority(), is(Path.DEFAULT)); }
@Test public void addsAPrefixToMethodsWhenTheGetControllerEndsWithSlashAndTheMethodAreAnnotatedWithRelativePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(EndSlashAnnotatedGetController.class)); Route route = getRouteMatching(routes, "/endSlash/relativePath"); assertThat(route, canHandle(EndSlashAnnotatedGetController.class, "withRelativePath")); }
@Test public void addsAPrefixToMethodsWhenTheGetControllerEndsWithSlashAndTheMethodAreAnnotatedWithAbsolutePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(EndSlashAnnotatedGetController.class)); Route route = getRouteMatching(routes, "/endSlash/absolutePath"); assertThat(route, canHandle(EndSlashAnnotatedGetController.class, "withAbsolutePath")); }
@Test public void addsAPrefixToMethodsWhenTheGetControllerAndTheMethodAreAnnotatedWithAbsolutePath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(GetAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/absolutePath"); assertThat(route, canHandle(GetAnnotatedController.class, "withAbsolutePath")); }
@Test public void addsAPrefixToMethodsWhenTheGetControllerIsAnnotatedWithPath() throws Exception { List<Route> routes = parser.rulesFor(new DefaultResourceClass(GetAnnotatedController.class)); Route route = getRouteMatching(routes, "/prefix/withoutPath"); assertThat(route, canHandle(GetAnnotatedController.class, "withoutPath")); }
@Test(expected=IllegalArgumentException.class) public void throwsExceptionWhenTheGetControllerHasAmbiguousDeclaration() throws Exception { parser.rulesFor(new DefaultResourceClass(WrongGetAnnotatedController.class)); }