Search in sources :

Example 26 with Customer

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

Example 27 with Customer

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));
}
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 28 with Customer

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));
}
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 29 with Customer

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));
}
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 30 with Customer

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));
}
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)

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