io.vertx.ext.web.api.validation.ParameterType Java Examples

The following examples show how to use io.vertx.ext.web.api.validation.ParameterType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OpenAPI3ValidationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@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, refsCache);
  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.toBuffer(), 400, errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
 
Example #2
Source File: OpenAPI3ValidationTest.java    From vertx-web with Apache License 2.0 6 votes vote down vote up
@Test
public void testFormArrayParameterFailure() throws Exception {
  Operation op = testSpec.getPaths().get("/formTests/arraytest").getPost();
  if(op.getParameters()==null) op.setParameters(new ArrayList<>());
  OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
  loadHandlers("/formTests/arraytest", HttpMethod.POST, true, validationHandler, (routingContext) -> routingContext.response().setStatusMessage("ok").end());

  String id = "anId";

  List<String> valuesArray = new ArrayList<>();
  for (int i = 0; i < 4; i++)
    valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
  valuesArray.add(getFailureSample(ParameterType.INT));
  String values = serializeInCSVStringArray(valuesArray);

  MultiMap form = MultiMap.caseInsensitiveMultiMap();
  form.add("id", id);
  form.add("values", values);

  testRequestWithForm(HttpMethod.POST, "/formTests/arraytest", FormType.FORM_URLENCODED, form, 400, errorMessage
    (ValidationException.ErrorType.NO_MATCH));
}
 
Example #3
Source File: OpenAPI3MultipleFilesValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@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, refsCache);
    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.toBuffer(), 400,
            errorMessage(ValidationException.ErrorType.JSON_INVALID));
}
 
Example #4
Source File: OpenAPI3ValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testJsonBodyFailureErrorMessage() 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, refsCache);
  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());
  }, routingContext -> {
    ValidationException e = (ValidationException) routingContext.failure();
    routingContext
      .response()
      .setStatusCode(400)
      .setStatusMessage(errorMessage(ValidationException.ErrorType.JSON_INVALID))
      .putHeader("Content-Type", "application/json")
      .end(new JsonObject().put("field", e.parameterName()).toBuffer());
  });

  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(0, getFailureSample(ParameterType.INT));
  object.put("values", valuesArray);

  testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", object.toBuffer(), 400,
    errorMessage(ValidationException.ErrorType.JSON_INVALID),
    new JsonObject().put("field", "body.values[0]").toBuffer());

  testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest", new JsonArray().toBuffer(), 400,
    errorMessage(ValidationException.ErrorType.JSON_INVALID),
    new JsonObject().put("field", "body").toBuffer());
}
 
Example #5
Source File: OpenAPI3ValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testComplexMultipart() throws Exception {
  Operation op = testSpec.getPaths().get("/multipart/complex").getPost();
  OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
  loadHandlers("/multipart/complex", HttpMethod.POST, false, validationHandler, (routingContext) -> {
    RequestParameters params = routingContext.get("parsedParameters");
    assertNotNull(params.formParameter("param2").getJsonObject());
    assertEquals("Willy", params.formParameter("param2").getJsonObject().getString("name"));
    assertEquals(4, params.formParameter("param4").getArray().size());
    assertEquals((Integer)2, params.formParameter("param5").getInteger());
    routingContext.response().setStatusMessage("ok").end();
  });

  JsonObject pet = new JsonObject();
  pet.put("id", 14612);
  pet.put("name", "Willy");

  List<String> valuesArray = new ArrayList<>();
  for (int i = 0; i < 4; i++)
    valuesArray.add(getSuccessSample(ParameterType.FLOAT).getFloat().toString());

  MultipartForm form = MultipartForm.create()
    .textFileUpload("param1", "random.txt", "src/test/resources/random.txt", "text/plain")
    .attribute("param2", pet.encode())
    .textFileUpload("param3", "random.csv", "src/test/resources/random.txt", "text/csv")
    .attribute("param4", serializeInCSVStringArray(valuesArray))
    .attribute("param5", "2")
    .binaryFileUpload("param1Binary", "random-file", "src/test/resources/random-file", "text/plain");

  testRequestWithMultipartForm(HttpMethod.POST, "/multipart/complex", form, 200, "ok");
}
 
