Search in sources :

Example 1 with CustomerList

use of org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList in project camel by apache.

the class CxfRsConsumerSimpleBindingTest method testListVipCustomers.

@Test
public void testListVipCustomers() throws Exception {
    HttpGet get = new HttpGet("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/vip/gold");
    get.addHeader("Content-Type", "text/xml");
    get.addHeader("Accept", "text/xml");
    HttpResponse response = httpclient.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    CustomerList cl = (CustomerList) jaxb.createUnmarshaller().unmarshal(new StringReader(EntityUtils.toString(response.getEntity())));
    List<Customer> vips = cl.getCustomers();
    assertEquals(2, vips.size());
    assertEquals(123, vips.get(0).getId());
    assertEquals(456, vips.get(1).getId());
}
Also used : Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) HttpGet(org.apache.http.client.methods.HttpGet) StringReader(java.io.StringReader) HttpResponse(org.apache.http.HttpResponse) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList) Test(org.junit.Test)

Example 2 with CustomerList

use of org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList in project camel by apache.

the class CxfRsConsumerSimpleBindingTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        public void configure() {
            from(CXF_RS_ENDPOINT_URI).recipientList(simple("direct:${header.operationName}"));
            from("direct:getCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertNotNull(exchange.getIn().getHeader("id"));
                    long id = exchange.getIn().getHeader("id", Long.class);
                    if (id == 123) {
                        assertEquals("123", exchange.getIn().getHeader("id"));
                        assertEquals(MessageContentsList.class, exchange.getIn().getBody().getClass());
                        exchange.getOut().setBody(new Customer(123, "Raul"));
                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 200);
                    } else if (id == 456) {
                        exchange.getOut().setHeader(Exchange.HTTP_RESPONSE_CODE, 404);
                    } else {
                        fail();
                    }
                }
            });
            from("direct:updateCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertEquals(123, c.getId());
                    assertNotNull(c);
                }
            });
            from("direct:newCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertNotNull(c);
                    assertEquals(123, c.getId());
                }
            });
            from("direct:listVipCustomers").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals(MessageContentsList.class, exchange.getIn().getBody().getClass());
                    assertEquals(0, exchange.getIn().getBody(MessageContentsList.class).size());
                    CustomerList response = new CustomerList();
                    List<Customer> list = new ArrayList<Customer>(2);
                    list.add(new Customer(123, "Raul"));
                    list.add(new Customer(456, "Raul2"));
                    response.setCustomers(list);
                    exchange.getOut().setBody(response);
                }
            });
            from("direct:updateVipCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    Customer c = exchange.getIn().getBody(Customer.class);
                    assertEquals(123, c.getId());
                    assertNotNull(c);
                }
            });
            from("direct:deleteVipCustomer").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("gold", exchange.getIn().getHeader("status", String.class));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                }
            });
            from("direct:uploadImageInputStream").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertEquals("image/jpeg", exchange.getIn().getHeader("Content-Type"));
                    assertTrue(InputStream.class.isAssignableFrom(exchange.getIn().getBody().getClass()));
                    InputStream is = exchange.getIn().getBody(InputStream.class);
                    is.close();
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:uploadImageDataHandler").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertEquals("image/jpeg", exchange.getIn().getHeader("Content-Type"));
                    assertTrue(DataHandler.class.isAssignableFrom(exchange.getIn().getBody().getClass()));
                    DataHandler dh = exchange.getIn().getBody(DataHandler.class);
                    assertEquals("image/jpeg", dh.getContentType());
                    dh.getInputStream().close();
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:multipartPostWithParametersAndPayload").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("abcd", exchange.getIn().getHeader("query"));
                    assertEquals("123", exchange.getIn().getHeader("id"));
                    assertNotNull(exchange.getIn().getAttachment("part1"));
                    assertNotNull(exchange.getIn().getAttachment("part2"));
                    assertNull(exchange.getIn().getHeader("part1"));
                    assertNull(exchange.getIn().getHeader("part2"));
                    assertEquals(Customer.class, exchange.getIn().getHeader("body").getClass());
                    exchange.getOut().setBody(null);
                }
            });
            from("direct:multipartPostWithoutParameters").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertNotNull(exchange.getIn().getAttachment("part1"));
                    assertNotNull(exchange.getIn().getAttachment("part2"));
                    assertNull(exchange.getIn().getHeader("part1"));
                    assertNull(exchange.getIn().getHeader("part2"));
                    assertEquals(Customer.class, exchange.getIn().getHeader("body").getClass());
                    exchange.getOut().setBody(null);
                }
            });
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) MessageContentsList(org.apache.cxf.message.MessageContentsList) Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) InputStream(java.io.InputStream) MessageContentsList(org.apache.cxf.message.MessageContentsList) ArrayList(java.util.ArrayList) List(java.util.List) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList) DataHandler(javax.activation.DataHandler) CustomerList(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList)

Aggregations

Customer (org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer)2 CustomerList (org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList)2 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataHandler (javax.activation.DataHandler)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 MessageContentsList (org.apache.cxf.message.MessageContentsList)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 Test (org.junit.Test)1