Search in sources :

Example 21 with Customer

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

the class CxfRsAsyncProducerTest method testGetCustomerWithCxfRsEndpoint.

@Test
public void testGetCustomerWithCxfRsEndpoint() {
    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/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 22 with Customer

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

the class CxfRsAsyncProducerTest method testProducer422Response.

@Test
public void testProducer422Response() {
    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, "POST");
        message.setHeader(Exchange.HTTP_PATH, "/customerservice/customers");
        Customer customer = new Customer();
        customer.setId(8888);
        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));
}
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 23 with Customer

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

the class CxfRsProducerClientFactoryCacheTest method doRunTest.

private void doRunTest(ProducerTemplate template, final int clientPort) {
    Exchange exchange = template.send("direct://http", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.setPattern(ExchangePattern.InOut);
            Message inMessage = exchange.getIn();
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_USING_HTTP_API, Boolean.TRUE);
            inMessage.setHeader(Exchange.HTTP_METHOD, "GET");
            inMessage.setHeader(Exchange.HTTP_PATH, "/customerservice/customers/123");
            inMessage.setHeader(CxfConstants.CAMEL_CXF_RS_RESPONSE_CLASS, Customer.class);
            inMessage.setHeader("clientPort", clientPort);
            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) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) Customer(org.apache.camel.component.cxf.jaxrs.testbean.Customer)

Example 24 with Customer

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

the class CxfRsProducerTest method testSuppressGetCustomerExceptionWithCxfRsEndpoint.

@Test
public void testSuppressGetCustomerExceptionWithCxfRsEndpoint() {
    Exchange exchange = template.send("cxfrs://http://localhost:" + getPort1() + "/" + getClass().getSimpleName() + "/?httpClientAPI=true&throwExceptionOnFailure=false&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 
    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) 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 25 with Customer

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

the class CxfRsProducerTest method testAddCustomerUniqueResponseCodeWithHttpClientAPI.

@Test
public void testAddCustomerUniqueResponseCodeWithHttpClientAPI() {
    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");
            // create a new customer object
            Customer customer = new Customer();
            customer.setId(9999);
            customer.setName("HttpClient");
            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