You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2008/09/10 18:19:47 UTC

svn commit: r693883 - in /activemq/camel/trunk/components/camel-rest: ./ src/main/java/org/apache/camel/rest/model/ src/main/java/org/apache/camel/rest/resources/ src/main/webapp/WEB-INF/ src/test/java/org/apache/camel/rest/ src/test/java/org/apache/ca...

Author: jstrachan
Date: Wed Sep 10 09:19:47 2008
New Revision: 693883

URL: http://svn.apache.org/viewvc?rev=693883&view=rev
Log:
added some unit tests for CAMEL-888

Added:
    activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/Main.java   (with props)
    activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/
    activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/CamelContextResourceTest.java   (with props)
Modified:
    activemq/camel/trunk/components/camel-rest/pom.xml
    activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/EndpointLink.java
    activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/Endpoints.java
    activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java
    activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/EndpointResource.java
    activemq/camel/trunk/components/camel-rest/src/main/webapp/WEB-INF/web.xml

Modified: activemq/camel/trunk/components/camel-rest/pom.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/pom.xml?rev=693883&r1=693882&r2=693883&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-rest/pom.xml (original)
+++ activemq/camel/trunk/components/camel-rest/pom.xml Wed Sep 10 09:19:47 2008
@@ -37,7 +37,6 @@
     <jetty-port>8080</jetty-port>
   </properties>
 
-
   <repositories>
     <repository>
       <id>java.net.maven2</id>
@@ -61,6 +60,60 @@
       <version>${jersey-version}</version>
     </dependency>
     <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-json</artifactId>
