Search in sources :

Example 1 with RestServlet

use of password.pwm.ws.server.RestServlet in project pwm by pwm-project.

the class RestServletTest method testActionHandlerReturnTypes.

@Test
public void testActionHandlerReturnTypes() throws IllegalAccessException, InstantiationException {
    final Set<Class<? extends RestServlet>> classMap = getClasses();
    for (final Class<? extends RestServlet> restServlet : classMap) {
        if (restServlet.getAnnotation(RestWebServer.class) == null) {
            Assert.fail(restServlet.getName() + " is missing annotation type of " + RestWebServer.class.getName());
        }
        Collection<Method> methods = JavaHelper.getAllMethodsForClass(restServlet);
        final Set<RestMethodHandler> seenHandlers = new HashSet<>();
        for (final Method method : methods) {
            final RestMethodHandler methodHandler = method.getAnnotation(RestMethodHandler.class);
            if (methodHandler != null) {
                final String returnTypeName = method.getReturnType().getName();
                final RestMethodHandler restMethodHandler = method.getAnnotation(RestMethodHandler.class);
                if (!returnTypeName.equals(RestResultBean.class.getName())) {
                    boolean requiresRestResultBeanReturnType = true;
                    if (restMethodHandler != null) {
                        if (Arrays.asList(restMethodHandler.produces()).contains(HttpContentType.plain)) {
                            requiresRestResultBeanReturnType = false;
                        }
                    }
                    if (requiresRestResultBeanReturnType) {
                        Assert.fail("method " + restServlet.getName() + ":" + method.getName() + " should have return type of " + RestResultBean.class.getName());
                    }
                }
                final Class[] paramTypes = method.getParameterTypes();
                if (paramTypes == null || paramTypes.length != 1) {
                    Assert.fail("method " + restServlet.getName() + ":" + method.getName() + " should have exactly one parameter");
                }
                final String paramTypeName = paramTypes[0].getName();
                if (!paramTypeName.equals(RestRequest.class.getName())) {
                    Assert.fail("method " + restServlet.getName() + ":" + method.getName() + " parameter type must be type " + RestRequest.class.getName());
                }
                if (seenHandlers.contains(methodHandler)) {
                    Assert.fail("duplicate " + RestMethodHandler.class + " assertions on class " + restServlet.getName());
                }
                seenHandlers.add(methodHandler);
            }
        }
    }
}
Also used : Method(java.lang.reflect.Method) RestServlet(password.pwm.ws.server.RestServlet) RestMethodHandler(password.pwm.ws.server.RestMethodHandler) RestWebServer(password.pwm.ws.server.RestWebServer) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with RestServlet

use of password.pwm.ws.server.RestServlet in project pwm by pwm-project.

the class RestServletTest method getClasses.

private Set<Class<? extends RestServlet>> getClasses() {
    Reflections reflections = new Reflections(new ConfigurationBuilder().setUrls(ClasspathHelper.forPackage("password.pwm")).setScanners(new SubTypesScanner(), new TypeAnnotationsScanner(), new FieldAnnotationsScanner()));
    Set<Class<? extends RestServlet>> classes = reflections.getSubTypesOf(RestServlet.class);
    return Collections.unmodifiableSet(classes);
}
Also used : ConfigurationBuilder(org.reflections.util.ConfigurationBuilder) FieldAnnotationsScanner(org.reflections.scanners.FieldAnnotationsScanner) SubTypesScanner(org.reflections.scanners.SubTypesScanner) TypeAnnotationsScanner(org.reflections.scanners.TypeAnnotationsScanner) RestServlet(password.pwm.ws.server.RestServlet) Reflections(org.reflections.Reflections)

Aggregations

RestServlet (password.pwm.ws.server.RestServlet)2 Method (java.lang.reflect.Method)1 HashSet (java.util.HashSet)1 Test (org.junit.Test)1 Reflections (org.reflections.Reflections)1 FieldAnnotationsScanner (org.reflections.scanners.FieldAnnotationsScanner)1 SubTypesScanner (org.reflections.scanners.SubTypesScanner)1 TypeAnnotationsScanner (org.reflections.scanners.TypeAnnotationsScanner)1 ConfigurationBuilder (org.reflections.util.ConfigurationBuilder)1 RestMethodHandler (password.pwm.ws.server.RestMethodHandler)1 RestWebServer (password.pwm.ws.server.RestWebServer)1