users@jersey.java.net

Testing a resource which requires Security Principal & Spring bean injection

From: ManiKanta G <go4mani_at_gmail.com>
Date: Tue, 12 Oct 2010 19:30:01 +0530

Hi,

I m trying to write a test case for testing one of my resource using Jersey
test framework (v1.1.5). My service require both Spring bean injection &
user security principal.
I m able to get the injection work. But not able to get the security
principal work.

My code is similar to:

public class ResourceTest extends JerseyTest {
public ResourceTest() {
 super((new WebAppDescriptor.Builder("com.rest.jersey.resources.")
          .contextParam("contextConfigLocation",
"classpath:ApplicationContext.xml")
          .contextPath("/").servletClass(SpringServlet.class)
          .contextListenerClass(ContextLoaderListener.class)
          .requestListenerClass(RequestContextListener.class).build()));
}

@Before
public void setUser() throws Exception {
SecurityContext.setUserName("user1"); // set the user security context
 }

@Override
    protected TestContainerFactory getTestContainerFactory() {
 // we need GrizzlyWebTestContainerFactory as we need Spring injection
// though this is the default container for testing, mentioned just to be
more verbose
        return new GrizzlyWebTestContainerFactory();
    }
 @Test
 public void testGetXml() {
// TODO: get the Response or some object which has the response headers
 String html =
resource().path("/some").accept(MediaType.TEXT_HTML).get(String.class);
Assert.assertTrue(html.length() > 0); // need to check the resp code
 }
}


But when the test runs, I m getting

Oct 12, 2010 7:23:11 PM com.sun.grizzly.http.servlet.ServletAdapter service
SEVERE: service exception:
java.lang.IllegalStateException: Not yet implemented
at
com.sun.grizzly.http.servlet.HttpServletRequestImpl.isUserInRole(HttpServletRequestImpl.java:852)
 at
com.sun.jersey.spi.container.servlet.WebComponent$1.isUserInRole(WebComponent.java:361)
at
com.sun.jersey.spi.container.ContainerRequest.isUserInRole(ContainerRequest.java:778)
        ..........

When I checked the Grizzly
(grizzly-servlet-webserver-1.9.8.jar)'s
HttpServletRequestImpl.isUserInRole(...),
it is

    public boolean isUserInRole(String role) {
        throw new IllegalStateException("Not yet implemented");
    }


So, is this mean I can't use grizzly-servlet-webserver as test container? If
yes, which can I use for testing a resource which require Spring & security
principal?

Thanks in advance.

ManiKanta G
twitter.com/ManiKantaG