use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class XSLTJaxbProviderTest method testWriteWithoutTemplate.
@Test
public void testWriteWithoutTemplate() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
provider.setSupportJaxbOnly(true);
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()));
assertEquals(b, b2);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class XSLTJaxbProviderTest method testRead.
@Test
public void testRead() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
provider.setInTemplate(TEMPLATE_LOCATION);
Book b = new Book();
b.setId(123L);
b.setName("TheBook");
Book b2 = provider.readFrom(Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(BOOK_XML.getBytes()));
b.setName("TheBook2");
assertEquals("Transformation is bad", b, b2);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class XSLTJaxbProviderTest method testReadWithoutTemplate.
@Test
public void testReadWithoutTemplate() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>();
provider.setSupportJaxbOnly(true);
Book b = new Book();
b.setId(123L);
b.setName("TheBook");
Book b2 = provider.readFrom(Book.class, Book.class, b.getClass().getAnnotations(), MediaType.TEXT_XML_TYPE, new MetadataMap<String, String>(), new ByteArrayInputStream(BOOK_XML.getBytes()));
assertEquals("Transformation is bad", b, b2);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class XSLTJaxbProviderTest method testWriteToStreamWriter.
@Test
public void testWriteToStreamWriter() throws Exception {
XSLTJaxbProvider<Book> provider = new XSLTJaxbProvider<Book>() {
@Override
protected XMLStreamWriter getStreamWriter(Object obj, OutputStream os, MediaType mt) {
return StaxUtils.createXMLStreamWriter(os);
}
};
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);
}
use of org.apache.cxf.jaxrs.resources.Book in project cxf by apache.
the class SelectMethodCandidatesTest method testFindFromAbstractGenericImpl4.
@Test
public void testFindFromAbstractGenericImpl4() throws Exception {
JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
sf.setResourceClasses(GenericEntityImpl4.class);
sf.create();
List<ClassResourceInfo> resources = ((JAXRSServiceImpl) sf.getService()).getClassResourceInfos();
String contentTypes = "text/xml";
String acceptContentTypes = "text/xml";
Message m = new MessageImpl();
m.put(Message.CONTENT_TYPE, "text/xml");
Exchange ex = new ExchangeImpl();
ex.setInMessage(m);
m.setExchange(ex);
Endpoint e = EasyMock.createMock(Endpoint.class);
e.isEmpty();
EasyMock.expectLastCall().andReturn(true).anyTimes();
e.size();
EasyMock.expectLastCall().andReturn(0).anyTimes();
e.getEndpointInfo();
EasyMock.expectLastCall().andReturn(null).anyTimes();
e.get(ServerProviderFactory.class.getName());
EasyMock.expectLastCall().andReturn(ServerProviderFactory.getInstance()).times(2);
e.get("org.apache.cxf.jaxrs.comparator");
EasyMock.expectLastCall().andReturn(null);
EasyMock.replay(e);
ex.put(Endpoint.class, e);
MetadataMap<String, String> values = new MetadataMap<String, String>();
OperationResourceInfo ori = findTargetResourceClass(resources, m, "/books", "POST", values, contentTypes, sortMediaTypes(acceptContentTypes));
assertNotNull(ori);
assertEquals("resourceMethod needs to be selected", "postEntity", ori.getMethodToInvoke().getName());
String value = "<Books><Book><name>The Book</name><id>2</id></Book></Books>";
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());
}
Aggregations