Search in sources :

Example 1 with ParameterProcessorImpl

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testParsingFailure.

@Test
public void testParsingFailure() {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, false, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenThrow(new MalformedValueException("bla"));
    assertThatCode(() -> processor.process(new HashMap<>())).isInstanceOf(ParameterProcessorException.class).hasFieldOrPropertyWithValue("errorType", ParameterProcessorException.ParameterProcessorErrorType.PARSING_ERROR).hasFieldOrPropertyWithValue("location", ParameterLocation.QUERY).hasFieldOrPropertyWithValue("parameterName", "myParam").hasCauseInstanceOf(MalformedValueException.class);
}
Also used : ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 2 with ParameterProcessorImpl

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testValidationFailure.

@Test
public void testValidationFailure(VertxTestContext testContext) {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, true, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenReturn("aaa");
    when(mockedValidator.validate(any())).thenReturn(Future.failedFuture(ValidationException.createException("aaa", "aaa", "aaa")));
    processor.process(new HashMap<>()).onComplete(testContext.failing(throwable -> {
        testContext.verify(() -> {
            assertThat(throwable).isInstanceOf(ParameterProcessorException.class).hasFieldOrPropertyWithValue("errorType", ParameterProcessorException.ParameterProcessorErrorType.VALIDATION_ERROR).hasFieldOrPropertyWithValue("location", ParameterLocation.QUERY).hasFieldOrPropertyWithValue("parameterName", "myParam").hasCauseInstanceOf(ValidationException.class);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) RequestParameter(io.vertx.ext.web.validation.RequestParameter) ParameterParser(io.vertx.ext.web.validation.impl.parameter.ParameterParser) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) Vertx(io.vertx.core.Vertx) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) SchemaRouter(io.vertx.json.schema.SchemaRouter) Test(org.junit.jupiter.api.Test) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) ValueValidator(io.vertx.ext.web.validation.impl.validator.ValueValidator) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) ValidationException(io.vertx.json.schema.ValidationException) HashMap(java.util.HashMap) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 3 with ParameterProcessorImpl

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testOptionalParam.

@Test
public void testOptionalParam(VertxTestContext testContext) {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, true, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenReturn(null);
    when(mockedValidator.getDefault()).thenReturn(Future.succeededFuture());
    processor.process(new HashMap<>()).onComplete(testContext.succeeding(value -> {
        testContext.verify(() -> assertThat(value).isNull());
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) RequestParameter(io.vertx.ext.web.validation.RequestParameter) ParameterParser(io.vertx.ext.web.validation.impl.parameter.ParameterParser) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) Vertx(io.vertx.core.Vertx) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) SchemaRouter(io.vertx.json.schema.SchemaRouter) Test(org.junit.jupiter.api.Test) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) ValueValidator(io.vertx.ext.web.validation.impl.validator.ValueValidator) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) HashMap(java.util.HashMap) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 4 with ParameterProcessorImpl

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl in project vertx-web by vert-x3.

the class ParameterProcessorUnitTest method testValidation.

@Test
public void testValidation(VertxTestContext testContext) {
    ParameterProcessor processor = new ParameterProcessorImpl("myParam", ParameterLocation.QUERY, true, mockedParser, mockedValidator);
    when(mockedParser.parseParameter(any())).thenReturn("aaa");
    when(mockedValidator.validate(any())).thenReturn(Future.succeededFuture(RequestParameter.create("aaa")));
    processor.process(new HashMap<>()).onComplete(testContext.succeeding(rp -> {
        testContext.verify(() -> {
            assertThat(rp.isString()).isTrue();
            assertThat(rp.getString()).isEqualTo("aaa");
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HashMap(java.util.HashMap) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) RequestParameter(io.vertx.ext.web.validation.RequestParameter) ParameterParser(io.vertx.ext.web.validation.impl.parameter.ParameterParser) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) Vertx(io.vertx.core.Vertx) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) SchemaRouter(io.vertx.json.schema.SchemaRouter) Test(org.junit.jupiter.api.Test) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) ValueValidator(io.vertx.ext.web.validation.impl.validator.ValueValidator) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) HashMap(java.util.HashMap) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl) Test(org.junit.jupiter.api.Test)

Example 5 with ParameterProcessorImpl

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl in project vertx-web by vert-x3.

the class JsonParameterProcessorGenerator method generate.

@Override
public ParameterProcessor generate(JsonObject parameter, JsonObject fakeSchema, JsonPointer parameterPointer, ParameterLocation parsedLocation, String parsedStyle, GeneratorContext context) {
    JsonObject originalSchema = (JsonObject) SCHEMA_POINTER.queryJsonOrDefault(parameter, new JsonObject());
    SchemaHolder schemas = context.getSchemaHolder(originalSchema, context.fakeSchema(fakeSchema), parameterPointer.copy().append("content").append("application/json").append("schema"));
    return new ParameterProcessorImpl(parameter.getString("name"), parsedLocation, !parameter.getBoolean("required", false), new SingleValueParameterParser(parsedLocation.lowerCaseIfNeeded(parameter.getString("name")), ValueParser.JSON_PARSER), schemas.getValidator());
}
Also used : SingleValueParameterParser(io.vertx.ext.web.validation.impl.parameter.SingleValueParameterParser) JsonObject(io.vertx.core.json.JsonObject) ParameterProcessorImpl(io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl)

Aggregations

ParameterProcessorImpl (io.vertx.ext.web.validation.impl.parameter.ParameterProcessorImpl)7 ParameterProcessor (io.vertx.ext.web.validation.impl.parameter.ParameterProcessor)6 Test (org.junit.jupiter.api.Test)6 MalformedValueException (io.vertx.ext.web.validation.MalformedValueException)5 Future (io.vertx.core.Future)4 Vertx (io.vertx.core.Vertx)4 ParameterProcessorException (io.vertx.ext.web.validation.ParameterProcessorException)4 RequestParameter (io.vertx.ext.web.validation.RequestParameter)4 ParameterParser (io.vertx.ext.web.validation.impl.parameter.ParameterParser)4 ValueValidator (io.vertx.ext.web.validation.impl.validator.ValueValidator)4 SchemaParser (io.vertx.json.schema.SchemaParser)4 SchemaRouter (io.vertx.json.schema.SchemaRouter)4 SchemaRouterOptions (io.vertx.json.schema.SchemaRouterOptions)4 ValidationException (io.vertx.json.schema.ValidationException)4 Draft7SchemaParser (io.vertx.json.schema.draft7.Draft7SchemaParser)4 VertxExtension (io.vertx.junit5.VertxExtension)4 VertxTestContext (io.vertx.junit5.VertxTestContext)4 HashMap (java.util.HashMap)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Assertions.assertThatCode (org.assertj.core.api.Assertions.assertThatCode)4