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);
}
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());
}
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());
}
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);
}
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());
}
Aggregations