use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testQueryPipeDelimitedNoexplodeArray.
/**
* Test: query_pipeDelimited_noexplode_array
* Expected parameters sent:
* color: blue|black|brown
* Expected response: {"color":["blue","black","brown"]}
* @throws Exception
*/
@Test
public void testQueryPipeDelimitedNoexplodeArray() throws Exception {
routerFactory.addHandlerByOperationId("query_pipeDelimited_noexplode_array", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
RequestParameter color_query = params.queryParameter("color");
assertNotNull(color_query);
assertTrue(color_query.isArray());
res.put("color", new JsonArray(color_query.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
CountDownLatch latch = new CountDownLatch(1);
List<Object> color_query;
color_query = new ArrayList<>();
color_query.add("blue");
color_query.add("black");
color_query.add("brown");
startServer();
apiClient.queryPipeDelimitedNoexplodeArray(color_query, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
assertEquals(200, ar.result().statusCode());
assertTrue("Expected: " + new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
} else {
assertTrue(ar.cause().getMessage(), false);
}
latch.countDown();
});
awaitLatch(latch);
}
use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ParametersUnitTest method testPathMultiSimpleMatrix.
/**
* Test: path_multi_simple_matrix
* Expected parameters sent:
* color_simple: blue
* color_matrix: ;color=blue,black,brown
* Expected response: {"color_simple":"blue","color_matrix":["blue","black","brown"]}
* @throws Exception
*/
@Test
public void testPathMultiSimpleMatrix() throws Exception {
routerFactory.addHandlerByOperationId("path_multi_simple_matrix", routingContext -> {
RequestParameters params = routingContext.get("parsedParameters");
JsonObject res = new JsonObject();
RequestParameter colorSimple_path = params.pathParameter("color_simple");
assertNotNull(colorSimple_path);
assertTrue(colorSimple_path.isString());
assertEquals(colorSimple_path.getString(), "blue");
res.put("color_simple", colorSimple_path.getString());
RequestParameter colorMatrix_path = params.pathParameter("color_matrix");
assertNotNull(colorMatrix_path);
assertTrue(colorMatrix_path.isArray());
res.put("color_matrix", new JsonArray(colorMatrix_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
});
CountDownLatch latch = new CountDownLatch(1);
String colorSimple_path;
colorSimple_path = "blue";
List<Object> colorMatrix_path;
colorMatrix_path = new ArrayList<>();
colorMatrix_path.add("blue");
colorMatrix_path.add("black");
colorMatrix_path.add("brown");
startServer();
apiClient.pathMultiSimpleMatrix(colorSimple_path, colorMatrix_path, (AsyncResult<HttpResponse> ar) -> {
if (ar.succeeded()) {
assertEquals(200, ar.result().statusCode());
assertTrue("Expected: " + new JsonObject("{\"color_simple\":\"blue\",\"color_matrix\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color_simple\":\"blue\",\"color_matrix\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
} else {
assertTrue(ar.cause().getMessage(), false);
}
latch.countDown();
});
awaitLatch(latch);
}
use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testJsonBody.
@Test
public void testJsonBody() throws Exception {
Operation op = testSpec.getPaths().get("/jsonBodyTest/sampleTest").getPost();
if (op.getParameters() == null)
op.setParameters(new ArrayList<>());
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/jsonBodyTest/sampleTest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject object = new JsonObject();
object.put("id", "anId");
List<Integer> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger());
object.put("values", valuesArray);
testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", object, 200, "OK", object);
testRequestWithJSONAndCustomContentType(HttpMethod.POST, "/jsonBodyTest/sampleTest", "application/json; charset=utf-8", object, 200, "OK", object);
testRequestWithJSONAndCustomContentType(HttpMethod.POST, "/jsonBodyTest/sampleTest", "application/superapplication+json", object, 200, "OK", object);
}
use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testAdditionalPropertiesJson.
@Test
public void testAdditionalPropertiesJson() throws Exception {
Operation op = testSpec.getPaths().get("/additionalProperties").getPost();
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/additionalProperties", HttpMethod.POST, true, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject pet = new JsonObject();
pet.put("id", 14612);
pet.put("name", "Willy");
pet.put("lazyness", "Highest");
testRequestWithJSON(HttpMethod.POST, "/additionalProperties", pet, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
use of io.swagger.v3.oas.models.media.Content in project vertx-web by vert-x3.
the class OpenAPI3ValidationTest method testJsonBodyFailure.
@Test
public void testJsonBodyFailure() throws Exception {
Operation op = testSpec.getPaths().get("/jsonBodyTest/sampleTest").getPost();
if (op.getParameters() == null)
op.setParameters(new ArrayList<>());
OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec);
loadHandlers("/jsonBodyTest/sampleTest", HttpMethod.POST, true, validationHandler, (routingContext) -> {
RequestParameters params = routingContext.get("parsedParameters");
routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("Content-Type", "application/json").end(params.body().getJsonObject().encode());
});
JsonObject object = new JsonObject();
object.put("id", "anId");
List<String> valuesArray = new ArrayList<>();
for (int i = 0; i < 4; i++) valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
valuesArray.add(2, getFailureSample(ParameterType.INT));
object.put("values", valuesArray);
testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", object, 400, errorMessage(ValidationException.ErrorType.JSON_INVALID), null);
}
Aggregations