From 67fefdd5b25b17a3ac6c9bee89a97eb5da006787 Mon Sep 17 00:00:00 2001 From: Harrel Date: Sun, 12 Nov 2023 21:44:06 +0100 Subject: [PATCH 1/5] path fixes for windows --- .../org/creekservice/kafka/test/perf/testsuite/TestSuite.java | 4 +++- .../kafka/test/perf/testsuite/TestSuiteLoader.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java index abb6bc7..6fc37d0 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java +++ b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java @@ -22,6 +22,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonNode; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import java.io.File; import java.io.IOException; import java.nio.file.Path; import java.util.List; @@ -53,7 +55,7 @@ public TestSuite( this.suiteFilePath = requireNonNull(suiteFilePath, "suiteFilePath"); this.optional = suiteFilePath.getParent() != null - && suiteFilePath.getParent().toString().contains("/optional"); + && suiteFilePath.getParent().toString().contains(File.separator + "optional"); } catch (final IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java index d0aec30..17912ec 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java +++ b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java @@ -94,7 +94,7 @@ public JsonSchemaTestSuite load(final Path rootDir) { private static Map loadRemotes(final Path remotes) { final Function createKey = - path -> URI.create("http://localhost:1234/" + remotes.relativize(path)); + path -> URI.create("http://localhost:1234/" + remotes.relativize(path).toString().replace("\\", "/")); final Function readContent = path -> { From b31eebf00ec874761be33619d21336b0872a5e96 Mon Sep 17 00:00:00 2001 From: Harrel Date: Sun, 12 Nov 2023 21:45:17 +0100 Subject: [PATCH 2/5] add dev.harrel:json-schema --- build.gradle.kts | 2 + .../kafka/test/perf/JsonSerdeBenchmark.java | 24 ++-- .../test/perf/JsonValidateBenchmark.java | 27 ++-- .../DevHarrelImplementation.java | 133 ++++++++++++++++++ .../perf/implementations/Implementations.java | 3 +- 5 files changed, 168 insertions(+), 21 deletions(-) create mode 100644 src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java diff --git a/build.gradle.kts b/build.gradle.kts index b55c27a..2b79475 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -71,6 +71,8 @@ dependencies { implementation("org.leadpony.justify:justify:3.1.0") + implementation("dev.harrel:json-schema:1.4.1") + implementation("org.apache.logging.log4j:log4j-core:$log4jVersion") runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:$log4jVersion") diff --git a/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java b/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java index 96ac38a..d65cc67 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java +++ b/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java @@ -20,17 +20,8 @@ import java.nio.file.Path; import java.util.Map; -import org.creekservice.kafka.test.perf.implementations.ConfluentImplementation; -import org.creekservice.kafka.test.perf.implementations.EveritImplementation; -import org.creekservice.kafka.test.perf.implementations.Implementation; -import org.creekservice.kafka.test.perf.implementations.JacksonImplementation; -import org.creekservice.kafka.test.perf.implementations.JustifyImplementation; -import org.creekservice.kafka.test.perf.implementations.MedeiaImplementation; -import org.creekservice.kafka.test.perf.implementations.NetworkNtImplementation; -import org.creekservice.kafka.test.perf.implementations.SchemaFriendImplementation; -import org.creekservice.kafka.test.perf.implementations.SkemaImplementation; -import org.creekservice.kafka.test.perf.implementations.SnowImplementation; -import org.creekservice.kafka.test.perf.implementations.VertxImplementation; + +import org.creekservice.kafka.test.perf.implementations.*; import org.creekservice.kafka.test.perf.model.ModelState; import org.creekservice.kafka.test.perf.model.TestModel; import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; @@ -197,6 +188,17 @@ public TestModel measureJustifyRoundTrip(final JustifyState impl, final ModelSta return impl.roundTrip(model); } + public static class DevHarrelState extends ImplementationState { + public DevHarrelState() { + super(new DevHarrelImplementation()); + } + } + + @Benchmark + public TestModel measureDevHarrelRoundTrip(final DevHarrelState impl, final ModelState model) { + return impl.roundTrip(model); + } + @State(Scope.Thread) private static class ImplementationState { diff --git a/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java b/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java index 0f1a939..2a0d253 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java +++ b/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java @@ -19,15 +19,7 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import org.creekservice.api.test.util.TestPaths; -import org.creekservice.kafka.test.perf.implementations.EveritImplementation; -import org.creekservice.kafka.test.perf.implementations.Implementation; -import org.creekservice.kafka.test.perf.implementations.JustifyImplementation; -import org.creekservice.kafka.test.perf.implementations.MedeiaImplementation; -import org.creekservice.kafka.test.perf.implementations.NetworkNtImplementation; -import org.creekservice.kafka.test.perf.implementations.SchemaFriendImplementation; -import org.creekservice.kafka.test.perf.implementations.SkemaImplementation; -import org.creekservice.kafka.test.perf.implementations.SnowImplementation; -import org.creekservice.kafka.test.perf.implementations.VertxImplementation; +import org.creekservice.kafka.test.perf.implementations.*; import org.creekservice.kafka.test.perf.testsuite.JsonSchemaTestSuite; import org.creekservice.kafka.test.perf.testsuite.JsonSchemaTestSuite.Result; import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; @@ -301,6 +293,23 @@ public Result measureDraft_7_Justify(final JustifyValidator validator) { return validator.validate(SchemaSpec.DRAFT_07); } + public static class DevHarrelValidator extends ValidatorState { + + public DevHarrelValidator() { + super(new DevHarrelImplementation()); + } + } + + @Benchmark + public Result measureDraft_2019_09_DevHarrel(final DevHarrelValidator validator) { + return validator.validate(SchemaSpec.DRAFT_2019_09); + } + + @Benchmark + public Result measureDraft_2020_12_DevHarrel(final DevHarrelValidator validator) { + return validator.validate(SchemaSpec.DRAFT_2020_12); + } + @State(Scope.Benchmark) @SuppressWarnings("FieldMayBeFinal") // not final to avoid folding. abstract static class ValidatorState { diff --git a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java new file mode 100644 index 0000000..c5aa43e --- /dev/null +++ b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java @@ -0,0 +1,133 @@ +/* + * Copyright 2023 Creek Contributors (https://github.com/creek-service) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.creekservice.kafka.test.perf.implementations; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.json.JsonMapper; +import dev.harrel.jsonschema.Dialects; +import dev.harrel.jsonschema.SchemaResolver; +import dev.harrel.jsonschema.SpecificationVersion; +import dev.harrel.jsonschema.Validator; +import org.creekservice.kafka.test.perf.TestSchemas; +import org.creekservice.kafka.test.perf.model.TestModel; +import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; +import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; + +import java.io.IOException; +import java.net.URI; +import java.nio.charset.StandardCharsets; +import java.util.Map; +import java.util.Set; + +import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.*; + +@SuppressWarnings("FieldMayBeFinal") // not final to avoid folding. +public class DevHarrelImplementation implements Implementation { + + private static final MetaData METADATA = + new MetaData( + "json-schema (dev.harrel)", + "json-schema (dev.harrel)", + Language.Java, + Licence.MIT, + Set.of(DRAFT_2020_12, DRAFT_2019_09), + "https://github.com/harrel56/json-schema"); + + private final ObjectMapper mapper = JsonMapper.builder().build(); + private final Map validators; + private final URI testSchemaUri = URI.create("urn:test"); + private Map remotes = Map.of(); + + public DevHarrelImplementation() { + SchemaResolver schemaResolver = uri -> { + String resolved = remotes.get(URI.create(uri)); + if (resolved == null) { + return SchemaResolver.Result.empty(); + } + return SchemaResolver.Result.fromString(resolved); + }; + Validator validator2020 = new dev.harrel.jsonschema.ValidatorFactory() + .withSchemaResolver(schemaResolver) + .createValidator(); + Validator validator2019 = new dev.harrel.jsonschema.ValidatorFactory() + .withDialect(new Dialects.Draft2019Dialect()) + .withSchemaResolver(schemaResolver) + .createValidator(); + /* Validate against meta-schemas in order to parse them eagerly */ + validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); + validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); + + validator2020.registerSchema(testSchemaUri, TestSchemas.DRAFT_2020_SCHEMA); + + this.validators = Map.of( + DRAFT_2020_12, validator2020, + DRAFT_2019_09, validator2019); + } + + @Override + public MetaData metadata() { + return METADATA; + } + + @Override + public JsonValidator prepare( + final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { + DevHarrelImplementation.this.remotes = additionalSchemas.remotes(); + Validator validator = validators.get(spec); + URI schemaUri = validator.registerSchema(schema); + + return new JsonValidator() { + @Override + public void validate(final String json) { + Validator.Result result = validator.validate(schemaUri, json); + if (!result.isValid()) { + throw new RuntimeException(); + } + } + + @Override + public byte[] serialize(final TestModel model, final boolean validate) { + try { + String asString = mapper.writeValueAsString(model); + if (validate) { + validators.get(DRAFT_2020_12).validate(testSchemaUri, asString); + } + return asString.getBytes(StandardCharsets.UTF_8); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + @Override + public TestModel deserialize(final byte[] data) { + try { + validators.get(DRAFT_2020_12).validate(testSchemaUri, new String(data, StandardCharsets.UTF_8)); + return mapper.readValue(data, TestModel.class); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + }; + } + + // Final, empty finalize method stops spotbugs CT_CONSTRUCTOR_THROW + // Can be moved to base type after https://github.com/spotbugs/spotbugs/issues/2665 + @Override + @SuppressWarnings({"deprecation", "Finalize"}) + protected final void finalize() {} +} diff --git a/src/main/java/org/creekservice/kafka/test/perf/implementations/Implementations.java b/src/main/java/org/creekservice/kafka/test/perf/implementations/Implementations.java index ef8c34b..4c842ab 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/implementations/Implementations.java +++ b/src/main/java/org/creekservice/kafka/test/perf/implementations/Implementations.java @@ -34,7 +34,8 @@ public final class Implementations { new SchemaFriendImplementation(), new SkemaImplementation(), new SnowImplementation(), - new VertxImplementation()); + new VertxImplementation(), + new DevHarrelImplementation()); public static List all() { return IMPLS; From cf5ee335fb89a16b07abed7f8eefc79f23cf1fa6 Mon Sep 17 00:00:00 2001 From: Harrel Date: Sun, 12 Nov 2023 22:01:14 +0100 Subject: [PATCH 3/5] add test, fix checkstyle --- .../kafka/test/perf/JsonSerdeBenchmark.java | 14 +++- .../test/perf/JsonValidateBenchmark.java | 11 ++- .../DevHarrelImplementation.java | 75 +++++++++++-------- .../kafka/test/perf/testsuite/TestSuite.java | 6 +- .../test/perf/testsuite/TestSuiteLoader.java | 5 +- .../DevHarrelImplementationTest.java | 19 +++++ 6 files changed, 93 insertions(+), 37 deletions(-) create mode 100644 src/test/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementationTest.java diff --git a/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java b/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java index d65cc67..e76e6ea 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java +++ b/src/main/java/org/creekservice/kafka/test/perf/JsonSerdeBenchmark.java @@ -20,8 +20,18 @@ import java.nio.file.Path; import java.util.Map; - -import org.creekservice.kafka.test.perf.implementations.*; +import org.creekservice.kafka.test.perf.implementations.ConfluentImplementation; +import org.creekservice.kafka.test.perf.implementations.DevHarrelImplementation; +import org.creekservice.kafka.test.perf.implementations.EveritImplementation; +import org.creekservice.kafka.test.perf.implementations.Implementation; +import org.creekservice.kafka.test.perf.implementations.JacksonImplementation; +import org.creekservice.kafka.test.perf.implementations.JustifyImplementation; +import org.creekservice.kafka.test.perf.implementations.MedeiaImplementation; +import org.creekservice.kafka.test.perf.implementations.NetworkNtImplementation; +import org.creekservice.kafka.test.perf.implementations.SchemaFriendImplementation; +import org.creekservice.kafka.test.perf.implementations.SkemaImplementation; +import org.creekservice.kafka.test.perf.implementations.SnowImplementation; +import org.creekservice.kafka.test.perf.implementations.VertxImplementation; import org.creekservice.kafka.test.perf.model.ModelState; import org.creekservice.kafka.test.perf.model.TestModel; import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; diff --git a/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java b/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java index 2a0d253..36b54c1 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java +++ b/src/main/java/org/creekservice/kafka/test/perf/JsonValidateBenchmark.java @@ -19,7 +19,16 @@ import static java.util.concurrent.TimeUnit.MILLISECONDS; import org.creekservice.api.test.util.TestPaths; -import org.creekservice.kafka.test.perf.implementations.*; +import org.creekservice.kafka.test.perf.implementations.DevHarrelImplementation; +import org.creekservice.kafka.test.perf.implementations.EveritImplementation; +import org.creekservice.kafka.test.perf.implementations.Implementation; +import org.creekservice.kafka.test.perf.implementations.JustifyImplementation; +import org.creekservice.kafka.test.perf.implementations.MedeiaImplementation; +import org.creekservice.kafka.test.perf.implementations.NetworkNtImplementation; +import org.creekservice.kafka.test.perf.implementations.SchemaFriendImplementation; +import org.creekservice.kafka.test.perf.implementations.SkemaImplementation; +import org.creekservice.kafka.test.perf.implementations.SnowImplementation; +import org.creekservice.kafka.test.perf.implementations.VertxImplementation; import org.creekservice.kafka.test.perf.testsuite.JsonSchemaTestSuite; import org.creekservice.kafka.test.perf.testsuite.JsonSchemaTestSuite.Result; import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; diff --git a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java index c5aa43e..124cbe1 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java +++ b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java @@ -16,6 +16,9 @@ package org.creekservice.kafka.test.perf.implementations; +import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_2019_09; +import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_2020_12; + import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; @@ -23,18 +26,15 @@ import dev.harrel.jsonschema.SchemaResolver; import dev.harrel.jsonschema.SpecificationVersion; import dev.harrel.jsonschema.Validator; -import org.creekservice.kafka.test.perf.TestSchemas; -import org.creekservice.kafka.test.perf.model.TestModel; -import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; -import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; - import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; import java.util.Map; import java.util.Set; - -import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.*; +import org.creekservice.kafka.test.perf.TestSchemas; +import org.creekservice.kafka.test.perf.model.TestModel; +import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; +import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; @SuppressWarnings("FieldMayBeFinal") // not final to avoid folding. public class DevHarrelImplementation implements Implementation { @@ -54,29 +54,33 @@ public class DevHarrelImplementation implements Implementation { private Map remotes = Map.of(); public DevHarrelImplementation() { - SchemaResolver schemaResolver = uri -> { - String resolved = remotes.get(URI.create(uri)); - if (resolved == null) { - return SchemaResolver.Result.empty(); - } - return SchemaResolver.Result.fromString(resolved); - }; - Validator validator2020 = new dev.harrel.jsonschema.ValidatorFactory() - .withSchemaResolver(schemaResolver) - .createValidator(); - Validator validator2019 = new dev.harrel.jsonschema.ValidatorFactory() - .withDialect(new Dialects.Draft2019Dialect()) - .withSchemaResolver(schemaResolver) - .createValidator(); + final SchemaResolver schemaResolver = + uri -> { + final String resolved = remotes.get(URI.create(uri)); + if (resolved == null) { + return SchemaResolver.Result.empty(); + } + return SchemaResolver.Result.fromString(resolved); + }; + final Validator validator2020 = + new dev.harrel.jsonschema.ValidatorFactory() + .withSchemaResolver(schemaResolver) + .createValidator(); + final Validator validator2019 = + new dev.harrel.jsonschema.ValidatorFactory() + .withDialect(new Dialects.Draft2019Dialect()) + .withSchemaResolver(schemaResolver) + .createValidator(); /* Validate against meta-schemas in order to parse them eagerly */ validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); validator2020.registerSchema(testSchemaUri, TestSchemas.DRAFT_2020_SCHEMA); - this.validators = Map.of( - DRAFT_2020_12, validator2020, - DRAFT_2019_09, validator2019); + this.validators = + Map.of( + DRAFT_2020_12, validator2020, + DRAFT_2019_09, validator2019); } @Override @@ -88,13 +92,13 @@ public MetaData metadata() { public JsonValidator prepare( final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { DevHarrelImplementation.this.remotes = additionalSchemas.remotes(); - Validator validator = validators.get(spec); - URI schemaUri = validator.registerSchema(schema); + final Validator validator = validators.get(spec); + final URI schemaUri = validator.registerSchema(schema); return new JsonValidator() { @Override public void validate(final String json) { - Validator.Result result = validator.validate(schemaUri, json); + final Validator.Result result = validator.validate(schemaUri, json); if (!result.isValid()) { throw new RuntimeException(); } @@ -103,9 +107,13 @@ public void validate(final String json) { @Override public byte[] serialize(final TestModel model, final boolean validate) { try { - String asString = mapper.writeValueAsString(model); - if (validate) { - validators.get(DRAFT_2020_12).validate(testSchemaUri, asString); + final String asString = mapper.writeValueAsString(model); + if (validate + && !validators + .get(DRAFT_2020_12) + .validate(testSchemaUri, asString) + .isValid()) { + throw new RuntimeException(); } return asString.getBytes(StandardCharsets.UTF_8); } catch (JsonProcessingException e) { @@ -116,7 +124,12 @@ public byte[] serialize(final TestModel model, final boolean validate) { @Override public TestModel deserialize(final byte[] data) { try { - validators.get(DRAFT_2020_12).validate(testSchemaUri, new String(data, StandardCharsets.UTF_8)); + if (!validators + .get(DRAFT_2020_12) + .validate(testSchemaUri, new String(data, StandardCharsets.UTF_8)) + .isValid()) { + throw new RuntimeException(); + } return mapper.readValue(data, TestModel.class); } catch (IOException e) { throw new RuntimeException(e); diff --git a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java index 6fc37d0..0ec7f52 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java +++ b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuite.java @@ -22,7 +22,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.JsonNode; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import java.io.File; import java.io.IOException; import java.nio.file.Path; @@ -55,7 +54,10 @@ public TestSuite( this.suiteFilePath = requireNonNull(suiteFilePath, "suiteFilePath"); this.optional = suiteFilePath.getParent() != null - && suiteFilePath.getParent().toString().contains(File.separator + "optional"); + && suiteFilePath + .getParent() + .toString() + .contains(File.separator + "optional"); } catch (final IOException e) { throw new RuntimeException(e); } diff --git a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java index 17912ec..5973b61 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java +++ b/src/main/java/org/creekservice/kafka/test/perf/testsuite/TestSuiteLoader.java @@ -94,7 +94,10 @@ public JsonSchemaTestSuite load(final Path rootDir) { private static Map loadRemotes(final Path remotes) { final Function createKey = - path -> URI.create("http://localhost:1234/" + remotes.relativize(path).toString().replace("\\", "/")); + path -> + URI.create( + "http://localhost:1234/" + + remotes.relativize(path).toString().replace("\\", "/")); final Function readContent = path -> { diff --git a/src/test/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementationTest.java b/src/test/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementationTest.java new file mode 100644 index 0000000..4f6d43a --- /dev/null +++ b/src/test/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementationTest.java @@ -0,0 +1,19 @@ +/* + * Copyright 2023 Creek Contributors (https://github.com/creek-service) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.creekservice.kafka.test.perf.implementations; + +class DevHarrelImplementationTest extends ImplementationTest {} From 21e3fbb59758018dc893a5e887f63d7e73fb7211 Mon Sep 17 00:00:00 2001 From: Harrel Date: Mon, 13 Nov 2023 20:35:35 +0100 Subject: [PATCH 4/5] review changes --- .../DevHarrelImplementation.java | 95 +++++++++---------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java index 124cbe1..af1e11f 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java +++ b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java @@ -23,15 +23,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; import dev.harrel.jsonschema.Dialects; -import dev.harrel.jsonschema.SchemaResolver; import dev.harrel.jsonschema.SpecificationVersion; import dev.harrel.jsonschema.Validator; +import java.awt.Color; import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; -import java.util.Map; import java.util.Set; -import org.creekservice.kafka.test.perf.TestSchemas; import org.creekservice.kafka.test.perf.model.TestModel; import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; @@ -42,46 +40,14 @@ public class DevHarrelImplementation implements Implementation { private static final MetaData METADATA = new MetaData( "json-schema (dev.harrel)", - "json-schema (dev.harrel)", + "dev.harrel", Language.Java, Licence.MIT, Set.of(DRAFT_2020_12, DRAFT_2019_09), - "https://github.com/harrel56/json-schema"); - - private final ObjectMapper mapper = JsonMapper.builder().build(); - private final Map validators; - private final URI testSchemaUri = URI.create("urn:test"); - private Map remotes = Map.of(); - - public DevHarrelImplementation() { - final SchemaResolver schemaResolver = - uri -> { - final String resolved = remotes.get(URI.create(uri)); - if (resolved == null) { - return SchemaResolver.Result.empty(); - } - return SchemaResolver.Result.fromString(resolved); - }; - final Validator validator2020 = - new dev.harrel.jsonschema.ValidatorFactory() - .withSchemaResolver(schemaResolver) - .createValidator(); - final Validator validator2019 = - new dev.harrel.jsonschema.ValidatorFactory() - .withDialect(new Dialects.Draft2019Dialect()) - .withSchemaResolver(schemaResolver) - .createValidator(); - /* Validate against meta-schemas in order to parse them eagerly */ - validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); - validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); - - validator2020.registerSchema(testSchemaUri, TestSchemas.DRAFT_2020_SCHEMA); + "https://github.com/harrel56/json-schema", + new Color(235, 54, 172)); - this.validators = - Map.of( - DRAFT_2020_12, validator2020, - DRAFT_2019_09, validator2019); - } + private ObjectMapper mapper = JsonMapper.builder().build(); @Override public MetaData metadata() { @@ -91,11 +57,23 @@ public MetaData metadata() { @Override public JsonValidator prepare( final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { - DevHarrelImplementation.this.remotes = additionalSchemas.remotes(); - final Validator validator = validators.get(spec); + + final Validator validator = validator(spec); + /* Parse remotes eagerly and ignore errors from unknown specs */ + additionalSchemas + .remotes() + .forEach( + (uri, remote) -> { + try { + validator.registerSchema(uri, remote); + } catch (Exception e) { + /* ignore */ + } + }); final URI schemaUri = validator.registerSchema(schema); return new JsonValidator() { + @Override public void validate(final String json) { final Validator.Result result = validator.validate(schemaUri, json); @@ -108,11 +86,7 @@ public void validate(final String json) { public byte[] serialize(final TestModel model, final boolean validate) { try { final String asString = mapper.writeValueAsString(model); - if (validate - && !validators - .get(DRAFT_2020_12) - .validate(testSchemaUri, asString) - .isValid()) { + if (validate && !validator.validate(schemaUri, asString).isValid()) { throw new RuntimeException(); } return asString.getBytes(StandardCharsets.UTF_8); @@ -124,10 +98,8 @@ public byte[] serialize(final TestModel model, final boolean validate) { @Override public TestModel deserialize(final byte[] data) { try { - if (!validators - .get(DRAFT_2020_12) - .validate(testSchemaUri, new String(data, StandardCharsets.UTF_8)) - .isValid()) { + final String json = new String(data, StandardCharsets.UTF_8); + if (!validator.validate(schemaUri, json).isValid()) { throw new RuntimeException(); } return mapper.readValue(data, TestModel.class); @@ -138,6 +110,29 @@ public TestModel deserialize(final byte[] data) { }; } + private Validator validator(final SchemaSpec spec) { + switch (spec) { + case DRAFT_2020_12: + final Validator validator2020 = + new dev.harrel.jsonschema.ValidatorFactory() + .withDialect(new Dialects.Draft2020Dialect()) + .createValidator(); + /* Validate against meta-schema in order to parse it eagerly */ + validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); + return validator2020; + case DRAFT_2019_09: + final Validator validator2019 = + new dev.harrel.jsonschema.ValidatorFactory() + .withDialect(new Dialects.Draft2019Dialect()) + .createValidator(); + /* Validate against meta-schema in order to parse it eagerly */ + validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); + return validator2019; + default: + throw new RuntimeException("Unsupported Spec:" + spec); + } + } + // Final, empty finalize method stops spotbugs CT_CONSTRUCTOR_THROW // Can be moved to base type after https://github.com/spotbugs/spotbugs/issues/2665 @Override From d89281ead859ccc7e89387b46a6224d033855bd2 Mon Sep 17 00:00:00 2001 From: Harrel Date: Tue, 14 Nov 2023 18:42:57 +0100 Subject: [PATCH 5/5] review changes 2 --- .../DevHarrelImplementation.java | 53 ++++++++++++------- 1 file changed, 34 insertions(+), 19 deletions(-) diff --git a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java index af1e11f..7901d03 100644 --- a/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java +++ b/src/main/java/org/creekservice/kafka/test/perf/implementations/DevHarrelImplementation.java @@ -23,13 +23,18 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.json.JsonMapper; import dev.harrel.jsonschema.Dialects; +import dev.harrel.jsonschema.JsonNode; +import dev.harrel.jsonschema.SchemaResolver; import dev.harrel.jsonschema.SpecificationVersion; import dev.harrel.jsonschema.Validator; +import dev.harrel.jsonschema.providers.JacksonNode; import java.awt.Color; import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; +import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import org.creekservice.kafka.test.perf.model.TestModel; import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; @@ -45,7 +50,7 @@ public class DevHarrelImplementation implements Implementation { Licence.MIT, Set.of(DRAFT_2020_12, DRAFT_2019_09), "https://github.com/harrel56/json-schema", - new Color(235, 54, 172)); + new Color(22, 99, 0)); private ObjectMapper mapper = JsonMapper.builder().build(); @@ -58,18 +63,7 @@ public MetaData metadata() { public JsonValidator prepare( final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { - final Validator validator = validator(spec); - /* Parse remotes eagerly and ignore errors from unknown specs */ - additionalSchemas - .remotes() - .forEach( - (uri, remote) -> { - try { - validator.registerSchema(uri, remote); - } catch (Exception e) { - /* ignore */ - } - }); + final Validator validator = validator(spec, additionalSchemas); final URI schemaUri = validator.registerSchema(schema); return new JsonValidator() { @@ -78,7 +72,7 @@ public JsonValidator prepare( public void validate(final String json) { final Validator.Result result = validator.validate(schemaUri, json); if (!result.isValid()) { - throw new RuntimeException(); + throw new RuntimeException(result.getErrors().get(0).getError()); } } @@ -86,8 +80,9 @@ public void validate(final String json) { public byte[] serialize(final TestModel model, final boolean validate) { try { final String asString = mapper.writeValueAsString(model); - if (validate && !validator.validate(schemaUri, asString).isValid()) { - throw new RuntimeException(); + final Validator.Result result = validator.validate(schemaUri, asString); + if (validate && !result.isValid()) { + throw new RuntimeException(result.getErrors().get(0).getError()); } return asString.getBytes(StandardCharsets.UTF_8); } catch (JsonProcessingException e) { @@ -99,8 +94,9 @@ public byte[] serialize(final TestModel model, final boolean validate) { public TestModel deserialize(final byte[] data) { try { final String json = new String(data, StandardCharsets.UTF_8); - if (!validator.validate(schemaUri, json).isValid()) { - throw new RuntimeException(); + final Validator.Result result = validator.validate(schemaUri, json); + if (!result.isValid()) { + throw new RuntimeException(result.getErrors().get(0).getError()); } return mapper.readValue(data, TestModel.class); } catch (IOException e) { @@ -110,12 +106,29 @@ public TestModel deserialize(final byte[] data) { }; } - private Validator validator(final SchemaSpec spec) { + private Validator validator(final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { + final JacksonNode.Factory nodeFactory = new JacksonNode.Factory(); + final Map remotes = + additionalSchemas.remotes().entrySet().stream() + .collect( + Collectors.toMap( + e -> e.getKey().toString(), + e -> nodeFactory.create(e.getValue()))); + final SchemaResolver resolver = + uri -> { + final JsonNode schema = remotes.get(uri); + if (schema != null) { + return SchemaResolver.Result.fromJsonNode(schema); + } + return SchemaResolver.Result.empty(); + }; switch (spec) { case DRAFT_2020_12: final Validator validator2020 = new dev.harrel.jsonschema.ValidatorFactory() .withDialect(new Dialects.Draft2020Dialect()) + .withJsonNodeFactory(nodeFactory) + .withSchemaResolver(resolver) .createValidator(); /* Validate against meta-schema in order to parse it eagerly */ validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); @@ -124,6 +137,8 @@ private Validator validator(final SchemaSpec spec) { final Validator validator2019 = new dev.harrel.jsonschema.ValidatorFactory() .withDialect(new Dialects.Draft2019Dialect()) + .withJsonNodeFactory(nodeFactory) + .withSchemaResolver(resolver) .createValidator(); /* Validate against meta-schema in order to parse it eagerly */ validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}");