Search in sources :

Example 1 with Customer

use of org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer 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 Customer

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

the class CxfRsConsumerSimpleBindingTest method testGetCustomerOnlyHeaders.

@Test
public void testGetCustomerOnlyHeaders() throws Exception {
    HttpGet get = new HttpGet("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/123");
    get.addHeader("Accept", "text/xml");
    HttpResponse response = httpclient.execute(get);
    assertEquals(200, response.getStatusLine().getStatusCode());
    Customer entity = (Customer) jaxb.createUnmarshaller().unmarshal(response.getEntity().getContent());
    assertEquals(123, entity.getId());
}
Also used : Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) Test(org.junit.Test)

Example 3 with Customer

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

the class CxfRsConsumerSimpleBindingTest method testMultipartPostWithParametersAndPayload.

@Test
public void testMultipartPostWithParametersAndPayload() throws Exception {
    HttpPost post = new HttpPost("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/multipart/123?query=abcd");
    MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.STRICT);
    builder.addBinaryBody("part1", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
    builder.addBinaryBody("part2", new File(this.getClass().getClassLoader().getResource("java.jpg").toURI()), ContentType.create("image/jpeg"), "java.jpg");
    StringWriter sw = new StringWriter();
    jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
    builder.addTextBody("body", sw.toString(), ContentType.create("text/xml", Consts.UTF_8));
    post.setEntity(builder.build());
    HttpResponse response = httpclient.execute(post);
    assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MultipartEntityBuilder(org.apache.http.entity.mime.MultipartEntityBuilder) StringWriter(java.io.StringWriter) Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) HttpResponse(org.apache.http.HttpResponse) File(java.io.File) Test(org.junit.Test)

Example 4 with Customer

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

the class CxfRsConsumerSimpleBindingTest method testUpdateCustomerBodyAndHeaders.

@Test
public void testUpdateCustomerBodyAndHeaders() throws Exception {
    HttpPut put = new HttpPut("http://localhost:" + PORT_PATH + "/rest/customerservice/customers/123");
    StringWriter sw = new StringWriter();
    jaxb.createMarshaller().marshal(new Customer(123, "Raul"), sw);
    put.setEntity(new StringEntity(sw.toString()));
    put.addHeader("Content-Type", "text/xml");
    put.addHeader("Accept", "text/xml");
    HttpResponse response = httpclient.execute(put);
    assertEquals(200, response.getStatusLine().getStatusCode());
}
Also used : StringEntity(org.apache.http.entity.StringEntity) StringWriter(java.io.StringWriter) Customer(org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer) HttpResponse(org.apache.http.HttpResponse) HttpPut(org.apache.http.client.methods.HttpPut) Test(org.junit.Test)

Example 5 with Customer

use of org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.Customer 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)8 HttpResponse (org.apache.http.HttpResponse)7 Test (org.junit.Test)7 StringWriter (java.io.StringWriter)5 HttpPost (org.apache.http.client.methods.HttpPost)3 StringEntity (org.apache.http.entity.StringEntity)3 File (java.io.File)2 CustomerList (org.apache.camel.component.cxf.jaxrs.simplebinding.testbean.CustomerList)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPut (org.apache.http.client.methods.HttpPut)2 MultipartEntityBuilder (org.apache.http.entity.mime.MultipartEntityBuilder)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