Author: davsclaus
Date: Thu Dec 30 12:42:41 2010
New Revision: 1053877

URL: http://svn.apache.org/viewvc?rev=1053877&view=rev
Log:
CAMEL-3175: Added test.

Added:
    
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
      - copied, changed from r1053817, 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java

Copied: 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
 (from r1053817, 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java?p2=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java&p1=camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java&r1=1053817&r2=1053877&rev=1053877&view=diff
==============================================================================
--- 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConsumerTest.java
 (original)
+++ 
camel/trunk/components/camel-cxf/src/test/java/org/apache/camel/component/cxf/jaxrs/CxfRsConvertBodyToTest.java
 Thu Dec 30 12:42:41 2010
@@ -16,23 +16,11 @@
  */
 package org.apache.camel.component.cxf.jaxrs;
 
-import java.io.FileNotFoundException;
-import java.io.IOException;
-import java.io.InputStream;
-import java.net.URL;
-
-import javax.ws.rs.WebApplicationException;
 import javax.ws.rs.core.Response;
 
-import org.apache.camel.Exchange;
-import org.apache.camel.Message;
-import org.apache.camel.Processor;
-import org.apache.camel.RuntimeCamelException;
-import org.apache.camel.builder.NoErrorHandlerBuilder;
 import org.apache.camel.builder.RouteBuilder;
-import org.apache.camel.component.cxf.CxfConstants;
 import org.apache.camel.component.cxf.jaxrs.testbean.Customer;
-import org.apache.camel.component.cxf.util.CxfUtils;
+import org.apache.camel.component.mock.MockEndpoint;
 import org.apache.camel.test.junit4.CamelTestSupport;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.HttpClient;
@@ -42,92 +30,31 @@ import org.apache.http.impl.client.Defau
 import org.apache.http.util.EntityUtils;
 import org.junit.Test;
 
-
-public class CxfRsConsumerTest extends CamelTestSupport {
+public class CxfRsConvertBodyToTest extends CamelTestSupport {
     private static final String PUT_REQUEST = 
"<Customer><name>Mary</name><id>123</id></Customer>";
     private static final String CXF_RS_ENDPOINT_URI = 
"cxfrs://http://localhost:9000/rest?resourceClasses=org.apache.camel.component.cxf.jaxrs.testbean.CustomerService";;
-    
-    // START SNIPPET: example
+
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {
             public void configure() {
-                errorHandler(new NoErrorHandlerBuilder());
-                from(CXF_RS_ENDPOINT_URI).process(new Processor() {
+                Response OK = Response.ok().build();
 
-                    public void process(Exchange exchange) throws Exception {
-                        Message inMessage = exchange.getIn();                  
      
-                        // Get the operation name from in message
-                        String operationName = 
inMessage.getHeader(CxfConstants.OPERATION_NAME, String.class);
-                        if ("getCustomer".equals(operationName)) {
-                            String httpMethod = 
inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
-                            assertEquals("Get a wrong http method", "GET", 
httpMethod);
-                            String path = 
inMessage.getHeader(Exchange.HTTP_PATH, String.class);
-                            // The parameter of the invocation is stored in 
the body of in message
-                            String id = (String) 
inMessage.getBody(String.class);
-                            if ("/customerservice/customers/126".equals(path)) 
{                            
-                                Customer customer = new Customer();
-                                customer.setId(Long.parseLong(id));
-                                customer.setName("Willem");
-                                // We just put the response Object into the 
out message body
-                                exchange.getOut().setBody(customer);
-                            } else {
-                                if 
("/customerservice/customers/456".equals(path)) {
-                                    Response r = 
Response.status(404).entity("Can't found the customer with uri " + 
path).build();
-                                    throw new WebApplicationException(r);
-                                } else {
-                                    throw new RuntimeCamelException("Can't 
found the customer with uri " + path);
-                                }
-                            }
-                        }
-                        if ("updateCustomer".equals(operationName)) {
-                            assertEquals("Get a wrong customer message 
header", "header1;header2", inMessage.getHeader("test"));
-                            String httpMethod = 
inMessage.getHeader(Exchange.HTTP_METHOD, String.class);
-                            assertEquals("Get a wrong http method", "PUT", 
httpMethod);
-                            Customer customer = 
inMessage.getBody(Customer.class);
-                            assertNotNull("The customer should not be null.", 
customer);
-                            // Now you can do what you want on the customer 
object
-                            assertEquals("Get a wrong customer name.", "Mary", 
customer.getName());
-                            // set the response back
-                            exchange.getOut().setBody(Response.ok().build());
-                        }
-                    }
-                    
-                });
-            }
+                from(CXF_RS_ENDPOINT_URI)
+                    // should be able to convert to Customer
+                    .convertBodyTo(Customer.class)
+                    .to("mock:result")
+                    // respond with OK
+                    .transform(constant(OK));
+            };
         };
     }
-    // END SNIPPET: example
-    
-    @Test
-    public void testGetCustomer() throws Exception {
-        URL url = new 
URL("http://localhost:9000/rest/customerservice/customers/126";);
 
-        InputStream in = url.openStream();
-        assertEquals("{\"Customer\":{\"id\":126,\"name\":\"Willem\"}}", 
CxfUtils.getStringFromInputStream(in));
-       
-    }
-    
-    @Test
-    public void testGetWrongCustomer() throws Exception {
-        URL url = new 
URL("http://localhost:9000/rest/customerservice/customers/456";);
-        try {
-            url.openStream();
-            fail("Expect to get exception here");
-        } catch (FileNotFoundException exception) {
-            // do nothing here
-        }
-        url = new 
URL("http://localhost:9000/rest/customerservice/customers/256";);
-        try {
-            url.openStream();
-            fail("Expect to get exception here");
-        } catch (IOException exception) {
-            // expect the Internal error exception
-        }
-        
-    }
-    
     @Test
     public void testPutConsumer() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+        mock.message(0).body().isInstanceOf(Customer.class);
+
         HttpPut put = new 
HttpPut("http://localhost:9000/rest/customerservice/customers";);
         StringEntity entity = new StringEntity(PUT_REQUEST, "ISO-8859-1");
         entity.setContentType("text/xml; charset=ISO-8859-1");
@@ -143,6 +70,5 @@ public class CxfRsConsumerTest extends C
             httpclient.getConnectionManager().shutdown();
         }
     }
-        
 
 }


Reply via email to