Search in sources :

Example 6 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class EntityTypesTest method testJAXBListRepresentationFastInfoset.

/**
     * TODO, the unmarshalling fails.
     */
@Test
@Ignore("TODO: unignore once fi support implemented (JERSEY-1190)")
public // TODO: unignore once fi support implemented (JERSEY-1190)
void testJAXBListRepresentationFastInfoset() {
    final WebTarget target = target("JAXBListResourceFastInfoset");
    Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/fastinfoset"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/fastinfoset"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new LinkedList<>(a);
    b = target.path("queue").request().post(Entity.entity(new GenericEntity<Queue<JaxbBean>>((Queue<JaxbBean>) a) {
    }, "application/fastinfoset"), new GenericType<Queue<JaxbBean>>() {
    });
    assertEquals(a, b);
    a = new HashSet<>(a);
    b = target.path("set").request().post(Entity.entity(new GenericEntity<Set<JaxbBean>>((Set<JaxbBean>) a) {
    }, "application/fastinfoset"), new GenericType<Set<JaxbBean>>() {
    });
    final Comparator<JaxbBean> c = new Comparator<JaxbBean>() {

        @Override
        public int compare(final JaxbBean t, final JaxbBean t1) {
            return t.value.compareTo(t1.value);
        }
    };
    final TreeSet<JaxbBean> t1 = new TreeSet<>(c);
    final TreeSet<JaxbBean> t2 = new TreeSet<>(c);
    t1.addAll(a);
    t2.addAll(b);
    assertEquals(t1, t2);
    final Stack<JaxbBean> s = new Stack<>();
    s.addAll(a);
    b = target.path("stack").request().post(Entity.entity(new GenericEntity<Stack<JaxbBean>>(s) {
    }, "application/fastinfoset"), new GenericType<Stack<JaxbBean>>() {
    });
    assertEquals(s, b);
    a = new MyArrayList<>(a);
    b = target.path("custom").request().post(Entity.entity(new GenericEntity<MyArrayList<JaxbBean>>((MyArrayList<JaxbBean>) a) {
    }, "application/fastinfoset"), new GenericType<MyArrayList<JaxbBean>>() {
    });
    assertEquals(a, b);
}
Also used : GenericType(javax.ws.rs.core.GenericType) Set(java.util.Set) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Comparator(java.util.Comparator) Stack(java.util.Stack) GenericEntity(javax.ws.rs.core.GenericEntity) TreeSet(java.util.TreeSet) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Queue(java.util.Queue) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class EntityTypesTest method testJAXBListRepresentationJSONMediaType.

@Test
public void testJAXBListRepresentationJSONMediaType() throws Exception {
    final WebTarget target = target("JAXBListResourceJSONMediaType");
    final Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
// TODO: would be nice to produce/consume a real JSON array like following
// instead of what we have now:
//        JSONArray a = r.get(JSONArray.class);
//        JSONArray b = new JSONArray().
//                put(new JSONObject().put("value", "one")).
//                put(new JSONObject().put("value", "two")).
//                put(new JSONObject().put("value", "three"));
//        assertEquals(a.toString(), b.toString());
//        JSONArray c = r.post(JSONArray.class, b);
//        assertEquals(a.toString(), c.toString());
}
Also used : GenericType(javax.ws.rs.core.GenericType) GenericEntity(javax.ws.rs.core.GenericEntity) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 8 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class JsonMoxyTest method testJAXBListRepresentationJSONMediaType.

@Test
public void testJAXBListRepresentationJSONMediaType() throws Exception {
    final WebTarget target = target("JAXBListResourceJSONMediaType");
    final Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/foo+json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
}
Also used : GenericType(javax.ws.rs.core.GenericType) GenericEntity(javax.ws.rs.core.GenericEntity) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Example 9 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class JsonMoxyTest method testJAXBElementBeanJSONRepresentation.

@Test
public void testJAXBElementBeanJSONRepresentation() {
    final WebTarget target = target("JAXBElementBeanJSONResource");
    final GenericType<JAXBElement<String>> genericType = new GenericType<JAXBElement<String>>() {
    };
    final GenericEntity<JAXBElement<String>> jaxbElementGenericEntity = new GenericEntity<>(new JAXBElement<>(new QName("test"), String.class, "CONTENT"), genericType.getType());
    final Response rib = target.request().post(Entity.entity(jaxbElementGenericEntity, "application/json"));
    // TODO: the following would not be needed if i knew how to workaround JAXBElement<String>.class literal
    final byte[] inBytes = getRequestEntity();
    final byte[] outBytes = getEntityAsByteArray(rib);
    assertEquals(new String(outBytes), inBytes.length, outBytes.length);
    for (int i = 0; i < inBytes.length; i++) {
        if (inBytes[i] != outBytes[i]) {
            assertEquals("Index: " + i, inBytes[i], outBytes[i]);
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) GenericType(javax.ws.rs.core.GenericType) GenericEntity(javax.ws.rs.core.GenericEntity) QName(javax.xml.namespace.QName) WebTarget(javax.ws.rs.client.WebTarget) JAXBElement(javax.xml.bind.JAXBElement) Test(org.junit.Test)

Example 10 with GenericEntity

use of javax.ws.rs.core.GenericEntity in project jersey by jersey.

the class JsonMoxyTest method testJAXBListRepresentationJSONCollection.

@Test
public void testJAXBListRepresentationJSONCollection() throws Exception {
    final WebTarget target = target("JAXBListResourceJSON");
    final Collection<JaxbBean> a = target.request().get(new GenericType<Collection<JaxbBean>>() {
    });
    Collection<JaxbBean> b = target.request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
    b = target.path("type").request().post(Entity.entity(new GenericEntity<Collection<JaxbBean>>(a) {
    }, "application/json"), new GenericType<Collection<JaxbBean>>() {
    });
    assertEquals(a, b);
}
Also used : GenericType(javax.ws.rs.core.GenericType) GenericEntity(javax.ws.rs.core.GenericEntity) Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Test(org.junit.Test)

Aggregations

GenericEntity (javax.ws.rs.core.GenericEntity)27 WebTarget (javax.ws.rs.client.WebTarget)13 GenericType (javax.ws.rs.core.GenericType)13 List (java.util.List)12 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)11 Collection (java.util.Collection)10 Produces (javax.ws.rs.Produces)9 GET (javax.ws.rs.GET)7 Path (javax.ws.rs.Path)7 Response (javax.ws.rs.core.Response)5 IOException (java.io.IOException)4 Comparator (java.util.Comparator)4 HashSet (java.util.HashSet)4 LinkedList (java.util.LinkedList)4 Queue (java.util.Queue)4 Set (java.util.Set)4 Stack (java.util.Stack)4 TreeSet (java.util.TreeSet)4 WebApplicationException (javax.ws.rs.WebApplicationException)4