Search in sources :

Example 16 with Customer

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());
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 17 with Customer

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>"));
}
Also used : Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) Test(org.junit.Test)

Example 18 with 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));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 19 with Customer

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);
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) Test(org.junit.Test)

Example 20 with Customer

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));
}
Also used : Exchange(org.apache.camel.Exchange) Response(javax.ws.rs.core.Response) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Aggregations

Customer (org.apache.camel.component.cxf.jaxrs.testbean.Customer)33 Test (org.junit.Test)31 Exchange (org.apache.camel.Exchange)27 Message (org.apache.camel.Message)25 Processor (org.apache.camel.Processor)16 CxfOperationException (org.apache.camel.component.cxf.CxfOperationException)16 Response (javax.ws.rs.core.Response)4 WebTarget (javax.ws.rs.client.WebTarget)1 WebClient (org.apache.cxf.jaxrs.client.WebClient)1 ParameterizedCollectionType (org.apache.cxf.jaxrs.utils.ParameterizedCollectionType)1