Skip to content

Commit

Permalink
Add TCK tests for APIResponseSchema and RequestBodySchema annotations
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <michael@xlate.io>
  • Loading branch information
MikeEdgar committed Mar 25, 2020
1 parent 54822cd commit f58206e
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
Expand Up @@ -30,8 +30,10 @@
import org.eclipse.microprofile.openapi.annotations.media.Content;
import org.eclipse.microprofile.openapi.annotations.media.Schema;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBody;
import org.eclipse.microprofile.openapi.annotations.parameters.RequestBodySchema;
import org.eclipse.microprofile.openapi.annotations.media.ExampleObject;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponse;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema;
import org.eclipse.microprofile.openapi.annotations.responses.APIResponses;
import org.eclipse.microprofile.openapi.annotations.callbacks.CallbackOperation;

Expand Down Expand Up @@ -361,6 +363,7 @@ public Response findPetsByStatus(

@GET
@Path("/findByTags")
@Produces("application/json")
@Callback(
name = "tagsCallback",
callbackUrlExpression = "http://petstoreapp.com/pet",
Expand All @@ -383,6 +386,7 @@ public Response findPetsByStatus(
}
)
)
@APIResponseSchema(Pet[].class)
@Deprecated
public Response findPetsByTags(
@HeaderParam("apiKey") String apiKey,
Expand Down Expand Up @@ -438,4 +442,39 @@ public Response updatePetWithForm (
return Response.status(404).build();
}
}

@POST
@Path("/{petId}")
@Consumes({ "text/csv" })
@Produces({ "text/csv" })
@APIResponseSchema(value = Pet.class, responseCode = "204")
@Operation(summary = "Updates a pet in the store with CSV data")
public Response updatePetWithCsv (
@Parameter(
name = "petId",
description = "ID of pet that needs to be updated",
required = true)
@PathParam("petId") Long petId,
@RequestBodySchema(Pet.class)
String commaSeparatedValues
) {
Pet pet = petData.getPetById(petId);
if(pet != null) {
String[] values = commaSeparatedValues.split(",");
String name = values[2];
String status = values[5];

if(name != null && !"".equals(name)){
pet.setName(name);
}
if(status != null && !"".equals(status)){
pet.setStatus(status);
}
petData.addPet(pet);
return Response.ok().build();
}
else{
return Response.status(404).build();
}
}
}
Expand Up @@ -31,6 +31,8 @@
import io.restassured.filter.Filter;
import io.restassured.http.ContentType;
import io.restassured.parsing.Parser;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;

public abstract class AppTestBase extends Arquillian {
Expand Down Expand Up @@ -89,6 +91,26 @@ public ValidatableResponse callEndpoint(String type) {
return vr;
}

/**
* Lookup the object at the provided path in the response and if the object
* is a reference (contains a $ref property), return the reference path. If the
* object is not a reference, return the input path.
*
* @param vr the response
* @param path a path which may be a reference object (containing a $ref)
* @return the path the object references if present, else the input path
*/
public static String dereference(ValidatableResponse vr, String path) {
ExtractableResponse<Response> response = vr.extract();
String ref = response.path(path + ".$ref");

if (ref != null) {
return ref.replaceFirst("^#/?", "").replace('/', '.');
} else {
return path;
}
}

@DataProvider(name = "formatProvider")
public Object[][] provide() {
return new Object[][] { { "JSON" }, { "YAML" } };
Expand Down
Expand Up @@ -17,13 +17,18 @@
package org.eclipse.microprofile.openapi.tck;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasItems;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.hamcrest.collection.IsMapWithSize.aMapWithSize;
import static org.testng.Assert.assertNotNull;

import javax.ws.rs.core.MediaType;

Expand Down Expand Up @@ -205,4 +210,84 @@ public void testJsonResponseTypeWithQueryParameter() {
.and()
.body("openapi", startsWith("3.0."));
}

@RunAsClient
@Test(dataProvider = "formatProvider")
public void testRequestBodySchema(String type) {
ValidatableResponse vr = callEndpoint(type);
String path = dereference(vr, "paths.'/pet/{petId}'.post.requestBody");

// The requestBody is present (either embedded or a $ref)
vr.body(path, notNullValue());

String schemaPath = dereference(vr, path) + ".content.'text/csv'.schema";

// The schema is present (either embedded or a $ref)
vr.body(schemaPath, notNullValue());

String schemaObject = dereference(vr, schemaPath);

vr.body(schemaObject,
allOf(aMapWithSize(3),
hasEntry(equalTo("required"), notNullValue()),
hasEntry(equalTo("type"), equalTo("object")),
hasEntry(equalTo("properties"), notNullValue())));
}

@RunAsClient
@Test(dataProvider = "formatProvider")
public void testAPIResponseSchema(String type) {
ValidatableResponse vr = callEndpoint(type);
String path = dereference(vr, "paths.'/pet/{petId}'.post.responses.'204'");

// The response is present
vr.body(path, notNullValue());
vr.body(path, hasEntry("description", "No Content"));

String schemaPath = dereference(vr, path) + ".content.'text/csv'.schema";

// The schema is present
vr.body(schemaPath, notNullValue());

String schemaObject = dereference(vr, schemaPath);

vr.body(schemaObject,
allOf(aMapWithSize(3),
hasEntry(equalTo("required"), notNullValue()),
hasEntry(equalTo("type"), equalTo("object")),
hasEntry(equalTo("properties"), notNullValue())));
}

@RunAsClient
@Test(dataProvider = "formatProvider")
public void testAPIResponseSchemaDefaultResponseCode(String type) {
ValidatableResponse vr = callEndpoint(type);
String path = dereference(vr, "paths.'/pet/findByTags'.get.responses.'200'");

assertNotNull(path);

// The response is present
vr.body(path, notNullValue());
vr.body(path, hasEntry("description", "OK"));

String schemaPath = dereference(vr, path) + ".content.'application/json'.schema";

// The schema is present
vr.body(schemaPath, notNullValue());

String arraySchemaObject = dereference(vr, schemaPath);

vr.body(arraySchemaObject,
allOf(aMapWithSize(2),
hasEntry(equalTo("type"), equalTo("array")),
hasEntry(equalTo("items"), notNullValue())));

String schemaObject = dereference(vr, arraySchemaObject + ".items");

vr.body(schemaObject,
allOf(aMapWithSize(3),
hasEntry(equalTo("required"), notNullValue()),
hasEntry(equalTo("type"), equalTo("object")),
hasEntry(equalTo("properties"), notNullValue())));
}
}

0 comments on commit f58206e

Please sign in to comment.