cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From r...@apache.org
Subject cxf git commit: CXF-6046: Enhance SwaggerFeature to support JAX-RS annotations. Added test cases (systests)
Date Fri, 02 Jan 2015 14:40:17 GMT
Repository: cxf
Updated Branches:
  refs/heads/master 632cb29d8 -> b7657a47a


CXF-6046: Enhance SwaggerFeature to support JAX-RS annotations. Added test cases (systests)


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

Branch: refs/heads/master
Commit: b7657a47aca211d46df04998bf79a9e2ca76a545
Parents: 632cb29
Author: reta <drreta@gmail.com>
Authored: Fri Jan 2 09:39:52 2015 -0500
Committer: reta <drreta@gmail.com>
Committed: Fri Jan 2 09:39:52 2015 -0500

----------------------------------------------------------------------
 systests/jaxrs/pom.xml                          |  15 ++
 .../AbstractSwaggerServiceDescriptionTest.java  | 231 +++++++++++++++++++
 .../jaxrs/description/BookStoreSwagger.java     |  82 +++++++
 .../SwaggerFilterServiceDescriptionTest.java    |  47 ++++
 .../SwaggerRegularServiceDescriptionTest.java   |  47 ++++
 5 files changed, 422 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/b7657a47/systests/jaxrs/pom.xml
----------------------------------------------------------------------
diff --git a/systests/jaxrs/pom.xml b/systests/jaxrs/pom.xml
index b4e06f7..f5f5e22 100644
--- a/systests/jaxrs/pom.xml
+++ b/systests/jaxrs/pom.xml
@@ -39,6 +39,21 @@
     </properties>
     <dependencies>
         <dependency>
+            <groupId>com.wordnik</groupId>
+            <artifactId>swagger-jaxrs_2.10</artifactId>
+            <exclusions>
+                <exclusion>
+                     <groupId>javax.ws.rs</groupId>
+                     <artifactId>jsr311-api</artifactId>
+                </exclusion>
+            </exclusions>
+        </dependency>
+        <dependency>
+            <groupId>javassist</groupId>
+            <artifactId>javassist</artifactId>
+            <version>3.12.1.GA</version>
+        </dependency>        
+        <dependency>
             <groupId>javax.validation</groupId>
             <artifactId>validation-api</artifactId>
         </dependency>

