Skip to content

Commit

Permalink
add test, fix checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
harrel56 committed Nov 12, 2023
1 parent b31eebf commit cf5ee33
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,25 @@

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;
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.*;
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 {
Expand All @@ -54,29 +54,33 @@ public class DevHarrelImplementation implements Implementation {
private Map<URI, String> 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
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ public JsonSchemaTestSuite load(final Path rootDir) {
private static Map<URI, String> loadRemotes(final Path remotes) {

final Function<Path, URI> 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<Path, String> readContent =
path -> {
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {}

0 comments on commit cf5ee33

Please sign in to comment.