Search in sources :

Example 21 with Book

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

the class JSONProviderTest method testWriteBeanIgnorePropertyAtJsonLevel.

@Test
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\":"));
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 22 with Book

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

the class JSONProviderTest method testReadNullStringAsNull.

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

Example 23 with Book

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

the class JSONProviderTest method testWriteBookWithStringConverter.

@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);
    String s = os.toString();
    assertEquals("{\"Book\":{\"id\":\"125\",\"name\":\"CXF\",\"state\":\"\"}}", s);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 24 with Book

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

the class JSONProviderTest method testEscapeForwardSlashesAlways.

@Test
public void testEscapeForwardSlashesAlways() throws Exception {
    JSONProvider<Book> provider = new JSONProvider<Book>();
    provider.setEscapeForwardSlashesAlways(true);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(new Book("http://cxf", 123), Book.class, Book.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
    assertTrue(bos.toString().contains("\"name\":\"http:\\/\\/cxf\""));
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 25 with Book

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

the class JSONProviderTest method testReadEmbeddedArray.

@Test
public void testReadEmbeddedArray() throws Exception {
    String input = "{\"store\":" + "{\"books\":{" + "     \"book\":[" + "         {           " + "           \"name\":\"CXF 1\"" + "         },          " + "         {           " + "           \"name\":\"CXF 2\"" + "         }           " + "      ]              " + "   }                 " + " }                   " + "}                    ";
    Object storeObject = new JSONProvider<Store>().readFrom(Store.class, null, null, null, null, new ByteArrayInputStream(input.getBytes()));
    Store store = (Store) storeObject;
    List<Book> books = store.getBooks();
    assertEquals(2, books.size());
    assertEquals("CXF 1", books.get(0).getName());
    assertEquals("CXF 2", books.get(1).getName());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) Test(org.junit.Test)

Aggregations

Book (org.apache.cxf.jaxrs.resources.Book)42 SuperBook (org.apache.cxf.jaxrs.resources.SuperBook)35 Test (org.junit.Test)35 ByteArrayOutputStream (java.io.ByteArrayOutputStream)17 ByteArrayInputStream (java.io.ByteArrayInputStream)15 MessageImpl (org.apache.cxf.message.MessageImpl)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Method (java.lang.reflect.Method)5 MediaType (javax.ws.rs.core.MediaType)5 MetadataMap (org.apache.cxf.jaxrs.impl.MetadataMap)5 Exchange (org.apache.cxf.message.Exchange)5 ExchangeImpl (org.apache.cxf.message.ExchangeImpl)5 Message (org.apache.cxf.message.Message)5 OutputStream (java.io.OutputStream)4 StringReader (java.io.StringReader)4 Annotation (java.lang.annotation.Annotation)4 Response (javax.ws.rs.core.Response)4 Endpoint (org.apache.cxf.endpoint.Endpoint)4 InputStream (java.io.InputStream)3