use of org.leadpony.justify.api.JsonSchemaReader in project selenium_java by sergueik.
the class Validate method validateSchemaAt.
/**
* Reads and validates a JSON schema.
*
* @param location the location of the JSON schema to be read and validated.
* @return the schema read if successful, or {@code null} if errors occcured.
*/
private JsonSchema validateSchemaAt(Location location) {
JsonSchemaReaderFactory factory = createSchemaReaderFactory();
try (JsonSchemaReader reader = factory.createSchemaReader(openSchema(location))) {
JsonSchema schema = reader.read();
console.withColor(Color.SUCCESS).print(SCHEMA_VALID, location);
return schema;
} catch (JsonValidatingException e) {
List<Problem> problems = e.getProblems();
problemPrinter.handleProblems(problems);
console.withColor(Color.DANGER).print(SCHEMA_INVALID, location, Problems.countLeast(problems));
setStatus(Status.INVALID);
return null;
} catch (JsonParsingException e) {
console.withColor(Color.DANGER).print(SCHEMA_MALFORMED, e);
setStatus(Status.INVALID);
return null;
} catch (JsonException e) {
throw new CommandException(e);
}
}
use of org.leadpony.justify.api.JsonSchemaReader in project zilla by aklivity.
the class ConfigureTask method call.
@Override
public Void call() throws Exception {
String configText;
if (configURL == null) {
configText = "{\"name\":\"default\"}";
} else if ("https".equals(configURL.getProtocol()) || "https".equals(configURL.getProtocol())) {
HttpClient client = HttpClient.newBuilder().version(HTTP_2).followRedirects(NORMAL).build();
HttpRequest request = HttpRequest.newBuilder().GET().uri(configURL.toURI()).build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
String body = response.body();
configText = body;
} else {
URLConnection connection = configURL.openConnection();
try (InputStream input = connection.getInputStream()) {
configText = new String(input.readAllBytes(), UTF_8);
}
}
if (config.configSyntaxMustache()) {
configText = Mustache.resolve(configText, System::getenv);
}
logger.accept(configText);
List<String> errors = new LinkedList<>();
parse: try {
InputStream schemaInput = Engine.class.getResourceAsStream("internal/schema/engine.schema.json");
JsonProvider schemaProvider = JsonProvider.provider();
JsonReader schemaReader = schemaProvider.createReader(schemaInput);
JsonObject schemaObject = schemaReader.readObject();
for (URL schemaType : schemaTypes) {
InputStream schemaPatchInput = schemaType.openStream();
JsonReader schemaPatchReader = schemaProvider.createReader(schemaPatchInput);
JsonArray schemaPatchArray = schemaPatchReader.readArray();
JsonPatch schemaPatch = schemaProvider.createPatch(schemaPatchArray);
schemaObject = schemaPatch.apply(schemaObject);
}
JsonParser schemaParser = schemaProvider.createParserFactory(null).createParser(new StringReader(schemaObject.toString()));
JsonValidationService service = JsonValidationService.newInstance();
ProblemHandler handler = service.createProblemPrinter(errors::add);
JsonSchemaReader reader = service.createSchemaReader(schemaParser);
JsonSchema schema = new UniquePropertyKeysSchema(reader.read());
JsonProvider provider = service.createJsonProvider(schema, parser -> handler);
provider.createReader(new StringReader(configText)).read();
if (!errors.isEmpty()) {
break parse;
}
JsonbConfig config = new JsonbConfig().withAdapters(new NamespaceAdapter());
Jsonb jsonb = JsonbBuilder.newBuilder().withProvider(provider).withConfig(config).build();
NamespaceConfig namespace = jsonb.fromJson(configText, NamespaceConfig.class);
if (!errors.isEmpty()) {
break parse;
}
namespace.id = supplyId.applyAsInt(namespace.name);
for (BindingConfig binding : namespace.bindings) {
binding.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(binding.entry));
if (binding.vault != null) {
binding.vault.id = NamespacedId.id(supplyId.applyAsInt(ofNullable(binding.vault.namespace).orElse(namespace.name)), supplyId.applyAsInt(binding.vault.name));
}
// TODO: consider route exit namespace
for (RouteConfig route : binding.routes) {
route.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(route.exit));
}
// TODO: consider binding exit namespace
if (binding.exit != null) {
binding.exit.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(binding.exit.exit));
}
tuning.affinity(binding.id, tuning.affinity(binding.id));
}
for (VaultConfig vault : namespace.vaults) {
vault.id = NamespacedId.id(namespace.id, supplyId.applyAsInt(vault.name));
}
CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
for (DispatchAgent dispatcher : dispatchers) {
future = CompletableFuture.allOf(future, dispatcher.attach(namespace));
}
future.join();
extensions.forEach(e -> e.onConfigured(context));
} catch (Throwable ex) {
errorHandler.onError(ex);
}
if (!errors.isEmpty()) {
errors.forEach(msg -> errorHandler.onError(new JsonException(msg)));
}
return null;
}
use of org.leadpony.justify.api.JsonSchemaReader in project zilla by aklivity.
the class ConfigSchemaRule method apply.
@Override
public Statement apply(Statement base, Description description) {
Objects.requireNonNull(schemaName, "schema");
schemaPatchNames.forEach(n -> Objects.requireNonNull(n, "schemaPatch"));
Function<String, InputStream> findResource = description.getTestClass().getClassLoader()::getResourceAsStream;
InputStream schemaInput = findResource.apply(schemaName);
JsonProvider schemaProvider = JsonProvider.provider();
JsonReader schemaReader = schemaProvider.createReader(schemaInput);
JsonObject schemaObject = schemaReader.readObject();
for (String schemaPatchName : schemaPatchNames) {
InputStream schemaPatchInput = findResource.apply(schemaPatchName);
Objects.requireNonNull(schemaPatchInput, "schemaPatch");
JsonReader schemaPatchReader = schemaProvider.createReader(schemaPatchInput);
JsonArray schemaPatchArray = schemaPatchReader.readArray();
JsonPatch schemaPatch = schemaProvider.createPatch(schemaPatchArray);
schemaObject = schemaPatch.apply(schemaObject);
}
JsonParser schemaParser = schemaProvider.createParserFactory(null).createParser(new StringReader(schemaObject.toString()));
JsonValidationService service = JsonValidationService.newInstance();
ProblemHandler handler = service.createProblemPrinter(msg -> rethrowUnchecked(new JsonException(msg)));
JsonSchemaReader reader = service.createSchemaReader(schemaParser);
JsonSchema schema = reader.read();
provider = service.createJsonProvider(schema, parser -> handler);
if (configurationRoot != null) {
String configFormat = String.format("%s/%%s", configurationRoot);
findConfig = configName -> findResource.apply(String.format(configFormat, configName));
} else {
Class<?> testClass = description.getTestClass();
String configFormat = String.format("%s-%%s", testClass.getSimpleName());
findConfig = configName -> testClass.getResourceAsStream(String.format(configFormat, configName));
}
return base;
}
Aggregations