Search in sources :

Example 1 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testNull.

@Test
public void testNull(VertxTestContext testContext) {
    when(mockedContext.getBody()).thenReturn(Buffer.buffer("null"));
    BodyProcessor processor = Bodies.json(schema().withKeyword("type", "null")).create(parser);
    processor.process(mockedContext).onComplete(testContext.succeeding(rp -> {
        testContext.verify(() -> {
            assertThat(rp.isNull()).isTrue();
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DecodeException(io.vertx.core.json.DecodeException) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Schemas.schema(io.vertx.json.schema.draft7.dsl.Schemas.schema) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) 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) Buffer(io.vertx.core.buffer.Buffer) 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) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 2 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testInvalidJsonArray.

@Test
public void testInvalidJsonArray(VertxTestContext testContext) {
    when(mockerServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/json");
    when(mockedContext.request()).thenReturn(mockerServerRequest);
    when(mockedContext.getBody()).thenReturn(TestSchemas.INVALID_ARRAY.toBuffer());
    BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_ARRAY_SCHEMA_BUILDER).create(parser);
    processor.process(mockedContext).onComplete(testContext.failing(err -> {
        testContext.verify(() -> {
            assertThat(err).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/json").hasCauseInstanceOf(ValidationException.class);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DecodeException(io.vertx.core.json.DecodeException) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Schemas.schema(io.vertx.json.schema.draft7.dsl.Schemas.schema) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) 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) Buffer(io.vertx.core.buffer.Buffer) 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) ValidationException(io.vertx.json.schema.ValidationException) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 3 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testJsonArray.

@Test
public void testJsonArray(VertxTestContext testContext) {
    when(mockedContext.getBody()).thenReturn(TestSchemas.VALID_ARRAY.toBuffer());
    BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_ARRAY_SCHEMA_BUILDER).create(parser);
    processor.process(mockedContext).onComplete(testContext.succeeding(rp -> {
        testContext.verify(() -> {
            assertThat(rp.isJsonArray()).isTrue();
            assertThat(rp.getJsonArray()).isEqualTo(TestSchemas.VALID_ARRAY);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DecodeException(io.vertx.core.json.DecodeException) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Schemas.schema(io.vertx.json.schema.draft7.dsl.Schemas.schema) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) 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) Buffer(io.vertx.core.buffer.Buffer) 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) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 4 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class FormBodyProcessorImplTest method testFormBodyProcessorParsingFailure.

@Test
public void testFormBodyProcessorParsingFailure(VertxTestContext testContext) {
    ObjectSchemaBuilder schemaBuilder = TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER;
    MultiMap map = MultiMap.caseInsensitiveMultiMap();
    map.add("someNumbers", "1.1");
    map.add("someNumbers", "2.2");
    map.add("oneNumber", "3.3");
    map.add("someIntegers", "1");
    map.add("someIntegers", "hello");
    map.add("oneInteger", "3");
    map.add("aBoolean", "true");
    when(mockedServerRequest.getHeader(HttpHeaders.CONTENT_TYPE)).thenReturn("application/x-www-form-urlencoded");
    when(mockedServerRequest.formAttributes()).thenReturn(map);
    when(mockedContext.request()).thenReturn(mockedServerRequest);
    BodyProcessor processor = Bodies.formUrlEncoded(schemaBuilder).create(parser);
    assertThat(processor.canProcess("application/x-www-form-urlencoded")).isTrue();
    processor.process(mockedContext).onComplete(testContext.failing(err -> {
        testContext.verify(() -> {
            assertThat(err).isInstanceOf(BodyProcessorException.class).hasFieldOrPropertyWithValue("actualContentType", "application/x-www-form-urlencoded").hasCauseInstanceOf(MalformedValueException.class);
        });
        testContext.completeNow();
    }));
}
Also used : VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) HttpServerRequest(io.vertx.core.http.HttpServerRequest) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiMap(io.vertx.core.MultiMap) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) JsonObject(io.vertx.core.json.JsonObject) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) 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) JsonArray(io.vertx.core.json.JsonArray) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) SchemaRouterOptions(io.vertx.json.schema.SchemaRouterOptions) Draft7SchemaParser(io.vertx.json.schema.draft7.Draft7SchemaParser) ObjectSchemaBuilder(io.vertx.json.schema.common.dsl.ObjectSchemaBuilder) MultiMap(io.vertx.core.MultiMap) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) MalformedValueException(io.vertx.ext.web.validation.MalformedValueException) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Example 5 with BodyProcessor

use of io.vertx.ext.web.validation.impl.body.BodyProcessor in project vertx-web by vert-x3.

the class JsonBodyProcessorImplTest method testJsonObject.

@Test
public void testJsonObject(VertxTestContext testContext) {
    when(mockedContext.getBody()).thenReturn(TestSchemas.VALID_OBJECT.toBuffer());
    BodyProcessor processor = Bodies.json(TestSchemas.SAMPLE_OBJECT_SCHEMA_BUILDER).create(parser);
    processor.process(mockedContext).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) HttpServerRequest(io.vertx.core.http.HttpServerRequest) DecodeException(io.vertx.core.json.DecodeException) Mock(org.mockito.Mock) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RoutingContext(io.vertx.ext.web.RoutingContext) BodyProcessorException(io.vertx.ext.web.validation.BodyProcessorException) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ValidationException(io.vertx.json.schema.ValidationException) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) Schemas.schema(io.vertx.json.schema.draft7.dsl.Schemas.schema) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) SchemaParser(io.vertx.json.schema.SchemaParser) Mockito.when(org.mockito.Mockito.when) Bodies(io.vertx.ext.web.validation.builder.Bodies) 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) Buffer(io.vertx.core.buffer.Buffer) 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) BodyProcessor(io.vertx.ext.web.validation.impl.body.BodyProcessor) Test(org.junit.jupiter.api.Test)

Aggregations

BodyProcessor (io.vertx.ext.web.validation.impl.body.BodyProcessor)13 Test (org.junit.jupiter.api.Test)12 SchemaParser (io.vertx.json.schema.SchemaParser)9 Vertx (io.vertx.core.Vertx)8 HttpHeaders (io.vertx.core.http.HttpHeaders)8 HttpServerRequest (io.vertx.core.http.HttpServerRequest)8 RoutingContext (io.vertx.ext.web.RoutingContext)8 BodyProcessorException (io.vertx.ext.web.validation.BodyProcessorException)8 MalformedValueException (io.vertx.ext.web.validation.MalformedValueException)8 Bodies (io.vertx.ext.web.validation.builder.Bodies)8 TestSchemas (io.vertx.ext.web.validation.testutils.TestSchemas)8 SchemaRouter (io.vertx.json.schema.SchemaRouter)8 SchemaRouterOptions (io.vertx.json.schema.SchemaRouterOptions)8 Draft7SchemaParser (io.vertx.json.schema.draft7.Draft7SchemaParser)8 VertxExtension (io.vertx.junit5.VertxExtension)8 VertxTestContext (io.vertx.junit5.VertxTestContext)8 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)8 Mock (org.mockito.Mock)8