Search in sources :

Example 6 with Customer

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

the class CxfRsProducerTest method testGetCustomerExceptionWithCxfRsEndpoint.

@Test
public void testGetCustomerExceptionWithCxfRsEndpoint() {
    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();
            // set the Http method
            message.setHeader(Exchange.HTTP_METHOD, "PUT");
            // set the relative path
            message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
            // we just setup the customer with a wrong id
            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) 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) CxfOperationException(org.apache.camel.component.cxf.CxfOperationException) Test(org.junit.Test)

Example 7 with Customer

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

the class CxfOperationExceptionTest method testRestServerDirectlyAddCustomer.

@Test(expected = CamelExecutionException.class)
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:" + PORT1 + "/CxfOperationExceptionTest/customerservice/customers?throwExceptionOnFailure=true", 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 8 with Customer

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

the class CxfRsAsyncProducerTest method testGetCustomerWithVariableReplacementAndCxfRsEndpoint.

@Test
public void testGetCustomerWithVariableReplacementAndCxfRsEndpoint() {
    Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true", newExchange -> {
        newExchange.setPattern(ExchangePattern.InOut);
        Message inMessage = newExchange.getIn();
        inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
        inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/{customerId}");
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_VAR_VALUES, new String[] { "123" });
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
        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));
}
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 9 with Customer

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

the class CxfRsAsyncProducerTest method testGetCustomersWithHttpCentralClientAPI.

@Test
public void testGetCustomersWithHttpCentralClientAPI() {
    Exchange exchange = template.send("direct://proxy", newExchange -> {
        newExchange.setPattern(ExchangePattern.InOut);
        Message inMessage = newExchange.getIn();
        inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
        inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/");
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, List.class);
        inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_GENERIC_TYPE, new ParameterizedCollectionType(Customer.class));
        inMessage.setBody(null);
    });
    // get the response message 
    List<Customer> response = CastUtils.cast((List<?>) exchange.getOut().getBody());
    assertNotNull("The response should not be null ", response);
    assertTrue("Dan is missing!", response.contains(new Customer(113, "Dan")));
    assertTrue("John is missing!", response.contains(new Customer(123, "John")));
    assertEquals("Get a wrong response code", 200, exchange.getOut().getHeader(Exchange.HTTP_RESPONSE_CODE));
}
Also used : Exchange(org.apache.camel.Exchange) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer) ParameterizedCollectionType(org.apache.cxf.jaxrs.utils.ParameterizedCollectionType) Test(org.junit.Test)

Example 10 with Customer

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

the class CxfRsAsyncProducerTest method testGetCustomerWithClientProxyAPI.

@Test
public void testGetCustomerWithClientProxyAPI() {
    // START SNIPPET: ProxyExample
    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, "getCustomer");
            // using the proxy client API
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.FALSE);
            // set a customer header
            inMessage.setHeader("key", "value");
            // setup the accept content type
            inMessage.setHeader(Exchange.ACCEPT_CONTENT_TYPE, "application/json");
            // set the parameters , if you just have one parameter 
            // camel will put this object into an Object[] itself
            inMessage.setBody("123");
        }
    });
    // 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));
    assertEquals("Get a wrong header value", "value", exchange.getOut().getHeader("key"));
// END SNIPPET: ProxyExample     
}
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)

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