Package org.apache.cxf.jaxrs.resources

Examples of org.apache.cxf.jaxrs.resources.Book


    public void testReadNullStringAsNull() throws Exception {
       
        String input = "{\"Book\":{\"id\":123,\"name\":\"null\"}}";
   
        JSONProvider<Book> provider = new JSONProvider<Book>();
        Book theBook = provider.readFrom(Book.class, null, null,
                                   null, null, new ByteArrayInputStream(input.getBytes()));
        assertEquals(123L, theBook.getId());
        assertEquals("", theBook.getName());
    }
View Full Code Here


    public void testWriteBeanNoRootAtJsonLevel() throws Exception {
        JSONProvider<Book> provider = new JSONProvider<Book>();
        provider.setDropRootElement(true);
        provider.setDropElementsInXmlStream(false);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        provider.writeTo(new Book("cxf", 123), Book.class, Book.class,
                         new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
                         new MetadataMap<String, Object>(), bos);
        assertTrue(bos.toString().contains("\"name\":\"cxf\""));
        assertTrue(bos.toString().contains("\"id\":123"));
        assertFalse(bos.toString().startsWith("{\"Book\":"));
View Full Code Here

    public void testWriteBeanIgnorePropertyAtJsonLevel() throws Exception {
        JSONProvider<Book> provider = new JSONProvider<Book>();
        provider.setOutDropElements(Collections.singletonList("id"));
        provider.setDropElementsInXmlStream(false);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        provider.writeTo(new Book("cxf", 123), Book.class, Book.class,
                         new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
                         new MetadataMap<String, Object>(), bos);
        assertTrue(bos.toString().contains("\"name\":\"cxf\""));
        assertFalse(bos.toString().contains("\"id\":123"));
        assertTrue(bos.toString().startsWith("{\"Book\":"));
View Full Code Here

            }
        };
        provider.setWriteNullAsString(nullAsString);
       
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        provider.writeTo(new Book("cxf", 123), Book.class, Book.class,
                         new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
                         new MetadataMap<String, Object>(), bos);
        if (nullAsString) {
            assertTrue(bos.toString().contains("\"state\":\"null\""));
        } else {
View Full Code Here

   
    @Test
    public void testWriteBookWithStringConverter() throws Exception {
        JSONProvider<Book> p = new JSONProvider<Book>();
        p.setConvertTypesToStrings(true);
        Book book = new Book("CXF", 125);
       
        ByteArrayOutputStream os = new ByteArrayOutputStream();
       
        p.writeTo(book, Book.class, Book.class, Book.class.getAnnotations(),
                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
View Full Code Here

   
    @Test
    public void testWriteUnqualifiedCollection() throws Exception {
        JSONProvider<List<Book>> p = new JSONProvider<List<Book>>();
        List<Book> books = new ArrayList<Book>();
        books.add(new Book("CXF", 123L));
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        Method m = CollectionsResource.class.getMethod("getBooks", new Class[0]);
        p.writeTo(books, m.getReturnType(), m.getGenericReturnType(), new Annotation[0],
                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
        assertEquals("{\"Book\":[{\"id\":123,\"name\":\"CXF\",\"state\":\"\"}]}",
View Full Code Here

        Object o = provider.readFrom(
                       type, m.getGenericParameterTypes()[0],
                       new Annotation[0], MediaType.APPLICATION_JSON_TYPE,
                       new MetadataMap<String, String>(), is);
        assertNotNull(o);
        Book b1 = null;
        Book b2 = null;
        if (type.isArray()) {
            assertEquals(2, ((Book[])o).length);
            b1 = ((Book[])o)[0];
            b2 = ((Book[])o)[1];
        } else if (type == Set.class) {
            Set<Book> set = CastUtils.cast((Set<?>)o);
            List<Book> books = new ArrayList<Book>(new TreeSet<Book>(set));
            b1 = books.get(0);
            b2 = books.get(1);
        } else {
            List<Book> books = (List<Book>)o;
            b1 = books.get(0);
            b2 = books.get(1);
        }
       
        assertEquals(123, b1.getId());
        assertEquals("CXF in Action", b1.getName());
       
        assertEquals(124, b2.getId());
        assertEquals("CXF Rocks", b2.getName());   
    }
View Full Code Here

        m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
        List<Object> params = JAXRSUtils.processParameters(ori, values, m);
        assertEquals(1, params.size());
        List<?> books = (List<?>)params.get(0);
        assertEquals(1, books.size());
        Book book = (Book)books.get(0);
        assertNotNull(book);
        assertEquals(2L, book.getId());
        assertEquals("The Book", book.getName());
    }
View Full Code Here

       
        String value = "<Book><name>The Book</name><id>2</id></Book>";
        m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
        List<Object> params = JAXRSUtils.processParameters(ori, values, m);
        assertEquals(1, params.size());
        Book book = (Book)params.get(0);
        assertNotNull(book);
        assertEquals(2L, book.getId());
        assertEquals("The Book", book.getName());
    }
View Full Code Here

   
    @Test
    public void testWriteBookWithStringConverter() throws Exception {
        JSONProvider p = new JSONProvider();
        p.setConvertTypesToStrings(true);
        Book book = new Book("CXF", 125);
       
        ByteArrayOutputStream os = new ByteArrayOutputStream();
       
        p.writeTo(book, (Class)Book.class, Book.class, Book.class.getAnnotations(),
                  MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), os);
View Full Code Here

TOP

Related Classes of org.apache.cxf.jaxrs.resources.Book

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.