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);
}
}
}
}
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);
}
Aggregations