Search in sources :

Example 31 with Book

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);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) Test(org.junit.Test)

Example 32 with Book

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);
}
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 33 with Book

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);
}
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 34 with Book

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);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) StringReader(java.io.StringReader) MediaType(javax.ws.rs.core.MediaType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Unmarshaller(javax.xml.bind.Unmarshaller) MessageContextImpl(org.apache.cxf.jaxrs.ext.MessageContextImpl) Test(org.junit.Test)

Example 35 with Book

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());
}
Also used : ServerProviderFactory(org.apache.cxf.jaxrs.provider.ServerProviderFactory) Message(org.apache.cxf.message.Message) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) Exchange(org.apache.cxf.message.Exchange) MetadataMap(org.apache.cxf.jaxrs.impl.MetadataMap) Endpoint(org.apache.cxf.endpoint.Endpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) Book(org.apache.cxf.jaxrs.resources.Book) OperationResourceInfo(org.apache.cxf.jaxrs.model.OperationResourceInfo) List(java.util.List) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl) 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