use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class JSONProviderTest method testWriteBeanNoRootAtJsonLevel.
@Test
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\":"));
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class ProviderFactoryTest method testRegisterMbrMbwProviderAsMbwOnly.
@Test
public void testRegisterMbrMbwProviderAsMbwOnly() {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
JAXBElementProvider<Book> customProvider = new JAXBElementProvider<Book>();
pf.registerUserProvider((Feature) context -> {
context.register(customProvider, MessageBodyWriter.class);
return true;
});
MessageBodyWriter<Book> writer = pf.createMessageBodyWriter(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
assertSame(writer, customProvider);
MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, null, null, MediaType.TEXT_XML_TYPE, new MessageImpl());
assertTrue(reader instanceof JAXBElementProvider);
assertNotSame(reader, customProvider);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class ProviderFactoryTest method testGetComplexProvider.
@Test
public void testGetComplexProvider() throws Exception {
ServerProviderFactory pf = ServerProviderFactory.getInstance();
pf.registerUserProvider(new ComplexMessageBodyReader());
Message m = new MessageImpl();
Exchange ex = new ExchangeImpl();
m.setExchange(ex);
m.put(ProviderFactory.IGNORE_TYPE_VARIABLES, true);
MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, Book.class, null, MediaType.APPLICATION_JSON_TYPE, m);
assertTrue(ComplexMessageBodyReader.class == reader.getClass());
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class XPathProviderTest method testReadFrom.
@Test
public void testReadFrom() throws Exception {
String value = "<Book><name>The Book</name><id>2</id></Book>";
XPathProvider<Book> provider = new XPathProvider<Book>();
provider.setExpression("/Book");
provider.setClassName(Book.class.getName());
provider.setForceDOM(true);
Book book = provider.readFrom(Book.class, null, null, null, null, new ByteArrayInputStream(value.getBytes()));
assertNotNull(book);
assertEquals(2L, book.getId());
assertEquals("The Book", book.getName());
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class XSLTJaxbProviderTest method testWrite.
@Test
public void testWrite() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
provider.setOutTemplate(TEMPLATE_LOCATION);
provider.setMessageContext(new MessageContextImpl(createMessage()));
Book b = new Book();
b.setId(123L);
b.setName("TheBook");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
provider.writeTo(b, Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, Object>(), bos);
Unmarshaller um = provider.getClassContext(Book.class).createUnmarshaller();
Book b2 = (Book) um.unmarshal(new StringReader(bos.toString()));
b.setName("TheBook2");
assertEquals("Transformation is bad", b, b2);
}
Aggregations