Search in sources :

Example 16 with Book

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

the class JSONProviderTest method testWriteUnqualifiedCollection.

@Test
public void testWriteUnqualifiedCollection() throws Exception {
    JSONProvider<List<Book>> p = new JSONProvider<List<Book>>();
    List<Book> books = new ArrayList<>();
    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\":\"\"}]}", os.toString());
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 17 with Book

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

the class JSONProviderTest method doReadUnqualifiedCollection.

@SuppressWarnings("unchecked")
private <T> void doReadUnqualifiedCollection(String data, String mName, Class<T> type) throws Exception {
    JSONProvider<T> provider = new JSONProvider<T>();
    Method m = CollectionsResource.class.getMethod(mName, new Class[] { type });
    ByteArrayInputStream is = new ByteArrayInputStream(data.getBytes());
    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<>(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());
}
Also used : Set(java.util.Set) TreeSet(java.util.TreeSet) Method(java.lang.reflect.Method) ByteArrayInputStream(java.io.ByteArrayInputStream) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) TreeSet(java.util.TreeSet) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList)

Example 18 with Book

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

the class ProviderFactoryAllTest method testCustomJsonProvider.

@Test
public void testCustomJsonProvider() {
    ProviderFactory pf = ServerProviderFactory.getInstance();
    JSONProvider<Book> provider = new JSONProvider<Book>();
    pf.registerUserProvider(provider);
    MessageBodyReader<?> customJsonReader = pf.createMessageBodyReader(Book.class, null, null, MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
    assertSame(customJsonReader, provider);
    MessageBodyWriter<?> customJsonWriter = pf.createMessageBodyWriter(Book.class, null, null, MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
    assertSame(customJsonWriter, provider);
}
Also used : Book(org.apache.cxf.jaxrs.resources.Book) JSONProvider(org.apache.cxf.jaxrs.provider.json.JSONProvider) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 19 with Book

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

the class JSONProviderTest method doTestWriteNullValue.

private void doTestWriteNullValue(boolean nullAsString) throws Exception {
    JSONProvider<Book> provider = new JSONProvider<Book>() {

        protected XMLStreamWriter createWriter(Object actualObject, Class<?> actualClass, Type genericType, String enc, OutputStream os, boolean isCollection) throws Exception {
            return new NullWriter(super.createWriter(actualObject, actualClass, genericType, enc, os, isCollection));
        }
    };
    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 {
        assertTrue(bos.toString().contains("\"state\":null"));
    }
}
Also used : MediaType(javax.ws.rs.core.MediaType) XmlAccessorType(javax.xml.bind.annotation.XmlAccessorType) XmlAccessType(javax.xml.bind.annotation.XmlAccessType) Type(java.lang.reflect.Type) XmlType(javax.xml.bind.annotation.XmlType) ParameterizedType(java.lang.reflect.ParameterizedType) SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 20 with Book

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

the class JAXRSClientFactoryBeanTest method testCreateClientWithTwoUserResources.

@Test
public void testCreateClientWithTwoUserResources() throws Exception {
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress("http://bar");
    UserResource r1 = new UserResource();
    r1.setName(BookStore.class.getName());
    r1.setPath("/store");
    UserOperation op = new UserOperation();
    op.setName("getDescription");
    op.setVerb("GET");
    r1.setOperations(Collections.singletonList(op));
    UserResource r2 = new UserResource();
    r2.setName(Book.class.getName());
    r2.setPath("/book");
    UserOperation op2 = new UserOperation();
    op2.setName("getName");
    op2.setVerb("GET");
    r2.setOperations(Collections.singletonList(op2));
    bean.setModelBeans(r1, r2);
    bean.setServiceClass(Book.class);
    assertTrue(bean.create() instanceof Book);
}
Also used : BookStore(org.apache.cxf.jaxrs.resources.BookStore) UserOperation(org.apache.cxf.jaxrs.model.UserOperation) Book(org.apache.cxf.jaxrs.resources.Book) UserResource(org.apache.cxf.jaxrs.model.UserResource) 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