Skip to content

Commit

Permalink
refs swagger-api#2521 - dynamic jetty port and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
frantuma committed Nov 15, 2017
1 parent f44f6e3 commit 84fc5f8
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 14 deletions.
28 changes: 27 additions & 1 deletion modules/swagger-jaxrs2-servlet-initializer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<portNames>
<portName>jetty.port</portName>
<portName>jetty.port.stop</portName>
</portNames>
</configuration>
<executions>
<execution>
<id>reserve-port</id>
<phase>pre-integration-test</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand All @@ -48,6 +68,11 @@
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<jetty.port>${jetty.port}</jetty.port>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
Expand All @@ -56,8 +81,9 @@
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<httpConnector><port>${jetty.port}</port></httpConnector>
<stopKey>a</stopKey>
<stopPort>9999</stopPort>
<stopPort>${jetty.port.stop}</stopPort>
<useTestScope>true</useTestScope>
<webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
</configuration>
Expand Down
40 changes: 27 additions & 13 deletions modules/swagger-jaxrs2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@
</instructions>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<portNames>
<portName>jetty.port</portName>
<portName>jetty.port.stop</portName>
</portNames>
</configuration>
<executions>
<execution>
<id>reserve-port</id>
<phase>pre-integration-test</phase>
<goals>
<goal>reserve-network-port</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
Expand All @@ -53,6 +73,11 @@
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
<configuration>
<systemPropertyVariables>
<jetty.port>${jetty.port}</jetty.port>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
Expand All @@ -61,8 +86,9 @@
<artifactId>jetty-maven-plugin</artifactId>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<httpConnector><port>${jetty.port}</port></httpConnector>
<stopKey>a</stopKey>
<stopPort>9999</stopPort>
<stopPort>${jetty.port.stop}</stopPort>
<useTestScope>true</useTestScope>
<webAppSourceDirectory>${project.basedir}/src/test/webapp</webAppSourceDirectory>
</configuration>
Expand Down Expand Up @@ -158,18 +184,6 @@
<artifactId>mockito-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey2-version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<properties>
<models.version>2.0.0-SNAPSHOT</models.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,12 @@ public void compareAsYaml(Class<?> cls, String yaml) throws IOException {
OpenAPI openAPI = reader.read(cls);
SerializationMatchers.assertEqualsToYaml(openAPI, yaml);
}

public void compareAsYaml(String actualYaml, String expectedYaml) throws IOException {
SerializationMatchers.assertEqualsToYaml(Yaml.mapper().readValue(actualYaml, OpenAPI.class), expectedYaml);
}
public void compareAsJson(String actualJson, String expectedJson) throws IOException {
SerializationMatchers.assertEqualsToJson(Yaml.mapper().readValue(actualJson, OpenAPI.class), expectedJson);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
package io.swagger.v3.jaxrs2.it;

import com.jayway.restassured.http.ContentType;

import io.swagger.v3.jaxrs2.annotations.AbstractAnnotationTest;
import org.testng.SkipException;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static com.jayway.restassured.RestAssured.given;

/**
* <p>
* An functional integration test that runs during maven's integration-test phase,
* uses RestAssured to define REST API tests, and Jetty's Maven plugin to serve a simple
* sample app just prior to the integration-test phase starting.
*/
public class OpenApiResourceIT extends AbstractAnnotationTest {
private static final String EXPECTED_JSON = "{\n" +
" \"openapi\": \"3.0.0\",\n" +
" \"paths\": {\n" +
" \"/widgets/{widgetId}\": {\n" +
" \"get\": {\n" +
" \"tags\": [\n" +
" \"widgets\"\n" +
" ],\n" +
" \"summary\": \"Find pet by ID\",\n" +
" \"description\": \"Returns a pet when ID <= 10. ID > 10 or nonintegers will simulate API error conditions\",\n" +
" \"operationId\": \"getWidget\",\n" +
" \"parameters\": [\n" +
" {\n" +
" \"name\": \"widgetId\",\n" +
" \"in\": \"path\",\n" +
" \"required\": true,\n" +
" \"schema\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" }\n" +
" ],\n" +
" \"responses\": {\n" +
" \"200\": {\n" +
" \"description\": \"Returns widget with matching id\",\n" +
" \"content\": {\n" +
" \"application/json\": {\n" +
" \"schema\": {\n" +
" \"$ref\": \"#/components/schemas/Widget\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" },\n" +
" \"components\": {\n" +
" \"schemas\": {\n" +
" \"Widget\": {\n" +
" \"type\": \"object\",\n" +
" \"properties\": {\n" +
" \"a\": {\n" +
" \"type\": \"string\"\n" +
" },\n" +
" \"b\": {\n" +
" \"type\": \"string\"\n" +
" },\n" +
" \"id\": {\n" +
" \"type\": \"string\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
private static final String EXPECTED_YAML = "openapi: 3.0.0\n" +
"paths:\n" +
" /widgets/{widgetId}:\n" +
" get:\n" +
" tags:\n" +
" - widgets\n" +
" summary: Find pet by ID\n" +
" description: Returns a pet when ID <= 10. ID > 10 or nonintegers will simulate\n" +
" API error conditions\n" +
" operationId: getWidget\n" +
" parameters:\n" +
" - name: widgetId\n" +
" in: path\n" +
" required: true\n" +
" schema:\n" +
" type: string\n" +
" responses:\n" +
" 200:\n" +
" description: Returns widget with matching id\n" +
" content:\n" +
" application/json:\n" +
" schema:\n" +
" $ref: '#/components/schemas/Widget'\n" +
"components:\n" +
" schemas:\n" +
" Widget:\n" +
" type: object\n" +
" properties:\n" +
" a:\n" +
" type: string\n" +
" b:\n" +
" type: string\n" +
" id:\n" +
" type: string\n";

