users@jersey.java.net

Re: [Jersey] Testing resource class + data in request

From: Paul Sandoz <Paul.Sandoz_at_Sun.COM>
Date: Mon, 09 Nov 2009 10:01:25 +0100

On Nov 9, 2009, at 9:33 AM, Rob - wrote:

> Hi everyone
>
> I've defined the following resource:
>
> @Path("{one}/{two}")
> @Consumes("application/json")
> @Produces("application/json")
> @POST
> public MyBean createBean(List<ObjOne> objList) {
> String one = this.getParams("one");
> String two = this.getParams("two");
> return this.myManager.createBean(one, objList, two);
> }
>
> It basically reads the request (containing JSON data) and then
> converts it to List<ObjOne> so myManager can use for running the
> createBean method.
>
> When unit testing it, I'm doing:
>
> List<ObjOne> objList= new ArrayList<ObjOne>();
> objList.add(new ObjOne("One",null));
> objList.add(new ObjOne("Two",null));
> objList.add(new ObjOne("Three",null));
> objList.add(new ObjOne("Four",null));
> objList.add(new ObjOne("Five",null));
>
> this.webResource.path("one/
> two").accept(MediaType.APPLICATION_JSON).post(objList);
>

IIRC you want to preserve the generic type information for the list
instance you are sending as the request entity. Try:

MyBean myBean = this.webResource.path("one/
two").accept(MediaType.APPLICATION_JSON).post(
   MyBean.class, new GenericEntity<List<ObjOne>>(objList){});


> It fails with the following:
>
> com.sun.jersey.api.client.ClientHandlerException:
> java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
> at
> com
> .sun
> .jersey
> .client
> .urlconnection
> .URLConnectionClientHandler.handle(URLConnectionClientHandler.java:
> 128)
>

Any further exceptions logged as to the cause?

Paul.

> I tried adding the following:
>
> GenericType<List<ObjOne>> genericType = new
> GenericType<List<ObjOne>>(){};
>
> and changing the next line to be:
>
> this.webResource.path("one/
> two").accept(MediaType.APPLICATION_JSON).post(generictType, objList);
>
> but I still get:
> com.sun.jersey.api.client.ClientHandlerException:
> java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
> at
> com
> .sun
> .jersey
> .client
> .urlconnection
> .URLConnectionClientHandler.handle(URLConnectionClientHandler.java:
> 128)
> at com.sun.jersey.api.client.Client.handle(Client.java:454)
> at com.sun.jersey.api.client.WebResource.handle(WebResource.java:568)
> at com.sun.jersey.api.client.WebResource.access
> $400(WebResource.java:69)
> at com.sun.jersey.api.client.WebResource
> $Builder.post(WebResource.java:503)
>
> Even worse, if I change the last line to be:
>
> MyBean sampleBean = this.webResource.path("one/
> two").accept(MediaType.APPLICATION_JSON).post(generictType, objList);
>
> The compiler chokes with an incomptible type error (which I was
> expecting anyways).
>
> If I just run a .post() as in:
>
> this.webResource.path("one/
> two").accept(MediaType.APPLICATION_JSON).post();
>
> I get a 415 error (which of course makes sense, since no data is
> being passed...)
>
>
> So in short, when unit testing, how can I pass a List<T> as
> parameter for my resource and get a return object of MyBean type?
>
> Any ideas?? Thanks in advance!!
>
> With all the lastest places, searching has never been easier. Look
> now! Looking to move this spring?