+      <version>${jersey-version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-atom</artifactId>
+      <version>${jersey-version}</version>
+    </dependency>
+
+    <!-- testing -->
+    <dependency>
+      <groupId>com.sun.jersey</groupId>
+      <artifactId>jersey-client</artifactId>
+      <version>${jersey-version}</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- web container -->
+    <dependency>
+      <groupId>org.mortbay.jetty</groupId>
+      <artifactId>jetty</artifactId>
+      <version>${jetty-version}</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.mortbay.jetty</groupId>
+      <artifactId>jetty-util</artifactId>
+      <version>${jetty-version}</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- enable commons-logging when inside jetty6:run -->
+    <!--
+    <dependency>
+        <groupId>org.slf4j</groupId>
+        <artifactId>jcl104-over-slf4j</artifactId>
+        <version>1.5.0</version>
+    </dependency>
+    -->
+    <!--
+        <dependency>
+          <groupId>org.slf4j</groupId>
+          <artifactId>slf4j-jcl</artifactId>
+        </dependency>
+    -->
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-log4j12</artifactId>
+    </dependency>
+
+    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-test</artifactId>
       <scope>test</scope>
@@ -98,13 +151,15 @@
         <artifactId>maven-jetty-plugin</artifactId>
         <version>${jetty-version}</version>
 
-                <configuration>
+        <configuration>
+<!--
           <connectors>
             <connector implementation="org.mortbay.jetty.nio.SelectChannelConnector">
               <port>${jetty.port}</port>
               <maxIdleTime>60000</maxIdleTime>
             </connector>
           </connectors>
+-->
 
           <webAppConfig>
             <contextPath>/</contextPath>
@@ -114,7 +169,7 @@
             <!-- enable easy JMX connection to JConsole -->
             <systemProperty>
               <name>com.sun.management.jmxremote</name>
-              <value />
+              <value/>
             </systemProperty>
           </systemProperties>
           <scanIntervalSeconds>10</scanIntervalSeconds>

Modified: activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/EndpointLink.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/EndpointLink.java?rev=693883&r1=693882&r2=693883&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/EndpointLink.java (original)
+++ activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/EndpointLink.java Wed Sep 10 09:19:47 2008
@@ -19,10 +19,10 @@
 
 import org.apache.camel.Endpoint;
 
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlAttribute;
-import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAttribute;
+import javax.xml.bind.annotation.XmlRootElement;
 
 /**
  * @version $Revision: 1.1 $
@@ -35,6 +35,10 @@
     @XmlAttribute
     private String href;
 
+    @Override
+    public String toString() {
+        return "EndpointLink{href='" + href + "' uri='" + uri + "'}";
+    }
 
     public void load(Endpoint endpoint) {
         this.uri = endpoint.getEndpointUri();
@@ -59,6 +63,6 @@
 
     protected String createHref(String uri) {
         // TODO how to encode as a href?
-        return uri;
+        return "/camel/endpoint/" + uri;
     }
 }

Modified: activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/Endpoints.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/Endpoints.java?rev=693883&r1=693882&r2=693883&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/Endpoints.java (original)
+++ activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/model/Endpoints.java Wed Sep 10 09:19:47 2008
@@ -20,13 +20,13 @@
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
 
-import javax.xml.bind.annotation.XmlRootElement;
-import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlAccessorType;
 import javax.xml.bind.annotation.XmlElement;
+import javax.xml.bind.annotation.XmlRootElement;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.List;
-import java.util.ArrayList;
 
 /**
  * @version $Revision: 1.1 $
@@ -34,9 +34,15 @@
 @XmlRootElement
 @XmlAccessorType(XmlAccessType.FIELD)
 public class Endpoints {
-    @XmlElement(name="endpoint")
+    @XmlElement(name = "endpoint")
     private List<EndpointLink> endpoints = new ArrayList<EndpointLink>();
 
+    @Override
+    public String toString() {
+        return "Endpoints" + endpoints;
+
+    }
+
     public List<EndpointLink> getEndpoints() {
         return endpoints;
     }

Modified: activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java?rev=693883&r1=693882&r2=693883&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java (original)
+++ activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/CamelContextResource.java Wed Sep 10 09:19:47 2008
@@ -21,17 +21,20 @@
 import com.sun.jersey.spi.resource.Singleton;
 import org.apache.camel.CamelContext;
 import org.apache.camel.Endpoint;
+import org.apache.camel.model.RouteType;
+import org.apache.camel.model.RoutesType;
 import org.apache.camel.rest.model.Endpoints;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Path;
 import javax.ws.rs.PathParam;
 import javax.ws.rs.Produces;
+import java.util.List;
 
 /**
  * @version $Revision: 1.1 $
  */
-@Path("context")
+@Path("camel")
 @Singleton
 public class CamelContextResource {
 
@@ -61,7 +64,7 @@
     }
 
     @Path("endpoint/{id}")
-    public EndpointResource findWidget(@PathParam("id") String id) {
+    public EndpointResource getEndpoint(@PathParam("id") String id) {
         // TODO lets assume the ID is the endpoint
         Endpoint endpoint = getCamelContext().getEndpoint(id);
         if (endpoint != null) {
@@ -71,4 +74,16 @@
         }
     }
 
+    @GET
+    @Path("routes")
+    @Produces({"application/json", "application/xml"})
+    public RoutesType getRouteDefinitions() {
+        RoutesType answer = new RoutesType();
+        if (camelContext != null) {
+            List<RouteType> list = camelContext.getRouteDefinitions();
+            answer.setRoutes(list);
+        }
+        return answer;
+    }
+
 }

Modified: activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/EndpointResource.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/EndpointResource.java?rev=693883&r1=693882&r2=693883&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/EndpointResource.java (original)
+++ activemq/camel/trunk/components/camel-rest/src/main/java/org/apache/camel/rest/resources/EndpointResource.java Wed Sep 10 09:19:47 2008
@@ -23,6 +23,7 @@
 
 import javax.ws.rs.GET;
 import javax.ws.rs.Produces;
+import javax.ws.rs.Path;
 
 /**
  * @version $Revision: 1.1 $
@@ -34,5 +35,11 @@
     public EndpointResource(Endpoint endpoint) {
         this.endpoint = endpoint;
     }
-    
+
+
+    @GET
+    @Produces("text/plain")
+    public String getValue() {
+        return  endpoint.getEndpointUri();
+    }
 }

Modified: activemq/camel/trunk/components/camel-rest/src/main/webapp/WEB-INF/web.xml
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/main/webapp/WEB-INF/web.xml?rev=693883&r1=693882&r2=693883&view=diff
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/main/webapp/WEB-INF/web.xml (original)
+++ activemq/camel/trunk/components/camel-rest/src/main/webapp/WEB-INF/web.xml Wed Sep 10 09:19:47 2008
@@ -69,7 +69,10 @@
   </servlet>
   <servlet-mapping>
     <servlet-name>Jersey Spring</servlet-name>
-    <url-pattern>/api/*</url-pattern>
+    <url-pattern>/*</url-pattern>
   </servlet-mapping>
 
+  <welcome-file-list>
+    <welcome-file>/camel</welcome-file>
+  </welcome-file-list>
 </web-app>

Added: activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/Main.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/Main.java?rev=693883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/Main.java (added)
+++ activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/Main.java Wed Sep 10 09:19:47 2008
@@ -0,0 +1,82 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.rest;
+
+import org.mortbay.jetty.Connector;
+import org.mortbay.jetty.Handler;
+import org.mortbay.jetty.Server;
+import org.mortbay.jetty.nio.SelectChannelConnector;
+import org.mortbay.jetty.webapp.WebAppContext;
+
+/**
+ * A simple bootstrap class for starting Jetty in your IDE using the local web
+ * application.
+ *
+ * @version $Revision: 565003 $
+ */
+public final class Main {
+
+    public static final int PORT = 8080;
+
+    public static final String WEBAPP_DIR = "src/main/webapp";
+
+    public static final String WEBAPP_CTX = "/";
+    protected static Server server = new Server();
+
+    private Main() {
+    }
+
+    public static void main(String[] args) throws Exception {
+        // now lets start the web server
+        int port = PORT;
+        if (args.length > 0) {
+            String text = args[0];
+            port = Integer.parseInt(text);
+        }
+        System.out.println("Starting Web Server on port: " + port);
+        run(port);
+    }
+
+    public static void run(int port) throws Exception {
+        SelectChannelConnector connector = new SelectChannelConnector();
+        connector.setPort(port);
+        connector.setServer(server);
+        WebAppContext context = new WebAppContext();
+
+        context.setResourceBase(WEBAPP_DIR);
+        context.setContextPath(WEBAPP_CTX);
+        context.setServer(server);
+        server.setHandlers(new Handler[]{
+                context
+        });
+        server.setConnectors(new Connector[]{
+                connector
+        });
+        server.start();
+
+        System.out.println();
+        System.out.println("==============================================================================");
+        System.out.println("Started the Camel REST Console: point your web browser at http://localhost:" + port + "/");
+        System.out.println("==============================================================================");
+        System.out.println();
+    }
+
+    public static void stop() throws Exception {
+        server.stop();
+    }
+}

Propchange: activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/Main.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/CamelContextResourceTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/CamelContextResourceTest.java?rev=693883&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/CamelContextResourceTest.java (added)
+++ activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/CamelContextResourceTest.java Wed Sep 10 09:19:47 2008
@@ -0,0 +1,67 @@
+/**
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.rest.resources;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+import junit.framework.TestCase;
+import org.apache.camel.rest.Main;
+import org.apache.camel.rest.model.EndpointLink;
+import org.apache.camel.rest.model.Endpoints;
+
+import java.util.List;
+
+/**
+ * @version $Revision: 1.1 $
+ */
+public class CamelContextResourceTest extends TestCase {
+    protected int port = 9998;
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        Main.run(port);
+    }
+
+    @Override
+    protected void tearDown() throws Exception {
+        super.tearDown();
+        Main.stop();
+    }
+
+    public void testMain() throws Exception {
+        ClientConfig cc = new DefaultClientConfig();
+        // use the following jaxb context resolver
+        //cc.getProviderClasses().add(JAXBContextResolver.class);
+        Client c = Client.create(cc);
+
+        WebResource wr = c.resource("http://localhost:" + port + Main.WEBAPP_CTX);
+
+        // get the initial representation
+
+        Endpoints endpoints = wr.path("camel/endpoints").accept("application/xml").get(Endpoints.class);
+
+        // and print it out
+        System.out.println("Found: " + endpoints.getEndpoints());
+
+        List<EndpointLink> list = endpoints.getEndpoints();
+        assertTrue("Should have received some endpoints!", !list.isEmpty());
+    }
+}
\ No newline at end of file

Propchange: activemq/camel/trunk/components/camel-rest/src/test/java/org/apache/camel/rest/resources/CamelContextResourceTest.java
------------------------------------------------------------------------------
    svn:eol-style = native