Search in sources :

Example 16 with TagVO2

use of org.apache.cxf.jaxrs.resources.TagVO2 in project cxf by apache.

the class JSONProviderTest method doReadQualifiedCollection.

@SuppressWarnings({ "unchecked", "rawtypes" })
public void doReadQualifiedCollection(String data, boolean isArray) throws Exception {
    JSONProvider provider = new JSONProvider();
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://tags", "ns1");
    provider.setNamespaceMap(namespaceMap);
    Method m = null;
    if (!isArray) {
        m = CollectionsResource.class.getMethod("setTags", new Class[] { List.class });
    } else {
        m = CollectionsResource.class.getMethod("setTagsArray", new Class[] { TagVO2[].class });
    }
    ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
    Object o = provider.readFrom(m.getParameterTypes()[0], m.getGenericParameterTypes()[0], new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, String>(), is);
    assertNotNull(o);
    TagVO2 t1 = null;
    TagVO2 t2 = null;
    if (!isArray) {
        assertEquals(2, ((List<?>) o).size());
        t1 = (TagVO2) ((List<?>) o).get(0);
        t2 = (TagVO2) ((List<?>) o).get(1);
    } else {
        assertEquals(2, ((Object[]) o).length);
        t1 = (TagVO2) ((Object[]) o)[0];
        t2 = (TagVO2) ((Object[]) o)[1];
    }
    assertEquals("a", t1.getName());
    assertEquals("b", t1.getGroup());
    assertEquals("c", t2.getName());
    assertEquals("d", t2.getGroup());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) CollectionsResource(org.apache.cxf.jaxrs.resources.CollectionsResource) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method)

Example 17 with TagVO2

use of org.apache.cxf.jaxrs.resources.TagVO2 in project cxf by apache.

the class JSONProviderTest method testReadFromUnwrappedQualifiedTagRoot.

@Test
public void testReadFromUnwrappedQualifiedTagRoot() throws Exception {
    JSONProvider<TagVO2> p = new JSONProvider<TagVO2>();
    p.setSupportUnwrapped(true);
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://tags", "ns1");
    p.setNamespaceMap(namespaceMap);
    byte[] bytes = "{\"group\":\"b\",\"name\":\"a\"}".getBytes();
    Object tagsObject = p.readFrom(TagVO2.class, null, null, null, null, new ByteArrayInputStream(bytes));
    TagVO2 tag = (TagVO2) tagsObject;
    assertEquals("a", tag.getName());
    assertEquals("b", tag.getGroup());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.Test)

Example 18 with TagVO2

use of org.apache.cxf.jaxrs.resources.TagVO2 in project cxf by apache.

the class JSONProviderTest method doTestReadFromQualifiedTag.

private void doTestReadFromQualifiedTag(String nsSep) throws Exception {
    JSONProvider<TagVO2> p = new JSONProvider<TagVO2>();
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://tags", "ns1");
    p.setNamespaceMap(namespaceMap);
    if (!".".equals(nsSep)) {
        p.setNamespaceSeparator(nsSep);
    }
    byte[] bytes = ("{\"ns1" + nsSep + "thetag\":{\"group\":\"b\",\"name\":\"a\"}}").getBytes();
    Object tagsObject = p.readFrom(TagVO2.class, null, null, null, null, new ByteArrayInputStream(bytes));
    TagVO2 tag = (TagVO2) tagsObject;
    assertEquals("a", tag.getName());
    assertEquals("b", tag.getGroup());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 19 with TagVO2

use of org.apache.cxf.jaxrs.resources.TagVO2 in project cxf by apache.

the class JSONProviderTest method testOutAppendElementsDiffNs.

@Test
public void testOutAppendElementsDiffNs() throws Exception {
    JSONProvider<TagVO2> provider = new JSONProvider<TagVO2>();
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://tags", "ns2");
    provider.setNamespaceMap(namespaceMap);
    Map<String, String> map = new HashMap<>();
    map.put("{http://tags}thetag", "{http://tagsvo2}t");
    provider.setOutAppendElements(map);
    TagVO2 tag = new TagVO2("A", "B");
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(tag, TagVO2.class, TagVO2.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "{\"ps1.t\":{\"ns2.thetag\":{\"group\":\"B\",\"name\":\"A\"}}}";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 20 with TagVO2

use of org.apache.cxf.jaxrs.resources.TagVO2 in project cxf by apache.

the class JSONProviderTest method testWriteToSingleTag2NoNs.

@Test
public void testWriteToSingleTag2NoNs() throws Exception {
    JSONProvider<TagVO2> p = new JSONProvider<TagVO2>();
    p.setIgnoreNamespaces(true);
    TagVO2 tag = createTag2("a", "b");
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    p.writeTo(tag, TagVO2.class, TagVO2.class, TagVO2.class.getAnnotations(), MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
    String s = os.toString();
    assertEquals("{\"thetag\":{\"group\":\"b\",\"name\":\"a\"}}", s);
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

TagVO2 (org.apache.cxf.jaxrs.resources.TagVO2)38 Test (org.junit.Test)31 ByteArrayOutputStream (java.io.ByteArrayOutputStream)28 HashMap (java.util.HashMap)27 LinkedHashMap (java.util.LinkedHashMap)13 ByteArrayInputStream (java.io.ByteArrayInputStream)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Method (java.lang.reflect.Method)4 LinkedList (java.util.LinkedList)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 MediaType (javax.ws.rs.core.MediaType)2 XmlType (javax.xml.bind.annotation.XmlType)2 CollectionsResource (org.apache.cxf.jaxrs.resources.CollectionsResource)2 ParameterizedCollectionType (org.apache.cxf.jaxrs.utils.ParameterizedCollectionType)2 OutputStream (java.io.OutputStream)1 XmlAccessType (javax.xml.bind.annotation.XmlAccessType)1 XmlAccessorType (javax.xml.bind.annotation.XmlAccessorType)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1