Search in sources :

Example 1 with ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor 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 ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor 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 ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor 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 ParameterProcessor

use of io.vertx.ext.web.validation.impl.parameter.ParameterProcessor 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 ParameterProcessor

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

the class ParameterProcessorIntegrationTest method testJsonParam.

@Test
public void testJsonParam(VertxTestContext testContext) {
    ParameterProcessor processor = Parameters.jsonParam("myParam", TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER).create(ParameterLocation.QUERY, parser);
    Map<String, List<String>> map = new HashMap<>();
    map.put("myParam", Collections.singletonList(TestSchemas.VALID_OBJECT.encode()));
    processor.process(map).onComplete(testContext.succeeding(rp -> {
        testContext.verify(() -> {
            assertThat(rp.isJsonObject()).isTrue();
            assertThat(rp.getJsonObject()).isEqualTo(TestSchemas.VALID_OBJECT);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Vertx(io.vertx.core.Vertx) SchemaParser(io.vertx.json.schema.SchemaParser) HashMap(java.util.HashMap) VertxExtension(io.vertx.junit5.VertxExtension) SchemaRouter(io.vertx.json.schema.SchemaRouter) TestSchemas(io.vertx.ext.web.validation.testutils.TestSchemas) Test(org.junit.jupiter.api.Test) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Parameters(io.vertx.ext.web.validation.builder.Parameters) ParameterProcessorException(io.vertx.ext.web.validation.ParameterProcessorException) Map(java.util.Map) ValidationException(io.vertx.json.schema.ValidationException) Collections(java.util.Collections) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ParameterProcessor(io.vertx.ext.web.validation.impl.parameter.ParameterProcessor) HashMap(java.util.HashMap) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

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