users@jersey.java.net

Testing resource class + data in request

From: Rob - <skyscrapper666_at_hotmail.com>
Date: Mon, 9 Nov 2009 19:03:35 +1030

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

 

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)

 

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!!
                                               
_________________________________________________________________
Looking to move this spring? With all the lastest places, searching has never been easier. Look now!
http://clk.atdmt.com/NMN/go/157631292/direct/01/