|
| 1 | +/* |
| 2 | + * Copyright 2023 Creek Contributors (https://github.com/creek-service) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.creekservice.kafka.test.perf.implementations; |
| 18 | + |
| 19 | +import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_2019_09; |
| 20 | +import static org.creekservice.kafka.test.perf.testsuite.SchemaSpec.DRAFT_2020_12; |
| 21 | + |
| 22 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 23 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
| 25 | +import dev.harrel.jsonschema.Dialects; |
| 26 | +import dev.harrel.jsonschema.JsonNode; |
| 27 | +import dev.harrel.jsonschema.SchemaResolver; |
| 28 | +import dev.harrel.jsonschema.SpecificationVersion; |
| 29 | +import dev.harrel.jsonschema.Validator; |
| 30 | +import dev.harrel.jsonschema.providers.JacksonNode; |
| 31 | +import java.awt.Color; |
| 32 | +import java.io.IOException; |
| 33 | +import java.net.URI; |
| 34 | +import java.nio.charset.StandardCharsets; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Set; |
| 37 | +import java.util.stream.Collectors; |
| 38 | +import org.creekservice.kafka.test.perf.model.TestModel; |
| 39 | +import org.creekservice.kafka.test.perf.testsuite.AdditionalSchemas; |
| 40 | +import org.creekservice.kafka.test.perf.testsuite.SchemaSpec; |
| 41 | + |
| 42 | +@SuppressWarnings("FieldMayBeFinal") // not final to avoid folding. |
| 43 | +public class DevHarrelImplementation implements Implementation { |
| 44 | + |
| 45 | + private static final MetaData METADATA = |
| 46 | + new MetaData( |
| 47 | + "json-schema (dev.harrel)", |
| 48 | + "dev.harrel", |
| 49 | + Language.Java, |
| 50 | + Licence.MIT, |
| 51 | + Set.of(DRAFT_2020_12, DRAFT_2019_09), |
| 52 | + "https://github.com/harrel56/json-schema", |
| 53 | + new Color(22, 99, 0)); |
| 54 | + |
| 55 | + private ObjectMapper mapper = JsonMapper.builder().build(); |
| 56 | + |
| 57 | + @Override |
| 58 | + public MetaData metadata() { |
| 59 | + return METADATA; |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public JsonValidator prepare( |
| 64 | + final String schema, final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { |
| 65 | + |
| 66 | + final Validator validator = validator(spec, additionalSchemas); |
| 67 | + final URI schemaUri = validator.registerSchema(schema); |
| 68 | + |
| 69 | + return new JsonValidator() { |
| 70 | + |
| 71 | + @Override |
| 72 | + public void validate(final String json) { |
| 73 | + final Validator.Result result = validator.validate(schemaUri, json); |
| 74 | + if (!result.isValid()) { |
| 75 | + throw new RuntimeException(result.getErrors().get(0).getError()); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public byte[] serialize(final TestModel model, final boolean validate) { |
| 81 | + try { |
| 82 | + final String asString = mapper.writeValueAsString(model); |
| 83 | + final Validator.Result result = validator.validate(schemaUri, asString); |
| 84 | + if (validate && !result.isValid()) { |
| 85 | + throw new RuntimeException(result.getErrors().get(0).getError()); |
| 86 | + } |
| 87 | + return asString.getBytes(StandardCharsets.UTF_8); |
| 88 | + } catch (JsonProcessingException e) { |
| 89 | + throw new RuntimeException(e); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public TestModel deserialize(final byte[] data) { |
| 95 | + try { |
| 96 | + final String json = new String(data, StandardCharsets.UTF_8); |
| 97 | + final Validator.Result result = validator.validate(schemaUri, json); |
| 98 | + if (!result.isValid()) { |
| 99 | + throw new RuntimeException(result.getErrors().get(0).getError()); |
| 100 | + } |
| 101 | + return mapper.readValue(data, TestModel.class); |
| 102 | + } catch (IOException e) { |
| 103 | + throw new RuntimeException(e); |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + private Validator validator(final SchemaSpec spec, final AdditionalSchemas additionalSchemas) { |
| 110 | + final JacksonNode.Factory nodeFactory = new JacksonNode.Factory(); |
| 111 | + final Map<String, JsonNode> remotes = |
| 112 | + additionalSchemas.remotes().entrySet().stream() |
| 113 | + .collect( |
| 114 | + Collectors.toMap( |
| 115 | + e -> e.getKey().toString(), |
| 116 | + e -> nodeFactory.create(e.getValue()))); |
| 117 | + final SchemaResolver resolver = |
| 118 | + uri -> { |
| 119 | + final JsonNode schema = remotes.get(uri); |
| 120 | + if (schema != null) { |
| 121 | + return SchemaResolver.Result.fromJsonNode(schema); |
| 122 | + } |
| 123 | + return SchemaResolver.Result.empty(); |
| 124 | + }; |
| 125 | + switch (spec) { |
| 126 | + case DRAFT_2020_12: |
| 127 | + final Validator validator2020 = |
| 128 | + new dev.harrel.jsonschema.ValidatorFactory() |
| 129 | + .withDialect(new Dialects.Draft2020Dialect()) |
| 130 | + .withJsonNodeFactory(nodeFactory) |
| 131 | + .withSchemaResolver(resolver) |
| 132 | + .createValidator(); |
| 133 | + /* Validate against meta-schema in order to parse it eagerly */ |
| 134 | + validator2020.validate(URI.create(SpecificationVersion.DRAFT2020_12.getId()), "{}"); |
| 135 | + return validator2020; |
| 136 | + case DRAFT_2019_09: |
| 137 | + final Validator validator2019 = |
| 138 | + new dev.harrel.jsonschema.ValidatorFactory() |
| 139 | + .withDialect(new Dialects.Draft2019Dialect()) |
| 140 | + .withJsonNodeFactory(nodeFactory) |
| 141 | + .withSchemaResolver(resolver) |
| 142 | + .createValidator(); |
| 143 | + /* Validate against meta-schema in order to parse it eagerly */ |
| 144 | + validator2019.validate(URI.create(SpecificationVersion.DRAFT2019_09.getId()), "{}"); |
| 145 | + return validator2019; |
| 146 | + default: |
| 147 | + throw new RuntimeException("Unsupported Spec:" + spec); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Final, empty finalize method stops spotbugs CT_CONSTRUCTOR_THROW |
| 152 | + // Can be moved to base type after https://github.com/spotbugs/spotbugs/issues/2665 |
| 153 | + @Override |
| 154 | + @SuppressWarnings({"deprecation", "Finalize"}) |
| 155 | + protected final void finalize() {} |
| 156 | +} |
0 commit comments