Search in sources :

Example 11 with Book

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

the class JAXBElementProviderTest method testSingleJAXBContext.

@Test
public void testSingleJAXBContext() throws Exception {
    ClassResourceInfo cri = ResourceUtils.createClassResourceInfo(JAXBResource.class, JAXBResource.class, true, true);
    JAXBElementProvider<Book> provider = new JAXBElementProvider<Book>();
    provider.setSingleJaxbContext(true);
    provider.init(Collections.singletonList(cri));
    JAXBContext bookContext = provider.getJAXBContext(Book.class, Book.class);
    assertNotNull(bookContext);
    JAXBContext superBookContext = provider.getJAXBContext(SuperBook.class, SuperBook.class);
    assertSame(bookContext, superBookContext);
    JAXBContext book2Context = provider.getJAXBContext(Book2.class, Book2.class);
    assertSame(bookContext, book2Context);
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ClassResourceInfo(org.apache.cxf.jaxrs.model.ClassResourceInfo) JAXBContext(javax.xml.bind.JAXBContext) Test(org.junit.Test)

Example 12 with Book

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

the class ProviderFactoryTest method testGetComplexProvider2.

@Test
public void testGetComplexProvider2() throws Exception {
    ServerProviderFactory pf = ServerProviderFactory.getInstance();
    pf.registerUserProvider(new ComplexMessageBodyReader());
    MessageBodyReader<Book> reader = pf.createMessageBodyReader(Book.class, Book.class, null, MediaType.APPLICATION_JSON_TYPE, new MessageImpl());
    assertTrue(ComplexMessageBodyReader.class == reader.getClass());
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 13 with Book

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

the class SelectMethodCandidatesTest method doTestGenericSuperType.

private void doTestGenericSuperType(Class<?> serviceClass, String methodName) throws Exception {
    JAXRSServiceFactoryBean sf = new JAXRSServiceFactoryBean();
    sf.setResourceClasses(serviceClass);
    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(3);
    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", methodName, values, contentTypes, sortMediaTypes(acceptContentTypes));
    assertNotNull(ori);
    assertEquals("resourceMethod needs to be selected", methodName.toLowerCase() + "Entity", ori.getMethodToInvoke().getName());
    String value = "<Book><name>The Book</name><id>2</id></Book>";
    m.setContent(InputStream.class, new ByteArrayInputStream(value.getBytes()));
    List<Object> params = JAXRSUtils.processParameters(ori, values, m);
    assertEquals(1, params.size());
    Book book = (Book) params.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) MessageImpl(org.apache.cxf.message.MessageImpl) ExchangeImpl(org.apache.cxf.message.ExchangeImpl)

Example 14 with Book

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

the class ResponseImplTest method testGetEntity.

@Test
public void testGetEntity() {
    final Book book = new Book();
    Response r = Response.ok().entity(book).build();
    assertSame(book, r.getEntity());
}
Also used : Response(javax.ws.rs.core.Response) Book(org.apache.cxf.jaxrs.resources.Book) Test(org.junit.Test)

Example 15 with Book

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

the class JSONProviderTest method testDoNotEscapeForwardSlashes.

@Test
public void testDoNotEscapeForwardSlashes() throws Exception {
    JSONProvider<Book> provider = new JSONProvider<Book>();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    provider.writeTo(new Book("http://cxf", 123), Book.class, Book.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE, new MetadataMap<String, Object>(), bos);
    assertTrue(bos.toString().contains("\"name\":\"http://cxf\""));
}
Also used : SuperBook(org.apache.cxf.jaxrs.resources.SuperBook) Book(org.apache.cxf.jaxrs.resources.Book) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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