Search in sources :

Example 11 with TagVO2

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

the class JSONProviderTest method testDropRootElement.

@Test
public void testDropRootElement() throws Exception {
    JSONProvider<TagVO2> p = new JSONProvider<TagVO2>();
    p.setDropRootElement(true);
    p.setIgnoreNamespaces(true);
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://tags", "ns1");
    p.setNamespaceMap(namespaceMap);
    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("{\"group\":\"b\",\"name\":\"a\"}", s);
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 12 with TagVO2

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

the class JSONProviderTest method testOutAttributesAsElementsForList.

@Test
public void testOutAttributesAsElementsForList() throws Exception {
    // Provider
    JSONProvider<List<?>> provider = new JSONProvider<List<?>>();
    provider.setCollectionWrapperName("tagholders");
    Map<String, String> map = new HashMap<>();
    map.put("{http://tags}*", "*");
    provider.setOutTransformElements(map);
    provider.setAttributesToElements(true);
    // data setup
    TagVO2 tag = new TagVO2("A", "B");
    TagVO2Holder holder = new TagVO2Holder();
    holder.setTag(tag);
    List<TagVO2Holder> list = new ArrayList<>();
    list.add(holder);
    // ParameterizedType required for Lists of Objects
    ParameterizedType type = new ParameterizedType() {

        public Type getRawType() {
            return List.class;
        }

        public Type getOwnerType() {
            return null;
        }

        public Type[] getActualTypeArguments() {
            return new Type[] { TagVO2Holder.class };
        }
    };
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(list, ArrayList.class, type, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "{\"tagholders\":[" + "{\"attr\":\"attribute\",\"thetag\":{\"group\":\"B\",\"name\":\"A\"}}" + "]}";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ParameterizedType(java.lang.reflect.ParameterizedType) MediaType(javax.ws.rs.core.MediaType) XmlAccessorType(javax.xml.bind.annotation.XmlAccessorType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 13 with TagVO2

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

the class JSONProviderTest method testOutAppendElementsSameNs.

@Test
public void testOutAppendElementsSameNs() 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://tags}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 = "{\"ns2.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 14 with TagVO2

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

the class JSONProviderTest method doWriteQualifiedCollection.

public void doWriteQualifiedCollection(boolean drop, boolean serializeAsArray, boolean ignoreNamespaces, String data) throws Exception {
    JSONProvider<List<?>> p = new JSONProvider<List<?>>();
    p.setCollectionWrapperName("{http://tags}tag");
    p.setDropCollectionWrapperElement(drop);
    p.setSerializeAsArray(serializeAsArray);
    Map<String, String> namespaceMap = new HashMap<>();
    namespaceMap.put("http://tags", "ns1");
    p.setNamespaceMap(namespaceMap);
    p.setIgnoreNamespaces(ignoreNamespaces);
    List<TagVO2> tags = new ArrayList<>();
    tags.add(createTag2("a", "b"));
    tags.add(createTag2("c", "d"));
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    Method m = CollectionsResource.class.getMethod("getTags", new Class[0]);
    p.writeTo(tags, m.getReturnType(), m.getGenericReturnType(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
    String s = os.toString();
    assertEquals(s, data);
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 15 with TagVO2

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

the class JSONProviderTest method testAttributesAsElementsWithTransform.

@Test
public void testAttributesAsElementsWithTransform() throws Exception {
    JSONProvider<TagVO2Holder> provider = new JSONProvider<TagVO2Holder>() {

        protected XMLStreamWriter createTransformWriterIfNeeded(XMLStreamWriter writer, OutputStream os, boolean dropAtXmlLevel) {
            return TransformUtils.createTransformWriterIfNeeded(writer, os, Collections.emptyMap(), null, Collections.emptyMap(), true, null);
        }
    };
    provider.setIgnoreNamespaces(true);
    TagVO2 tag = new TagVO2("A", "B");
    tag.setAttrInt(123);
    TagVO2Holder holder = new TagVO2Holder();
    holder.setTag(tag);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(holder, TagVO2Holder.class, TagVO2Holder.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
    String expected = "{\"tagholder\":{\"attr\":\"attribute\",\"thetag\":{\"attrInt\":123,\"group\":\"B\",\"name\":\"A\"}}}";
    assertEquals(expected, bos.toString());
}
Also used : TagVO2(org.apache.cxf.jaxrs.resources.TagVO2) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) DelegatingXMLStreamWriter(org.apache.cxf.staxutils.DelegatingXMLStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) 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