Example #6
Source File: OpenAPI3ValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@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, refsCache);
  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.toBuffer(), 200, "OK", object.toBuffer());
  testRequestWithBufferResponse(HttpMethod.POST,
    "/jsonBodyTest/sampleTest",
    "application/json; charset=utf-8",
    object.toBuffer(), 200, "OK", "application/json", b -> assertEquals(object, b.toJsonObject()));
  testRequestWithBufferResponse(HttpMethod.POST,
    "/jsonBodyTest/sampleTest",
    "application/superapplication+json",
    object.toBuffer(), 200, "OK", "application/json", b -> assertEquals(object, b.toJsonObject()));
}
 
Example #7
Source File: OpenAPI3ValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormArrayParameter() throws Exception {
  Operation op = testSpec.getPaths().get("/formTests/arraytest").getPost();
  if(op.getParameters()==null) op.setParameters(new ArrayList<>());
  OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(op, op.getParameters(), testSpec, refsCache);
  loadHandlers("/formTests/arraytest", HttpMethod.POST, false, validationHandler, (routingContext) -> {
    RequestParameters params = routingContext.get("parsedParameters");
    List<String> result = new ArrayList<>();
    for (RequestParameter r : params.formParameter("values").getArray())
      result.add(r.getInteger().toString());
    routingContext.response().setStatusMessage(params.formParameter("id").getString() + serializeInCSVStringArray
      (result)).end();
  });

  String id = "anId";

  List<String> valuesArray = new ArrayList<>();
  for (int i = 0; i < 4; i++)
    valuesArray.add(getSuccessSample(ParameterType.INT).getInteger().toString());
  String values = serializeInCSVStringArray(valuesArray);

  MultiMap form = MultiMap.caseInsensitiveMultiMap();
  form.add("id", id);
  form.add("values", values);

  testRequestWithForm(HttpMethod.POST, "/formTests/arraytest", FormType.FORM_URLENCODED, form, 200, id + values);
}
 
Example #8
Source File: MainVerticle.java    From vertx-in-production with MIT License 5 votes vote down vote up
@Override
public void start() {
    configureLogging();

    this.pingHandler = new PingHandler();
    this.failureHandler = new FailureHandler();
    this.pokemonHandler = new PokemonHandler(vertx);
    this.greetingHandler = new GreetingHandler();

    configureRouteHandlers(config());
    configureEventBus();
    HealthChecks healthChecks = pokemonHandler.getHealthchecks();

    HTTPRequestValidationHandler greetingValidationHandler = HTTPRequestValidationHandler
            .create()
            .addHeaderParam("Authorization", ParameterType.GENERIC_STRING, true)
            .addHeaderParam("Version", ParameterType.INT, true)
            .addPathParamWithCustomTypeValidator("name", new NameValidator(), false);

    Router router = Router.router(vertx);
    router.route().consumes("application/json");
    router.route().produces("application/json");
    router.route().handler(BodyHandler.create());

    router.get("/ping").handler(pingHandler).failureHandler(failureHandler);
    router.get("/pokemons").handler(pokemonHandler).failureHandler(failureHandler);

    router.get("/greetings/:name")
            .handler(greetingValidationHandler)
            .handler(greetingHandler)
            .failureHandler(failureHandler);

    router.get("/alive").handler(HealthCheckHandler.create(vertx));
    router.get("/healthy").handler(HealthCheckHandler.createWithHealthChecks(healthChecks));

    vertx.createHttpServer().requestHandler(router).listen(8080);
}
 
