Validator { public void validate(HttpServerRequest req, String type, Buffer jsonBuffer, Handler<ValidationResult> callback) { final Logger log = RequestLoggerFactory.getLogger(Validator.class, req); if(!req.path().startsWith(schemaRoot)) { log.debug("Validating request"); doValidate(jsonBuffer, req.path(), schemaRoot, type, (req.path().replaceFirst("^/", "")+"/"+type).split("/"), log, callback); } } Validator(ResourceStorage storage, String schemaRoot); void validate(HttpServerRequest req, String type, Buffer jsonBuffer, Handler<ValidationResult> callback); static ValidationResult validateStatic(Buffer dataToBeValidated, String schemaAsString, Logger log);  }
@Test public void testValidationWithVariables(TestContext context){ prepareSchema("{\n" + " \"$schema\": \"http: " \"type\": \"object\"\n" + "}"); class GETValidationResourceRequest extends HttpServerRequestMock { @Override public HttpMethod method() { return HttpMethod.GET; } @Override public String uri() { return "/foo/mediadata/v1/specials/03"; } @Override public String path() { return "/foo/mediadata/v1/specials/03"; } } String type = "GET/out"; Buffer jsonBuffer = Buffer.buffer(); final SettableFuture<String> future = SettableFuture.create(); validator.validate(new GETValidationResourceRequest(), type, jsonBuffer, validationResult -> { String message = validationResult.getMessage(); context.assertFalse(message.contains("Could not get path"), message); context.assertFalse(message.contains("No schema for"), message); context.assertTrue(message.contains("Invalid JSON"), message); future.set(message); }); context.assertTrue(future.isDone()); }
@Test public void testValidationWithNonValidResourceContent(TestContext context){ Async async = context.async(); prepareSchema(SAMPLE_SCHEMA); class GETValidationResourceRequest extends HttpServerRequestMock { @Override public HttpMethod method() { return HttpMethod.GET; } @Override public String uri() { return "/foo/mediadata/v1/specials/03"; } @Override public String path() { return "/foo/mediadata/v1/specials/03"; } } String type = "GET/out"; Buffer jsonBuffer = Buffer.buffer(CONTENT_NOT_MATCHING_SAMPLE_SCHEMA); validator.validate(new GETValidationResourceRequest(), type, jsonBuffer, validationResult -> { context.assertFalse(validationResult.isSuccess()); context.assertEquals(ValidationStatus.VALIDATED_NEGATIV, validationResult.getValidationStatus()); String message = validationResult.getMessage(); context.assertFalse(message.contains("Could not get path"), message); context.assertFalse(message.contains("No schema for"), message); context.assertTrue(message.contains("Invalid JSON for /foo/mediadata/v1/specials/03"), message); context.assertTrue(message.contains("\"message\" : \"$.lastName: is missing but it is required\""), message); async.complete(); }); }
@Test public void testValidationWithValidResourceContent(TestContext context){ Async async = context.async(); prepareSchema(SAMPLE_SCHEMA); class GETValidationResourceRequest extends HttpServerRequestMock { @Override public HttpMethod method() { return HttpMethod.GET; } @Override public String uri() { return "/foo/mediadata/v1/specials/03"; } @Override public String path() { return "/foo/mediadata/v1/specials/03"; } } String type = "GET/out"; Buffer jsonBuffer = Buffer.buffer(CONTENT_MATCHING_SAMPLE_SCHEMA); validator.validate(new GETValidationResourceRequest(), type, jsonBuffer, validationResult -> { context.assertTrue(validationResult.isSuccess(), "ValidationResult should be a success (VALIDATED_POSITIV)"); context.assertEquals(ValidationStatus.VALIDATED_POSITIV, validationResult.getValidationStatus()); context.assertNull(validationResult.getMessage(), "Message should be null when validation result was positive"); async.complete(); }); }
@Test public void testValidation(TestContext context){ storage.putMockData("/foo/schemas/apis/foo/","{\"foo\": [\"mediamessage\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/","{\"mediamessage\": [\"v1\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/","{\"v1\": [\"output\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/output/","{\"output\": [\"front\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/output/front/","{\"front\": [\"GET\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/output/front/GET/","{\"GET\": [\"out\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/output/front/GET/out","{\n" + " \"$schema\": \"http: " \"type\": \"object\"\n" + "}"); class GETValidationResourceRequest extends HttpServerRequestMock { @Override public HttpMethod method() { return HttpMethod.GET; } @Override public String uri() { return "/foo/mediamessage/v1/output/front"; } @Override public String path() { return "/foo/mediamessage/v1/output/front"; } } String type = "GET/out"; Buffer jsonBuffer = Buffer.buffer(); final SettableFuture<String> future = SettableFuture.create(); validator.validate(new GETValidationResourceRequest(), type, jsonBuffer, validationResult -> { String message = validationResult.getMessage(); context.assertFalse(message.contains("Could not get path"), message); context.assertFalse(message.contains("No schema for"), message); context.assertTrue(message.contains("Invalid JSON"), message); future.set(message); }); context.assertTrue(future.isDone()); }
@Test public void testValidationWithNoSchema(TestContext context){ storage.putMockData("/foo/schemas/apis/foo/","{\"foo\": [\"mediamessage\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/","{\"mediamessage\": [\"v1\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/","{\"v1\": [\"output\"]}"); storage.putMockData("/foo/schemas/apis/foo/mediamessage/v1/output/","{\"output\": [\"front\"]}"); class GETValidationResourceRequest extends HttpServerRequestMock { @Override public HttpMethod method() { return HttpMethod.GET; } @Override public String uri() { return "/foo/mediamessage/v1/output/front"; } @Override public String path() { return "/foo/mediamessage/v1/output/front"; } } String type = "GET/out"; Buffer jsonBuffer = Buffer.buffer(); final SettableFuture<String> future = SettableFuture.create(); validator.validate(new GETValidationResourceRequest(), type, jsonBuffer, validationResult -> { String message = validationResult.getMessage(); context.assertTrue(message.contains("Could not get path"), message); future.set(message); }); context.assertTrue(future.isDone()); }