use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.
the class JAXBElementProviderTest method readSuperBook.
private void readSuperBook(String data, boolean xsiTypeExpected) throws Exception {
if (xsiTypeExpected) {
assertTrue(data.contains("xsi:type"));
}
JAXBElementProvider<SuperBook> provider = new JAXBElementProvider<SuperBook>();
ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
SuperBook book = provider.readFrom(SuperBook.class, SuperBook.class, new Annotation[0], MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), is);
assertEquals(124L, book.getSuperId());
}
use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.
the class JAXBElementProviderTest method testExtraClassWithoutSingleContext.
@Test
public void testExtraClassWithoutSingleContext() throws Exception {
ClassResourceInfo cri = ResourceUtils.createClassResourceInfo(BookStore.class, BookStore.class, true, true);
JAXBElementProvider<SuperBook> provider = new JAXBElementProvider<SuperBook>();
provider.setExtraClass(new Class[] { SuperBook.class });
provider.init(Collections.singletonList(cri));
JAXBContext bookContext = provider.getJAXBContext(Book.class, Book.class);
ByteArrayOutputStream os = new ByteArrayOutputStream();
bookContext.createMarshaller().marshal(new SuperBook("name", 1L, 2L), os);
SuperBook book = (SuperBook) bookContext.createUnmarshaller().unmarshal(new ByteArrayInputStream(os.toByteArray()));
assertEquals(2L, book.getSuperId());
}
use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.
the class JSONProviderTest method testReadListOfDerivedTypesWithNullField.
@Test
public void testReadListOfDerivedTypesWithNullField() throws Exception {
JSONProvider<Books2> p = new JSONProvider<Books2>();
Map<String, String> namespaceMap = new HashMap<>();
namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsins");
p.setNamespaceMap(namespaceMap);
String data = "{\"books2\":{\"books\":{\"@xsins.type\":\"superBook\",\"id\":123," + "\"name\":null,\"superId\":124}}}";
byte[] bytes = data.getBytes();
Object books2Object = p.readFrom(Books2.class, null, null, null, null, new ByteArrayInputStream(bytes));
Books2 books = (Books2) books2Object;
List<? extends Book> list = books.getBooks();
assertEquals(1, list.size());
SuperBook book = (SuperBook) list.get(0);
assertEquals(124L, book.getSuperId());
assertEquals(0, book.getName().length());
}
use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.
the class JSONProviderTest method testReadListOfDerivedTypes.
@Test
public void testReadListOfDerivedTypes() throws Exception {
JSONProvider<Books2> p = new JSONProvider<Books2>();
Map<String, String> namespaceMap = new HashMap<>();
namespaceMap.put("http://www.w3.org/2001/XMLSchema-instance", "xsins");
p.setNamespaceMap(namespaceMap);
String data = "{\"books2\":{\"books\":{\"@xsins.type\":\"superBook\",\"id\":123," + "\"name\":\"CXF Rocks\",\"superId\":124}}}";
byte[] bytes = data.getBytes();
Object books2Object = p.readFrom(Books2.class, null, null, null, null, new ByteArrayInputStream(bytes));
Books2 books = (Books2) books2Object;
List<? extends Book> list = books.getBooks();
assertEquals(1, list.size());
SuperBook book = (SuperBook) list.get(0);
assertEquals(124L, book.getSuperId());
assertEquals(123L, book.getId());
assertEquals("CXF Rocks", book.getName());
}
use of org.apache.cxf.jaxrs.resources.SuperBook in project cxf by apache.
the class JSONProviderTest method testWriteCollectionWithoutXmlRootElement.
@Test
public void testWriteCollectionWithoutXmlRootElement() throws Exception {
JSONProvider<List<SuperBook>> provider = new JSONProvider<List<SuperBook>>();
provider.setCollectionWrapperName("{http://superbooks}SuperBooks");
provider.setJaxbElementClassMap(Collections.singletonMap(SuperBook.class.getName(), "{http://superbooks}SuperBook"));
SuperBook b = new SuperBook("CXF in Action", 123L, 124L);
List<SuperBook> books = Collections.singletonList(b);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(books, List.class, SuperBook.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
String expected = "{\"ns1.SuperBooks\":[{\"id\":123,\"name\":\"CXF in Action\"," + "\"state\":\"\",\"superId\":124}]}";
assertEquals(expected, bos.toString());
}
Aggregations