private static final int jettyPort = System.getProperties().containsKey("jetty.port") ? Integer.parseInt(System.getProperty("jetty.port")): -1;

@BeforeMethod
public void checkJetty() {
if (jettyPort == -1) {
throw new SkipException("Jetty not configured");
}
}

@Test
public void testSwaggerJson() throws Exception {

String actualBody = given()
.port(jettyPort)
.log().all()
.when()
.get("/openapi.json")
.then()
.log().all()
.assertThat()
.statusCode(200)
.contentType(ContentType.JSON)
.extract()
.response().body().asString();

compareAsJson(actualBody, EXPECTED_JSON);
}

@Test
public void testSwaggerJsonUsingAcceptHeader() throws Exception {
String actualBody = given()
.port(jettyPort)
.log().all()
.accept(ContentType.JSON)
.when()
.get("/openapi")
.then()
.log().all()
.assertThat()
.statusCode(200)
.contentType(ContentType.JSON)
.extract().response().body().asString();

compareAsJson(actualBody, EXPECTED_JSON);
}

@Test
public void testSwaggerYaml() throws Exception {
String actualBody = given()
.port(jettyPort)
.log().all()
.when()
.get("/openapi.yaml")
.then()
.log().all()
.assertThat()
.statusCode(200)
.contentType("application/yaml")
.extract().response().body().asString();

compareAsYaml(actualBody, EXPECTED_YAML);
}

@Test
public void testSwaggerYamlUsingAcceptHeader() throws Exception {
String actualBody = given()
.port(jettyPort)
.log().all()
.accept("application/yaml")
.when()
.get("/openapi")
.then()
.log().all()
.assertThat()
.statusCode(200)
.contentType("application/yaml")
.extract().response().body().asString();

compareAsYaml(actualBody, EXPECTED_YAML);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.swagger.v3.jaxrs2.it.model;

public class Widget {

private String a;
private String b;
private String id;

public String getA() {
return a;
}

public Widget setA(String a) {
this.a = a;
return this;
}

public String getB() {
return b;
}

public Widget setB(String b) {
this.b = b;
return this;
}

public String getId() {
return id;
}

public Widget setId(String id) {
this.id = id;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.swagger.v3.jaxrs2.it.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;

import io.swagger.v3.jaxrs2.it.model.Widget;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.ws.rs.core.Response;

@Path("/widgets")
@Tag(name="widgets")
@Produces("application/json")
@Consumes("application/json")
public class WidgetResource {

@Path("/{widgetId}")
@GET
@Operation(summary = "Find pet by ID",
description = "Returns a pet when ID <= 10. ID > 10 or nonintegers will simulate API error conditions",
responses = @ApiResponse(
content = @Content(schema = @Schema(implementation = Widget.class)),
description = "Returns widget with matching id",
responseCode = "200"
)
)
public Response getWidget(@PathParam("widgetId") String widgetId) {
return Response.ok(new Widget().setA("foo").setB("bar")).build();
}
}
Loading

0 comments on commit 84fc5f8

Please sign in to comment.