cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ashaki...@apache.org
Subject git commit: Added smoke JAX-RS OSGi tests
Date Thu, 25 Sep 2014 16:26:34 GMT
Repository: cxf
Updated Branches:
  refs/heads/master eae0b0362 -> 702f27bc4


Added smoke JAX-RS OSGi tests


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/702f27bc
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/702f27bc
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/702f27bc

Branch: refs/heads/master
Commit: 702f27bc4a334b2d117262252632bf580f20f711
Parents: eae0b03
Author: Andrei Shakirin <andrei.shakirin@gmail.com>
Authored: Thu Sep 25 18:26:18 2014 +0200
Committer: Andrei Shakirin <andrei.shakirin@gmail.com>
Committed: Thu Sep 25 18:26:18 2014 +0200

----------------------------------------------------------------------
 osgi/itests/pom.xml                             |  10 ++
 .../cxf/osgi/itests/CXFOSGiTestSupport.java     |   6 +-
 .../org/apache/cxf/osgi/itests/jaxrs/Book.java  |  52 +++++++
 .../apache/cxf/osgi/itests/jaxrs/BookStore.java | 140 +++++++++++++++++++
 .../cxf/osgi/itests/jaxrs/JaxRsServiceTest.java | 112 +++++++++++++++
 .../osgi/itests/jaxrs/JaxRsTestActivator.java   |  44 ++++++
 6 files changed, 361 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/702f27bc/osgi/itests/pom.xml
----------------------------------------------------------------------
diff --git a/osgi/itests/pom.xml b/osgi/itests/pom.xml
index 29936c4..03cb76b 100644
--- a/osgi/itests/pom.xml
+++ b/osgi/itests/pom.xml
@@ -45,6 +45,16 @@
         </dependency>
         <dependency>
             <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-frontend-jaxrs</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
+            <artifactId>cxf-rt-rs-client</artifactId>
+            <version>${project.version}</version>
+        </dependency>		
+        <dependency>
+            <groupId>org.apache.cxf</groupId>
             <artifactId>cxf-testutils</artifactId>
             <version>${project.version}</version>
         </dependency>

http://git-wip-us.apache.org/repos/asf/cxf/blob/702f27bc/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/CXFOSGiTestSupport.java
----------------------------------------------------------------------
diff --git a/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/CXFOSGiTestSupport.java
b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/CXFOSGiTestSupport.java
index e98ca8e..6efd826 100644
--- a/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/CXFOSGiTestSupport.java
+++ b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/CXFOSGiTestSupport.java
@@ -45,9 +45,7 @@ import javax.inject.Inject;
 import org.apache.felix.service.command.CommandProcessor;
 import org.apache.felix.service.command.CommandSession;
 import org.apache.karaf.features.FeaturesService;
-
 import org.junit.Assert;
-
 import org.ops4j.pax.exam.Option;
 import org.ops4j.pax.exam.ProbeBuilder;
 import org.ops4j.pax.exam.TestProbeBuilder;
