use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsConsumerTest method testEchoCustomerDefaultHandlerAndModel.
@Test
public void testEchoCustomerDefaultHandlerAndModel() throws Exception {
WebTarget target = ClientBuilder.newClient().target("http://localhost:" + CXT + "/" + "rest4" + "/customerservice/customers");
Customer c = target.request(MediaType.APPLICATION_JSON).post(Entity.json(new Customer(333, "Barry")), Customer.class);
assertEquals(333L, c.getId());
assertEquals("Barry", c.getName());
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsProducerTest method testGetCustomerWithCxfRsEndpoint.
@Test
public void testGetCustomerWithCxfRsEndpoint() {
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, "GET");
// set the relative path
inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123");
// Specify the response class , cxfrs will use InputStream as the response object type
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
// since we use the Get method, so we don't need to set the message body
inMessage.setBody(null);
}
});
// get the response message
Customer response = (Customer) exchange.getOut().getBody();
assertNotNull("The response should not be null ", response);
assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
assertEquals("Get a wrong customer name", response.getName(), "John");
assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsProducerTest method testAddCustomerUniqueResponseCode.
@Test
public void testAddCustomerUniqueResponseCode() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "?httpClientAPI=true&synchronous=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 CxfRsProducerTest method testGetCustomerWithVariableReplacementAndCxfRsEndpoint.
@Test
public void testGetCustomerWithVariableReplacementAndCxfRsEndpoint() {
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, "GET");
// set the relative path
inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/{customerId}");
// Set variables for replacement
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, new String[] { "123" });
// Specify the response class , cxfrs will use InputStream as the response object type
inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
// since we use the Get method, so we don't need to set the message body
inMessage.setBody(null);
}
});
// get the response message
Customer response = (Customer) exchange.getOut().getBody();
assertNotNull("The response should not be null ", response);
assertEquals("Get a wrong customer id ", String.valueOf(response.getId()), "123");
assertEquals("Get a wrong customer name", response.getName(), "John");
assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
use of org.apache.camel.component.cxf.jaxrs.testbean.Customer in project camel by apache.
the class CxfRsProducerTest method testProducer422Response.
@Test
public void testProducer422Response() {
Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true&synchronous=true", new Processor() {
public void process(Exchange exchange) throws Exception {
exchange.setPattern(ExchangePattern.InOut);
Message message = exchange.getIn();
// Try to create a new Customer with an invalid name
message.setHeader(Exchange.HTTP_METHOD, "POST");
message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
Customer customer = new Customer();
customer.setId(8888);
// will trigger a 422 response (a common REST server validation response code)
customer.setName("");
message.setBody(customer);
}
});
assertNotNull("Expect the exception here", exchange.getException());
assertThat("Exception should be a CxfOperationException", exchange.getException(), instanceOf(CxfOperationException.class));
CxfOperationException cxfOperationException = CxfOperationException.class.cast(exchange.getException());
assertThat("CXF operation exception has correct response code", cxfOperationException.getStatusCode(), is(422));
}
Aggregations