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());
}
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());
}
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);
}
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"));
}
}
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);
}
Aggregations