users@jersey.java.net

Re: [Jersey] Testing a resource which requires Security Principal & Spring bean injection

From: Paul Sandoz <Paul.Sandoz_at_oracle.com>
Date: Wed, 13 Oct 2010 12:42:22 +0200

On Oct 13, 2010, at 7:43 AM, ManiKanta G wrote:

> Thanks Srinivas.
>
> I m using that one now. Just want to know what I thought was correct
> or not.
>

The Grizzly servlet implementation has some limitations. How are you
defining your security requirements? because if you are using the web
app descriptor builder it does not create a full representation of a
web.xml where security constraints could be defined and used say with
Jetty or GlassFish.

Paul.


> Regards,
> ManiKanta G
> twitter.com/ManiKantaG
>
>
> On Wed, Oct 13, 2010 at 9:46 AM, Srinivas Naresh Bhimisetty <shri.naresh_at_gmail.com
> > wrote:
> Manikanta,
>
> may be you can try the EmbeddedGlassFish or some external
> container, say GlassFish.
>
> - Naresh
>
> On Tue, Oct 12, 2010 at 7:30 PM, ManiKanta G <go4mani_at_gmail.com>
> wrote:
>
> 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
>
>