@@ -69,6 +67,7 @@ import static org.ops4j.pax.exam.CoreOptions.when;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
 
 /**
  * 
@@ -138,7 +137,8 @@ public class CXFOSGiTestSupport {
                              .name("Apache Karaf")
                              .useDeployFolder(false)
                              .unpackDirectory(new File("target/paxexam/")),
-                         features(cxfUrl, "cxf-core", "cxf-jaxws"),
+                         keepRuntimeFolder(),
+                         features(cxfUrl, "cxf-core", "cxf-jaxws", "cxf-jaxrs"),        
                
                          systemProperty("java.awt.headless").value("true"),
                          when(testUtils).useOptions(mavenBundle()
                                                     .groupId("org.apache.cxf")

http://git-wip-us.apache.org/repos/asf/cxf/blob/702f27bc/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/Book.java
----------------------------------------------------------------------
diff --git a/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/Book.java b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/Book.java
new file mode 100644
index 0000000..a244633
--- /dev/null
+++ b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/Book.java
@@ -0,0 +1,52 @@
+/**
+ * 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.cxf.osgi.itests.jaxrs;
+
+import javax.xml.bind.annotation.XmlRootElement;
+
+@XmlRootElement(name = "Book")
+public class Book {
+    private String name;
+    private long id;
+    
+    public Book() {
+    }
+    
+    public Book(String name, long id) {
+        this.name = name;
+        this.id = id;
+    }
+    
+    public void setName(String n) {
+        name = n;
+    }
+
+    public String getName() {
+        return name;
+    }
+    
+    public void setId(long i) {
+        id = i;
+    }
+    public long getId() {
+        return id;
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/702f27bc/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/BookStore.java
----------------------------------------------------------------------
diff --git a/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/BookStore.java b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/BookStore.java
new file mode 100644
index 0000000..416c341
--- /dev/null
+++ b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/BookStore.java
@@ -0,0 +1,140 @@
+/**
+ * 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.cxf.osgi.itests.jaxrs;
+
+
+import java.net.URI;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.ws.rs.DELETE;
+import javax.ws.rs.GET;
+import javax.ws.rs.POST;
+import javax.ws.rs.PUT;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.container.ResourceContext;
+import javax.ws.rs.container.ResourceInfo;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.SecurityContext;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+
+@Path("/bookstore")
+@Produces("application/xml")
+public class BookStore {
+    private Map<Long, Book> books = new HashMap<Long, Book>();
+
+    @Context 
+    private UriInfo ui;
+
+    @Context 
+    private ResourceInfo rcInfo;
+    
+    @Context
+    private ResourceContext resourceContext;
+
+    @Context
+    private SecurityContext securityContext;
+
+    public BookStore() {
+        init();
+    }
+    
+    @GET
+    @Path("/books/{id}")
+    public Response getBookRoot(@PathParam("id") Long id) {
+        assertInjections();
+        Book b = books.get(id);
+        if (b == null) {
+            return Response.status(Status.NOT_FOUND).build();
+        }
+        return Response.ok(b).build();
+    }
+
+    @PUT
+    @Path("/books/{id}")
+    public Response updateBook(@PathParam("id") Long id, Book book) {
+        assertInjections();        
+        Book b = books.get(id);
+        if (b == null) {
+            return Response.status(Status.NOT_FOUND).build();
+        } else {
+            b.setName(book.getName());
+            return Response.ok().build();
+        }
+    }
+    
+    @POST
+    @Path("/books")
+    public Response createBook(Book book) {
+        assertInjections();
+        Book b = books.get(book.getId());
+        if (b != null) {
+            return Response.status(Status.CONFLICT).build();
+        } else {
+            books.put(book.getId(), book);
+            URI createdURI = UriBuilder.fromUri(ui.getAbsolutePath())
+                .path(Long.toString(book.getId())).build();
+            return Response.created(createdURI).build();
+        }
+    }
+
+    @DELETE
+    @Path("/books/{id}")
+    public Response removeBook(@PathParam("id") Long id) {
+        assertInjections();
+        Book b = books.remove(id);
+        if (b == null) {
+            return Response.status(Status.NOT_FOUND).build();
+        } else {
+            return Response.ok().build();
+        }
+    }
+
+    private void init() {
+        books.clear();
+        
+        Book book = new Book();
+        book.setId(123);
+        book.setName("CXF in Action");
+        books.put(book.getId(), book);
+    }
+    
+    private void assertInjections() {
+        if (ui.getAbsolutePath() == null) {
+            throw new IllegalArgumentException("UriInfo absolute path is null");
+        }
+        if (rcInfo.getResourceMethod() == null) {
+            throw new IllegalArgumentException("ResourceInfo resource method is null");
+        }
+        if (resourceContext.getResource(BookStore.class) == null) {
+            throw new IllegalArgumentException("ResourceContext returns null resource");
+        }
+        if (securityContext.isSecure()) {
+            throw new IllegalArgumentException("Expected unsecure communication");
+        }
+    }
+}
+
+

http://git-wip-us.apache.org/repos/asf/cxf/blob/702f27bc/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsServiceTest.java
----------------------------------------------------------------------
diff --git a/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsServiceTest.java
b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsServiceTest.java
new file mode 100644
index 0000000..0a62845
--- /dev/null
+++ b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsServiceTest.java
@@ -0,0 +1,112 @@
+/**
+ * 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.cxf.osgi.itests.jaxrs;
+
+import java.io.InputStream;
+
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.ClientBuilder;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import org.apache.cxf.osgi.itests.CXFOSGiTestSupport;
+import org.junit.Assert;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Configuration;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.PaxExam;
+import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
+import org.ops4j.pax.exam.spi.reactors.PerClass;
+import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Constants;
+
+import static org.ops4j.pax.exam.CoreOptions.provision;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
+
+
+@RunWith(PaxExam.class)
+@ExamReactorStrategy(PerClass.class)
+public class JaxRsServiceTest extends CXFOSGiTestSupport {
+
+    private static final String BASE_URL = "http://localhost:8181/cxf/jaxrs/bookstore";
+
+    private final WebTarget wt;
+
+    public JaxRsServiceTest() {
+        Client client = ClientBuilder.newClient();
+        wt = client.target(BASE_URL);
+    }
+    
+    @Test
+    public void testJaxRsGet() throws Exception {
+        Book book = wt.path("/books/123").request("application/xml").get(Book.class);
+        Assert.assertNotNull(book);
+    }
+    
+    @Test
+    public void testJaxRsPost() throws Exception {
+        Book book = new Book();
+        book.setId(321);
+        book.setName("New Book");
+        Response response = wt.path("/books/").request("application/xml").post(Entity.xml(book));
+        Assert.assertEquals(Status.CREATED.getStatusCode(), response.getStatus());
+        Assert.assertNotNull(response.getLocation());
+    }
+
+    @Test
+    public void testJaxRsDelete() throws Exception {
+        Response response = wt.path("/books/123").request("application/xml").delete();
+        Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus());
+    }
+
+    @Test
+    public void testJaxRsPut() throws Exception {
+        Book book = new Book();
+        book.setId(123);
+        book.setName("Updated Book");
+        Response response = wt.path("/books/123").request("application/xml").put(Entity.xml(book));
+        Assert.assertEquals(Status.OK.getStatusCode(), response.getStatus());
+    }
+
+    
+    @Configuration
+    public Option[] config() {
+        return new Option[] {
+            cxfBaseConfigWithTestUtils(),
+            logLevel(LogLevel.INFO),
+            features(cxfUrl, "cxf-http"),
+            provision(serviceBundle())
+        };
+    }
+
+    private InputStream serviceBundle() {
+        return TinyBundles.bundle()
+                  .add(JaxRsTestActivator.class)
+                  .add(Book.class)
+                  .add(BookStore.class)
+                  .set(Constants.BUNDLE_ACTIVATOR, JaxRsTestActivator.class.getName())
+                  .build(TinyBundles.withBnd());
+    }
+    
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/702f27bc/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsTestActivator.java
----------------------------------------------------------------------
diff --git a/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsTestActivator.java
b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsTestActivator.java
new file mode 100644
index 0000000..8613f1d
--- /dev/null
+++ b/osgi/itests/src/test/java/org/apache/cxf/osgi/itests/jaxrs/JaxRsTestActivator.java
@@ -0,0 +1,44 @@
+/**
+ * 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.cxf.osgi.itests.jaxrs;
+
+import org.apache.cxf.endpoint.Server;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+public class JaxRsTestActivator implements BundleActivator {
+//    public static final String PORT = TestUtil.getPortNumber(JaxRsTestActivator.class);
+    private Server server;
+
+    @Override
+    public void start(BundleContext arg0) throws Exception {
+        JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+        sf.setResourceClasses(BookStore.class);
+        sf.setAddress("/jaxrs");
+        server = sf.create();
+    }
+
+    @Override
+    public void stop(BundleContext arg0) throws Exception {
+        server.stop();
+        server.destroy();
+    }
+
+}


Mime
View raw message