leoScomme
leoScomme

Reputation: 109

Annotation Retrieval given the URI

I want to retrieve the annotations of a service ( in particular @RolesAllowed ) given the URI corresponding to the service. Here an example:

The service:

@GET
@Path("/example")
@RolesAllowed({ "BASIC_USER", "ADMIN" })
public Response foo() {
    //Service implementation 
}

I want to retrieve { "BASIC_USER", "ADMIN" } given the String "/example".

I use RestAssured for testing so, if possible, I prefer a solution with the latter. Thank you.

Upvotes: 0

Views: 139

Answers (1)

Robert
Robert

Reputation: 488

I am not familiar with RestAssured, but I wrote the following Junit test and it works. Perhaps you can adapt it to work with RestAssured.

First the Service:

public class Service {

  @GET
  @Path("/example")
  @RolesAllowed({ "BASIC_USER", "ADMIN" })
  public Response foo() {
    return new Response();
}

}

And this is the corresponding Junit test:

@Test
public void testFooRoles() throws Exception {
    Method method = Service.class.getMethod("foo");
    Annotation path = method.getDeclaredAnnotation(javax.ws.rs.Path.class);
    assertTrue(((Path) path).value().equals("/example"));

    RolesAllowed annotation = method.getDeclaredAnnotation(RolesAllowed.class);
    List<String> roles = Arrays.asList(annotation.value());
    assertEquals(2, roles.size());
    assertTrue(roles.contains("BASIC_USER"));
    assertTrue(roles.contains("ADMIN"));
}

Upvotes: 1

Related Questions