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());
}
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());
}
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());
}
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());
}
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);
}
});
}
};
}
Aggregations