You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by ay...@apache.org on 2015/07/09 14:18:35 UTC

[2/6] cxf git commit: [CXF-6476] adding systests

[CXF-6476] adding systests


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

Branch: refs/heads/3.0.x-fixes
Commit: b2794a49b4b7a88f7da3d940c08ee86d51661c7c
Parents: 6d6e891
Author: Akitoshi Yoshida <ay...@apache.org>
Authored: Wed Jul 8 19:22:57 2015 +0200
Committer: Akitoshi Yoshida <ay...@apache.org>
Committed: Thu Jul 9 13:40:26 2015 +0200

----------------------------------------------------------------------
 .../cxf/jaxrs/swagger/Swagger2Feature.java      |  19 +---
 systests/jaxrs/pom.xml                          |  12 ++
 .../AbstractSwagger2ServiceDescriptionTest.java | 113 +++++++++++++++++++
 .../jaxrs/description/BookStoreSwagger2.java    |  82 ++++++++++++++
 .../Swagger2FilterServiceDescriptionTest.java   |  45 ++++++++
 .../Swagger2RegularServiceDescriptionTest.java  |  45 ++++++++
 .../systest/jaxrs/description/swagger2-json.txt |  52 +++++++++
 7 files changed, 355 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
----------------------------------------------------------------------
diff --git a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
index d21c559..d2b7649 100644
--- a/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
+++ b/rt/rs/description/src/main/java/org/apache/cxf/jaxrs/swagger/Swagger2Feature.java
@@ -44,9 +44,9 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
 
     @Override
     protected void addSwaggerResource(Server server) {
-        ApiListingResource apiListingResource = new ApiListingResource();
         if (!runAsFilter) {
             List<Object> serviceBeans = new ArrayList<Object>();
+            ApiListingResource apiListingResource = new ApiListingResource();
             serviceBeans.add(apiListingResource);
             JAXRSServiceFactoryBean sfb = 
                 (JAXRSServiceFactoryBean)server.getEndpoint().get(JAXRSServiceFactoryBean.class.getName());
@@ -59,11 +59,11 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
         }
         List<Object> providers = new ArrayList<Object>();
         if (runAsFilter) {
-            providers.add(new SwaggerContainerRequestFilter(apiListingResource));
+            providers.add(new SwaggerContainerRequestFilter());
         }
         providers.add(new SwaggerSerializers());
         ((ServerProviderFactory)server.getEndpoint().get(
-                ServerProviderFactory.class.getName())).setUserProviders(providers);
+            ServerProviderFactory.class.getName())).setUserProviders(providers);
         
         BeanConfig beanConfig = new BeanConfig();
         beanConfig.setResourcePackage(getResourcePackage());
@@ -78,30 +78,23 @@ public class Swagger2Feature extends AbstractSwaggerFeature {
     }
 
     @PreMatching
-    private static class SwaggerContainerRequestFilter implements ContainerRequestFilter {
+    private static class SwaggerContainerRequestFilter extends ApiListingResource implements ContainerRequestFilter {
         private static final String APIDOCS_LISTING_PATH_JSON = "swagger.json";
         private static final String APIDOCS_LISTING_PATH_YAML = "swagger.yaml";
         
-        private ApiListingResource apiListingResource;
         @Context
         private MessageContext mc;
-        public SwaggerContainerRequestFilter(ApiListingResource apiListingResource) {
-            this.apiListingResource = apiListingResource;
-        }
 
         @Override
         public void filter(ContainerRequestContext requestContext) throws IOException {
             UriInfo ui = mc.getUriInfo();
             if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_JSON)) {
-                Response r = 
-                    apiListingResource.getListingJson(null, mc.getServletConfig(), mc.getHttpHeaders(), ui);
+                Response r = getListingJson(null, mc.getServletConfig(), mc.getHttpHeaders(), ui);
                 requestContext.abortWith(r);
             } else if (ui.getPath().endsWith(APIDOCS_LISTING_PATH_YAML)) {
-                Response r = 
-                    apiListingResource.getListingYaml(null, mc.getServletConfig(), mc.getHttpHeaders(), ui);
+                Response r = getListingYaml(null, mc.getServletConfig(), mc.getHttpHeaders(), ui); 
                 requestContext.abortWith(r);
             }
         }
-        
     }
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/systests/jaxrs/pom.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/pom.xml b/systests/jaxrs/pom.xml
index ba64f50..6564e02 100644
--- a/systests/jaxrs/pom.xml
+++ b/systests/jaxrs/pom.xml
@@ -38,6 +38,7 @@
 
     </properties>
     <dependencies>
+        <!-- for swagger 1.2 tests -->
         <dependency>
             <groupId>com.wordnik</groupId>
             <artifactId>swagger-jaxrs_2.10</artifactId>
@@ -48,6 +49,17 @@
                 </exclusion>
             </exclusions>
         </dependency>