http://git-wip-us.apache.org/repos/asf/cxf/blob/b7657a47/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwaggerServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwaggerServiceDescriptionTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwaggerServiceDescriptionTest.java
new file mode 100644
index 0000000..570b99f
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/AbstractSwaggerServiceDescriptionTest.java
@@ -0,0 +1,231 @@
+/**
+ * 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.json.Json;
+import javax.json.JsonObject;
+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.SwaggerFeature;
+import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.AbstractBusTestServerBase;
+import org.junit.Ignore;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+
+public abstract class AbstractSwaggerServiceDescriptionTest extends AbstractBusClientServerTestBase
{
+    private static final JsonObject DELETE_METHOD_SPEC = Json.createObjectBuilder()
+        .add("method", "DELETE")
+        .add("summary", "Delete book")
+        .add("notes", "Delete book")
+        .add("type", "void")
+        .add("nickname", "delete")
+        .add("parameters", Json.createArrayBuilder()
+            .add(Json.createObjectBuilder()
+                .add("name", "id")
+                .add("description", "id")
+                .add("required", true)
+                .add("type", "string")
+                .add("paramType", "path")
+                .add("allowMultiple", false)
+            )
+        ).build();
+    
+    private static final JsonObject GET_BY_ID_METHOD_SPEC = Json.createObjectBuilder()
+        .add("method", "GET")
+        .add("summary", "Get book by Id")
+        .add("notes", "Get book by Id")
+        .add("type", "Book")
+        .add("nickname", "getBook")
+        .add("produces", Json.createArrayBuilder().add("application/json"))
+        .add("parameters", Json.createArrayBuilder()
+            .add(Json.createObjectBuilder()
+                .add("name", "id")
+                .add("description", "id")
+                .add("required", true)
+                .add("type", "integer")
+                .add("format", "int64")
+                .add("paramType", "path")
+                .add("allowMultiple", false)
+            )
+        ).build();
+    
+    private static final JsonObject GET_METHOD_SPEC = Json.createObjectBuilder()
+        .add("method", "GET")
+        .add("summary", "Get books")
+        .add("notes", "Get books")
+        .add("type", "array")
+        .add("items", Json.createObjectBuilder().add("$ref", "Book"))
+        .add("nickname", "getBooks")
+        .add("produces", Json.createArrayBuilder().add("application/json"))
+        .add("parameters", Json.createArrayBuilder()
+            .add(Json.createObjectBuilder()
+                .add("name", "page")
+                .add("description", "Page to fetch")
+                .add("defaultValue", "1")
+                .add("required", true)
+                .add("type", "integer")
+                .add("format", "int32")
+                .add("paramType", "query")
+                .add("allowMultiple", false)
+            )
+        ).build();
+    
+    private static final JsonObject BOOK_MODEL_SPEC = Json.createObjectBuilder()
+        .add("Book", Json.createObjectBuilder()
+            .add("id", "Book")
+            .add("discriminator", "class")
+            .add("properties", Json.createObjectBuilder()
+                .add("name", Json.createObjectBuilder().add("type", "string"))
+                .add("id", Json.createObjectBuilder()
+                    .add("type", "integer")
+                    .add("format", "int64")
+                )
+            )
+        ).build();
+    
+    @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(BookStoreSwagger.class);
+            sf.setResourceProvider(BookStoreSwagger.class, 
+                new SingletonResourceProvider(new BookStoreSwagger()));
+            sf.setProvider(new JacksonJsonProvider());
+            final SwaggerFeature feature = new SwaggerFeature();
+            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 testApiListingIsProperlyReturned() throws Exception {
+        final WebClient client = createWebClient("/api-docs");
+        
+        try {
+            final Response r = client.get();
+            assertEquals(Status.OK.getStatusCode(), r.getStatus());
+            
+            assertThat(IOUtils.readStringFromStream((InputStream)r.getEntity()), 
+                equalTo(Json.createObjectBuilder()
+                    .add("apiVersion", "1.0.0")
+                    .add("swaggerVersion", "1.2")
+                    .add("apis", Json.createArrayBuilder()
+                        .add(Json.createObjectBuilder()
+                            .add("path", "/bookstore")
+                            .add("description", "Sample JAX-RS service with Swagger documentation")
+                        )
+                    )
+                    .add("info", Json.createObjectBuilder()
+                        .add("title", "Sample REST Application")
+                        .add("description", "The Application")
+                        .add("contact", "committer@apache.org")
+                        .add("license", "Apache 2.0 License")
+                        .add("licenseUrl", "http://www.apache.org/licenses/LICENSE-2.0.html")
+                    ).build().toString()));
+        } finally {
+            client.close();
+        }
+    }
+    
+    @Test
+    public void testApiResourcesAreProperlyReturned() throws Exception {
+        final WebClient client = createWebClient("/api-docs/bookstore");
+        
+        try {
+            final Response r = client.get();
+            assertEquals(Status.OK.getStatusCode(), r.getStatus());
+                    
+            assertThat(IOUtils.readStringFromStream((InputStream)r.getEntity()), 
+                equalTo(Json.createObjectBuilder()
+                    .add("apiVersion", "1.0.0")
+                    .add("swaggerVersion", "1.2")
+                    .add("basePath", "http://localhost:" + getPort() + "/")
+                    .add("resourcePath", "/bookstore")
+                    .add("apis", Json.createArrayBuilder()
+                        .add(Json.createObjectBuilder()
+                            .add("path", "/bookstore/{id}")
+                            .add("operations", Json.createArrayBuilder()
+                                .add(DELETE_METHOD_SPEC)
+                                .add(GET_BY_ID_METHOD_SPEC)))
+                        .add(Json.createObjectBuilder()
+                            .add("path", "/bookstore")
+                            .add("operations", Json.createArrayBuilder().add(GET_METHOD_SPEC))))
+                    .add("models", BOOK_MODEL_SPEC).build().toString()));
+        } finally {
+            client.close();
+        }
+    }
+    
+    @Test
+    public void testNonRegisteredApiResourcesAreNotReturned() throws Exception {
+        final Response r = createWebClient("/api-docs/books").get();
+        assertEquals(Status.NOT_FOUND.getStatusCode(), r.getStatus());
+    }
+    
+    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/b7657a47/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger.java
new file mode 100644
index 0000000..49709f7
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/BookStoreSwagger.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 com.wordnik.swagger.annotations.Api;
+import com.wordnik.swagger.annotations.ApiOperation;
+import com.wordnik.swagger.annotations.ApiParam;
+
+import org.apache.cxf.systest.jaxrs.Book;
+
+@Path("/bookstore") 
+@Api(value = "/bookstore", description = "Sample JAX-RS service with Swagger documentation")
+public class BookStoreSwagger {
+    @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/b7657a47/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerFilterServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerFilterServiceDescriptionTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerFilterServiceDescriptionTest.java
new file mode 100644
index 0000000..09d8306
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerFilterServiceDescriptionTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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;
+import org.junit.Ignore;
+
+public class SwaggerFilterServiceDescriptionTest extends AbstractSwaggerServiceDescriptionTest
{
+    private static final String PORT = allocatePort(SwaggerFilterServiceDescriptionTest.class);
+    
+    @Ignore
+    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/b7657a47/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerRegularServiceDescriptionTest.java
----------------------------------------------------------------------
diff --git a/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerRegularServiceDescriptionTest.java
b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerRegularServiceDescriptionTest.java
new file mode 100644
index 0000000..c583c16
--- /dev/null
+++ b/systests/jaxrs/src/test/java/org/apache/cxf/systest/jaxrs/description/SwaggerRegularServiceDescriptionTest.java
@@ -0,0 +1,47 @@
+/**
+ * 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;
+import org.junit.Ignore;
+
+public class SwaggerRegularServiceDescriptionTest extends AbstractSwaggerServiceDescriptionTest
{
+    private static final String PORT = allocatePort(SwaggerRegularServiceDescriptionTest.class);
+    
+    @Ignore
+    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;
+    }
+}


Mime
View raw message