Example #9
Source File: OpenAPI3MultipleFilesValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@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, refsCache);
    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.toBuffer(), 200, "OK", object.toBuffer());
    testRequestWithBufferResponse(HttpMethod.POST,
            "/jsonBodyTest/sampleTest", "application/json; charset=utf-8",
            object.toBuffer(), 200, "OK", "application/json",
            b -> assertEquals(object, b.toJsonObject()));
    testRequestWithBufferResponse(HttpMethod.POST,
            "/jsonBodyTest/sampleTest", "application/superapplication+json",
            object.toBuffer(), 200, "OK", "application/json",
            b -> assertEquals(object, b.toJsonObject()));
}
 
Example #10
Source File: OpenAPI3MultipleFilesValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormArrayParameterFailure() throws Exception {
    Operation op = testSpec.getPaths().get("/formTests/arraytest")
            .getPost();
    if (op.getParameters() == null)
        op.setParameters(new ArrayList<>());
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(
            op, op.getParameters(), testSpec, refsCache);
    loadHandlers("/formTests/arraytest", HttpMethod.POST, true,
            validationHandler, (routingContext) -> routingContext.response()
                    .setStatusMessage("ok").end());

    String id = "anId";

    List<String> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++)
        valuesArray.add(getSuccessSample(ParameterType.INT).getInteger()
                .toString());
    valuesArray.add(getFailureSample(ParameterType.INT));
    String values = serializeInCSVStringArray(valuesArray);

    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("id", id);
    form.add("values", values);

    testRequestWithForm(HttpMethod.POST, "/formTests/arraytest",
            FormType.FORM_URLENCODED, form, 400,
            errorMessage(ValidationException.ErrorType.NO_MATCH));
}
 
Example #11
Source File: OpenAPI3MultipleFilesValidationTest.java    From vertx-web with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormArrayParameter() throws Exception {
    Operation op = testSpec.getPaths().get("/formTests/arraytest")
            .getPost();
    if (op.getParameters() == null)
        op.setParameters(new ArrayList<>());
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(
            op, op.getParameters(), testSpec, refsCache);
    loadHandlers("/formTests/arraytest", HttpMethod.POST, false,
            validationHandler, (routingContext) -> {
                RequestParameters params = routingContext
                        .get("parsedParameters");
                List<String> result = new ArrayList<>();
                for (RequestParameter r : params.formParameter("values")
                        .getArray())
                    result.add(r.getInteger().toString());
                routingContext.response()
                        .setStatusMessage(
                                params.formParameter("id").getString()
                                        + serializeInCSVStringArray(result))
                        .end();
            });

    String id = "anId";

    List<String> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++)
        valuesArray.add(getSuccessSample(ParameterType.INT).getInteger()
                .toString());
    String values = serializeInCSVStringArray(valuesArray);

    MultiMap form = MultiMap.caseInsensitiveMultiMap();
    form.add("id", id);
    form.add("values", values);

    testRequestWithForm(HttpMethod.POST, "/formTests/arraytest",
            FormType.FORM_URLENCODED, form, 200, id + values);
}
 
Example #12
Source File: OpenAPI3MultipleFilesValidationTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testComplexMultipart() throws Exception {
    Operation op = testSpec.getPaths().get("/multipart/complex").getPost();
    OpenAPI3RequestValidationHandler validationHandler = new OpenAPI3RequestValidationHandlerImpl(
            op, op.getParameters(), testSpec, refsCache);
    loadHandlers("/multipart/complex", HttpMethod.POST, false,
            validationHandler, (routingContext) -> {
                RequestParameters params = routingContext
                        .get("parsedParameters");
                assertNotNull(
                        params.formParameter("param2").getJsonObject());
                assertEquals("Willy", params.formParameter("param2")
                        .getJsonObject().getString("name"));
                assertEquals(4,
                        params.formParameter("param4").getArray().size());
                assertEquals((Integer) 2,
                        params.formParameter("param5").getInteger());
                routingContext.response().setStatusMessage("ok").end();
            });

    JsonObject pet = new JsonObject();
    pet.put("id", 14612);
    pet.put("name", "Willy");

    List<String> valuesArray = new ArrayList<>();
    for (int i = 0; i < 4; i++)
        valuesArray.add(getSuccessSample(ParameterType.FLOAT).getFloat()
                .toString());

    MultipartForm form = MultipartForm.create()
            .textFileUpload("param1", "random.txt",
                    "src/test/resources/random.txt", "text/plain")
            .attribute("param2", pet.encode())
            .textFileUpload("param3", "random.csv",
                    "src/test/resources/random.txt", "text/csv")
            .attribute("param4", serializeInCSVStringArray(valuesArray))
            .attribute("param5", "2").binaryFileUpload("param1Binary",
                    "random-file", "src/test/resources/random-file",
                    "text/plain");

    testRequestWithMultipartForm(HttpMethod.POST, "/multipart/complex",
            form, 200, "ok");
}
 