+        <!-- for swagger 2.0 tests -->
+        <dependency>
+            <groupId>io.swagger</groupId>
+            <artifactId>swagger-jaxrs</artifactId>
+            <exclusions>
+                <exclusion>
+                     <groupId>javax.ws.rs</groupId>
+                     <artifactId>jsr311-api</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
         <dependency>
             <groupId>javassist</groupId>
             <artifactId>javassist</artifactId>

http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
new file mode 100644
index 0000000..13be973
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwagger2ServiceDescriptionTest.java
@@ -0,0 +1,113 @@
+/**
+ * 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.systest.jaxrs.description;
+
+import java.io.InputStream;
+import java.util.Arrays;
+
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.Response.Status;
+
+import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;
+
+import org.apache.cxf.helpers.IOUtils;
+import org.apache.cxf.jaxrs.JAXRSServerFactoryBean;
+import org.apache.cxf.jaxrs.client.WebClient;
+import org.apache.cxf.jaxrs.lifecycle.SingletonResourceProvider;
+import org.apache.cxf.jaxrs.model.AbstractResourceInfo;
+import org.apache.cxf.jaxrs.swagger.Swagger2Feature;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+import org.skyscreamer.jsonassert.JSONAssert;
+
+public abstract class AbstractSwagger2ServiceDescriptionTest extends AbstractBusClientServerTestBase {
+    
+    @Ignore
+    public abstract static class Server extends AbstractBusTestServerBase {
+        private final String port;
+        private final boolean runAsFilter;
+        
+        Server(final String port, final boolean runAsFilter) {
+            this.port = port;
+            this.runAsFilter = runAsFilter;
+        }
+        
+        protected void run() {
+            final JAXRSServerFactoryBean sf = new JAXRSServerFactoryBean();
+            sf.setResourceClasses(BookStoreSwagger2.class);
+            sf.setResourceProvider(BookStoreSwagger2.class, 
+                new SingletonResourceProvider(new BookStoreSwagger2()));
+            sf.setProvider(new JacksonJsonProvider());
+            final Swagger2Feature feature = new Swagger2Feature();
+            feature.setRunAsFilter(runAsFilter);
+            sf.setFeatures(Arrays.asList(feature));
+            sf.setAddress("http://localhost:" + port + "/");
+            sf.create();
+        }
+        
+        protected static void start(final Server s) {
+            try {
+                s.start();
+            } catch (Exception ex) {
+                ex.printStackTrace();
+                System.exit(-1);
+            } finally {
+                System.out.println("done!");
+            }
+        }
+    }
+    
+    protected static void startServers(final Class< ? extends Server> serverClass) throws Exception {
+        AbstractResourceInfo.clearAllMaps();
+        //keep out of process due to stack traces testing failures
+        assertTrue("server did not launch correctly", launchServer(serverClass, false));
+        createStaticBus();
+    }
+
+    protected abstract String getPort();
+    
+    @Test
+    public void testApiListingIsProperlyReturnedJSON() throws Exception {
+        final WebClient client = createWebClient("/swagger.json");
+        
+        try {
+            final Response r = client.get();
+            assertEquals(Status.OK.getStatusCode(), r.getStatus());
+            JSONAssert.assertEquals(
+                IOUtils.readStringFromStream((InputStream)r.getEntity()), 
+                String.format(IOUtils.readStringFromStream(getClass().getResourceAsStream("swagger2-json.txt")), 
+                              getPort()),
+                false);
+        } finally {
+            client.close();
+        }
+    }
+
+    private WebClient createWebClient(final String url) {
+        return WebClient
+            .create("http://localhost:" + getPort() + url, 
+                Arrays.< Object >asList(new JacksonJsonProvider()))
+            .accept(MediaType.APPLICATION_JSON);
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
new file mode 100644
index 0000000..755a89b
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger2.java
@@ -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.cxf.systest.jaxrs.description;
+
+import java.util.Arrays;
+
+import javax.ws.rs.DELETE;
+import javax.ws.rs.DefaultValue;
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.PathParam;
+import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+
+import org.apache.cxf.systest.jaxrs.Book;
+
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import io.swagger.annotations.ApiParam;
+
+@Path("/bookstore") 
+@Api(value = "/bookstore", description = "Sample JAX-RS service with Swagger documentation")
+public class BookStoreSwagger2 {
+    @Produces({ MediaType.APPLICATION_JSON })
+    @GET
+    @ApiOperation(
+        value = "Get books", 
+        notes = "Get books", 
+        response = Book.class, 
+        responseContainer = "List"
+    )
+    public Response getBooks(
+        @ApiParam(value = "Page to fetch", required = true) @QueryParam("page") @DefaultValue("1") int page) {
+        return Response.ok(
+            Arrays.asList(
+                new Book("Book 1", 1),
+                new Book("Book 2", 2)
+            )
+        ).build();
+    }
+    
+    @Produces({ MediaType.APPLICATION_JSON })
+    @Path("/{id}")
+    @GET
+    @ApiOperation(
+        value = "Get book by Id", 
+        notes = "Get book by Id",
+        response = Book.class
+    )
+    public Book getBook(@ApiParam(value = "id", required = true) @PathParam("id") Long id) {
+        return new Book("Book", id);
+    }
+    
+    @Path("/{id}")
+    @DELETE
+    @ApiOperation(
+        value = "Delete book", 
+        notes = "Delete book"
+    )    
+    public Response delete(@ApiParam(value = "id", required = true) @PathParam("id") String id) {
+        return Response.ok().build();
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2FilterServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2FilterServiceDescriptionTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2FilterServiceDescriptionTest.java
new file mode 100644
index 0000000..142575f
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2FilterServiceDescriptionTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.systest.jaxrs.description;
+
+import org.junit.BeforeClass;
+
+public class Swagger2FilterServiceDescriptionTest extends AbstractSwagger2ServiceDescriptionTest {
+    private static final String PORT = allocatePort(Swagger2FilterServiceDescriptionTest.class);
+    
+    public static class SwaggerFilter extends Server {
+        public SwaggerFilter() {
+            super(PORT, true);
+        }
+        
+        public static void main(String[] args) {
+            start(new SwaggerFilter());
+        }
+    }
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        startServers(SwaggerFilter.class);
+    }
+    
+    @Override
+    protected String getPort() {
+        return PORT;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2RegularServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2RegularServiceDescriptionTest.java b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2RegularServiceDescriptionTest.java
new file mode 100644
index 0000000..25b255b
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/Swagger2RegularServiceDescriptionTest.java
@@ -0,0 +1,45 @@
+/**
+ * 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.systest.jaxrs.description;
+
+import org.junit.BeforeClass;
+
+public class Swagger2RegularServiceDescriptionTest extends AbstractSwagger2ServiceDescriptionTest {
+    private static final String PORT = allocatePort(Swagger2RegularServiceDescriptionTest.class);
+    
+    public static class SwaggerRegular extends Server {
+        public SwaggerRegular() {
+            super(PORT, false);
+        }
+        
+        public static void main(String[] args) {
+            start(new SwaggerRegular());
+        }
+    }
+    
+    @BeforeClass
+    public static void startServers() throws Exception {
+        startServers(SwaggerRegular.class);
+    }
+    
+    @Override
+    protected String getPort() {
+        return PORT;
+    }
+}

http://git-wip-us.apache.org/repos/asf/cxf/blob/b2794a49/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
new file mode 100644
index 0000000..4377a41
--- /dev/null
+++ b/systests/jaxrs/src/test/resources/org/apache/cxf/systest/jaxrs/description/swagger2-json.txt
@@ -0,0 +1,52 @@
+{"swagger":"2.0",
+ "info":{"description":"The Application",
+         "version":"1.0.0",
+         "title":"Sample REST Application",
+         "contact":{"name":"committer@apache.org"},
+         "license":{"name":"Apache 2.0 License",
+                    "url":"http://www.apache.org/licenses/LICENSE-2.0.html"}},
+ "basePath":"/http://localhost:%s/",
+ "tags":[{"name":"bookstore"}],
+ "paths":{"/bookstore":{"get":{"tags":["bookstore"],
+                               "summary":"Get books",
+                               "description":"Get books",
+                               "operationId":"getBooks",
+                               "produces":["application/json"],
+                               "parameters":[{"name":"page",
+                                              "in":"query",
+                                              "description":"Page to fetch",
+                                              "required":true,
+                                              "type":"integer",
+                                              "default":"1",
+                                              "format":"int32"}],
+                               "responses":{"200":{"description":"successful operation",
+                                                   "schema":{"type":"array",
+                                                             "items":{"$ref":"#/definitions/Book"}}}}}},
+          "/bookstore/{id}":{"get":{"tags":["bookstore"],
+                                    "summary":"Get book by Id",
+                                    "description":"Get book by Id",
+                                    "operationId":"getBook",
+                                    "produces":["application/json"],
+                                    "parameters":[{"name":"id",
+                                                   "in":"path",
+                                                   "description":"id",
+                                                   "required":true,
+                                                   "type":"integer",
+                                                   "format":"int64"}],
+                                    "responses":{"200":{"description":"successful operation",
+                                                        "schema":{"$ref":"#/definitions/Book"}}}},
+                             "delete":{"tags":["bookstore"],
+                                       "summary":"Delete book",
+                                       "description":"Delete book",
+                                       "operationId":"delete",
+                                       "parameters":[{"name":"id",
+                                                      "in":"path",
+                                                      "description":"id",
+                                                      "required":true,
+                                                      "type":"string"}],
+                                       "responses":{"default":{"description":"successful operation"}}}}},
+ "definitions":{"Book":{"type":"object",
+                        "properties":{"name":{"type":"string"},
+                                      "id":{"type":"integer",
+                                            "format":"int64"}},
+                        "xml":{"name":"Book"}}}}