Search in sources :

Example 1 with SuperBook

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook)

Example 2 with SuperBook

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());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) JAXBContext(javax.xml.bind.JAXBContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 3 with SuperBook

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());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Test(org.junit.Test)

Example 4 with SuperBook

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());
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Test(org.junit.Test)

Example 5 with SuperBook

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());
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

SuperBook (org.apache.cxf.jaxrs.resources.SuperBook)11 Test (org.junit.Test)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JAXBContext (javax.xml.bind.JAXBContext)2 ClassResourceInfo (org.apache.cxf.jaxrs.model.ClassResourceInfo)2 Book (org.apache.cxf.jaxrs.resources.Book)2 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1