use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsAsyncProducerTest method testGetCustomerExceptionWithCxfRsEndpoint.
@Test
public void testGetCustomerExceptionWithCxfRsEndpoint() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true", newExchange -> {
newExchange.setPattern(ExchangePattern.InOut);
Message message = newExchange.getIn();
message.setHeader(Exchange.HTTP_METHOD, "PUT");
message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
Customer customer = new Customer();
customer.setId(222);
customer.setName("user");
message.setBody(customer);
});
// we should get the exception here
assertNotNull("Expect the exception here", exchange.getException());
CxfOperationException exception = (CxfOperationException) exchange.getException();
assertEquals("Get a wrong response body", "Cannot find the customer!", exception.getResponseBody());
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsAsyncProducerTest method testRestServerDirectlyAddCustomer.
@Test
public void testRestServerDirectlyAddCustomer() {
Customer input = new Customer();
input.setName("Donald Duck");
// we cannot convert directly to Customer as we need camel-jaxb
String response = template.requestBodyAndHeader("cxfrs:http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/customerservice/customers", input, Exchange.HTTP_METHOD, "POST", String.class);
assertNotNull(response);
assertTrue(response.endsWith("<name>Donald Duck</name></Customer>"));
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsAsyncProducerTest method testAddCustomerUniqueResponseCode.
@Test
public void testAddCustomerUniqueResponseCode() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "?httpClientAPI=true", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
// set the Http method
inMessage.setHeader(Exchange.HTTP_METHOD, "POST");
// set the relative path
inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customersUniqueResponseCode");
// put the response's entity into out message body
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
// create a new customer object
Customer customer = new Customer();
customer.setId(8888);
customer.setName("Willem");
inMessage.setBody(customer);
}
});
// get the response message
Customer response = (Customer) exchange.getOut().getBody();
assertNotNull("The response should not be null ", response);
assertTrue("Get a wrong customer id ", response.getId() != 8888);
assertEquals("Get a wrong customer name", response.getName(), "Willem");
assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsAsyncProducerTest method testSuppressGetCustomerExceptionWithCxfRsEndpoint.
@Test
public void testSuppressGetCustomerExceptionWithCxfRsEndpoint() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true&throwExceptionOnFailure=false", newExchange -> {
newExchange.setPattern(ExchangePattern.InOut);
Message message = newExchange.getIn();
message.setHeader(Exchange.HTTP_METHOD, "PUT");
message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
Customer customer = new Customer();
customer.setId(222);
customer.setName("user");
message.setBody(customer);
});
// we should get the exception here
assertNull("Don't expect the exception here", exchange.getException());
Message result = exchange.getOut();
assertEquals("Get a wrong http status code.", result.getHeader(Exchange.HTTP_RESPONSE_CODE), 406);
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsAsyncProducerTest method testAddCustomerUniqueResponseCodeWithProxyAPI.
@Test
public void testAddCustomerUniqueResponseCodeWithProxyAPI() {
Exchange exchange = template.send("direct://proxy", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message inMessage = exchange.getIn();
// set the operation name
inMessage.setHeader(CxfConstants.OPERATION_NAME, "addCustomerUniqueResponseCode");
// using the proxy client API
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
// set the parameters , if you just have one parameter
// camel will put this object into an Object[] itself
Customer customer = new Customer();
customer.setId(8888);
customer.setName("ProxyAPI");
inMessage.setBody(customer);
}
});
// get the response message
Response response = (Response) exchange.getOut().getBody();
assertNotNull("The response should not be null ", response);
assertNotNull("The response entity should not be null", response.getEntity());
// check the response code
assertEquals("Get a wrong response code", 201, response.getStatus());
// check the response code from message header
assertEquals("Get a wrong response code", 201, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
Aggregations