Example #13
Source File: OpenAPI3MultipleFilesValidationTest.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
@Test
public void testJsonBodyFailureErrorMessage() 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, refsCache);
    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());
            }, routingContext -> {
                ValidationException e = (ValidationException) routingContext
                        .failure();
                routingContext.response().setStatusCode(400)
                        .setStatusMessage(errorMessage(
                                ValidationException.ErrorType.JSON_INVALID))
                        .putHeader("Content-Type", "application/json")
                        .end(new JsonObject()
                                .put("field", e.parameterName())
                                .toBuffer());
            });

    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(0, getFailureSample(ParameterType.INT));
    object.put("values", valuesArray);

    testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest",
            object.toBuffer(), 400,
            errorMessage(ValidationException.ErrorType.JSON_INVALID),
            new JsonObject().put("field", "body.values[0]").toBuffer());

    testRequestWithJSON(HttpMethod.POST, "/jsonBodyTest/sampleTest",
            new JsonArray().toBuffer(), 400,
            errorMessage(ValidationException.ErrorType.JSON_INVALID),
            new JsonObject().put("field", "body").toBuffer());
}
 
Example #14
Source File: ValidationExamples.java    From vertx-web with Apache License 2.0 4 votes vote down vote up
public void example1(Vertx vertx, Router router) {
  // Create Validation Handler with some stuff
  HTTPRequestValidationHandler validationHandler = HTTPRequestValidationHandler.create().addQueryParam("parameterName", ParameterType.INT, true).addFormParamWithPattern("formParameterName", "a{4}", true).addPathParam("pathParam", ParameterType.FLOAT);
}
 
Example #15
Source File: HttpRequestValidator.java    From vertx-postgresql-starter with MIT License 4 votes vote down vote up
public static HTTPRequestValidationHandler upsertBookByIdValidationHandler() {
  return HTTPRequestValidationHandler.create()
    .addPathParam("id", ParameterType.INT);
}
 
Example #16
Source File: HttpRequestValidator.java    From vertx-postgresql-starter with MIT License 4 votes vote down vote up
public static HTTPRequestValidationHandler getBookByIdValidationHandler() {
  return HTTPRequestValidationHandler.create()
    .addPathParam("id", ParameterType.INT);
}
 
Example #17
Source File: HttpRequestValidator.java    From vertx-postgresql-starter with MIT License 4 votes vote down vote up
public static HTTPRequestValidationHandler deleteBookByIdValidationHandler() {
  return HTTPRequestValidationHandler.create()
    .addPathParam("id", ParameterType.INT);
}
 
Example #18
Source File: HttpRequestValidator.java    From vertx-postgresql-starter with MIT License 4 votes vote down vote up
public static HTTPRequestValidationHandler addBookValidationHandler() {
  return HTTPRequestValidationHandler.create()
    .addQueryParam("title", ParameterType.GENERIC_STRING, false)
    .addQueryParam("category", ParameterType.GENERIC_STRING, false)
    .addQueryParam("publicationDate", ParameterType.DATE, false);
}