From 78975835bb2a2d02e74f5b4eaec4401428b7735c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20E=2E=20Garcia=20Maci=C3=B1eiras?= <68995937+jemacineiras@users.noreply.github.com> Date: Thu, 2 Nov 2023 11:55:28 +0100 Subject: [PATCH] 332 comsngularkloadgenserializerprotobufserializer nullpointerexception (#409) --- .github/workflows/maven-central-push.yml | 4 +- .github/workflows/maven-pr.yml | 4 +- pom-maven-central.xml | 162 ++++--- pom.xml | 87 ++-- .../SchemaRegistryConfigElement.java | 10 +- .../SchemaRegistryConfigElementBeanInfo.java | 13 +- .../SchemaRegistryConfigElementValue.java | 11 +- .../kloadgen/extractor/SchemaExtractor.java | 20 +- .../extractors/ExtractorFactory.java | 49 +-- .../avro/AbstractAvroFileExtractor.java | 16 +- .../extractors/avro/AvroExtractor.java | 11 +- .../json/AbstractJsonExtractor.java | 399 ++++++++++-------- .../extractors/json/JsonDefaultExtractor.java | 36 +- .../extractors/json/JsonExtractor.java | 15 +- .../protobuff/AbstractProtoFileExtractor.java | 28 +- .../protobuff/ProtoBufConfluentExtractor.java | 2 +- .../protobuff/ProtobuffExtractor.java | 21 +- .../loadgen/impl/ProtobufLoadGenerator.java | 4 +- .../kloadgen/processor/SchemaProcessor.java | 2 +- .../ObjectCreatorFactoryHelper.java | 19 +- .../impl/AvroObjectCreatorFactory.java | 30 +- .../impl/JsonObjectCreatorFactory.java | 2 +- .../impl/ProtobufObjectCreatorFactory.java | 6 +- .../processor/util/SchemaProcessorUtils.java | 61 +-- .../editor/FileSubjectPropertyEditor.java | 15 +- .../editor/KeyDeserializerPropertyEditor.java | 4 +- .../editor/KeySerializerPropertyEditor.java | 1 + .../editor/NameStrategyPropertyEditor.java | 7 +- .../PlainKeySerializerPropertyEditor.java | 1 + .../PlainValueSerializerPropertyEditor.java | 1 + .../property/editor/ReflectionUtils.java | 1 + .../editor/SchemaConverterPropertyEditor.java | 1 + .../SchemaRegistryConfigPropertyEditor.java | 17 +- .../SchemaRegistryNamePropertyEditor.java | 1 + .../editor/SchemaTypePropertyEditor.java | 1 + .../property/editor/SerDesPackageValue.java | 13 +- .../SerialisedSubjectPropertyEditor.java | 11 +- .../ValueDeserializerPropertyEditor.java | 1 + .../editor/ValueSerializerPropertyEditor.java | 1 + .../generator/ProtoBufGeneratorTool.java | 34 +- .../randomtool/random/RandomObject.java | 2 +- .../sngular/kloadgen/sampler/SamplerUtil.java | 43 +- .../SchemaRegistryConstants.java | 13 - .../SchemaRegistryManagerFactory.java | 15 +- .../impl/AbstractParsedSchemaAdapter.java | 8 + ...ApicurioAbstractParsedSchemaMetadata.java} | 9 +- .../adapter/impl/ApicurioSchemaMetadata.java | 6 +- .../adapter/impl/BaseParsedSchema.java | 4 +- .../adapter/impl/BaseSchemaMetadata.java | 7 +- ...onfluentAbstractParsedSchemaMetadata.java} | 25 +- .../adapter/impl/ConfluentSchemaMetadata.java | 18 +- .../adapter/impl/GenericSchemaRegistry.java | 3 +- .../impl/GenericSchemaRegistryAdapter.java | 9 +- .../adapter/impl/ParsedSchemaAdapter.java | 7 - .../impl/ApicurioSchemaRegistry.java | 30 +- .../impl/ConfluentSchemaRegistry.java | 36 +- .../schema/KLoadSchemaMetadata.java | 14 - .../kloadgen/serializer/AvroDeserializer.java | 2 +- .../kloadgen/serializer/AvroSerializer.java | 1 + .../serializer/ProtobufSerializer.java | 6 +- .../sngular/kloadgen/util/AutoCompletion.java | 3 +- .../sngular/kloadgen/util/JMeterHelper.java | 2 + ...KafkaHeadersConfigElementBeanInfoTest.java | 2 +- ...leSerializedConfigElementBeanInfoTest.java | 2 +- ...eySerializedConfigElementBeanInfoTest.java | 2 +- ...leSerializedConfigElementBeanInfoTest.java | 14 +- ...ueSerializedConfigElementBeanInfoTest.java | 12 +- .../extractor/SchemaExtractorTest.java | 49 ++- .../avro/AvroApicurioExtractorTest.java | 6 - .../extractors/json/JsonDefaultTest.java | 31 +- .../ProtobufApicurioExtractorTest.java | 2 +- .../ProtobufConfluentExtractorTest.java | 3 +- .../processor/AvroSchemaProcessorTest.java | 4 +- .../ProtobufSchemaProcessorTest.java | 49 ++- ...chemaRegistryConfigPropertyEditorTest.java | 3 +- .../randomtool/random/RandomArrayTest.java | 6 +- .../kloadgen/sampler/SamplerUtilTest.java | 209 +++++---- .../serializer/AvroDeserializerTest.java | 67 ++- .../AvroSerializersTestFixture.java | 151 +++---- .../GenericRecordAvroSerializersTest.java | 82 ++-- .../serializer/JsonSerializerTest.java | 67 ++- .../serializer/JsonSerializerTestFixture.java | 304 ++++++------- .../serializer/ProtobufSerializerTest.java | 78 ++-- .../ProtobuffSerializerTestFixture.java | 239 +++++------ .../serializer/SerializerTestFixture.java | 7 +- .../kloadgen/testutil/SchemaParseUtil.java | 37 +- .../resources/proto-files/complexTest.proto | 1 - 87 files changed, 1407 insertions(+), 1405 deletions(-) delete mode 100644 src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryConstants.java create mode 100644 src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/AbstractParsedSchemaAdapter.java rename src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/{ApicurioParsedSchemaMetadata.java => ApicurioAbstractParsedSchemaMetadata.java} (58%) rename src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/{ConfluentParsedSchemaMetadata.java => ConfluentAbstractParsedSchemaMetadata.java} (53%) delete mode 100644 src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ParsedSchemaAdapter.java delete mode 100644 src/main/java/com/sngular/kloadgen/schemaregistry/schema/KLoadSchemaMetadata.java diff --git a/.github/workflows/maven-central-push.yml b/.github/workflows/maven-central-push.yml index b2665d60..3564e316 100644 --- a/.github/workflows/maven-central-push.yml +++ b/.github/workflows/maven-central-push.yml @@ -25,10 +25,10 @@ jobs: with: ref: ${{ github.event.pull_request.head.ref }} - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v2 with: - java-version: "11" + java-version: "17" distribution: "adopt" server-id: ossrh server-username: MAVEN_USERNAME diff --git a/.github/workflows/maven-pr.yml b/.github/workflows/maven-pr.yml index 2b2873ea..0b3ba30a 100644 --- a/.github/workflows/maven-pr.yml +++ b/.github/workflows/maven-pr.yml @@ -36,10 +36,10 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - - name: Set up JDK 11 + - name: Set up JDK 17 uses: actions/setup-java@v2 with: - java-version: "11" + java-version: "17" distribution: "adopt" - name: Build with Maven run: mvn -B package --file pom.xml diff --git a/pom-maven-central.xml b/pom-maven-central.xml index 2137a7e0..e0380d1a 100644 --- a/pom-maven-central.xml +++ b/pom-maven-central.xml @@ -7,7 +7,7 @@ kloadgen - 5.6.7 + 5.6.8 KLoadGen Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial @@ -20,22 +20,11 @@ Mozilla Public License 2.0 https://github.com/sngular/kloadgen/blob/master/LICENSE repo - + - - AnxoGomezSngular - Anxo Gómez Sánchez - anxo.gomez@sngular.com - Sngular - https://sngular.github.io/ - - Developer - - Europe/Madrid - jegarcia Jose Enrique Garcia Maciñeiras @@ -195,13 +184,24 @@ davidgarciago David García Gondell david.garciag@sngular.com - Corunet - https://corunet.github.io/ + Sngular + https://sngular.github.io/ Junior Developer Europe/Madrid + + AnxoGomezSngular + Anxo Gómez Sánchez + anxo.gomez@sngular.com + Sngular + https://sngular.github.io/ + + Developer + + Europe/Madrid + pedrogarcia Pedro Garcia @@ -235,6 +235,17 @@ Europe/Madrid + + rabad-sng + Raúl Abad + raul.abad@sngular.com + Sngular + https://www.sngular.com + + Senior Backend Developer + + Europe/Madrid + dhergon David Hernández González @@ -310,24 +321,24 @@ - 3.22.0 + 3.24.2 1.2.0 1.5.1 - 1.11.0 + 1.11.2 1.9.4 4.4 3.12.0 2.8.6 - 31.0.1-jre + 32.0.0-jre 2.4.0-b180830.0359 - 11 + 17 provided - 5.5 + 5.6.2 2.6.0 5.8.2 7.1.1 - 2.4.1.Final - 3.1.0 + 2.4.3.Final + 3.5.1 1.18.22 0.9.5 4.2.0 @@ -335,21 +346,10 @@ 4.5.0 2.0.0-alpha1 1.3.1 - 2.32.0 + 2.35.1 - - org.apache.jmeter - jorphan - ${jmeter.version} - - - org.slf4j - slf4j-api - - - org.apache.kafka kafka-clients @@ -365,11 +365,6 @@ - - com.google.guava - guava - ${guava.version} - com.eclipsesource.minimal-json minimal-json @@ -389,7 +384,12 @@ com.fasterxml.jackson.core jackson-core - 2.13.1 + 2.15.2 + + + com.fasterxml.jackson.core + jackson-annotations + 2.15.2 org.projectlombok @@ -437,6 +437,12 @@ io.apicurio apicurio-registry-client ${apicurio-registry.version} + + + org.jboss.slf4j + slf4j-jboss-logmanager + + io.apicurio @@ -461,7 +467,7 @@ org.apache.avro avro-protobuf - 1.11.0 + 1.11.2 com.github.os72 @@ -474,10 +480,16 @@ 4.0.1 runtime + + com.google.api.grpc + proto-google-common-protos + 2.24.0 + org.slf4j slf4j-api ${slf4j-api.version} + provided org.apache.commons @@ -502,7 +514,7 @@ com.github.everit-org.json-schema org.everit.json.schema - 1.14.0 + 1.14.2 @@ -636,7 +648,42 @@ + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 3.1.2 + + + com.puppycrawl.tools + checkstyle + 10.2 + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + checkstyle.xml + true + true + false + + + + validate + validate + + check + + + + com.github.ekryd.sortpom sortpom-maven-plugin @@ -653,7 +700,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.6.0 + 3.8.1 true true @@ -698,7 +745,7 @@ cobertura-maven-plugin 2.7 - + xml @@ -718,7 +765,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + 3.1.2 org.sonatype.plugins @@ -769,10 +816,6 @@ ${jmeter.version} provided - - org.apache.jmeter - jorphan - org.apache.logging.log4j log4j-slf4j-impl @@ -804,13 +847,16 @@ org.apache.maven.plugins maven-shade-plugin - 2.4.2 + 3.2.4 org/apache/jmeter/jmeter.properties + + *.proto + ${project.artifactId}-${project.version} @@ -839,10 +885,6 @@ ApacheJMeter_java ${jmeter.version} - - org.apache.jmeter - jorphan - org.apache.logging.log4j log4j-slf4j-impl @@ -945,4 +987,16 @@ + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + checkstyle.xml + + + + + diff --git a/pom.xml b/pom.xml index b9fad739..deb1aaac 100644 --- a/pom.xml +++ b/pom.xml @@ -7,11 +7,11 @@ kloadgen - 5.6.7 + 5.6.8 KLoadGen Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial - data base on Data specification + data based on Data specification https://sngular.github.io/kloadgen/ @@ -191,6 +191,17 @@ Europe/Madrid + + AnxoGomezSngular + Anxo Gómez Sánchez + anxo.gomez@sngular.com + Sngular + https://sngular.github.io/ + + Developer + + Europe/Madrid + pedrogarcia Pedro Garcia @@ -213,6 +224,17 @@ Europe/Madrid + + nuriaricorf + Nuria Rico Freire + nuria.rico@sngular.com + Sngular + https://sngular.github.io/ + + Junior Developer + + Europe/Madrid + rabad-sng Raúl Abad @@ -299,24 +321,24 @@ - 3.22.0 + 3.24.2 1.2.0 1.5.1 - 1.11.0 + 1.11.2 1.9.4 4.4 3.12.0 2.8.6 32.0.0-jre 2.4.0-b180830.0359 - 11 + 17 provided - 5.5 + 5.6.2 2.6.0 5.8.2 7.1.1 - 2.4.1.Final - 3.1.0 + 2.4.3.Final + 3.5.1 1.18.22 0.9.5 4.2.0 @@ -328,17 +350,6 @@ - - org.apache.jmeter - jorphan - ${jmeter.version} - - - org.slf4j - slf4j-api - - - org.apache.kafka kafka-clients @@ -354,11 +365,6 @@ - - com.google.guava - guava - ${guava.version} - com.eclipsesource.minimal-json minimal-json @@ -378,7 +384,12 @@ com.fasterxml.jackson.core jackson-core - 2.13.1 + 2.15.2 + + + com.fasterxml.jackson.core + jackson-annotations + 2.15.2 org.projectlombok @@ -456,7 +467,7 @@ org.apache.avro avro-protobuf - 1.11.0 + 1.11.2 com.github.os72 @@ -469,6 +480,11 @@ 4.0.1 runtime + + com.google.api.grpc + proto-google-common-protos + 2.24.0 + org.slf4j slf4j-api @@ -498,7 +514,7 @@ com.github.everit-org.json-schema org.everit.json.schema - 1.14.0 + 1.14.2 @@ -685,7 +701,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.6.0 + 3.8.1 true true @@ -724,7 +740,7 @@ org.apache.maven.plugins maven-surefire-plugin - 2.22.2 + 3.1.2 @@ -744,10 +760,6 @@ ${jmeter.version} provided - - org.apache.jmeter - jorphan - org.apache.logging.log4j log4j-slf4j-impl @@ -779,13 +791,16 @@ org.apache.maven.plugins maven-shade-plugin - 2.4.2 + 3.2.4 org/apache/jmeter/jmeter.properties + + *.proto + ${project.artifactId}-${project.version} @@ -814,10 +829,6 @@ ApacheJMeter_java ${jmeter.version} - - org.apache.jmeter - jorphan - org.apache.logging.log4j log4j-slf4j-impl diff --git a/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElement.java b/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElement.java index cdcc7490..f586de45 100644 --- a/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElement.java +++ b/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElement.java @@ -6,10 +6,6 @@ package com.sngular.kloadgen.config.schemaregistry; -import static com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_NAME; -import static com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_PROPERTIES; -import static com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_URL; - import java.util.HashMap; import java.util.List; import java.util.Map; @@ -83,7 +79,7 @@ private void serializeProperties() { private Map getProperties() { final Map result = new HashMap<>(); - final JMeterProperty property = getProperty(SCHEMA_REGISTRY_PROPERTIES); + final JMeterProperty property = getProperty(SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_PROPERTIES); if (Objects.nonNull(property)) { result.putAll( this.fromTestElementToPropertiesMap((List) property.getObjectValue())); @@ -94,7 +90,7 @@ private Map getProperties() { } private String getRegistryName() { - String registryName = getPropertyAsString(SCHEMA_REGISTRY_NAME); + String registryName = getPropertyAsString(SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_NAME); if (StringUtils.isBlank(registryName)) { registryName = this.schemaRegistryName; } @@ -102,7 +98,7 @@ private String getRegistryName() { } private String getRegistryUrl() { - String registryUrl = getPropertyAsString(SCHEMA_REGISTRY_URL); + String registryUrl = getPropertyAsString(SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_URL); if (StringUtils.isBlank(registryUrl)) { registryUrl = this.schemaRegistryUrl; } diff --git a/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementBeanInfo.java b/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementBeanInfo.java index cc90a49c..772fe2c6 100644 --- a/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementBeanInfo.java +++ b/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementBeanInfo.java @@ -6,10 +6,6 @@ package com.sngular.kloadgen.config.schemaregistry; -import static com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_NAME; -import static com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_PROPERTIES; -import static com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_URL; - import java.beans.PropertyDescriptor; import com.sngular.kloadgen.model.PropertyMapping; @@ -26,22 +22,23 @@ public SchemaRegistryConfigElementBeanInfo() { super(SchemaRegistryConfigElement.class); - createPropertyGroup("schema_registry_config", new String[]{SCHEMA_REGISTRY_NAME, SCHEMA_REGISTRY_URL, SCHEMA_REGISTRY_PROPERTIES}); + createPropertyGroup("schema_registry_config", new String[]{SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_NAME, SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_PROPERTIES, + SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_URL}); - final PropertyDescriptor schemaRegistryName = property(SCHEMA_REGISTRY_NAME); + final PropertyDescriptor schemaRegistryName = property(SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_NAME); schemaRegistryName.setPropertyEditorClass(SchemaRegistryNamePropertyEditor.class); schemaRegistryName.setValue(NOT_UNDEFINED, Boolean.TRUE); schemaRegistryName.setValue(DEFAULT, SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME_DEFAULT); schemaRegistryName.setValue(NOT_EXPRESSION, Boolean.FALSE); - final PropertyDescriptor schemaRegistryUrl = property(SCHEMA_REGISTRY_URL); + final PropertyDescriptor schemaRegistryUrl = property(SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_URL); schemaRegistryUrl.setPropertyEditorClass(SchemaRegistryConfigPropertyEditor.class); schemaRegistryUrl.setValue(NOT_UNDEFINED, Boolean.TRUE); schemaRegistryUrl.setValue(DEFAULT, SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL_DEFAULT); schemaRegistryUrl.setValue(NOT_EXPRESSION, Boolean.FALSE); final TypeEditor tableEditor = TypeEditor.TableEditor; - final PropertyDescriptor tableProperties = property(SCHEMA_REGISTRY_PROPERTIES, tableEditor); + final PropertyDescriptor tableProperties = property(SchemaRegistryConfigElementValue.SCHEMA_REGISTRY_PROPERTIES, tableEditor); tableProperties.setValue(TableEditor.CLASSNAME, PropertyMapping.class.getName()); tableProperties.setValue(TableEditor.HEADERS, new String[]{"Property Name", "Property Value"}); tableProperties.setValue(TableEditor.OBJECT_PROPERTIES, new String[]{PropertyMapping.PROPERTY_NAME, PropertyMapping.PROPERTY_VALUE}); diff --git a/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementValue.java b/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementValue.java index 153418d4..8aeae711 100644 --- a/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementValue.java +++ b/src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElementValue.java @@ -1,8 +1,11 @@ package com.sngular.kloadgen.config.schemaregistry; -public interface SchemaRegistryConfigElementValue { +public final class SchemaRegistryConfigElementValue { - String SCHEMA_REGISTRY_NAME = "schemaRegistryName"; - String SCHEMA_REGISTRY_URL = "schemaRegistryUrl"; - String SCHEMA_REGISTRY_PROPERTIES = "schemaRegistryProperties"; + public static final String SCHEMA_REGISTRY_NAME = "schemaRegistryName"; + public static final String SCHEMA_REGISTRY_URL = "schemaRegistryUrl"; + public static final String SCHEMA_REGISTRY_PROPERTIES = "schemaRegistryProperties"; + + private SchemaRegistryConfigElementValue() { + } } diff --git a/src/main/java/com/sngular/kloadgen/extractor/SchemaExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/SchemaExtractor.java index 67c7977b..a7a7b760 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/SchemaExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/SchemaExtractor.java @@ -12,20 +12,18 @@ import com.sngular.kloadgen.extractor.extractors.ExtractorFactory; import com.sngular.kloadgen.model.FieldValueMapping; import io.confluent.kafka.schemaregistry.ParsedSchema; +import lombok.Getter; import org.apache.commons.lang3.tuple.Pair; -public class SchemaExtractor { +@Getter +public final class SchemaExtractor { private SchemaRegistryEnum type; public SchemaExtractor() { } - public SchemaRegistryEnum getType(){ - return type; - } - - public void setType(SchemaRegistryEnum type){ + public void setType(final SchemaRegistryEnum type) { this.type = type; } @@ -34,11 +32,11 @@ public static Pair> flatPropertiesList(final Str } public static List flatPropertiesList(final ParsedSchema parserSchema) { - return ExtractorFactory.getExtractor(parserSchema.schemaType(), "CONFLUENT").processSchema(parserSchema, SchemaRegistryEnum.CONFLUENT); + return ExtractorFactory.getExtractor(parserSchema.schemaType()).processSchema(parserSchema, SchemaRegistryEnum.CONFLUENT); } - public static List schemaTypesList(final File schemaFile, final String schemaType, String registry) throws IOException { - return ExtractorFactory.getExtractor(schemaType, registry).getSchemaNameList(readLineByLine(schemaFile.getPath()), + public static List schemaTypesList(final File schemaFile, final String schemaType, final String registry) throws IOException { + return ExtractorFactory.getExtractor(schemaType).getSchemaNameList(readLineByLine(schemaFile.getPath()), ExtractorFactory.getSchemaRegistry(registry)); } @@ -52,8 +50,4 @@ private static String readLineByLine(final String filePath) throws IOException { return contentBuilder.toString(); } - private static List processSchema(final ParsedSchema schema) { - return ExtractorFactory.getExtractor(schema.schemaType(),SchemaRegistryEnum.CONFLUENT.name()).processSchema(schema.rawSchema().toString(), SchemaRegistryEnum.CONFLUENT); - } - } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/ExtractorFactory.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/ExtractorFactory.java index 3c1e143b..371d2de4 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/ExtractorFactory.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/ExtractorFactory.java @@ -12,8 +12,8 @@ import com.sngular.kloadgen.extractor.extractors.json.JsonExtractor; import com.sngular.kloadgen.extractor.extractors.protobuff.ProtobuffExtractor; import com.sngular.kloadgen.model.FieldValueMapping; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioParsedSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter; +import com.sngular.kloadgen.schemaregistry.adapter.impl.AbstractParsedSchemaAdapter; +import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioAbstractParsedSchemaMetadata; import com.sngular.kloadgen.util.JMeterHelper; import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import org.apache.commons.lang3.EnumUtils; @@ -21,30 +21,28 @@ import org.apache.jmeter.threads.JMeterContextService; public final class ExtractorFactory { - private static AvroExtractor avroExtractor = new AvroExtractor(); + private static final AvroExtractor AVRO_EXTRACTOR = new AvroExtractor(); - private static JsonExtractor jsonExtractor = new JsonExtractor(); + private static final JsonExtractor JSON_EXTRACTOR = new JsonExtractor(); - private static ProtobuffExtractor protobuffExtractor = new ProtobuffExtractor(); + private static final ProtobuffExtractor PROTOBUFF_EXTRACTOR = new ProtobuffExtractor(); private ExtractorFactory() { } - public static ExtractorRegistry getExtractor(final String schemaType, final String schemaRegistryEnum) { - - SchemaRegistryEnum registryEnum = getSchemaRegistry(schemaRegistryEnum); + public static ExtractorRegistry getExtractor(final String schemaType) { if (schemaType != null && EnumUtils.isValidEnum(SchemaTypeEnum.class, schemaType.toUpperCase())) { final ExtractorRegistry response; switch (SchemaTypeEnum.valueOf(schemaType.toUpperCase())) { case JSON: - response = jsonExtractor; + response = JSON_EXTRACTOR; break; case AVRO: - response = avroExtractor; + response = AVRO_EXTRACTOR; break; case PROTOBUF: - response = protobuffExtractor; + response = PROTOBUFF_EXTRACTOR; break; default: throw new KLoadGenException(String.format("Schema type not supported %s", schemaType)); @@ -55,7 +53,7 @@ public static ExtractorRegistry getExtractor(final String schemaType, final Stri } } - public static SchemaRegistryEnum getSchemaRegistry(String schemaRegistryEnum) { + public static SchemaRegistryEnum getSchemaRegistry(final String schemaRegistryEnum) { if (schemaRegistryEnum != null && EnumUtils.isValidEnum(SchemaRegistryEnum.class, schemaRegistryEnum.toUpperCase())) { return SchemaRegistryEnum.valueOf(schemaRegistryEnum.toUpperCase()); } else { @@ -67,27 +65,20 @@ public static Pair> flatPropertiesList(final Str final Properties properties = JMeterContextService.getContext().getProperties(); final var schemaParsed = JMeterHelper.getParsedSchema(subjectName, properties); final String registryName = properties.getProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME); - String schemaType = null; - final ParsedSchemaAdapter parsedSchemaAdapter = schemaParsed.getParsedSchemaAdapter(); - schemaType = parsedSchemaAdapter.getType(); + final AbstractParsedSchemaAdapter abstractParsedSchemaAdapter = schemaParsed.getParsedSchemaAdapter(); + final String schemaType = abstractParsedSchemaAdapter.getType(); - List attributeList = new ArrayList<>(); - SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registryName.toUpperCase()); + final List attributeList = new ArrayList<>(); + final SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registryName.toUpperCase()); - Object schema = null; + final Object schema; if (Objects.nonNull(registryName)) { //TODO change parser - switch (schemaRegistryEnum) { - case APICURIO: - schema = ((ApicurioParsedSchemaMetadata) parsedSchemaAdapter).getSchema(); - break; - case CONFLUENT: - schema = parsedSchemaAdapter.getRawSchema(); - break; - default: - throw new KLoadGenException("Schema Registry Type nos supported " + registryName.toUpperCase()); - } - attributeList = getExtractor(schemaType,registryName.toUpperCase()).processSchema(schema, schemaRegistryEnum); + schema = switch (schemaRegistryEnum) { + case APICURIO -> ((ApicurioAbstractParsedSchemaMetadata) abstractParsedSchemaAdapter).getSchema(); + case CONFLUENT -> abstractParsedSchemaAdapter.getRawSchema(); + }; + attributeList.addAll(getExtractor(schemaType).processSchema(schema, schemaRegistryEnum)); } return Pair.of(schemaType, attributeList); } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AbstractAvroFileExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AbstractAvroFileExtractor.java index 882c068e..b79d2d6e 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AbstractAvroFileExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AbstractAvroFileExtractor.java @@ -23,7 +23,7 @@ public abstract class AbstractAvroFileExtractor { protected AbstractAvroFileExtractor() { } - public List processSchemaDefault(final Schema schemaReceived) { + public final List processSchemaDefault(final Schema schemaReceived) { final var attributeList = new ArrayList(); Schema aux = null; if (checkIfUnion(schemaReceived)) { @@ -31,19 +31,17 @@ public List processSchemaDefault(final Schema schemaReceived) } else if (checkIfRecord(schemaReceived)) { aux = schemaReceived; } - if(aux != null && (checkIfUnion(schemaReceived) || checkIfRecord(schemaReceived))){ + if (aux != null && (checkIfUnion(schemaReceived) || checkIfRecord(schemaReceived))) { aux.getFields().forEach(field -> processField(field, attributeList, true, true)); } return attributeList; } - public List getSchemaNameList(Schema schema) { - List result = new ArrayList<>(); - result.addAll(extractSchemaNames(schema)); - return result; + public final List getSchemaNameList(final Schema schema) { + return new ArrayList<>(extractSchemaNames(schema)); } - public void processField( + public final void processField( final Schema.Field innerField, final List completeFieldList, final boolean isAncestorRequired, final boolean isAncestor) { if (checkIfRecord(innerField.schema())) { processRecordFieldList(innerField.name(), ".", processFieldList(innerField.schema().getFields(), isAncestorRequired), completeFieldList); @@ -354,8 +352,8 @@ private boolean checkIfRequiredField(final Schema innerSchema) { return result; } - private Set extractSchemaNames(Schema schema) { - Set schemaNames = new HashSet<>(); + private Set extractSchemaNames(final Schema schema) { + final Set schemaNames = new HashSet<>(); if (checkIfRecord(schema)) { schemaNames.add(schema.getName()); } else if (checkIfArray(schema)) { diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroExtractor.java index 677b36db..88e48b8d 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroExtractor.java @@ -1,8 +1,5 @@ package com.sngular.kloadgen.extractor.extractors.avro; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.APICURIO; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.CONFLUENT; - import java.util.List; import java.util.Map; @@ -13,14 +10,14 @@ public class AvroExtractor implements ExtractorRegistry { - static Map schemaRegistryMap = Map.of(CONFLUENT, new AvroConfluentExtractor(), APICURIO, - new AvroApicurioExtractor()); + private static Map schemaRegistryMap = Map.of(SchemaRegistryEnum.CONFLUENT, new AvroConfluentExtractor(), SchemaRegistryEnum.APICURIO, + new AvroApicurioExtractor()); - public final List processSchema(final Object schema, SchemaRegistryEnum registryEnum) { + public final List processSchema(final Object schema, final SchemaRegistryEnum registryEnum) { return schemaRegistryMap.get(registryEnum).processSchema(schema); } - public final List getSchemaNameList(final String schema, SchemaRegistryEnum registryEnum) { + public final List getSchemaNameList(final String schema, final SchemaRegistryEnum registryEnum) { return schemaRegistryMap.get(registryEnum).getSchemaNameList(schema); } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/json/AbstractJsonExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/json/AbstractJsonExtractor.java index eab7bfef..a1aa15f9 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/json/AbstractJsonExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/json/AbstractJsonExtractor.java @@ -19,219 +19,246 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.Transformer; import org.apache.commons.lang3.StringUtils; +import org.jetbrains.annotations.NotNull; -public class AbstractJsonExtractor { +public abstract class AbstractJsonExtractor { - public final JSONSchemaParser jsonSchemaParser = new JSONSchemaParser(); + private final JSONSchemaParser jsonSchemaParser = new JSONSchemaParser(); - private List extractInternalFields(final ObjectField field, final Boolean isAncestorRequired) { - final List completeFieldList = new ArrayList<>(); - for (Field innerField : field.getProperties()) { - completeFieldList.addAll(processField(innerField, false, isAncestorRequired)); - } - return completeFieldList; - } + protected final JSONSchemaParser getSchemaParser() { + return jsonSchemaParser; + } - private Transformer fixName(final String fieldName, final String splitter) { - return fieldValue -> { - fieldValue.setFieldName(extractFieldName(fieldName) + splitter + fieldValue.getFieldName()); - return fieldValue; - }; + private static String extractFieldName(final String fieldName) { + String fieldNameClean = fieldName; + if (fieldName.endsWith("[][]") || fieldName.endsWith("[:][]")) { + fieldNameClean = fieldName.substring(0, fieldName.length() - 2); + } else if (fieldName.endsWith("[][:]") || fieldName.endsWith("[:][:]")) { + fieldNameClean = fieldName.substring(0, fieldName.length() - 3); } + return fieldNameClean; + } - private static String extractFieldName(final String fieldName) { - String fieldNameClean = fieldName; - if (fieldName.endsWith("[][]") || fieldName.endsWith("[:][]")) { - fieldNameClean = fieldName.substring(0, fieldName.length() - 2); - } else if (fieldName.endsWith("[][:]") || fieldName.endsWith("[:][:]")) { - fieldNameClean = fieldName.substring(0, fieldName.length() - 3); - } - return fieldNameClean; + private List extractInternalFields(final ObjectField field, final Boolean isAncestorRequired) { + final List completeFieldList = new ArrayList<>(); + for (Field innerField : field.getProperties()) { + completeFieldList.addAll(processField(innerField, false, isAncestorRequired)); } + return completeFieldList; + } - public List processField(final Field innerField, final Boolean isRootElement, final Boolean isAncestorRequired) { - final List completeFieldList = new ArrayList<>(); - if (innerField instanceof ObjectField) { - processRecordFieldList(innerField.getName(), ".", - extractInternalFields((ObjectField) innerField, isAncestorRequired != null ? isAncestorRequired : ((ObjectField) innerField).isFieldRequired()), - completeFieldList, checkRequiredElement(isRootElement, isAncestorRequired, ((ObjectField) innerField).isFieldRequired())); - } else if (innerField instanceof ArrayField) { - completeFieldList.addAll(extractArrayInternalFields((ArrayField) innerField, isRootElement, - checkRequiredElement(isRootElement, isAncestorRequired, ((ArrayField) innerField).isFieldRequired()), "")); - } else if (innerField instanceof EnumField) { - completeFieldList.add(FieldValueMapping - .builder() - .fieldName(innerField.getName()) - .fieldType(innerField.getType()) - .valueLength(0) - .fieldValueList(String.join(",", ((EnumField) innerField).getEnumValues())) - .build()); - } else if (innerField instanceof MapField) { - completeFieldList.addAll( - extractMapInternalFields((MapField) innerField, isRootElement, - checkRequiredElement(isRootElement, isAncestorRequired, ((MapField) innerField).isFieldRequired()), "")); - } else if (innerField instanceof NumberField) { - final FieldValueMapping.FieldValueMappingBuilder builder = FieldValueMapping - .builder() - .fieldName(innerField.getName()) - .fieldType(innerField.getType()); - - addConstraint(builder, ConstraintTypeEnum.EXCLUDED_MAXIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getExclusiveMaximum())); - addConstraint(builder, ConstraintTypeEnum.EXCLUDED_MINIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getExclusiveMinimum())); - addConstraint(builder, ConstraintTypeEnum.MAXIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getMaximum())); - addConstraint(builder, ConstraintTypeEnum.MINIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getMinimum())); - addConstraint(builder, ConstraintTypeEnum.MULTIPLE_OF, getSafeNumberAsString(((NumberField) innerField).getMultipleOf())); - - completeFieldList.add(builder.build()); - } else if (innerField instanceof StringField) { - final FieldValueMapping.FieldValueMappingBuilder builder = FieldValueMapping - .builder() - .fieldName(innerField.getName()) - .fieldType(innerField.getType()); - - addConstraint(builder, ConstraintTypeEnum.REGEX, ((StringField) innerField).getRegex()); - addConstraint(builder, ConstraintTypeEnum.MAXIMUM_VALUE, getSafeNumberAsString(((StringField) innerField).getMaxlength())); - addConstraint(builder, ConstraintTypeEnum.MINIMUM_VALUE, getSafeNumberAsString(((StringField) innerField).getMinLength())); - addConstraint(builder, ConstraintTypeEnum.FORMAT, ((StringField) innerField).getFormat()); - - completeFieldList.add(builder.build()); - } else { - completeFieldList.add(FieldValueMapping.builder().fieldName(innerField.getName()).fieldType(innerField.getType()).build()); - } - return completeFieldList; - } + private Transformer fixName(final String fieldName, final String splitter) { + return fieldValue -> { + fieldValue.setFieldName(extractFieldName(fieldName) + splitter + fieldValue.getFieldName()); + return fieldValue; + }; + } + + public final List processField(final Field innerField, final Boolean isRootElement, final Boolean isAncestorRequired) { + final List completeFieldList = new ArrayList<>(); + if (innerField instanceof ObjectField) { + processRecordFieldList(innerField.getName(), ".", + extractInternalFields((ObjectField) innerField, isAncestorRequired != null ? isAncestorRequired : ((ObjectField) innerField).isFieldRequired()), + completeFieldList, checkRequiredElement(isRootElement, isAncestorRequired, ((ObjectField) innerField).isFieldRequired())); + } else if (innerField instanceof ArrayField) { + completeFieldList.addAll(extractArrayInternalFields((ArrayField) innerField, isRootElement, + checkRequiredElement(isRootElement, isAncestorRequired, ((ArrayField) innerField).isFieldRequired()), "")); + } else if (innerField instanceof EnumField) { + completeFieldList.add(FieldValueMapping + .builder() + .fieldName(innerField.getName()) + .fieldType(innerField.getType()) + .valueLength(0) + .fieldValueList(String.join(",", ((EnumField) innerField).getEnumValues())) + .build()); + } else if (innerField instanceof MapField) { + completeFieldList.addAll( + extractMapInternalFields((MapField) innerField, isRootElement, + checkRequiredElement(isRootElement, isAncestorRequired, ((MapField) innerField).isFieldRequired()), "")); + } else if (innerField instanceof NumberField) { + final FieldValueMapping.FieldValueMappingBuilder builder = FieldValueMapping + .builder() + .fieldName(innerField.getName()) + .fieldType(innerField.getType()); + + addConstraint(builder, ConstraintTypeEnum.EXCLUDED_MAXIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getExclusiveMaximum())); + addConstraint(builder, ConstraintTypeEnum.EXCLUDED_MINIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getExclusiveMinimum())); + addConstraint(builder, ConstraintTypeEnum.MAXIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getMaximum())); + addConstraint(builder, ConstraintTypeEnum.MINIMUM_VALUE, getSafeNumberAsString(((NumberField) innerField).getMinimum())); + addConstraint(builder, ConstraintTypeEnum.MULTIPLE_OF, getSafeNumberAsString(((NumberField) innerField).getMultipleOf())); - private void addConstraint(final FieldValueMapping.FieldValueMappingBuilder builder, final ConstraintTypeEnum constraint, final String constrainValue) { - if (StringUtils.isNotBlank(constrainValue)) { - builder.constraint(constraint, constrainValue); - } + completeFieldList.add(builder.build()); + } else if (innerField instanceof StringField) { + final FieldValueMapping.FieldValueMappingBuilder builder = FieldValueMapping + .builder() + .fieldName(innerField.getName()) + .fieldType(innerField.getType()); + + addConstraint(builder, ConstraintTypeEnum.REGEX, ((StringField) innerField).getRegex()); + addConstraint(builder, ConstraintTypeEnum.MAXIMUM_VALUE, getSafeNumberAsString(((StringField) innerField).getMaxlength())); + addConstraint(builder, ConstraintTypeEnum.MINIMUM_VALUE, getSafeNumberAsString(((StringField) innerField).getMinLength())); + addConstraint(builder, ConstraintTypeEnum.FORMAT, ((StringField) innerField).getFormat()); + + completeFieldList.add(builder.build()); + } else { + completeFieldList.add(FieldValueMapping.builder().fieldName(innerField.getName()).fieldType(innerField.getType()).build()); } + return completeFieldList; + } - private String getSafeNumberAsString(final Number exclusiveMaximum) { - String result = null; - if (Objects.nonNull(exclusiveMaximum)) { - result = exclusiveMaximum.toString(); - } - return result; + private void addConstraint(final FieldValueMapping.FieldValueMappingBuilder builder, final ConstraintTypeEnum constraint, final String constrainValue) { + if (StringUtils.isNotBlank(constrainValue)) { + builder.constraint(constraint, constrainValue); } + } - private List extractObjectInternalFields(final ObjectField value, final String innerFieldName, final Boolean isAncestorRequired, final String breadCrumb, - final String endValue) { - final List completeFieldList = new ArrayList<>(); - final List requiredInternalFields = value.getRequired(); - for (Field propertiesField : value.getProperties()) { - final List processedField = processField(propertiesField, false, isAncestorRequired); - processedField.get(0).setAncestorRequired(isAncestorRequired != null && isAncestorRequired); - processedField.get(0).setRequired(checkRequiredByType(propertiesField, requiredInternalFields, processedField.get(0))); - CollectionUtils.collect( - processedField, - fixName(StringUtils.isNotEmpty(breadCrumb) ? breadCrumb + endValue : innerFieldName, endValue + "."), - completeFieldList); - } - - return completeFieldList; + private String getSafeNumberAsString(final Number exclusiveMaximum) { + String result = null; + if (Objects.nonNull(exclusiveMaximum)) { + result = exclusiveMaximum.toString(); } + return result; + } - private List extractArrayInternalFields( - final ArrayField innerField, final Boolean isRootElement, - final Boolean isAncestorRequired, final String breadCrumb) { - - final List completeFieldList = new ArrayList<>(); - - for (Field value : innerField.getValues()) { - if (value instanceof ObjectField) { - completeFieldList.addAll(extractObjectInternalFields((ObjectField) value, innerField.getName(), isAncestorRequired, breadCrumb, "[]")); - } else if (value instanceof ArrayField) { - completeFieldList.addAll(extractArrayInternalFields( - (ArrayField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[]"))); - } else if (value instanceof MapField) { - completeFieldList.addAll(extractMapInternalFields( - (MapField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[]"))); - } else { - final String name = (StringUtils.isNotEmpty(breadCrumb) ? breadCrumb : innerField.getName() + "[]") + (StringUtils.isNotEmpty(breadCrumb) ? "[]" : breadCrumb); - completeFieldList.add( - FieldValueMapping.builder() - .fieldName(name) - .fieldType(value.getType() + SchemaExtractorUtil.ARRAY_TYPE_POSTFIX + ((StringUtils.isNotEmpty(breadCrumb) && breadCrumb.endsWith("[]")) ? - SchemaExtractorUtil.ARRAY_TYPE_POSTFIX : - (StringUtils.isNotEmpty(breadCrumb) && breadCrumb.endsWith("[:]")) ? "-map" : breadCrumb)) - .required(!name.endsWith("][]") && !name.endsWith("][:]") && innerField.isFieldRequired()) - .isAncestorRequired(!isRootElement && isAncestorRequired != null && isAncestorRequired) - .build()); - } - } - return completeFieldList; + private List extractObjectInternalFields( + final ObjectField value, final String innerFieldName, final Boolean isAncestorRequired, final String breadCrumb, + final String endValue) { + final List completeFieldList = new ArrayList<>(); + final List requiredInternalFields = value.getRequired(); + for (Field propertiesField : value.getProperties()) { + final List processedField = processField(propertiesField, false, isAncestorRequired); + processedField.get(0).setAncestorRequired(isAncestorRequired != null && isAncestorRequired); + processedField.get(0).setRequired(checkRequiredByType(propertiesField, requiredInternalFields, processedField.get(0))); + CollectionUtils.collect( + processedField, + fixName(StringUtils.isNotEmpty(breadCrumb) ? breadCrumb + endValue : innerFieldName, endValue + "."), + completeFieldList); } - private List extractMapInternalFields( - final MapField innerField, final Boolean isRootElement, - final Boolean isAncestorRequired, final String breadCrumb) { - final List completeFieldList = new ArrayList<>(); - final Field value = innerField.getMapType(); - - if (value instanceof ObjectField) { - completeFieldList.addAll(extractObjectInternalFields((ObjectField) value, innerField.getName(), isAncestorRequired, breadCrumb, "[:]")); - } else if (value instanceof ArrayField) { - completeFieldList.addAll(extractArrayInternalFields( - (ArrayField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[:]"))); - } else if (value instanceof MapField) { - completeFieldList.addAll(extractMapInternalFields( - (MapField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[:]"))); - } else { - final String name = (StringUtils.isNotEmpty(breadCrumb) ? breadCrumb : innerField.getName() + "[:]") + (StringUtils.isNotEmpty(breadCrumb) ? "[:]" : breadCrumb); - completeFieldList.add( - FieldValueMapping.builder() - .fieldName(name) - .fieldType(value.getType() + "-map" + ((StringUtils.isNotEmpty(breadCrumb) && breadCrumb.endsWith("[:]")) ? "-map" : - (StringUtils.isNotEmpty(breadCrumb) && breadCrumb.endsWith("[]")) ? SchemaExtractorUtil.ARRAY_TYPE_POSTFIX : breadCrumb)) - .required(!name.endsWith("][]") && !name.endsWith("][:]") && innerField.isFieldRequired()) - .isAncestorRequired(!isRootElement && isAncestorRequired != null && isAncestorRequired) - .build()); - } - return completeFieldList; + return completeFieldList; + } + + private List extractArrayInternalFields( + final ArrayField innerField, final Boolean isRootElement, + final Boolean isAncestorRequired, final String breadCrumb) { + + final List completeFieldList = new ArrayList<>(); + + for (Field value : innerField.getValues()) { + if (value instanceof ObjectField) { + completeFieldList.addAll(extractObjectInternalFields((ObjectField) value, innerField.getName(), isAncestorRequired, breadCrumb, "[]")); + } else if (value instanceof ArrayField) { + completeFieldList.addAll(extractArrayInternalFields( + (ArrayField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[]"))); + } else if (value instanceof MapField) { + completeFieldList.addAll(extractMapInternalFields( + (MapField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[]"))); + } else { + final String name = (StringUtils.isNotEmpty(breadCrumb) ? breadCrumb : innerField.getName() + "[]") + (StringUtils.isNotEmpty(breadCrumb) ? "[]" : breadCrumb); + completeFieldList.add( + FieldValueMapping.builder() + .fieldName(name) + .fieldType(calculateFieldType(breadCrumb, value)) + .required(!name.endsWith("][]") && !name.endsWith("][:]") && innerField.isFieldRequired()) + .isAncestorRequired(!isRootElement && isAncestorRequired != null && isAncestorRequired) + .build()); + } } + return completeFieldList; + } + + @NotNull + private static String calculateFieldType(final String breadCrumb, final Field value) { + return value.getType() + + SchemaExtractorUtil.ARRAY_TYPE_POSTFIX + + ((StringUtils.isNotEmpty(breadCrumb) + && breadCrumb.endsWith("[]")) ? SchemaExtractorUtil.ARRAY_TYPE_POSTFIX + : (StringUtils.isNotEmpty(breadCrumb) + && breadCrumb.endsWith("[:]")) ? "-map" + : breadCrumb); + } + + private List extractMapInternalFields( + final MapField innerField, final Boolean isRootElement, + final Boolean isAncestorRequired, final String breadCrumb) { + final List completeFieldList = new ArrayList<>(); + final Field value = innerField.getMapType(); - private void processRecordFieldList( - final String fieldName, final String splitter, final List internalFields, - final List completeFieldList, final boolean isAncestorRequired) { - internalFields.forEach(internalField -> { - if (Objects.nonNull(internalField.getFieldName())) { - internalField.setFieldName(fieldName + splitter + internalField.getFieldName()); - } else { - internalField.setFieldName(fieldName); - } - final String[] splittedName = internalField.getFieldName().split("\\."); - String parentName = splittedName[splittedName.length - 2]; - parentName = parentName.replace("[]", ""); - if (fieldName.equals(parentName)) { - internalField.setAncestorRequired(isAncestorRequired); - } - completeFieldList.add(internalField); - }); + if (value instanceof ObjectField) { + completeFieldList.addAll(extractObjectInternalFields((ObjectField) value, innerField.getName(), isAncestorRequired, breadCrumb, "[:]")); + } else if (value instanceof ArrayField) { + completeFieldList.addAll(extractArrayInternalFields( + (ArrayField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[:]"))); + } else if (value instanceof MapField) { + completeFieldList.addAll(extractMapInternalFields( + (MapField) value, false, isAncestorRequired, generateBreadCrumb(breadCrumb, innerField.getName(), "[:]"))); + } else { + final String name = (StringUtils.isNotEmpty(breadCrumb) ? breadCrumb : innerField.getName() + "[:]") + (StringUtils.isNotEmpty(breadCrumb) ? "[:]" : breadCrumb); + completeFieldList.add( + FieldValueMapping.builder() + .fieldName(name) + .fieldType(getFieldType(breadCrumb, value)) + .required(!name.endsWith("][]") && !name.endsWith("][:]") && innerField.isFieldRequired()) + .isAncestorRequired(!isRootElement && isAncestorRequired != null && isAncestorRequired) + .build()); } + return completeFieldList; + } - private Boolean checkRequiredByType( - final Field innerField, final List requiredInternalFields, - final FieldValueMapping fieldValueMapping) { - boolean result = fieldValueMapping.getRequired(); - if (!(innerField instanceof ArrayField) && !(innerField instanceof MapField)) { - result = requiredInternalFields.contains(fieldValueMapping.getFieldName()); - } - return result; + @NotNull + private static String getFieldType(final String breadCrumb, final Field value) { + return value.getType() + + "-map" + + ((StringUtils.isNotEmpty(breadCrumb) + && breadCrumb.endsWith("[:]")) ? "-map" + : (StringUtils.isNotEmpty(breadCrumb) + && breadCrumb.endsWith("[]")) ? SchemaExtractorUtil.ARRAY_TYPE_POSTFIX + : breadCrumb); + } + + private void processRecordFieldList(final String fieldName, final String splitter, final List internalFields, final List completeFieldList, + final boolean isAncestorRequired) { + internalFields.forEach(internalField -> extracted(fieldName, splitter, completeFieldList, isAncestorRequired, internalField)); + } + + private static void extracted(final String fieldName, final String splitter, final List completeFieldList, final boolean isAncestorRequired, + final FieldValueMapping internalField) { + if (Objects.nonNull(internalField.getFieldName())) { + internalField.setFieldName(fieldName + splitter + internalField.getFieldName()); + } else { + internalField.setFieldName(fieldName); } + final String[] splitName = internalField.getFieldName().split("\\."); + String parentName = splitName[splitName.length - 2]; + parentName = parentName.replace("[]", ""); + if (fieldName.equals(parentName)) { + internalField.setAncestorRequired(isAncestorRequired); + } + completeFieldList.add(internalField); + } - private Boolean checkRequiredElement(final Boolean isRootElement, final Boolean isAncestorRequired, final Boolean isFieldRequired) { - final boolean isRequired = isAncestorRequired != null && isAncestorRequired; - return isRootElement != null && isRootElement ? isFieldRequired : isRequired; + private Boolean checkRequiredByType( + final Field innerField, final List requiredInternalFields, + final FieldValueMapping fieldValueMapping) { + boolean result = fieldValueMapping.getRequired(); + if (!(innerField instanceof ArrayField) && !(innerField instanceof MapField)) { + result = requiredInternalFields.contains(fieldValueMapping.getFieldName()); } + return result; + } - private String generateBreadCrumb(final String breadCrumb, final String fieldName, final String endValue) { + private Boolean checkRequiredElement(final Boolean isRootElement, final Boolean isAncestorRequired, final Boolean isFieldRequired) { + final boolean isRequired = isAncestorRequired != null && isAncestorRequired; + return isRootElement != null && isRootElement ? isFieldRequired : isRequired; + } - if (breadCrumb.endsWith("[]") || breadCrumb.endsWith("[:]")) { - throw new KLoadGenException("Wrong Json Schema, 3+ consecutive nested collections are not allowed"); - } + private String generateBreadCrumb(final String breadCrumb, final String fieldName, final String endValue) { - return fieldName + endValue; + if (breadCrumb.endsWith("[]") || breadCrumb.endsWith("[:]")) { + throw new KLoadGenException("Wrong Json Schema, 3+ consecutive nested collections are not allowed"); } + return fieldName + endValue; + } + } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultExtractor.java index 957021c4..528e836c 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultExtractor.java @@ -13,27 +13,27 @@ public class JsonDefaultExtractor extends AbstractJsonExtractor implements Extractor { - @Override - public List processSchema(org.everit.json.schema.Schema schema) { + @Override + public final List processSchema(final org.everit.json.schema.Schema schema) { - Schema parsed = jsonSchemaParser.parse(schema.toString()); + final Schema parsed = getSchemaParser().parse(schema.toString()); - final List attributeList = new ArrayList<>(); - parsed.getProperties().forEach(field -> attributeList.addAll(processField(field, true, null))); + final List attributeList = new ArrayList<>(); + parsed.getProperties().forEach(field -> attributeList.addAll(processField(field, true, null))); - final Set requiredFields = new HashSet<>(parsed.getRequiredFields()); + final Set requiredFields = new HashSet<>(parsed.getRequiredFields()); - for (FieldValueMapping field : attributeList) { - if (!field.getFieldName().contains("[]") && !field.getFieldName().contains("[:]")) { - field.setRequired(requiredFields.contains(field.getFieldName())); - } - } - return attributeList; - } - - @Override - public List getSchemaNameList(String schema) { - Schema parsed = jsonSchemaParser.parse(schema); - return parsed.getProperties().stream().map(Field::getName).collect(Collectors.toList()); + for (FieldValueMapping field : attributeList) { + if (!field.getFieldName().contains("[]") && !field.getFieldName().contains("[:]")) { + field.setRequired(requiredFields.contains(field.getFieldName())); + } } + return attributeList; + } + + @Override + public final List getSchemaNameList(final String schema) { + final Schema parsed = getSchemaParser().parse(schema); + return parsed.getProperties().stream().map(Field::getName).collect(Collectors.toList()); + } } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonExtractor.java index ee654edf..1f754f2b 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/json/JsonExtractor.java @@ -1,8 +1,5 @@ package com.sngular.kloadgen.extractor.extractors.json; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.APICURIO; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.CONFLUENT; - import java.util.List; import java.util.Map; @@ -14,15 +11,15 @@ public class JsonExtractor implements ExtractorRegistry { - static Map schemaRegistryMap = Map.of(CONFLUENT, new JsonDefaultExtractor(), - APICURIO, new JsonDefaultExtractor()); + private static final Map SCHEMA_REGISTRY_MAP = Map.of(SchemaRegistryEnum.CONFLUENT, new JsonDefaultExtractor(), + SchemaRegistryEnum.APICURIO, new JsonDefaultExtractor()); - public final List processSchema(final Object schemaReceived, SchemaRegistryEnum registryEnum) { - return schemaRegistryMap.get(registryEnum).processSchema(schemaReceived); + public final List processSchema(final Object schemaReceived, final SchemaRegistryEnum registryEnum) { + return SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schemaReceived); } - public final List getSchemaNameList(final String schema, SchemaRegistryEnum registryEnum) { - return schemaRegistryMap.get(registryEnum).getSchemaNameList(schema); + public final List getSchemaNameList(final String schema, final SchemaRegistryEnum registryEnum) { + return SCHEMA_REGISTRY_MAP.get(registryEnum).getSchemaNameList(schema); } } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/AbstractProtoFileExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/AbstractProtoFileExtractor.java index 78d03403..32282b7d 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/AbstractProtoFileExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/AbstractProtoFileExtractor.java @@ -40,14 +40,14 @@ public abstract class AbstractProtoFileExtractor { protected AbstractProtoFileExtractor() { } - public List processSchemaDefault(ProtoFileElement schema){ + public final List processSchemaDefault(final ProtoFileElement schema) { final List attributeList = new ArrayList<>(); final HashMap nestedTypes = new HashMap<>(); schema.getTypes().forEach(field -> processField(field, attributeList, schema.getImports(), true, nestedTypes)); return attributeList; } - public List getSchemaNameListDefault(String schema){ + public final List getSchemaNameListDefault(final String schema) { final DynamicSchema dynamicSchema; try { dynamicSchema = DynamicSchema.parseFrom(schema.getBytes(StandardCharsets.UTF_8)); @@ -62,8 +62,7 @@ public static void processField( final List imports, final boolean isAncestorRequired, final Map nestedTypes) { fillNestedTypes(field, nestedTypes); - if (field instanceof MessageElement) { - final var messageField = (MessageElement) field; + if (field instanceof final MessageElement messageField) { if (!messageField.getOneOfs().isEmpty()) { extractOneOfs((MessageElement) field, completeFieldList, nestedTypes, isAncestorRequired); } @@ -80,9 +79,8 @@ public static void processField( } } - private static void processField( - final FieldElement field, - final List completeFieldList, final List imports, final boolean isAncestorRequired, final Map nestedTypes) { + private static void processField(final FieldElement field, final List completeFieldList, final List imports, final boolean isAncestorRequired, + final Map nestedTypes) { final Field.Label label = checkNullLabel(field); final boolean isArray = "repeated".equalsIgnoreCase(Objects.requireNonNull(label.toString())); final boolean isOptional = Objects.equals(Objects.requireNonNull(label.toString()), "optional"); @@ -109,8 +107,8 @@ private static List processFieldList(final TypeElement fieldL return completeFieldList; } - private static void extractOneOfs( - final MessageElement field, final List completeFieldList, final Map nestedTypes, final boolean isAncestorRequired) { + private static void extractOneOfs(final MessageElement field, final List completeFieldList, final Map nestedTypes, + final boolean isAncestorRequired) { final List oneOfs = new ArrayList<>(field.getOneOfs()); for (OneOfElement oneOfElement : oneOfs) { if (!oneOfElement.getFields().isEmpty()) { @@ -131,8 +129,7 @@ private static void extractOneOfs( } } - private static FieldValueMapping extractDotTypeWhenNotNestedType( - final FieldElement subfield, final boolean isArray, final String dotType, final boolean isRequired, + private static FieldValueMapping extractDotTypeWhenNotNestedType(final FieldElement subfield, final boolean isArray, final String dotType, final boolean isRequired, final boolean isAncestorRequired) { final FieldValueMapping completeFieldList; if (isArray) { @@ -144,9 +141,8 @@ private static FieldValueMapping extractDotTypeWhenNotNestedType( return completeFieldList; } - private static void extractMapType( - final List completeFieldList, final Map nestedTypes, final FieldElement subfield, final List imports, - final boolean isRequired, final boolean isAncestorRequired) { + private static void extractMapType(final List completeFieldList, final Map nestedTypes, final FieldElement subfield, + final List imports, final boolean isRequired, final boolean isAncestorRequired) { final String subFieldType = extractInternalMapFields(subfield); final String dotTypeMap = checkDotType(subFieldType, imports); if (ProtobufHelper.isValidType(subFieldType)) { @@ -170,8 +166,8 @@ private static String extractInternalMapFields(final FieldElement subfield) { return mapSplit[1].replace(">", "").trim(); } - private static void extractPrimitiveTypes( - final List completeFieldList, final FieldElement subfield, final boolean isArray, final boolean isRequired, final boolean isAncestorRequired) { + private static void extractPrimitiveTypes(final List completeFieldList, final FieldElement subfield, final boolean isArray, final boolean isRequired, + final boolean isAncestorRequired) { if (isArray) { completeFieldList.add(FieldValueMapping.builder().fieldName(subfield.getName() + "[]") .fieldType(subfield.getType().replace(subfield.getType(), ProtobufHelper.translateType(subfield.getType())) + ARRAY_POSTFIX) diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtoBufConfluentExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtoBufConfluentExtractor.java index ddbe8d8a..3b24537a 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtoBufConfluentExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtoBufConfluentExtractor.java @@ -9,7 +9,7 @@ public class ProtoBufConfluentExtractor extends AbstractProtoFileExtractor implements Extractor { public final List processSchema(final ProtobufSchema schemaReceived) { - return processSchemaDefault(schemaReceived.rawSchema()); + return processSchemaDefault(schemaReceived.rawSchema()); } public final List getSchemaNameList(final String schema) { diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobuffExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobuffExtractor.java index 0b2b7180..d95212cc 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobuffExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobuffExtractor.java @@ -1,8 +1,6 @@ package com.sngular.kloadgen.extractor.extractors.protobuff; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.APICURIO; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.CONFLUENT; - +import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -14,18 +12,21 @@ public class ProtobuffExtractor implements ExtractorRegistry { - static Map schemaRegistryMap = Map.of(CONFLUENT, new ProtoBufConfluentExtractor(), - APICURIO, new ProtoBufApicurioExtractor()); + private static final Map SCHEMA_REGISTRY_MAP = Map.of(SchemaRegistryEnum.CONFLUENT, new ProtoBufConfluentExtractor(), + SchemaRegistryEnum.APICURIO, new ProtoBufApicurioExtractor()); - public final List processSchema(final Object schemaReceived, SchemaRegistryEnum registryEnum) { + public final List processSchema(final Object schemaReceived, final SchemaRegistryEnum registryEnum) { + final var resultSchema = new ArrayList(); if (schemaReceived instanceof ProtoFileElement) { - return schemaRegistryMap.get(APICURIO).processSchema(schemaReceived); + resultSchema.addAll(SCHEMA_REGISTRY_MAP.get(SchemaRegistryEnum.APICURIO).processSchema(schemaReceived)); + } else { + resultSchema.addAll(SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schemaReceived)); } - return schemaRegistryMap.get(registryEnum).processSchema(schemaReceived); + return resultSchema; } - public final List getSchemaNameList(final String schema, SchemaRegistryEnum registryEnum) { - return schemaRegistryMap.get(registryEnum).getSchemaNameList(schema); + public final List getSchemaNameList(final String schema, final SchemaRegistryEnum registryEnum) { + return SCHEMA_REGISTRY_MAP.get(registryEnum).getSchemaNameList(schema); } } \ No newline at end of file diff --git a/src/main/java/com/sngular/kloadgen/loadgen/impl/ProtobufLoadGenerator.java b/src/main/java/com/sngular/kloadgen/loadgen/impl/ProtobufLoadGenerator.java index 66033462..9e1f747e 100644 --- a/src/main/java/com/sngular/kloadgen/loadgen/impl/ProtobufLoadGenerator.java +++ b/src/main/java/com/sngular/kloadgen/loadgen/impl/ProtobufLoadGenerator.java @@ -46,8 +46,8 @@ public final void setUpGenerator(final String schema, final List( - ConfluentSchemaMetadata.parse(new SchemaMetadata(1, 1, SchemaTypeEnum.PROTOBUF.name(), Collections.emptyList(), schema))) - , fieldExprMappings); + ConfluentSchemaMetadata.parse(new SchemaMetadata(1, 1, SchemaTypeEnum.PROTOBUF.name(), Collections.emptyList(), schema))), + fieldExprMappings); } @Override diff --git a/src/main/java/com/sngular/kloadgen/processor/SchemaProcessor.java b/src/main/java/com/sngular/kloadgen/processor/SchemaProcessor.java index 5b6d3c58..b5f44f6a 100644 --- a/src/main/java/com/sngular/kloadgen/processor/SchemaProcessor.java +++ b/src/main/java/com/sngular/kloadgen/processor/SchemaProcessor.java @@ -224,7 +224,7 @@ private boolean searchFieldWithSharedPathAndMakeItProcessable( if (otherFieldIsRequired) { shouldProcess = false; } else { - if (listFieldsSharingPath.size() > 0) { + if (!listFieldsSharingPath.isEmpty()) { makeFieldValueMappingRequiredAndNotNullable(fetchFieldSharingPathToMakeItRequired(listFieldsSharingPath)); shouldProcess = fieldValueMapping.getRequired(); } else { diff --git a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/ObjectCreatorFactoryHelper.java b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/ObjectCreatorFactoryHelper.java index 8fb6a097..380fba6c 100644 --- a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/ObjectCreatorFactoryHelper.java +++ b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/ObjectCreatorFactoryHelper.java @@ -20,19 +20,12 @@ private ObjectCreatorFactoryHelper() {} public static ObjectCreatorFactory getInstance(final SchemaTypeEnum schemaType, final Object schema, final BaseSchemaMetadata metadata) { final ObjectCreatorFactory objectCreatorFactory; try { - switch (schemaType) { - case JSON: - objectCreatorFactory = new JsonObjectCreatorFactory(); - break; - case AVRO: - objectCreatorFactory = new AvroObjectCreatorFactory(schema, metadata); - break; - case PROTOBUF: - objectCreatorFactory = new ProtobufObjectCreatorFactory(schema, metadata); - break; - default: - throw new KLoadGenException("Unsupported schema type"); - } + objectCreatorFactory = switch (schemaType) { + case JSON -> new JsonObjectCreatorFactory(); + case AVRO -> new AvroObjectCreatorFactory(schema, metadata); + case PROTOBUF -> new ProtobufObjectCreatorFactory(schema, metadata); + default -> throw new KLoadGenException("Unsupported schema type"); + }; } catch (KLoadGenException | DescriptorValidationException | IOException e) { final String logMsg = "Please, make sure that the schema sources fed are correct"; log.error(logMsg, e); diff --git a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/AvroObjectCreatorFactory.java b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/AvroObjectCreatorFactory.java index 98235e12..c92f8344 100644 --- a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/AvroObjectCreatorFactory.java +++ b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/AvroObjectCreatorFactory.java @@ -18,10 +18,10 @@ import com.sngular.kloadgen.processor.objectcreatorfactory.ObjectCreatorFactory; import com.sngular.kloadgen.processor.util.SchemaProcessorUtils; import com.sngular.kloadgen.randomtool.generator.AvroGeneratorTool; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioParsedSchemaMetadata; +import com.sngular.kloadgen.schemaregistry.adapter.impl.AbstractParsedSchemaAdapter; +import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioAbstractParsedSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.SchemaMetadataAdapter; import com.sngular.kloadgen.serializer.EnrichedRecord; import io.confluent.kafka.schemaregistry.ParsedSchema; @@ -31,7 +31,7 @@ import org.apache.avro.generic.GenericRecord; import org.apache.commons.collections4.IteratorUtils; -public class AvroObjectCreatorFactory implements ObjectCreatorFactory { +public final class AvroObjectCreatorFactory implements ObjectCreatorFactory { private static final AvroGeneratorTool AVRO_GENERATOR_TOOL = new AvroGeneratorTool(); @@ -50,9 +50,9 @@ public AvroObjectCreatorFactory(final Object schema, final BaseSchemaMetadata generateFunction, final boolean isInnerMap) { + public Object createMap(final SchemaProcessorPOJO pojo, final Function generateFunction, final boolean isInnerMap) { Map map = new HashMap<>(); if (pojo.isLastFilterTypeOfLastElement()) { @@ -93,7 +93,7 @@ private String generateString(final Integer valueLength) { } @Override - public final Object createArray(final SchemaProcessorPOJO pojo, final Function generateFunction, final boolean isInnerArray) { + public Object createArray(final SchemaProcessorPOJO pojo, final Function generateFunction, final boolean isInnerArray) { List list = new ArrayList<>(); if (pojo.isLastFilterTypeOfLastElement()) { @@ -112,7 +112,7 @@ public final Object createArray(final SchemaProcessorPOJO pojo, final Function createFinalArray(final SchemaProcessorPOJO pojo) { pojo.getFieldValuesList(), pojo.getFieldSize(), Collections.emptyMap()); } - public final Object assignObject(final String targetObjectName, final String fieldName, final Object objectToAssign) { + public Object assignObject(final String targetObjectName, final String fieldName, final Object objectToAssign) { final GenericRecord entityObject = entity.get(targetObjectName); entityObject.put(fieldName, objectToAssign); return entityObject; diff --git a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/JsonObjectCreatorFactory.java b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/JsonObjectCreatorFactory.java index d9b97298..909e2d40 100644 --- a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/JsonObjectCreatorFactory.java +++ b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/JsonObjectCreatorFactory.java @@ -123,7 +123,7 @@ public final boolean isOptionalFieldAccordingToSchema(final String completeField } @Override - public Object getRootNode(final String rootNode) { + public final Object getRootNode(final String rootNode) { return entity.get(rootNode); } diff --git a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/ProtobufObjectCreatorFactory.java b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/ProtobufObjectCreatorFactory.java index cf1428a0..abab03c3 100644 --- a/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/ProtobufObjectCreatorFactory.java +++ b/src/main/java/com/sngular/kloadgen/processor/objectcreatorfactory/impl/ProtobufObjectCreatorFactory.java @@ -24,9 +24,9 @@ import com.sngular.kloadgen.processor.objectcreatorfactory.ObjectCreatorFactory; import com.sngular.kloadgen.processor.util.SchemaProcessorUtils; import com.sngular.kloadgen.randomtool.generator.ProtoBufGeneratorTool; +import com.sngular.kloadgen.schemaregistry.adapter.impl.AbstractParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.SchemaMetadataAdapter; import com.sngular.kloadgen.serializer.EnrichedRecord; import com.squareup.wire.schema.internal.parser.ProtoFileElement; @@ -50,7 +50,7 @@ public ProtobufObjectCreatorFactory(final Object schema, final BaseSchemaMetadat this.schema = SchemaProcessorUtils.buildProtoDescriptor((ProtoFileElement) schema, metadata); } else if (schema instanceof BaseParsedSchema) { final BaseParsedSchema schemaParse = (BaseParsedSchema) schema; - final ParsedSchemaAdapter adapterParse = schemaParse.getParsedSchemaAdapter(); + final AbstractParsedSchemaAdapter adapterParse = schemaParse.getParsedSchemaAdapter(); final Object schemaParsed = adapterParse.getRawSchema(); this.schema = SchemaProcessorUtils.buildProtoDescriptor((ProtoFileElement) schemaParsed, metadata); } else { @@ -171,7 +171,7 @@ public final boolean isOptionalFieldAccordingToSchema(final String completeField } @Override - public Object getRootNode(final String rootNode) { + public final Object getRootNode(final String rootNode) { return entity.get(rootNode); } diff --git a/src/main/java/com/sngular/kloadgen/processor/util/SchemaProcessorUtils.java b/src/main/java/com/sngular/kloadgen/processor/util/SchemaProcessorUtils.java index f8a81fcf..8f035c90 100644 --- a/src/main/java/com/sngular/kloadgen/processor/util/SchemaProcessorUtils.java +++ b/src/main/java/com/sngular/kloadgen/processor/util/SchemaProcessorUtils.java @@ -15,6 +15,7 @@ import java.util.ListIterator; import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentLinkedQueue; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -25,8 +26,8 @@ import com.google.protobuf.Descriptors; import com.google.protobuf.Descriptors.DescriptorValidationException; import com.sngular.kloadgen.model.FieldValueMapping; +import com.sngular.kloadgen.schemaregistry.adapter.impl.AbstractParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.SchemaMetadataAdapter; import com.sngular.kloadgen.util.JMeterHelper; import com.sngular.kloadgen.util.ProtobufHelper; @@ -156,27 +157,9 @@ public static Descriptors.Descriptor buildProtoDescriptor(final ProtoFileElement throws Descriptors.DescriptorValidationException, IOException { final DynamicSchema.Builder schemaBuilder = DynamicSchema.newBuilder(); - final List imports = schema.getImports(); + final var imports = new ConcurrentLinkedQueue<>(schema.getImports()); for (final String importedClass : imports) { - try (final InputStream resourceStream = SchemaProcessorUtils.class.getClassLoader().getResourceAsStream(importedClass)) { - if (null != resourceStream) { - final String schemaToString = new String(resourceStream.readAllBytes()); - final var lines = new ArrayList<>(CollectionUtils.select(Arrays.asList(schemaToString.split("\\n")), isValid())); - if (!ProtobufHelper.NOT_ACCEPTED_IMPORTS.contains(importedClass)) { - final var importedSchema = processImported(lines); - schemaBuilder.addDependency(importedSchema.getFileDescriptorSet().getFile(0).getName()); - schemaBuilder.addSchema(importedSchema); - } - } else { - final ParsedSchemaAdapter protoFileElement = JMeterHelper.getParsedSchema(getSubjectName(importedClass, metadata), - JMeterContextService.getContext().getProperties()).getParsedSchemaAdapter(); - final var importedProtobufSchema = new ProtobufSchema(protoFileElement.getRawSchema(), metadata.getSchemaMetadataAdapter().getReferences(), new HashMap<>()); - if (!ProtobufHelper.NOT_ACCEPTED_IMPORTS.contains(importedClass)) { - schemaBuilder.addDependency(importedProtobufSchema.toDescriptor().getFullName()); - schemaBuilder.addSchema(convertDynamicSchema(importedProtobufSchema)); - } - } - } + processImport(metadata, importedClass, schemaBuilder); } final MessageElement messageElement = (MessageElement) schema.getTypes().get(0); @@ -192,6 +175,29 @@ public static Descriptors.Descriptor buildProtoDescriptor(final ProtoFileElement return schemaBuilder.build().getMessageDescriptor(messageElement.getName()); } + private static void processImport(final BaseSchemaMetadata metadata, final String importedClass, final DynamicSchema.Builder schemaBuilder) + throws IOException, DescriptorValidationException { + try (final InputStream resourceStream = SchemaProcessorUtils.class.getClassLoader().getResourceAsStream(importedClass)) { + if (null != resourceStream) { + final String schemaToString = new String(resourceStream.readAllBytes()); + final var lines = new ArrayList<>(CollectionUtils.select(Arrays.asList(schemaToString.split("\\n")), isValid())); + if (!ProtobufHelper.NOT_ACCEPTED_IMPORTS.contains(importedClass)) { + final var importedSchema = processImported(lines, metadata); + schemaBuilder.addDependency(importedSchema.getFileDescriptorSet().getFile(0).getName()); + schemaBuilder.addSchema(importedSchema); + } + } else { + final AbstractParsedSchemaAdapter protoFileElement = JMeterHelper.getParsedSchema(getSubjectName(importedClass, metadata), + JMeterContextService.getContext().getProperties()).getParsedSchemaAdapter(); + final var importedProtobufSchema = new ProtobufSchema(protoFileElement.getRawSchema(), metadata.getSchemaMetadataAdapter().getReferences(), new HashMap<>()); + if (!ProtobufHelper.NOT_ACCEPTED_IMPORTS.contains(importedClass)) { + schemaBuilder.addDependency(importedProtobufSchema.toDescriptor().getFullName()); + schemaBuilder.addSchema(convertDynamicSchema(importedProtobufSchema, metadata)); + } + } + } + } + private static String getSubjectName(final String importedClass, final BaseSchemaMetadata metadata) { final List references = metadata.getSchemaMetadataAdapter().getReferences(); String subjectName = null; @@ -206,15 +212,17 @@ private static String getSubjectName(final String importedClass, final BaseSchem return Objects.requireNonNullElse(subjectName, importedClass); } - private static DynamicSchema convertDynamicSchema(final ProtobufSchema importSchema) throws DescriptorValidationException { - return processImported(Arrays.asList(importSchema.rawSchema().toSchema().split("\\n"))); + private static DynamicSchema convertDynamicSchema(final ProtobufSchema importSchema, final BaseSchemaMetadata metadata) + throws DescriptorValidationException, IOException { + return processImported(Arrays.asList(importSchema.rawSchema().toSchema().split("\\n")), metadata); } private static Predicate isValid() { return line -> !line.contains("//") && !line.isEmpty(); } - private static DynamicSchema processImported(final List importedLines) throws DescriptorValidationException { + private static DynamicSchema processImported(final List importedLines, final BaseSchemaMetadata metadata) + throws DescriptorValidationException, IOException { final DynamicSchema.Builder schemaBuilder = DynamicSchema.newBuilder(); @@ -234,7 +242,7 @@ private static DynamicSchema processImported(final List importedLines) t } if (fileLine.startsWith("import")) { - schemaBuilder.addDependency(fileLine.substring(6)); + processImport(metadata, fileLine.substring(6), schemaBuilder); } } @@ -382,8 +390,7 @@ private static void addDefinition( final MessageDefinition.Builder msgDef, final String typeName, final TypeElement typeElement, final HashMap>> globalNestedTypesByLevelAndMessage, final int deepLevel) { - if (typeElement instanceof EnumElement) { - final var enumElement = (EnumElement) typeElement; + if (typeElement instanceof final EnumElement enumElement) { final EnumDefinition.Builder builder = EnumDefinition.newBuilder(enumElement.getName()); for (final var constant : enumElement.getConstants()) { builder.addValue(constant.getName(), constant.getTag()); diff --git a/src/main/java/com/sngular/kloadgen/property/editor/FileSubjectPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/FileSubjectPropertyEditor.java index 6db26c5b..091135d1 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/FileSubjectPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/FileSubjectPropertyEditor.java @@ -22,13 +22,6 @@ import java.util.List; import java.util.Objects; -import com.sngular.kloadgen.common.SchemaTypeEnum; -import com.sngular.kloadgen.extractor.SchemaExtractor; -import com.sngular.kloadgen.model.FieldValueMapping; -import com.sngular.kloadgen.util.AutoCompletion; -import com.sngular.kloadgen.util.PropsKeysHelper; -import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; -import io.confluent.kafka.schemaregistry.ParsedSchema; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; @@ -36,6 +29,14 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.filechooser.FileSystemView; + +import com.sngular.kloadgen.common.SchemaTypeEnum; +import com.sngular.kloadgen.extractor.SchemaExtractor; +import com.sngular.kloadgen.model.FieldValueMapping; +import com.sngular.kloadgen.util.AutoCompletion; +import com.sngular.kloadgen.util.PropsKeysHelper; +import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; +import io.confluent.kafka.schemaregistry.ParsedSchema; import lombok.extern.slf4j.Slf4j; import org.apache.avro.AvroRuntimeException; import org.apache.jmeter.gui.ClearGui; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/KeyDeserializerPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/KeyDeserializerPropertyEditor.java index 4a3a7464..2605bb84 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/KeyDeserializerPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/KeyDeserializerPropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; @@ -59,7 +60,8 @@ private void init() { private void fillDeserializer(final JComboBox objectJComboBox) { deserializerComboBox = objectJComboBox; final Reflections reflections = new Reflections(new ConfigurationBuilder().addUrls(ClasspathHelper.forClass(Deserializer.class)).filterInputsBy( - new FilterBuilder().includePackage(SerDesPackageValue.COM_SNGULAR_KLOADGEN_SERIALIZER).includePackage(SerDesPackageValue.IO_CONFLUENT_KAFKA_SERIALIZERS)) + new FilterBuilder().includePackage(SerDesPackageValue.COM_SNGULAR_KLOADGEN_SERIALIZER) + .includePackage(SerDesPackageValue.IO_CONFLUENT_KAFKA_SERIALIZERS)) .setScanners(Scanners.SubTypes)); ReflectionUtils.extractDeserializers(deserializerComboBox, reflections, Deserializer.class); } diff --git a/src/main/java/com/sngular/kloadgen/property/editor/KeySerializerPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/KeySerializerPropertyEditor.java index 80b7b092..937baad6 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/KeySerializerPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/KeySerializerPropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/NameStrategyPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/NameStrategyPropertyEditor.java index c055e577..8fddcbcf 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/NameStrategyPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/NameStrategyPropertyEditor.java @@ -14,10 +14,11 @@ import java.beans.PropertyEditorSupport; import java.util.Objects; -import io.apicurio.registry.resolver.strategy.ArtifactReferenceResolverStrategy; -import io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy; import javax.swing.JComboBox; import javax.swing.JPanel; + +import io.apicurio.registry.resolver.strategy.ArtifactReferenceResolverStrategy; +import io.confluent.kafka.serializers.subject.strategy.SubjectNameStrategy; import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; @@ -61,7 +62,7 @@ private void fillSerializer(final JComboBox objectJComboBox) { getComboItemValues(ArtifactReferenceResolverStrategy.class); } - private void getComboItemValues(Class targetClass) { + private void getComboItemValues(final Class targetClass) { final Reflections reflections = new Reflections( new ConfigurationBuilder() .addUrls(ClasspathHelper.forClass(targetClass)) diff --git a/src/main/java/com/sngular/kloadgen/property/editor/PlainKeySerializerPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/PlainKeySerializerPropertyEditor.java index e66decbe..6d968585 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/PlainKeySerializerPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/PlainKeySerializerPropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/PlainValueSerializerPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/PlainValueSerializerPropertyEditor.java index eab4956d..98ed9345 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/PlainValueSerializerPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/PlainValueSerializerPropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/ReflectionUtils.java b/src/main/java/com/sngular/kloadgen/property/editor/ReflectionUtils.java index 0d79a56a..758b3a2f 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/ReflectionUtils.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/ReflectionUtils.java @@ -12,6 +12,7 @@ import java.util.Set; import javax.swing.JComboBox; + import org.apache.kafka.common.serialization.Deserializer; import org.apache.kafka.common.serialization.Serializer; import org.reflections.Reflections; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/SchemaConverterPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/SchemaConverterPropertyEditor.java index e90fa6ff..e094bed4 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/SchemaConverterPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/SchemaConverterPropertyEditor.java @@ -14,6 +14,7 @@ import java.util.Objects; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.NotImplementedException; import org.apache.jmeter.gui.ClearGui; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditor.java index 10baebe1..b8544e7b 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditor.java @@ -18,6 +18,11 @@ import java.util.Map; import java.util.Objects; +import javax.swing.JButton; +import javax.swing.JOptionPane; +import javax.swing.JPanel; +import javax.swing.JTextField; + import com.sngular.kloadgen.exception.KLoadGenException; import com.sngular.kloadgen.model.PropertyMapping; import com.sngular.kloadgen.schemaregistry.SchemaRegistryAdapter; @@ -26,10 +31,6 @@ import com.sngular.kloadgen.util.PropsKeysHelper; import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig; -import javax.swing.JButton; -import javax.swing.JOptionPane; -import javax.swing.JPanel; -import javax.swing.JTextField; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.gui.ClearGui; @@ -138,7 +139,7 @@ public final void actionPerformed(final ActionEvent actionEvent) { //Retrieve TableEditor and set all fields with default values to it final var propertyEditors = (PropertyEditor[]) editors.get(testBeanCustomizer); - Map schemaProperties = new HashMap<>(); + final Map schemaProperties = new HashMap<>(); for (PropertyEditor propertyEditor : propertyEditors) { if (propertyEditor instanceof TableEditor) { //noinspection unchecked @@ -149,7 +150,7 @@ public final void actionPerformed(final ActionEvent actionEvent) { } final Map originals = new HashMap<>(); - String schemaRegistryName = schemaProperties.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME); + final String schemaRegistryName = schemaProperties.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME); JMeterContextService.getContext().getProperties().setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, schemaRegistryName); final SchemaRegistryAdapter schemaRegistryManager = SchemaRegistryManagerFactory.getSchemaRegistry(schemaRegistryName); @@ -162,8 +163,8 @@ public final void actionPerformed(final ActionEvent actionEvent) { JMeterContextService.getContext().getProperties().setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_AUTH_KEY, SchemaRegistryKeyHelper.SCHEMA_REGISTRY_AUTH_BASIC_TYPE); originals.put(AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, "USER_INFO"); - originals.put(AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG, schemaProperties.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_USERNAME_KEY) + ":" + - schemaProperties.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_PASSWORD_KEY)); + originals.put(AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG, schemaProperties.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_USERNAME_KEY) + ":" + + schemaProperties.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_PASSWORD_KEY)); } } diff --git a/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryNamePropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryNamePropertyEditor.java index 19338290..6d2e4dff 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryNamePropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/SchemaRegistryNamePropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/SchemaTypePropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/SchemaTypePropertyEditor.java index a0628a4c..e46b0571 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/SchemaTypePropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/SchemaTypePropertyEditor.java @@ -14,6 +14,7 @@ import java.util.Objects; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.NotImplementedException; import org.apache.jmeter.gui.ClearGui; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/SerDesPackageValue.java b/src/main/java/com/sngular/kloadgen/property/editor/SerDesPackageValue.java index ecc2e00f..cf55f1c9 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/SerDesPackageValue.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/SerDesPackageValue.java @@ -2,13 +2,16 @@ public class SerDesPackageValue { - public static final String COM_SNGULAR_KLOADGEN_SERIALIZER = "com.sngular.kloadgen.serializer"; + public static final String COM_SNGULAR_KLOADGEN_SERIALIZER = "com.sngular.kloadgen.serializer"; - public static final String IO_CONFLUENT_KAFKA_SERIALIZERS = "io.confluent.kafka.serializers"; + public static final String IO_CONFLUENT_KAFKA_SERIALIZERS = "io.confluent.kafka.serializers"; - public static final String IO_APICURIO_REGISTRY_SERDE_AVRO_AVRO_KAFKA_DESERIALIZER = "io.apicurio.registry.serde.avro"; + public static final String IO_APICURIO_REGISTRY_SERDE_AVRO_AVRO_KAFKA_DESERIALIZER = "io.apicurio.registry.serde.avro"; - public static final String IO_APICURIO_REGISTRY_SERDE_JSONSCHEMA_JSON_SCHEMA_KAFKA_DESERIALIZER = "io.apicurio.registry.serde.jsonschema"; + public static final String IO_APICURIO_REGISTRY_SERDE_JSONSCHEMA_JSON_SCHEMA_KAFKA_DESERIALIZER = "io.apicurio.registry.serde.jsonschema"; - public static final String IO_APICURIO_REGISTRY_SERDE_PROTOBUF_PROTOBUF_KAFKA_DESERIALIZER = "io.apicurio.registry.serde.protobuf"; + public static final String IO_APICURIO_REGISTRY_SERDE_PROTOBUF_PROTOBUF_KAFKA_DESERIALIZER = "io.apicurio.registry.serde.protobuf"; + + private SerDesPackageValue() { + } } diff --git a/src/main/java/com/sngular/kloadgen/property/editor/SerialisedSubjectPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/SerialisedSubjectPropertyEditor.java index 51be9de8..328a8c63 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/SerialisedSubjectPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/SerialisedSubjectPropertyEditor.java @@ -17,16 +17,17 @@ import java.util.List; import java.util.Objects; -import com.sngular.kloadgen.extractor.SchemaExtractor; -import com.sngular.kloadgen.model.FieldValueMapping; -import com.sngular.kloadgen.util.AutoCompletion; -import com.sngular.kloadgen.util.PropsKeysHelper; -import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import javax.swing.DefaultComboBoxModel; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JOptionPane; import javax.swing.JPanel; + +import com.sngular.kloadgen.extractor.SchemaExtractor; +import com.sngular.kloadgen.model.FieldValueMapping; +import com.sngular.kloadgen.util.AutoCompletion; +import com.sngular.kloadgen.util.PropsKeysHelper; +import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import lombok.extern.slf4j.Slf4j; import org.apache.commons.collections4.IterableUtils; import org.apache.commons.collections4.Predicate; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/ValueDeserializerPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/ValueDeserializerPropertyEditor.java index b7d4f1ca..27457119 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/ValueDeserializerPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/ValueDeserializerPropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; diff --git a/src/main/java/com/sngular/kloadgen/property/editor/ValueSerializerPropertyEditor.java b/src/main/java/com/sngular/kloadgen/property/editor/ValueSerializerPropertyEditor.java index 704ec72e..3c2b83d0 100644 --- a/src/main/java/com/sngular/kloadgen/property/editor/ValueSerializerPropertyEditor.java +++ b/src/main/java/com/sngular/kloadgen/property/editor/ValueSerializerPropertyEditor.java @@ -16,6 +16,7 @@ import javax.swing.JComboBox; import javax.swing.JPanel; + import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.gui.ClearGui; import org.apache.jmeter.testbeans.gui.TestBeanPropertyEditor; diff --git a/src/main/java/com/sngular/kloadgen/randomtool/generator/ProtoBufGeneratorTool.java b/src/main/java/com/sngular/kloadgen/randomtool/generator/ProtoBufGeneratorTool.java index b7b94f31..f3fe5164 100644 --- a/src/main/java/com/sngular/kloadgen/randomtool/generator/ProtoBufGeneratorTool.java +++ b/src/main/java/com/sngular/kloadgen/randomtool/generator/ProtoBufGeneratorTool.java @@ -29,13 +29,13 @@ public class ProtoBufGeneratorTool { private final Map context = new HashMap<>(); - public final List generateArray(final String fieldName, final String fieldType, final int arraySize, final Integer valueLength, final List fieldValuesList) { + public final List generateArray(final String fieldName, final String fieldType, final int arraySize, final Integer valueLength, final List fieldValuesList) { final List parameterList = new ArrayList<>(fieldValuesList); parameterList.replaceAll( fieldValue -> fieldValue.matches("\\$\\{\\w*}") ? JMeterContextService.getContext().getVariables().get(fieldValue.substring(2, fieldValue.length() - 1)) : fieldValue); - final List value = new ArrayList<>(arraySize); + final List value = new ArrayList<>(arraySize); if ("seq".equals(fieldType)) { if (!fieldValuesList.isEmpty() && (fieldValuesList.size() > 1 || RandomSequence.isTypeNotSupported(fieldType))) { value.add(RandomSequence.generateSeq(fieldName, fieldType, parameterList, context)); @@ -45,7 +45,7 @@ public final List generateArray(final String fieldName, final String fieldType, } } } else { - value.addAll((ArrayList) RANDOM_ARRAY.generateArray(fieldType, valueLength, parameterList, arraySize, Collections.emptyMap())); + value.addAll((List) RANDOM_ARRAY.generateArray(fieldType, valueLength, parameterList, arraySize, Collections.emptyMap())); } return value; @@ -62,6 +62,20 @@ public final Object generateObject(final Descriptors.EnumDescriptor descriptor, return value; } + public final Object generateObject( + final FieldDescriptor descriptor, final String fieldType, final Integer valueLength, final List fieldValuesList, final Map constraints) { + Object result = null; + if (Objects.nonNull(descriptor.getJavaType())) { + result = generateRawObject(fieldType, valueLength, fieldValuesList, constraints); + } + return result; + } + + public final Object generateRawObject( + final String fieldType, final Integer valueLength, final List fieldValuesList, final Map constraints) { + return RANDOM_OBJECT.generateRandom(fieldType, valueLength, fieldValuesList, constraints); + } + private Object getEnumOrGenerate(final Descriptors.EnumDescriptor descriptor, final String fieldType, final List parameterList) { Object value = null; @@ -79,20 +93,6 @@ private Object getEnumOrGenerate(final Descriptors.EnumDescriptor descriptor, fi return value; } - public final Object generateObject( - final FieldDescriptor descriptor, final String fieldType, final Integer valueLength, final List fieldValuesList, final Map constraints) { - Object result = null; - if (Objects.nonNull(descriptor.getJavaType())) { - result = generateRawObject(fieldType, valueLength, fieldValuesList, constraints); - } - return result; - } - - public final Object generateRawObject( - final String fieldType, final Integer valueLength, final List fieldValuesList, final Map constraints) { - return RANDOM_OBJECT.generateRandom(fieldType, valueLength, fieldValuesList, constraints); - } - private Object getArrayEnumOrGenerate(final Descriptors.EnumDescriptor descriptor, final String fieldType, final int arraySize, final List parameterList) { final List value = new ArrayList<>(arraySize); for (int i = 0; i < arraySize; i++) { diff --git a/src/main/java/com/sngular/kloadgen/randomtool/random/RandomObject.java b/src/main/java/com/sngular/kloadgen/randomtool/random/RandomObject.java index 3d791c60..02dbde14 100644 --- a/src/main/java/com/sngular/kloadgen/randomtool/random/RandomObject.java +++ b/src/main/java/com/sngular/kloadgen/randomtool/random/RandomObject.java @@ -159,7 +159,7 @@ private BigInteger getIntegerValueOrRandom(final Integer valueLength, final List Number maximum; if (valueLength == 0) { maximum = 1000; - int num = rand.nextInt((Integer) maximum); + final int num = rand.nextInt((Integer) maximum); value = new BigInteger(String.valueOf(num)); } else { maximum = calculateMaximum(valueLength, constraints); diff --git a/src/main/java/com/sngular/kloadgen/sampler/SamplerUtil.java b/src/main/java/com/sngular/kloadgen/sampler/SamplerUtil.java index 841311aa..a8712639 100644 --- a/src/main/java/com/sngular/kloadgen/sampler/SamplerUtil.java +++ b/src/main/java/com/sngular/kloadgen/sampler/SamplerUtil.java @@ -101,15 +101,15 @@ public static Properties setupCommonProperties(final JavaSamplerContext context) final Properties props = new Properties(); props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, context.getParameter(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG)); - if ("true".equals(context.getJMeterVariables().get(PropsKeysHelper.SCHEMA_KEYED_MESSAGE_KEY))) { - props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, context.getJMeterVariables().get(PropsKeysHelper.KEY_SERIALIZER_CLASS_PROPERTY)); - } else if ("true".equals(context.getJMeterVariables().get(PropsKeysHelper.SIMPLE_KEYED_MESSAGE_KEY))) { - props.put(PropsKeysHelper.MESSAGE_KEY_KEY_TYPE, context.getJMeterVariables().get(PropsKeysHelper.KEY_TYPE)); - props.put(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE, context.getJMeterVariables().get(PropsKeysHelper.KEY_VALUE)); - if (Objects.nonNull(context.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA_TYPE))) { - props.put(PropsKeysHelper.KEY_SCHEMA_TYPE, context.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA_TYPE)); + if ("true".equals(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.SCHEMA_KEYED_MESSAGE_KEY))) { + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_SERIALIZER_CLASS_PROPERTY)); + } else if ("true".equals(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.SIMPLE_KEYED_MESSAGE_KEY))) { + props.put(PropsKeysHelper.MESSAGE_KEY_KEY_TYPE, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_TYPE)); + props.put(PropsKeysHelper.MESSAGE_KEY_KEY_VALUE, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_VALUE)); + if (Objects.nonNull(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA_TYPE))) { + props.put(PropsKeysHelper.KEY_SCHEMA_TYPE, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA_TYPE)); } - props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, context.getJMeterVariables().get(PropsKeysHelper.KEY_SERIALIZER_CLASS_PROPERTY)); + props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_SERIALIZER_CLASS_PROPERTY)); } else { props.put(PropsKeysHelper.SCHEMA_KEYED_MESSAGE_KEY, Boolean.FALSE); } @@ -128,15 +128,15 @@ public static Properties setupCommonProperties(final JavaSamplerContext context) props.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG, context.getParameter(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG)); props.put(ProducerKeysHelper.SASL_MECHANISM, context.getParameter(ProducerKeysHelper.SASL_MECHANISM)); - final String schemaRegistryNameValue = context.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME); + final String schemaRegistryNameValue = JavaSamplerContext.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME); final String enableSchemaRegistrationValue = context.getParameter(SchemaRegistryKeyHelper.ENABLE_AUTO_SCHEMA_REGISTRATION_CONFIG); if (SchemaRegistryKeyHelper.SCHEMA_REGISTRY_APICURIO.equalsIgnoreCase(schemaRegistryNameValue)) { props.put(SerdeConfig.AUTO_REGISTER_ARTIFACT, enableSchemaRegistrationValue); - props.put(SchemaResolverConfig.REGISTRY_URL, context.getJMeterVariables().get(SchemaResolverConfig.REGISTRY_URL)); - props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, context.getJMeterVariables().get(SchemaResolverConfig.REGISTRY_URL)); + props.put(SchemaResolverConfig.REGISTRY_URL, JavaSamplerContext.getJMeterVariables().get(SchemaResolverConfig.REGISTRY_URL)); + props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, JavaSamplerContext.getJMeterVariables().get(SchemaResolverConfig.REGISTRY_URL)); } else { props.put(SchemaRegistryKeyHelper.ENABLE_AUTO_SCHEMA_REGISTRATION_CONFIG, enableSchemaRegistrationValue); - final String schemaRegistryURL = context.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL); + final String schemaRegistryURL = JavaSamplerContext.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL); if (StringUtils.isNotBlank(schemaRegistryURL)) { props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, schemaRegistryURL); } @@ -267,14 +267,14 @@ public static Properties setupCommonConsumerProperties(final JavaSamplerContext props.put(CommonClientConfigs.CLIENT_ID_CONFIG, context.getParameter(CommonClientConfigs.CLIENT_ID_CONFIG)); - if (Objects.nonNull(context.getJMeterVariables().get(PropsKeysHelper.VALUE_SCHEMA))) { - props.put(PropsKeysHelper.VALUE_SCHEMA, context.getJMeterVariables().get(PropsKeysHelper.VALUE_SCHEMA)); + if (Objects.nonNull(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.VALUE_SCHEMA))) { + props.put(PropsKeysHelper.VALUE_SCHEMA, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.VALUE_SCHEMA)); } - if (Objects.nonNull(context.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA))) { - props.put(PropsKeysHelper.KEY_SCHEMA, context.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA)); + if (Objects.nonNull(JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA))) { + props.put(PropsKeysHelper.KEY_SCHEMA, JavaSamplerContext.getJMeterVariables().get(PropsKeysHelper.KEY_SCHEMA)); } - if (Objects.nonNull(context.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL))) { - props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, context.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL)); + if (Objects.nonNull(JavaSamplerContext.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL))) { + props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, JavaSamplerContext.getJMeterVariables().get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL)); } props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, context.getParameter(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG)); @@ -331,8 +331,7 @@ private static void verifySecurity(final JavaSamplerContext context, final Prope props.put(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG, context.getParameter(SslConfigs.SSL_ENABLED_PROTOCOLS_CONFIG)); props.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, - propertyOrDefault(context.getParameter(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG), - ProducerKeysHelper.DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM, + propertyOrDefault(context.getParameter(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG), ProducerKeysHelper.DEFAULT_SSL_ENDPOINT_IDENTIFICATION_ALGORITHM, "")); props.put(SslConfigs.SSL_KEYMANAGER_ALGORITHM_CONFIG, context.getParameter(SslConfigs.SSL_KEYMANAGER_ALGORITHM_CONFIG)); @@ -355,9 +354,9 @@ public static BaseLoadGenerator configureValueGenerator(final Properties props) final String valueNameStrategy = jMeterVariables.get(ProducerKeysHelper.VALUE_NAME_STRATEGY); if (SchemaRegistryKeyHelper.SCHEMA_REGISTRY_APICURIO.equalsIgnoreCase(jMeterVariables.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME))) { - props.put(SchemaResolverConfig.ARTIFACT_RESOLVER_STRATEGY, (Objects.nonNull(valueNameStrategy) ? valueNameStrategy : ProducerKeysHelper.TOPIC_NAME_STRATEGY_APICURIO)); + props.put(SchemaResolverConfig.ARTIFACT_RESOLVER_STRATEGY, Objects.nonNull(valueNameStrategy) ? valueNameStrategy : ProducerKeysHelper.TOPIC_NAME_STRATEGY_APICURIO); } else if (SchemaRegistryKeyHelper.SCHEMA_REGISTRY_CONFLUENT.equalsIgnoreCase(jMeterVariables.get(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME))) { - props.put(ProducerKeysHelper.VALUE_NAME_STRATEGY, (Objects.nonNull(valueNameStrategy) ? valueNameStrategy : ProducerKeysHelper.TOPIC_NAME_STRATEGY_CONFLUENT)); + props.put(ProducerKeysHelper.VALUE_NAME_STRATEGY, Objects.nonNull(valueNameStrategy) ? valueNameStrategy : ProducerKeysHelper.TOPIC_NAME_STRATEGY_CONFLUENT); } generator = getLoadGenerator(jMeterVariables); diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryConstants.java b/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryConstants.java deleted file mode 100644 index d435b865..00000000 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryConstants.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.sngular.kloadgen.schemaregistry; - -public final class SchemaRegistryConstants { - - public static final String SCHEMA_REGISTRY_CONFLUENT = "CONFLUENT"; - - public static final String SCHEMA_REGISTRY_APICURIO = "APICURIO"; - - public static final String BASIC_AUTH_CREDENTIALS = "BASIC_AUTH_CREDENTIALS"; - public static final String USER_INFO_CONFIG = "USER_INFO_CONFIG"; - public static final String BEARER_AUTH_CREDENTIALS = "BEARER_AUTH_CREDENTIALS"; - public static final String BEARER_AUTH_TOKEN_CONFIG = "BEARER_AUTH_TOKEN_CONFIG"; -} diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryManagerFactory.java b/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryManagerFactory.java index f6a28875..ddd75acf 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryManagerFactory.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/SchemaRegistryManagerFactory.java @@ -1,8 +1,5 @@ package com.sngular.kloadgen.schemaregistry; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.APICURIO; -import static com.sngular.kloadgen.common.SchemaRegistryEnum.CONFLUENT; - import java.util.Map; import com.sngular.kloadgen.common.SchemaRegistryEnum; @@ -14,12 +11,16 @@ @Slf4j public class SchemaRegistryManagerFactory { - static Map schemaRegistryMap = Map.of(CONFLUENT, new ConfluentSchemaRegistry(), APICURIO, new ApicurioSchemaRegistry()); + private static final Map SCHEMA_REGISTRY_MAP = + Map.of(SchemaRegistryEnum.CONFLUENT, new ConfluentSchemaRegistry(), SchemaRegistryEnum.APICURIO, new ApicurioSchemaRegistry()); + + protected SchemaRegistryManagerFactory() { + } - public static SchemaRegistryAdapter getSchemaRegistry(String registry) { + public static SchemaRegistryAdapter getSchemaRegistry(final String registry) { try { - SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registry.toUpperCase()); - return schemaRegistryMap.get(schemaRegistryEnum); + final SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registry.toUpperCase()); + return SCHEMA_REGISTRY_MAP.get(schemaRegistryEnum); } catch (final IllegalArgumentException e) { final String logMsg = "Can not parse the registry " + registry; log.error(logMsg, e); diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/AbstractParsedSchemaAdapter.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/AbstractParsedSchemaAdapter.java new file mode 100644 index 00000000..527f6ee4 --- /dev/null +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/AbstractParsedSchemaAdapter.java @@ -0,0 +1,8 @@ +package com.sngular.kloadgen.schemaregistry.adapter.impl; + +public abstract class AbstractParsedSchemaAdapter { + + public abstract T getType(); + + public abstract T getRawSchema(); +} diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioParsedSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioAbstractParsedSchemaMetadata.java similarity index 58% rename from src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioParsedSchemaMetadata.java rename to src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioAbstractParsedSchemaMetadata.java index 72ef8f66..f18cdb15 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioParsedSchemaMetadata.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioAbstractParsedSchemaMetadata.java @@ -8,7 +8,7 @@ @Getter @Setter @NoArgsConstructor -public class ApicurioParsedSchemaMetadata extends ParsedSchemaAdapter { +public class ApicurioAbstractParsedSchemaMetadata extends AbstractParsedSchemaAdapter { private Object schema; @@ -16,12 +16,13 @@ public class ApicurioParsedSchemaMetadata extends ParsedSchemaAdapter { private String type; - public ApicurioParsedSchemaMetadata(ParsedSchema parsedSchema){ + public ApicurioAbstractParsedSchemaMetadata(final ParsedSchema parsedSchema) { this.rawSchema = parsedSchema.canonicalString(); this.type = parsedSchema.schemaType(); this.schema = parsedSchema.rawSchema(); } - public static ParsedSchemaAdapter parse(final ParsedSchema parsedSchema) { - return new ApicurioParsedSchemaMetadata(parsedSchema); + + public static AbstractParsedSchemaAdapter parse(final ParsedSchema parsedSchema) { + return new ApicurioAbstractParsedSchemaMetadata(parsedSchema); } } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioSchemaMetadata.java index 78c4fccf..3107cf79 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioSchemaMetadata.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ApicurioSchemaMetadata.java @@ -61,13 +61,13 @@ public ApicurioSchemaMetadata(final ArtifactMetaData artifactMetaData) { } @Override - public String getType() { + public final String getType() { return this.schemaType; } @Override - public List getReferences() { - return Collections.emptyList(); + public final List getReferences() { + return Collections.emptyList(); } } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseParsedSchema.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseParsedSchema.java index 55df89e6..407c1339 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseParsedSchema.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseParsedSchema.java @@ -2,7 +2,7 @@ import org.apache.avro.Schema.Parser; -public class BaseParsedSchema extends Parser { +public class BaseParsedSchema extends Parser { private final T parsedSchemaAdapter; @@ -10,7 +10,7 @@ public BaseParsedSchema(final T parsedSchemaAdapter) { this.parsedSchemaAdapter = parsedSchemaAdapter; } - public T getParsedSchemaAdapter(){ + public final T getParsedSchemaAdapter() { return parsedSchemaAdapter; } } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseSchemaMetadata.java index 04e84d1d..f4c04009 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseSchemaMetadata.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/BaseSchemaMetadata.java @@ -1,9 +1,11 @@ package com.sngular.kloadgen.schemaregistry.adapter.impl; import lombok.Builder; +import lombok.Getter; +@Getter @Builder -public class BaseSchemaMetadata { +public final class BaseSchemaMetadata { private final T schemaMetadataAdapter; @@ -11,7 +13,4 @@ public BaseSchemaMetadata(final T schemaMetadataAdapter) { this.schemaMetadataAdapter = schemaMetadataAdapter; } - public T getSchemaMetadataAdapter() { - return schemaMetadataAdapter; - } } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentAbstractParsedSchemaMetadata.java similarity index 53% rename from src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java rename to src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentAbstractParsedSchemaMetadata.java index a24ea866..42e1f5e3 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentAbstractParsedSchemaMetadata.java @@ -6,7 +6,7 @@ import org.apache.avro.Schema; @Getter -public class ConfluentParsedSchemaMetadata extends ParsedSchemaAdapter { +public final class ConfluentAbstractParsedSchemaMetadata extends AbstractParsedSchemaAdapter { private String schemaType; @@ -14,40 +14,41 @@ public class ConfluentParsedSchemaMetadata extends ParsedSchemaAdapter { private String canonicalString; - private Object rawSchema; + private Object rawSchema; - private ConfluentParsedSchemaMetadata(ParsedSchema parsedSchema){ + private ConfluentAbstractParsedSchemaMetadata(final ParsedSchema parsedSchema) { this.schemaType = parsedSchema.schemaType(); this.name = parsedSchema.name(); this.canonicalString = parsedSchema.canonicalString(); this.rawSchema = parsedSchema.rawSchema(); } - private ConfluentParsedSchemaMetadata(Schema schema){ + private ConfluentAbstractParsedSchemaMetadata(final Schema schema) { this.schemaType = schema.getType().getName(); this.name = schema.getName(); } - public ConfluentParsedSchemaMetadata(ProtobufSchema schema){ + public ConfluentAbstractParsedSchemaMetadata(final ProtobufSchema schema) { this.schemaType = schema.schemaType(); this.name = schema.name(); this.rawSchema = schema.rawSchema(); this.canonicalString = schema.canonicalString(); } - public ConfluentParsedSchemaMetadata() { + public ConfluentAbstractParsedSchemaMetadata() { } - public static ParsedSchemaAdapter parse(final ParsedSchema parsedSchema) { - return new ConfluentParsedSchemaMetadata(parsedSchema); + public static AbstractParsedSchemaAdapter parse(final ParsedSchema parsedSchema) { + return new ConfluentAbstractParsedSchemaMetadata(parsedSchema); } - public static ParsedSchemaAdapter parse(final Schema schema) { - return new ConfluentParsedSchemaMetadata(schema); + public static AbstractParsedSchemaAdapter parse(final Schema schema) { + return new ConfluentAbstractParsedSchemaMetadata(schema); } - public static ParsedSchemaAdapter parse (final ProtobufSchema schema){ - return new ConfluentParsedSchemaMetadata(schema); + + public static AbstractParsedSchemaAdapter parse(final ProtobufSchema schema) { + return new ConfluentAbstractParsedSchemaMetadata(schema); } @Override diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentSchemaMetadata.java index 4af84f87..db16a217 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentSchemaMetadata.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentSchemaMetadata.java @@ -9,17 +9,17 @@ @Getter public class ConfluentSchemaMetadata implements SchemaMetadataAdapter { - private Integer id; + private final Integer id; - private Integer version; + private final Integer version; - private String schemaType; + private final String schemaType; - private String schema; + private final String schema; - private List references; + private final List references; - private ConfluentSchemaMetadata(SchemaMetadata schemaMetadata) { + private ConfluentSchemaMetadata(final SchemaMetadata schemaMetadata) { this.id = schemaMetadata.getId(); this.version = schemaMetadata.getVersion(); this.schemaType = schemaMetadata.getSchemaType(); @@ -27,17 +27,17 @@ private ConfluentSchemaMetadata(SchemaMetadata schemaMetadata) { this.references = schemaMetadata.getReferences(); } - public static ConfluentSchemaMetadata parse(SchemaMetadata schemaMetadata) { + public static ConfluentSchemaMetadata parse(final SchemaMetadata schemaMetadata) { return new ConfluentSchemaMetadata(schemaMetadata); } @Override - public Integer getGlobalId() { + public final Integer getGlobalId() { return this.getId(); } @Override - public String getType() { + public final String getType() { return this.schemaType; } } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistry.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistry.java index 2ad7c68b..b8bbaca5 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistry.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistry.java @@ -11,9 +11,10 @@ @Setter @AllArgsConstructor @NoArgsConstructor -public class GenericSchemaRegistry{ +public class GenericSchemaRegistry { private GenericSchemaRegistryAdapter genericSchemaRegistryAdapter; + private T id; private T version; diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistryAdapter.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistryAdapter.java index b1ef073a..c04b8c6d 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistryAdapter.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/GenericSchemaRegistryAdapter.java @@ -1,11 +1,12 @@ package com.sngular.kloadgen.schemaregistry.adapter.impl; -public interface GenericSchemaRegistryAdapter { - T getId(); +public interface GenericSchemaRegistryAdapter { - T getVersion(); + T getId(); - U getSchemaType(); + T getVersion(); + + U getSchemaType(); } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ParsedSchemaAdapter.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ParsedSchemaAdapter.java deleted file mode 100644 index 2d1848b9..00000000 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ParsedSchemaAdapter.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.sngular.kloadgen.schemaregistry.adapter.impl; - -public abstract class ParsedSchemaAdapter { - public abstract T getType(); - - public abstract T getRawSchema(); -} diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ApicurioSchemaRegistry.java b/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ApicurioSchemaRegistry.java index a0583868..ad990e80 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ApicurioSchemaRegistry.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ApicurioSchemaRegistry.java @@ -13,14 +13,11 @@ import com.sngular.kloadgen.common.SchemaTypeEnum; import com.sngular.kloadgen.exception.KLoadGenException; import com.sngular.kloadgen.schemaregistry.SchemaRegistryAdapter; -import com.sngular.kloadgen.schemaregistry.SchemaRegistryConstants; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioParsedSchemaMetadata; +import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioAbstractParsedSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.SchemaMetadataAdapter; -import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import io.apicurio.registry.resolver.SchemaParser; import io.apicurio.registry.rest.client.RegistryClient; import io.apicurio.registry.rest.client.RegistryClientFactory; @@ -38,30 +35,25 @@ public final class ApicurioSchemaRegistry implements SchemaRegistryAdapter { - private static final Map propertiesMap = Map.of(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryConstants.SCHEMA_REGISTRY_APICURIO, - SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL_KEY, SerdeConfig.REGISTRY_URL); - private RegistryClient schemaRegistryClient; - private ApicurioParsedSchemaMetadata apicurioParsedSchemaMetadata; - - public final String getSchemaRegistryUrlKey() { + public String getSchemaRegistryUrlKey() { return SerdeConfig.REGISTRY_URL; } @Override - public final void setSchemaRegistryClient(final String url, final Map properties) { + public void setSchemaRegistryClient(final String url, final Map properties) { this.schemaRegistryClient = RegistryClientFactory.create(url); } @Override - public final void setSchemaRegistryClient(final Map properties) { + public void setSchemaRegistryClient(final Map properties) { final String url = Objects.toString(properties.get(this.getSchemaRegistryUrlKey()), ""); this.schemaRegistryClient = RegistryClientFactory.create(url); } @Override - public final Collection getAllSubjects() throws KLoadGenException { + public Collection getAllSubjects() throws KLoadGenException { final Collection subjects = new ArrayList<>(); try { final List artifacts = this.schemaRegistryClient.searchArtifacts(null, null, null, @@ -77,7 +69,7 @@ public final Collection getAllSubjects() throws KLoadGenException { } @Override - public final BaseSchemaMetadata getLatestSchemaMetadata(final String artifactId) throws KLoadGenException { + public BaseSchemaMetadata getLatestSchemaMetadata(final String artifactId) throws KLoadGenException { try { final SearchedArtifact searchedArtifact = getLastestSearchedArtifact(artifactId); final ArtifactMetaData artifactMetaData = this.schemaRegistryClient.getArtifactMetaData(searchedArtifact.getGroupId(), searchedArtifact.getId()); @@ -88,8 +80,8 @@ public final BaseSchemaMetadata getLatestSchemaMetadata( } @Override - public final BaseParsedSchema getSchemaBySubject(final String artifactId) { - final ApicurioParsedSchemaMetadata schema = new ApicurioParsedSchemaMetadata(); + public BaseParsedSchema getSchemaBySubject(final String artifactId) { + final ApicurioAbstractParsedSchemaMetadata schema = new ApicurioAbstractParsedSchemaMetadata(); try { final SearchedArtifact searchedArtifact = getLastestSearchedArtifact(artifactId); final InputStream inputStream = this.schemaRegistryClient.getLatestArtifact(searchedArtifact.getGroupId(), searchedArtifact.getId()); @@ -102,7 +94,7 @@ public final BaseParsedSchema getSchemaBySubject(f } } - private static void setSchemaBySchemaType(final ApicurioParsedSchemaMetadata schema, final byte[] result, final String searchedArtifactType) { + private static void setSchemaBySchemaType(final ApicurioAbstractParsedSchemaMetadata schema, final byte[] result, final String searchedArtifactType) { switch (SchemaTypeEnum.valueOf(searchedArtifactType)) { case AVRO: @@ -124,9 +116,9 @@ private static void setSchemaBySchemaType(final ApicurioParsedSchemaMetadata sch } @Override - public final BaseParsedSchema getSchemaBySubjectAndId( + public final BaseParsedSchema getSchemaBySubjectAndId( final String subjectName, final BaseSchemaMetadata metadata) { - final ApicurioParsedSchemaMetadata schema = new ApicurioParsedSchemaMetadata(); + final ApicurioAbstractParsedSchemaMetadata schema = new ApicurioAbstractParsedSchemaMetadata(); final SchemaMetadataAdapter schemaMetadataAdapter = metadata.getSchemaMetadataAdapter(); try { diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ConfluentSchemaRegistry.java b/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ConfluentSchemaRegistry.java index 3770815c..3204aca1 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ConfluentSchemaRegistry.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/impl/ConfluentSchemaRegistry.java @@ -8,14 +8,12 @@ import com.sngular.kloadgen.exception.KLoadGenException; import com.sngular.kloadgen.schemaregistry.SchemaRegistryAdapter; -import com.sngular.kloadgen.schemaregistry.SchemaRegistryConstants; +import com.sngular.kloadgen.schemaregistry.adapter.impl.AbstractParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentParsedSchemaMetadata; +import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentAbstractParsedSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentSchemaMetadata; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter; import com.sngular.kloadgen.schemaregistry.adapter.impl.SchemaMetadataAdapter; -import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import io.confluent.kafka.schemaregistry.ParsedSchema; import io.confluent.kafka.schemaregistry.avro.AvroSchemaProvider; import io.confluent.kafka.schemaregistry.client.CachedSchemaRegistryClient; @@ -24,23 +22,14 @@ import io.confluent.kafka.schemaregistry.json.JsonSchemaProvider; import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchemaProvider; import io.confluent.kafka.serializers.AbstractKafkaSchemaSerDeConfig; +import lombok.extern.slf4j.Slf4j; import org.apache.jmeter.threads.JMeterContextService; -public class ConfluentSchemaRegistry implements SchemaRegistryAdapter { +@Slf4j +public final class ConfluentSchemaRegistry implements SchemaRegistryAdapter { private SchemaRegistryClient schemaRegistryClient; - private Map propertiesMap; - - public ConfluentSchemaRegistry() { - this.propertiesMap = Map.of(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryConstants.SCHEMA_REGISTRY_CONFLUENT, - SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL_KEY, AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG, - SchemaRegistryConstants.BASIC_AUTH_CREDENTIALS, AbstractKafkaSchemaSerDeConfig.BASIC_AUTH_CREDENTIALS_SOURCE, - SchemaRegistryConstants.USER_INFO_CONFIG, AbstractKafkaSchemaSerDeConfig.USER_INFO_CONFIG, - SchemaRegistryConstants.BEARER_AUTH_CREDENTIALS, AbstractKafkaSchemaSerDeConfig.BEARER_AUTH_CREDENTIALS_SOURCE, - SchemaRegistryConstants.BEARER_AUTH_TOKEN_CONFIG, AbstractKafkaSchemaSerDeConfig.BEARER_AUTH_TOKEN_CONFIG); - } - @Override public String getSchemaRegistryUrlKey() { return AbstractKafkaSchemaSerDeConfig.SCHEMA_REGISTRY_URL_CONFIG; @@ -48,12 +37,14 @@ public String getSchemaRegistryUrlKey() { @Override public void setSchemaRegistryClient(final String url, final Map properties) { + log.debug("CREATEION"); this.schemaRegistryClient = new CachedSchemaRegistryClient(List.of(checkPropertyOrVariable(url)), 1000, List.of(new AvroSchemaProvider(), new JsonSchemaProvider(), new ProtobufSchemaProvider()), properties); } @Override public void setSchemaRegistryClient(final Map properties) { + log.debug("CREATITIN"); final String url = properties.get(this.getSchemaRegistryUrlKey()).toString(); this.schemaRegistryClient = new CachedSchemaRegistryClient(List.of(checkPropertyOrVariable(url)), 1000, List.of(new AvroSchemaProvider(), new JsonSchemaProvider(), new ProtobufSchemaProvider()), properties); @@ -79,22 +70,23 @@ public BaseSchemaMetadata getLatestSchemaMetadata(final } } - public BaseParsedSchema getSchemaBySubject(final String subjectName) { + public BaseParsedSchema getSchemaBySubject(final String subjectName) { try { final ConfluentSchemaMetadata schemaMetadata = ConfluentSchemaMetadata.parse(this.schemaRegistryClient.getLatestSchemaMetadata(subjectName)); final ParsedSchema parsedSchema = this.schemaRegistryClient.getSchemaBySubjectAndId(subjectName, schemaMetadata.getId()); - final ParsedSchemaAdapter parsedSchemaAdapter = ConfluentParsedSchemaMetadata.parse(parsedSchema); - return new BaseParsedSchema(parsedSchemaAdapter); + final AbstractParsedSchemaAdapter abstractParsedSchemaAdapter = ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema); + return new BaseParsedSchema(abstractParsedSchemaAdapter); } catch (RestClientException | IOException e) { throw new KLoadGenException(e.getMessage()); } } - public BaseParsedSchema getSchemaBySubjectAndId(final String subjectName, BaseSchemaMetadata metadata) { + public BaseParsedSchema getSchemaBySubjectAndId(final String subjectName, + final BaseSchemaMetadata metadata) { try { final ParsedSchema parsedSchema = this.schemaRegistryClient.getSchemaBySubjectAndId(subjectName, metadata.getSchemaMetadataAdapter().getId()); - final ParsedSchemaAdapter parsedSchemaAdapter = ConfluentParsedSchemaMetadata.parse(parsedSchema); - return new BaseParsedSchema(parsedSchemaAdapter); + final AbstractParsedSchemaAdapter abstractParsedSchemaAdapter = ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema); + return new BaseParsedSchema(abstractParsedSchemaAdapter); } catch (RestClientException | IOException e) { throw new KLoadGenException(e.getMessage()); } diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/schema/KLoadSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/schema/KLoadSchemaMetadata.java deleted file mode 100644 index 37e1ed35..00000000 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/schema/KLoadSchemaMetadata.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.sngular.kloadgen.schemaregistry.schema; - -import lombok.Value; - -@Value -public class KLoadSchemaMetadata { - - String id; - - String version; - - String schemaType; - -} diff --git a/src/main/java/com/sngular/kloadgen/serializer/AvroDeserializer.java b/src/main/java/com/sngular/kloadgen/serializer/AvroDeserializer.java index 6ad9c9b7..1bc7c78b 100644 --- a/src/main/java/com/sngular/kloadgen/serializer/AvroDeserializer.java +++ b/src/main/java/com/sngular/kloadgen/serializer/AvroDeserializer.java @@ -54,7 +54,7 @@ public final Object deserialize(final String topic, final byte[] data) { final var buffer = getByteBuffer(data); final var reader = new GenericDatumReader<>(avroSchema); - final int length = buffer.limit() - 1-4; + final int length = buffer.limit() - 5; final int start = buffer.position() + buffer.arrayOffset(); try { diff --git a/src/main/java/com/sngular/kloadgen/serializer/AvroSerializer.java b/src/main/java/com/sngular/kloadgen/serializer/AvroSerializer.java index 8efc40ff..378cc67a 100644 --- a/src/main/java/com/sngular/kloadgen/serializer/AvroSerializer.java +++ b/src/main/java/com/sngular/kloadgen/serializer/AvroSerializer.java @@ -11,6 +11,7 @@ import java.nio.ByteBuffer; import javax.xml.bind.DatatypeConverter; + import lombok.extern.slf4j.Slf4j; import org.apache.avro.generic.GenericDatumWriter; import org.apache.avro.generic.GenericRecord; diff --git a/src/main/java/com/sngular/kloadgen/serializer/ProtobufSerializer.java b/src/main/java/com/sngular/kloadgen/serializer/ProtobufSerializer.java index e3d9a65e..18a264cd 100644 --- a/src/main/java/com/sngular/kloadgen/serializer/ProtobufSerializer.java +++ b/src/main/java/com/sngular/kloadgen/serializer/ProtobufSerializer.java @@ -9,12 +9,14 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.nio.ByteBuffer; +import java.util.Arrays; + +import javax.xml.bind.DatatypeConverter; import com.google.protobuf.Descriptors.Descriptor; import com.google.protobuf.DynamicMessage; import io.confluent.kafka.schemaregistry.protobuf.MessageIndexes; import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema; -import javax.xml.bind.DatatypeConverter; import lombok.extern.slf4j.Slf4j; import org.apache.kafka.common.errors.SerializationException; import org.apache.kafka.common.header.Headers; @@ -37,7 +39,7 @@ public final byte[] serialize(final String topic, final T data) { final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); byteArrayOutputStream.write(MAGIC_BYTE); - byteArrayOutputStream.write(ByteBuffer.allocate(ID_SIZE).put(data.getSchemaMetadata().getId().toString().getBytes()).array()); + byteArrayOutputStream.write(ByteBuffer.allocate(ID_SIZE).put(Arrays.copyOfRange(data.getSchemaMetadata().getId().toString().getBytes(), 0, ID_SIZE)).array()); final Descriptor descriptor = ((DynamicMessage) data.getGenericRecord()).getDescriptorForType(); final ProtobufSchema schema = new ProtobufSchema(descriptor); final MessageIndexes indexes = schema.toMessageIndexes(descriptor.getFullName()); diff --git a/src/main/java/com/sngular/kloadgen/util/AutoCompletion.java b/src/main/java/com/sngular/kloadgen/util/AutoCompletion.java index aafeecdf..505c59a6 100644 --- a/src/main/java/com/sngular/kloadgen/util/AutoCompletion.java +++ b/src/main/java/com/sngular/kloadgen/util/AutoCompletion.java @@ -13,7 +13,6 @@ import java.awt.event.KeyListener; import java.util.Objects; -import com.sngular.kloadgen.exception.KLoadGenException; import javax.swing.ComboBoxEditor; import javax.swing.ComboBoxModel; import javax.swing.JComboBox; @@ -24,6 +23,8 @@ import javax.swing.text.JTextComponent; import javax.swing.text.PlainDocument; +import com.sngular.kloadgen.exception.KLoadGenException; + public class AutoCompletion extends PlainDocument { private final JComboBox comboBox; diff --git a/src/main/java/com/sngular/kloadgen/util/JMeterHelper.java b/src/main/java/com/sngular/kloadgen/util/JMeterHelper.java index b40592c6..ba029b23 100644 --- a/src/main/java/com/sngular/kloadgen/util/JMeterHelper.java +++ b/src/main/java/com/sngular/kloadgen/util/JMeterHelper.java @@ -12,10 +12,12 @@ import com.sngular.kloadgen.schemaregistry.SchemaRegistryAdapter; import com.sngular.kloadgen.schemaregistry.SchemaRegistryManagerFactory; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema; +import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.threads.JMeterContextService; @SuppressWarnings("checkstyle:AbbreviationAsWordInName") +@Slf4j public final class JMeterHelper { private JMeterHelper() { diff --git a/src/test/java/com/sngular/kloadgen/config/kafkaheaders/KafkaHeadersConfigElementBeanInfoTest.java b/src/test/java/com/sngular/kloadgen/config/kafkaheaders/KafkaHeadersConfigElementBeanInfoTest.java index f2e58370..cc7579d2 100644 --- a/src/test/java/com/sngular/kloadgen/config/kafkaheaders/KafkaHeadersConfigElementBeanInfoTest.java +++ b/src/test/java/com/sngular/kloadgen/config/kafkaheaders/KafkaHeadersConfigElementBeanInfoTest.java @@ -29,7 +29,7 @@ public void setUp() { @Test final void shouldGenerateElements() { final PropertyDescriptor[] propertyDescriptors = kafkaHeadersConfigElementBeanInfo.getPropertyDescriptors(); - Assertions.assertThat(propertyDescriptors).hasSize(1); + Assertions.assertThat(propertyDescriptors).hasSize(3); Assertions.assertThat(propertyDescriptors[0].getName()).isEqualTo(KAFKA_HEADERS); } } \ No newline at end of file diff --git a/src/test/java/com/sngular/kloadgen/config/keyfileserialized/KeyFileSerializedConfigElementBeanInfoTest.java b/src/test/java/com/sngular/kloadgen/config/keyfileserialized/KeyFileSerializedConfigElementBeanInfoTest.java index 4aec184b..e64b7005 100644 --- a/src/test/java/com/sngular/kloadgen/config/keyfileserialized/KeyFileSerializedConfigElementBeanInfoTest.java +++ b/src/test/java/com/sngular/kloadgen/config/keyfileserialized/KeyFileSerializedConfigElementBeanInfoTest.java @@ -39,7 +39,7 @@ public void setUp() { @Test void shouldGenerateElements() { final PropertyDescriptor[] propertyDescriptors = keyFileSerializedConfigElementBeanInfo.getPropertyDescriptors(); - Assertions.assertThat(propertyDescriptors).hasSize(6); + Assertions.assertThat(propertyDescriptors).hasSize(8); Assertions.assertThat(propertyDescriptors[0].getName()).isEqualTo(KEY_NAME_STRATEGY); Assertions.assertThat(propertyDescriptors[1].getName()).isEqualTo(KEY_SCHEMA_DEFINITION); Assertions.assertThat(propertyDescriptors[2].getName()).isEqualTo(KEY_SCHEMA_PROPERTIES); diff --git a/src/test/java/com/sngular/kloadgen/config/keyserialized/KeySerializedConfigElementBeanInfoTest.java b/src/test/java/com/sngular/kloadgen/config/keyserialized/KeySerializedConfigElementBeanInfoTest.java index c57ad23f..f5433c59 100644 --- a/src/test/java/com/sngular/kloadgen/config/keyserialized/KeySerializedConfigElementBeanInfoTest.java +++ b/src/test/java/com/sngular/kloadgen/config/keyserialized/KeySerializedConfigElementBeanInfoTest.java @@ -37,7 +37,7 @@ public void setUp() { @Test void shouldGenerateElements() { final PropertyDescriptor[] propertyDescriptors = keySerializedConfigElementBeanInfo.getPropertyDescriptors(); - Assertions.assertThat(propertyDescriptors).hasSize(5); + Assertions.assertThat(propertyDescriptors).hasSize(7); Assertions.assertThat(propertyDescriptors[0].getName()).isEqualTo(KEY_NAME_STRATEGY); Assertions.assertThat(propertyDescriptors[1].getName()).isEqualTo(KEY_SCHEMA_PROPERTIES); Assertions.assertThat(propertyDescriptors[2].getName()).isEqualTo(KEY_SCHEMA_TYPE); diff --git a/src/test/java/com/sngular/kloadgen/config/valuefileserialized/ValueFileSerializedConfigElementBeanInfoTest.java b/src/test/java/com/sngular/kloadgen/config/valuefileserialized/ValueFileSerializedConfigElementBeanInfoTest.java index 1346ec90..73e52752 100644 --- a/src/test/java/com/sngular/kloadgen/config/valuefileserialized/ValueFileSerializedConfigElementBeanInfoTest.java +++ b/src/test/java/com/sngular/kloadgen/config/valuefileserialized/ValueFileSerializedConfigElementBeanInfoTest.java @@ -39,12 +39,12 @@ public void setUp() { @Test void shouldGenerateElements() { final PropertyDescriptor[] propertyDescriptors = valueFileSerializedConfigElementBeanInfo.getPropertyDescriptors(); - Assertions.assertThat(propertyDescriptors).hasSize(6); - Assertions.assertThat(propertyDescriptors[0].getName()).isEqualTo(VALUE_NAME_STRATEGY); - Assertions.assertThat(propertyDescriptors[1].getName()).isEqualTo(VALUE_SCHEMA_DEFINITION); - Assertions.assertThat(propertyDescriptors[2].getName()).isEqualTo(VALUE_SCHEMA_PROPERTIES); - Assertions.assertThat(propertyDescriptors[3].getName()).isEqualTo(VALUE_SCHEMA_TYPE); - Assertions.assertThat(propertyDescriptors[4].getName()).isEqualTo(SERIALIZER_PROPERTY); - Assertions.assertThat(propertyDescriptors[5].getName()).isEqualTo(VALUE_SUBJECT_NAME); + Assertions.assertThat(propertyDescriptors).hasSize(8); + Assertions.assertThat(propertyDescriptors[2].getName()).isEqualTo(VALUE_NAME_STRATEGY); + Assertions.assertThat(propertyDescriptors[3].getName()).isEqualTo(VALUE_SCHEMA_DEFINITION); + Assertions.assertThat(propertyDescriptors[4].getName()).isEqualTo(VALUE_SCHEMA_PROPERTIES); + Assertions.assertThat(propertyDescriptors[5].getName()).isEqualTo(VALUE_SCHEMA_TYPE); + Assertions.assertThat(propertyDescriptors[6].getName()).isEqualTo(SERIALIZER_PROPERTY); + Assertions.assertThat(propertyDescriptors[7].getName()).isEqualTo(VALUE_SUBJECT_NAME); } } \ No newline at end of file diff --git a/src/test/java/com/sngular/kloadgen/config/valueserialized/ValueSerializedConfigElementBeanInfoTest.java b/src/test/java/com/sngular/kloadgen/config/valueserialized/ValueSerializedConfigElementBeanInfoTest.java index 6bd40d5d..e4477b9e 100644 --- a/src/test/java/com/sngular/kloadgen/config/valueserialized/ValueSerializedConfigElementBeanInfoTest.java +++ b/src/test/java/com/sngular/kloadgen/config/valueserialized/ValueSerializedConfigElementBeanInfoTest.java @@ -36,11 +36,11 @@ public void setUp() { @Test void shouldGenerateElements() { final var propertyDescriptors = valueSerializedConfigElementBeanInfo.getPropertyDescriptors(); - Assertions.assertThat(propertyDescriptors).hasSize(5); - Assertions.assertThat(propertyDescriptors[0].getName()).isEqualTo(VALUE_NAME_STRATEGY); - Assertions.assertThat(propertyDescriptors[1].getName()).isEqualTo(VALUE_SCHEMA_PROPERTIES); - Assertions.assertThat(propertyDescriptors[2].getName()).isEqualTo(VALUE_SCHEMA_TYPE); - Assertions.assertThat(propertyDescriptors[3].getName()).isEqualTo(VALUE_SERIALIZER_CONFIGURATION); - Assertions.assertThat(propertyDescriptors[4].getName()).isEqualTo(VALUE_SUBJECT_NAME); + Assertions.assertThat(propertyDescriptors).hasSize(7); + Assertions.assertThat(propertyDescriptors[2].getName()).isEqualTo(VALUE_NAME_STRATEGY); + Assertions.assertThat(propertyDescriptors[3].getName()).isEqualTo(VALUE_SCHEMA_PROPERTIES); + Assertions.assertThat(propertyDescriptors[4].getName()).isEqualTo(VALUE_SCHEMA_TYPE); + Assertions.assertThat(propertyDescriptors[5].getName()).isEqualTo(VALUE_SERIALIZER_CONFIGURATION); + Assertions.assertThat(propertyDescriptors[6].getName()).isEqualTo(VALUE_SUBJECT_NAME); } } \ No newline at end of file diff --git a/src/test/java/com/sngular/kloadgen/extractor/SchemaExtractorTest.java b/src/test/java/com/sngular/kloadgen/extractor/SchemaExtractorTest.java index 6e940a85..9f62af25 100644 --- a/src/test/java/com/sngular/kloadgen/extractor/SchemaExtractorTest.java +++ b/src/test/java/com/sngular/kloadgen/extractor/SchemaExtractorTest.java @@ -13,16 +13,15 @@ import com.sngular.kloadgen.common.SchemaRegistryEnum; import com.sngular.kloadgen.exception.KLoadGenException; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioParsedSchemaMetadata; +import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioAbstractParsedSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema; -import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentParsedSchemaMetadata; +import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentAbstractParsedSchemaMetadata; import com.sngular.kloadgen.testutil.FileHelper; import com.sngular.kloadgen.testutil.ParsedSchemaUtil; import com.sngular.kloadgen.testutil.SchemaParseUtil; import com.sngular.kloadgen.util.JMeterHelper; import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import io.confluent.kafka.schemaregistry.ParsedSchema; -import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException; import org.apache.jmeter.threads.JMeterContext; import org.apache.jmeter.threads.JMeterContextService; import org.apache.jmeter.threads.JMeterVariables; @@ -45,9 +44,9 @@ class SchemaExtractorTest { private final Properties properties = new Properties(); - private MockedStatic jMeterHelperMockedStatic; + private MockedStatic jmeterHelperMockedStatic; - private MockedStatic jMeterContextServiceMockedStatic; + private MockedStatic jmeterContextServiceMockedStatic; @BeforeEach void setUp() { @@ -57,28 +56,28 @@ void setUp() { final JMeterContext jmcx = JMeterContextService.getContext(); jmcx.setVariables(new JMeterVariables()); JMeterUtils.setLocale(Locale.ENGLISH); - jMeterHelperMockedStatic = Mockito.mockStatic(JMeterHelper.class); - jMeterContextServiceMockedStatic = Mockito.mockStatic(JMeterContextService.class, Answers.RETURNS_DEEP_STUBS); + jmeterHelperMockedStatic = Mockito.mockStatic(JMeterHelper.class); + jmeterContextServiceMockedStatic = Mockito.mockStatic(JMeterContextService.class, Answers.RETURNS_DEEP_STUBS); } @AfterEach void tearDown() { - jMeterHelperMockedStatic.close(); - jMeterContextServiceMockedStatic.close(); + jmeterHelperMockedStatic.close(); + jmeterContextServiceMockedStatic.close(); properties.clear(); } @Test @DisplayName("Test flatPropertiesList with AVRO") - void testFlatPropertiesListWithAVRO() throws IOException, RestClientException { + void testFlatPropertiesListWithAVRO() throws IOException { final File testFile = fileHelper.getFile("/avro-files/embedded-avros-example-test.avsc"); properties.setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryEnum.APICURIO.toString()); final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "AVRO"); - final var baseParsedSchema = new BaseParsedSchema<>(ApicurioParsedSchemaMetadata.parse(parsedSchema)); - jMeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); - jMeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); + final var baseParsedSchema = new BaseParsedSchema<>(ApicurioAbstractParsedSchemaMetadata.parse(parsedSchema)); + jmeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); + jmeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); final var result = SchemaExtractor.flatPropertiesList("avroSubject"); Assertions.assertThat(result).isNotNull(); @@ -86,15 +85,15 @@ void testFlatPropertiesListWithAVRO() throws IOException, RestClientException { @Test @DisplayName("Test flatPropertiesList with Json") - void testFlatPropertiesListWithJson() throws IOException, RestClientException { + void testFlatPropertiesListWithJson() throws IOException { final File testFile = fileHelper.getFile("/jsonschema/basic.jcs"); properties.setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryEnum.CONFLUENT.toString()); final var parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "JSON"); - final var baseParsedSchema = new BaseParsedSchema<>(ConfluentParsedSchemaMetadata.parse(parsedSchema)); - jMeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); - jMeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); + final var baseParsedSchema = new BaseParsedSchema<>(ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema)); + jmeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); + jmeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); final var result = SchemaExtractor.flatPropertiesList("jsonSubject"); Assertions.assertThat(result).isNotNull(); @@ -102,30 +101,30 @@ void testFlatPropertiesListWithJson() throws IOException, RestClientException { @Test @DisplayName("Test flatPropertiesList with Protobuf") - void testFlatPropertiesListWithProtobuf() throws RestClientException, IOException { + void testFlatPropertiesListWithProtobuf() throws IOException { final File testFile = fileHelper.getFile("/proto-files/easyTest.proto"); properties.setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryEnum.CONFLUENT.toString()); final var parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "PROTOBUF"); - final var baseParsedSchema = new BaseParsedSchema<>(ConfluentParsedSchemaMetadata.parse(parsedSchema)); - jMeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); - jMeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); + final var baseParsedSchema = new BaseParsedSchema<>(ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema)); + jmeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); + jmeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); final var result = SchemaExtractor.flatPropertiesList("protobufSubject"); Assertions.assertThat(result).isNotNull(); } @Test @DisplayName("Test flatPropertiesList throws exception schema type not supported") - void testFlatPropertiesListWithException() throws IOException, RestClientException { + void testFlatPropertiesListWithException() { final var parsedSchema = new ParsedSchemaUtil(); - final var baseParsedSchema = new BaseParsedSchema<>(ConfluentParsedSchemaMetadata.parse(parsedSchema)); + final var baseParsedSchema = new BaseParsedSchema<>(ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema)); properties.setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryEnum.CONFLUENT.toString()); - jMeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); + jmeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties); - jMeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); + jmeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema); Assertions.assertThatExceptionOfType(KLoadGenException.class) .isThrownBy(() -> SchemaExtractor.flatPropertiesList("exceptionSubject") ).withMessage(String.format("Schema type not supported %s", parsedSchema.schemaType())); diff --git a/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroApicurioExtractorTest.java b/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroApicurioExtractorTest.java index 552feaca..387197f7 100644 --- a/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroApicurioExtractorTest.java +++ b/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroApicurioExtractorTest.java @@ -67,7 +67,6 @@ void testOptionalEnum() throws Exception { void testFlatPropertiesOptionalMapArray() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testOptionalMap.avsc"); - final List parsedSchema = avroApicurioExtractor.getSchemaNameList(testFile); final Schema schema = new Schema.Parser().parse(testFile); final List fieldValueMappingList = avroApicurioExtractor.processSchema(schema); @@ -97,7 +96,6 @@ void testFlatPropertiesOptionalMapArray() throws Exception { @DisplayName("Should extract Map of Record") void testFlatPropertiesMap() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testMap.avsc"); - final List parsedSchema = avroApicurioExtractor.getSchemaNameList(testFile); final Schema schema = new Schema.Parser().parse(testFile); final List fieldValueMappingList = avroApicurioExtractor.processSchema(schema); Assertions.assertThat(fieldValueMappingList) @@ -129,7 +127,6 @@ void testFlatPropertiesMap() throws Exception { void testFlatPropertiesLogicalTypes() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testLogicalTypes.avsc"); - final List parsedSchema = avroApicurioExtractor.getSchemaNameList(testFile); final Schema schema = new Schema.Parser().parse(testFile); final List fieldValueMappingList = avroApicurioExtractor.processSchema(schema); Assertions.assertThat(fieldValueMappingList) @@ -159,7 +156,6 @@ void testFlatPropertiesLogicalTypes() throws Exception { void testFlatPropertiesOptionalArray() throws Exception { final String testFile = fileHelper.getContent("/avro-files/issue.avsc"); - final List parsedSchema = avroApicurioExtractor.getSchemaNameList(testFile); final Schema schema = new Schema.Parser().parse(testFile); final List fieldValueMappingList = avroApicurioExtractor.processSchema(schema); Assertions.assertThat(fieldValueMappingList) @@ -179,7 +175,6 @@ void testFlatPropertiesOptionalArray() throws Exception { void testFlatPropertiesUnionRecordAvros() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testUnionRecord.avsc"); - final List parsedSchema = avroApicurioExtractor.getSchemaNameList(testFile); final Schema schema = new Schema.Parser().parse(testFile); final List fieldValueMappingList = avroApicurioExtractor.processSchema(schema); @@ -210,7 +205,6 @@ void testFlatPropertiesUnionRecordAvros() throws Exception { void testFlatPropertiesRecordUnionReverseOrder() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testUnionReverseOrder.avsc"); - final List parsedSchema = avroApicurioExtractor.getSchemaNameList(testFile); final Schema schema = new Schema.Parser().parse(testFile); final List fieldValueMappingList = avroApicurioExtractor.processSchema(schema); diff --git a/src/test/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultTest.java b/src/test/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultTest.java index 632d8919..b4f0898e 100644 --- a/src/test/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultTest.java +++ b/src/test/java/com/sngular/kloadgen/extractor/extractors/json/JsonDefaultTest.java @@ -6,7 +6,6 @@ import com.sngular.kloadgen.exception.KLoadGenException; import com.sngular.kloadgen.extractor.extractors.Extractor; -import com.sngular.kloadgen.extractor.parser.impl.JSONSchemaParser; import com.sngular.kloadgen.model.ConstraintTypeEnum; import com.sngular.kloadgen.model.FieldValueMapping; import com.sngular.kloadgen.testutil.FileHelper; @@ -23,8 +22,6 @@ class JsonDefaultTest { private final Extractor jsonDefaultExtractor = new JsonDefaultExtractor(); - public final JSONSchemaParser jsonSchemaParser = new JSONSchemaParser(); - @Test @DisplayName("Should extract basic types") void testBasic() throws Exception { @@ -40,8 +37,7 @@ void testBasic() throws Exception { .containsExactlyInAnyOrder( FieldValueMapping.builder().fieldName("firstName").fieldType("string").constraints(constraints).required(false).isAncestorRequired(false).build(), FieldValueMapping.builder().fieldName("lastName").fieldType("string").constraints(constraints).required(true).isAncestorRequired(false).build(), - FieldValueMapping.builder().fieldName("age").fieldType("number").required(true).isAncestorRequired(false).build() - ); + FieldValueMapping.builder().fieldName("age").fieldType("number").required(true).isAncestorRequired(false).build()); } @Test @@ -59,8 +55,7 @@ void testBasicArray() throws Exception { .containsExactlyInAnyOrder( FieldValueMapping.builder().fieldName("fruits[]").fieldType("string-array").required(true).isAncestorRequired(false).build(), FieldValueMapping.builder().fieldName("vegetables[].veggieName").fieldType("string").constraints(constraints).required(true).isAncestorRequired(true).build(), - FieldValueMapping.builder().fieldName("vegetables[].veggieLike").fieldType("boolean").required(true).isAncestorRequired(true).build() - ); + FieldValueMapping.builder().fieldName("vegetables[].veggieLike").fieldType("boolean").required(true).isAncestorRequired(true).build()); } @Test @@ -84,11 +79,9 @@ void testBasicNumber() throws Exception { .hasSize(2) .containsExactlyInAnyOrder( FieldValueMapping.builder().fieldName("latitude").fieldType("number").constraints(constraintsLatitude).required(true).isAncestorRequired(false).build(), - FieldValueMapping.builder().fieldName("longitude").fieldType("number").constraints(constraintsLongitude).required(true).isAncestorRequired(false).build() - ); + FieldValueMapping.builder().fieldName("longitude").fieldType("number").constraints(constraintsLongitude).required(true).isAncestorRequired(false).build()); } - @Test @DisplayName("Should extract optional collections and optional collections inside objects") void testFlatPropertiesOptionalCollections() throws Exception { @@ -123,8 +116,7 @@ void testFlatPropertiesOptionalCollections() throws Exception { FieldValueMapping.builder().fieldName("objectOfCollectionsOfObject.mapOfObjectsDog[:].vetData.dogId").fieldType("number").required(false).isAncestorRequired(true) .build(), FieldValueMapping.builder().fieldName("objectOfCollectionsOfObject.mapOfObjectsDog[:].vetData.breedName").fieldType("string").constraints(constraints) - .required(false).isAncestorRequired(true).build() - ); + .required(false).isAncestorRequired(true).build()); } @Test @@ -152,8 +144,7 @@ void testComplexDefinitions() throws Exception { FieldValueMapping.builder().fieldName("mapOfMaps[:][:].stringControlObject").fieldType("string").constraints(constraints).required(true).isAncestorRequired(true) .build(), FieldValueMapping.builder().fieldName("mapOfMaps[:][:].arrayOfArraysOfStrings[][]").fieldType("string-array-array").required(false).isAncestorRequired(true) - .build() - ); + .build()); } @Test @@ -177,8 +168,7 @@ void testRequiredPropagationChildrenFields() throws Exception { FieldValueMapping.builder().fieldName("geopoliticalSubdivisions.level2.code").fieldType("string").constraints(constraintsCode).required(false) .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("geopoliticalSubdivisions.level2.freeForm").fieldType("string").constraints(constraintsFreeForm).required(false) - .isAncestorRequired(true).build() - ); + .isAncestorRequired(true).build()); } @Test @@ -196,8 +186,7 @@ void testShouldExtractJsonSchemaDefinitions() throws Exception { .contains( FieldValueMapping.builder().fieldName("duty.amount.value").fieldType("number").required(false).isAncestorRequired(false).build(), FieldValueMapping.builder().fieldName("duty.amount.currency").fieldType("string").constraints(constraints).required(false).isAncestorRequired(false).build(), - FieldValueMapping.builder().fieldName("duty.amount.exponent").fieldType("number").required(false).isAncestorRequired(false).build() - ); + FieldValueMapping.builder().fieldName("duty.amount.exponent").fieldType("number").required(false).isAncestorRequired(false).build()); } @Test @@ -212,8 +201,7 @@ void testMultipleType() throws Exception { Assertions.assertThat(fieldValueMappingList) .hasSize(1) .satisfiesExactly( - fieldValueMapping -> Set.of("number", "uuid").contains(fieldValueMapping.getFieldType()) - ); + fieldValueMapping -> Set.of("number", "uuid").contains(fieldValueMapping.getFieldType())); } @Test @@ -241,8 +229,7 @@ void testFlatPropertiesOptionalNestedCollections() throws Exception { FieldValueMapping.builder().fieldName("mapOfObjectsOfCollections[:].arrayOfMapsOfObject[][:].stringControl").fieldType("string").constraints(constraints) .required(false).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("mapOfObjectsOfCollections[:].arrayOfMapsOfObject[][:].numberControl").fieldType("number").required(false) - .isAncestorRequired(true).build() - ); + .isAncestorRequired(true).build()); } @Test diff --git a/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufApicurioExtractorTest.java b/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufApicurioExtractorTest.java index de8c10cf..8f35d767 100644 --- a/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufApicurioExtractorTest.java +++ b/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufApicurioExtractorTest.java @@ -25,7 +25,7 @@ class ProtobufApicurioExtractorTest { private final Extractor protoBufApicurioExtractor = new ProtoBufApicurioExtractor(); - private Location location = Location.get("",""); + private final Location location = Location.get("", ""); @BeforeEach public void setUp() { diff --git a/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufConfluentExtractorTest.java b/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufConfluentExtractorTest.java index 6f10e056..6f46ca64 100644 --- a/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufConfluentExtractorTest.java +++ b/src/test/java/com/sngular/kloadgen/extractor/extractors/protobuff/ProtobufConfluentExtractorTest.java @@ -54,7 +54,8 @@ void testFlatProperties() throws Exception { void testEmbeddedTypes() throws Exception { final String testFile = fileHelper.getContent("/proto-files/embeddedTypeTest.proto"); final ProtobufSchema schema = new ProtobufSchema(testFile); - final List fieldValueMappingList = protoBufConfluentExtractor.processSchema(schema); Assertions.assertThat(fieldValueMappingList) + final List fieldValueMappingList = protoBufConfluentExtractor.processSchema(schema); + Assertions.assertThat(fieldValueMappingList) .hasSize(2) .containsExactlyInAnyOrder( FieldValueMapping.builder().fieldName("phones.addressesPhone[:].id[]").fieldType("string-array").required(true).isAncestorRequired(true).build(), diff --git a/src/test/java/com/sngular/kloadgen/processor/AvroSchemaProcessorTest.java b/src/test/java/com/sngular/kloadgen/processor/AvroSchemaProcessorTest.java index 64a7b57e..9608c3d2 100644 --- a/src/test/java/com/sngular/kloadgen/processor/AvroSchemaProcessorTest.java +++ b/src/test/java/com/sngular/kloadgen/processor/AvroSchemaProcessorTest.java @@ -190,9 +190,9 @@ private GenericRecord setUpEntityForEmbeddedAvroTest(final ParsedSchema parsedSc GenericData.Record entity = null; //TODO HERE if (parsedSchema.rawSchema() instanceof Schema) { - Schema schema = (Schema) parsedSchema.rawSchema(); + final Schema schema = (Schema) parsedSchema.rawSchema(); if (Schema.Type.UNION.equals(schema.getType())) { - entity = new GenericData.Record(schema.getTypes().get(schema.getTypes().size()-1)); + entity = new GenericData.Record(schema.getTypes().get(schema.getTypes().size() - 1)); } else { entity = new GenericData.Record(schema); } diff --git a/src/test/java/com/sngular/kloadgen/processor/ProtobufSchemaProcessorTest.java b/src/test/java/com/sngular/kloadgen/processor/ProtobufSchemaProcessorTest.java index 5ab1ea48..40e1ba83 100644 --- a/src/test/java/com/sngular/kloadgen/processor/ProtobufSchemaProcessorTest.java +++ b/src/test/java/com/sngular/kloadgen/processor/ProtobufSchemaProcessorTest.java @@ -9,17 +9,20 @@ import java.util.Map; import com.google.protobuf.Descriptors; -import com.google.protobuf.Descriptors.DescriptorValidationException; import com.google.protobuf.DynamicMessage; import com.sngular.kloadgen.common.SchemaTypeEnum; import com.sngular.kloadgen.exception.KLoadGenException; import com.sngular.kloadgen.extractor.SchemaExtractor; import com.sngular.kloadgen.model.FieldValueMapping; +import com.sngular.kloadgen.schemaregistry.SchemaRegistryAdapter; +import com.sngular.kloadgen.schemaregistry.SchemaRegistryManagerFactory; import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentSchemaMetadata; import com.sngular.kloadgen.serializer.EnrichedRecord; import com.sngular.kloadgen.testutil.FileHelper; import com.sngular.kloadgen.testutil.SchemaParseUtil; +import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; +import org.apache.commons.collections.MapUtils; import org.apache.jmeter.threads.JMeterContext; import org.apache.jmeter.threads.JMeterContextService; import org.apache.jmeter.threads.JMeterVariables; @@ -46,18 +49,20 @@ public void setUp() { final JMeterContext jmcx = JMeterContextService.getContext(); jmcx.setVariables(new JMeterVariables()); JMeterUtils.setLocale(Locale.ENGLISH); + JMeterContextService.getContext().getProperties().put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, "CONFLUENT"); + } @Test @DisplayName("Be able to process embedded schema") - void textEmbeddedTypeTestSchemaProcessor() throws KLoadGenException, IOException, DescriptorValidationException { + void textEmbeddedTypeTestSchemaProcessor() throws KLoadGenException, IOException { final File testFile = fileHelper.getFile("/proto-files/embeddedTypeTest.proto"); final List fieldValueMappingList = List.of( FieldValueMapping.builder().fieldName("phones.addressesPhone[1:].id[1]").fieldType("string-array").fieldValueList("Pablo").required(true).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("phones.phoneType").fieldType("enum").fieldValueList("[MOBILE, HOME, WORK]").required(true).isAncestorRequired(true).build()); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); @@ -84,14 +89,14 @@ private String getIdFieldForEmbeddedTypeTest(final List assertValues) { @Test @DisplayName("Be able to process complex types like StringValue or Int32Value and get values by default") - void testProtobufGoogleTypes() throws IOException, DescriptorValidationException { + void testProtobufGoogleTypes() throws IOException { final File testFile = fileHelper.getFile("/proto-files/googleTypesTest.proto"); final List fieldValueMappingList = List.of( FieldValueMapping.builder().fieldName("id").fieldType("Int32Value").required(true).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("occurrence_id").fieldType("StringValue").fieldValueList("Isabel").required(true).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("load_number").fieldType("Int32Value").required(true).isAncestorRequired(true).build()); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); @@ -114,12 +119,12 @@ void testProtobufGoogleTypes() throws IOException, DescriptorValidationException @Test @DisplayName("Be able to process enum in the schema") - void testProtoBufEnumSchemaProcessor() throws IOException, DescriptorValidationException { + void testProtoBufEnumSchemaProcessor() throws IOException { final File testFile = fileHelper.getFile("/proto-files/enumTest.proto"); final List fieldValueMappingList = schemaExtractor.flatPropertiesList(SchemaParseUtil.getParsedSchema(testFile, "PROTOBUF")); fieldValueMappingList.get(0).setFieldValuesList("HOME, WORK"); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); @@ -153,11 +158,11 @@ void testProtoBufEnumSchemaProcessor() throws IOException, DescriptorValidationE @Test @DisplayName("Be able to process easy schema") - void testProtoBufEasyTestProcessor() throws IOException, DescriptorValidationException { + void testProtoBufEasyTestProcessor() throws IOException { final File testFile = fileHelper.getFile("/proto-files/easyTest.proto"); final List fieldValueMappingList = schemaExtractor.flatPropertiesList(SchemaParseUtil.getParsedSchema(testFile, "PROTOBUF")); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); @@ -183,12 +188,12 @@ void testProtoBufEasyTestProcessor() throws IOException, DescriptorValidationExc @Test @DisplayName("Be able to process oneOf fields") - void testProtoBufOneOfProcessor() throws IOException, DescriptorValidationException { + void testProtoBufOneOfProcessor() throws IOException { final File testFile = fileHelper.getFile("/proto-files/oneOfTest.proto"); final List fieldValueMappingList = schemaExtractor.flatPropertiesList(SchemaParseUtil.getParsedSchema(testFile, "PROTOBUF")); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); @@ -214,7 +219,7 @@ void testProtoBufOneOfProcessor() throws IOException, DescriptorValidationExcept @Test @DisplayName("Be able to process map in schema") - void testProtoBufMapTestProcessor() throws IOException, DescriptorValidationException { + void testProtoBufMapTestProcessor() throws IOException { final File testFile = fileHelper.getFile("/proto-files/mapTest.proto"); final List fieldValueMappingList = Arrays.asList( FieldValueMapping.builder().fieldName("name[:]").fieldType("string-map").fieldValueList("Pablo").required(true).isAncestorRequired(true).build(), @@ -270,8 +275,11 @@ private Object getSubFieldForMapTestProcessor(final DynamicMessage dynamicMessag @Test @DisplayName("Be able to process complex schema") - void testProtoBufComplexTestProcessor() throws IOException, DescriptorValidationException { + void testProtoBufComplexTestProcessor() throws IOException { final File testFile = fileHelper.getFile("/proto-files/complexTest.proto"); + final SchemaRegistryAdapter schemaRegistryManager = SchemaRegistryManagerFactory.getSchemaRegistry("CONFLUENT"); + schemaRegistryManager.setSchemaRegistryClient("http://localhost:8080", MapUtils.EMPTY_MAP); + final List fieldValueMappingList = Arrays.asList( FieldValueMapping.builder().fieldName("phone_types[].phone").fieldType("long").required(true).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("phone_types[].principal").fieldType("boolean").required(true).isAncestorRequired(true).build(), @@ -320,7 +328,7 @@ void testProtoBufComplexTestProcessor() throws IOException, DescriptorValidation @Test @DisplayName("Be able to process provided complex schema") - void testProtoBufProvidedComplexTestProcessor() throws IOException, DescriptorValidationException { + void testProtoBufProvidedComplexTestProcessor() throws IOException { final File testFile = fileHelper.getFile("/proto-files/providedTest.proto"); final List fieldValueMappingList = Arrays.asList( FieldValueMapping.builder().fieldName("id").fieldType("int").required(true).isAncestorRequired(true).build(), @@ -401,7 +409,7 @@ void testProtoBufProvidedComplexTestProcessor() throws IOException, DescriptorVa } @Test - void testFailing() throws IOException, DescriptorValidationException { + void testFailing() throws IOException { final File testFile = fileHelper.getFile("/proto-files/deveTest.proto"); final List fieldValueMappingList = Arrays.asList( FieldValueMapping.builder().fieldName("load_type").fieldType("string").required(true).isAncestorRequired(true).build(), @@ -463,7 +471,8 @@ void testFailing() throws IOException, DescriptorValidationException { ); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, + SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); final List assertKeys = new ArrayList<>(); @@ -494,7 +503,8 @@ void testProtoBufProvidedWithNotNestedTypeProcessor() throws IOException { FieldValueMapping.builder().fieldName("customer_account.billing_party.address.address_line_one").fieldType("string").required(true).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("customer_account.billing_party.address.address_line_two").fieldType("string").required(true).isAncestorRequired(true).build()); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, + SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); final List assertKeys = new ArrayList<>(); @@ -520,14 +530,15 @@ void testProtoBufProvidedWithNotNestedTypeProcessor() throws IOException { @Test @DisplayName("Be able to process Date and TimeOfDay types") - void testDateTimeTypes() throws IOException, DescriptorValidationException { + void testDateTimeTypes() throws IOException { final File testFile = fileHelper.getFile("/proto-files/dateTimeTest.proto"); final List fieldValueMappingList = List.of( FieldValueMapping.builder().fieldName("incident_date").fieldType(".google.type.Date").fieldValueList("2022-05-30").required(true).isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("incident_time").fieldType(".google.type.TimeOfDay").fieldValueList("14:20:30-05:00").required(true).isAncestorRequired(true) .build()); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); - protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, SchemaParseUtil.getParsedSchema(testFile,SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); + protobufSchemaProcessor.processSchema(SchemaTypeEnum.PROTOBUF, + SchemaParseUtil.getParsedSchema(testFile, SchemaTypeEnum.PROTOBUF.name()), confluentBaseSchemaMetadata, fieldValueMappingList); final EnrichedRecord message = (EnrichedRecord) protobufSchemaProcessor.next(); final DynamicMessage genericRecord = (DynamicMessage) message.getGenericRecord(); final Map map = genericRecord.getAllFields(); diff --git a/src/test/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditorTest.java b/src/test/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditorTest.java index eb182531..39bdc435 100644 --- a/src/test/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditorTest.java +++ b/src/test/java/com/sngular/kloadgen/property/editor/SchemaRegistryConfigPropertyEditorTest.java @@ -10,10 +10,11 @@ import java.beans.PropertyDescriptor; import java.util.Locale; -import com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElement; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.JTextField; + +import com.sngular.kloadgen.config.schemaregistry.SchemaRegistryConfigElement; import org.apache.jmeter.threads.JMeterContext; import org.apache.jmeter.threads.JMeterContextService; import org.apache.jmeter.threads.JMeterVariables; diff --git a/src/test/java/com/sngular/kloadgen/randomtool/random/RandomArrayTest.java b/src/test/java/com/sngular/kloadgen/randomtool/random/RandomArrayTest.java index a6a713fc..94f824d2 100644 --- a/src/test/java/com/sngular/kloadgen/randomtool/random/RandomArrayTest.java +++ b/src/test/java/com/sngular/kloadgen/randomtool/random/RandomArrayTest.java @@ -50,9 +50,9 @@ void generateArrayRandomValue(final String fieldType, final Integer valueLength, void generateArrayRandomValueZero(final String fieldType, final Integer valueLength, final List fieldValuesList) { Assertions.assertThat((List) new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap())) .isNotNull(); - Object number1 = new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap()); - Object number2 = new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap()); - List number3 = (List) new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap()); + final Object number1 = new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap()); + final Object number2 = new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap()); + final List number3 = (List) new RandomArray().generateArray(fieldType, valueLength, fieldValuesList, 1, Collections.emptyMap()); Assertions.assertThat(number1).isNotNull(); Assertions.assertThat(number2).isNotNull(); Assertions.assertThat(number3).isNotNull(); diff --git a/src/test/java/com/sngular/kloadgen/sampler/SamplerUtilTest.java b/src/test/java/com/sngular/kloadgen/sampler/SamplerUtilTest.java index c2a823a6..2fe87967 100644 --- a/src/test/java/com/sngular/kloadgen/sampler/SamplerUtilTest.java +++ b/src/test/java/com/sngular/kloadgen/sampler/SamplerUtilTest.java @@ -6,8 +6,6 @@ package com.sngular.kloadgen.sampler; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.createFieldValueMapping; - import java.io.File; import java.io.IOException; import java.util.Arrays; @@ -15,9 +13,9 @@ import java.util.Properties; import java.util.stream.Stream; -import com.sngular.kloadgen.extractor.SchemaExtractor; import com.sngular.kloadgen.model.FieldValueMapping; import com.sngular.kloadgen.processor.fixture.JsonSchemaFixturesConstants; +import com.sngular.kloadgen.serializer.SerializerTestFixture; import com.sngular.kloadgen.testutil.FileHelper; import com.sngular.kloadgen.testutil.SchemaParseUtil; import com.sngular.kloadgen.util.PropsKeysHelper; @@ -34,112 +32,111 @@ class SamplerUtilTest { - - private JMeterContext jmcx; - - private final FileHelper fileHelper = new FileHelper(); - - private final SchemaExtractor extractor = new SchemaExtractor(); - - private static Stream parametersForConfigureValueGeneratorTest() { - return Stream.of("localhost:8081", ""); - } - - private static Stream parametersForConfigureKeyGeneratorTest() { - return Stream.of("avro", "json","protobuf"); - } - - @BeforeEach - public final void setUp() throws IOException { - final File file = new File("src/test/resources"); - final String absolutePath = file.getAbsolutePath(); - JMeterUtils.loadJMeterProperties(absolutePath + "/kloadgen.properties"); - jmcx = JMeterContextService.getContext(); - jmcx.setVariables(new JMeterVariables()); - JMeterUtils.setLocale(Locale.ENGLISH); - } - - public JMeterVariables getVariablesAvro() throws IOException { - final File testFile = fileHelper.getFile("/avro-files/avros-example-with-sub-entity-array-test.avsc"); - final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "AVRO"); - var variables = new JMeterVariables(); - variables.put(PropsKeysHelper.KEY_SCHEMA_TYPE, "avro"); - variables.put(PropsKeysHelper.VALUE_SUBJECT_NAME, "test"); - variables.put(PropsKeysHelper.KEY_SUBJECT_NAME, "test"); - variables.put(PropsKeysHelper.VALUE_SCHEMA, String.valueOf(parsedSchema)); - variables.put(PropsKeysHelper.KEY_SCHEMA, String.valueOf(parsedSchema)); - variables.putObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES, Arrays.asList( - FieldValueMapping.builder().fieldName("subEntity.anotherLevel.subEntityIntArray[2]").fieldType("int-array").valueLength(0).fieldValueList("[1]").required(true) - .isAncestorRequired(true).build(), - FieldValueMapping.builder().fieldName("topLevelIntArray[3]").fieldType("int-array").valueLength(0).fieldValueList("[2]").required(true).isAncestorRequired(true).build()) - ); - return variables; + private final FileHelper fileHelper = new FileHelper(); + + private JMeterContext jmcx; + + private static Stream parametersForConfigureValueGeneratorTest() { + return Stream.of("localhost:8081", ""); + } + + private static Stream parametersForConfigureKeyGeneratorTest() { + return Stream.of("avro", "json", "protobuf"); + } + + @BeforeEach + public final void setUp() throws IOException { + final File file = new File("src/test/resources"); + final String absolutePath = file.getAbsolutePath(); + JMeterUtils.loadJMeterProperties(absolutePath + "/kloadgen.properties"); + jmcx = JMeterContextService.getContext(); + jmcx.setVariables(new JMeterVariables()); + JMeterUtils.setLocale(Locale.ENGLISH); + } + + @ParameterizedTest + @MethodSource("parametersForConfigureValueGeneratorTest") + void configureValueGeneratorTest(final String jmeterProps) throws IOException { + final Properties props = new Properties(); + props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, jmeterProps); + jmcx.setVariables(getVariablesAvro()); + final var generator = SamplerUtil.configureValueGenerator(props); + Assertions.assertThat(generator.nextMessage()).isNotNull(); + } + + public JMeterVariables getVariablesAvro() throws IOException { + final File testFile = fileHelper.getFile("/avro-files/avros-example-with-sub-entity-array-test.avsc"); + final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "AVRO"); + final var variables = new JMeterVariables(); + variables.put(PropsKeysHelper.KEY_SCHEMA_TYPE, "avro"); + variables.put(PropsKeysHelper.VALUE_SUBJECT_NAME, "test"); + variables.put(PropsKeysHelper.KEY_SUBJECT_NAME, "test"); + variables.put(PropsKeysHelper.VALUE_SCHEMA, String.valueOf(parsedSchema)); + variables.put(PropsKeysHelper.KEY_SCHEMA, String.valueOf(parsedSchema)); + variables.putObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES, Arrays.asList( + FieldValueMapping.builder().fieldName("subEntity.anotherLevel.subEntityIntArray[2]").fieldType("int-array").valueLength(0).fieldValueList("[1]").required(true) + .isAncestorRequired(true).build(), + FieldValueMapping.builder().fieldName("topLevelIntArray[3]").fieldType("int-array").valueLength(0).fieldValueList("[2]").required(true).isAncestorRequired(true).build()) + ); + return variables; + } + + @ParameterizedTest + @MethodSource("parametersForConfigureKeyGeneratorTest") + void configureKeyGeneratorTest(final String jmeterProps) throws IOException { + final Properties props = new Properties(); + props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, "localhost:8081"); + props.put(PropsKeysHelper.KEY_SCHEMA_TYPE, jmeterProps); + + if ("json".equalsIgnoreCase(jmeterProps)) { + jmcx.setVariables(getVariablesJsonSchema()); } - - public JMeterVariables getVariablesProtobuf() throws IOException { - - final File testFile = fileHelper.getFile("/proto-files/easyTest.proto"); - final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "PROTOBUF"); - var variables = new JMeterVariables(); - variables.put(PropsKeysHelper.KEY_SCHEMA_TYPE, "protobuf"); - variables.put(PropsKeysHelper.VALUE_SUBJECT_NAME, "protobufSubject"); - variables.put(PropsKeysHelper.VALUE_SCHEMA, String.valueOf(parsedSchema)); - variables.put(PropsKeysHelper.KEY_SCHEMA, String.valueOf(parsedSchema)); - variables.put(PropsKeysHelper.KEY_SUBJECT_NAME, "protobufSubject"); - variables.putObject(PropsKeysHelper.KEY_SCHEMA_PROPERTIES, Arrays.asList( - createFieldValueMapping("street", "string"), - createFieldValueMapping("number[]", "int-array"), - createFieldValueMapping("zipcode", "long"))); - variables.putObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES, Arrays.asList( - createFieldValueMapping("street", "string"), - createFieldValueMapping("number[]", "int-array"), - createFieldValueMapping("zipcode", "long"))); - return variables; - } - - public JMeterVariables getVariablesJsonSchema() throws IOException { - - final File testFile = fileHelper.getFile("/jsonschema/basic.jcs"); - final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "JSON"); - - var variables = new JMeterVariables(); - variables.put(PropsKeysHelper.KEY_SCHEMA_TYPE, "JSON"); - variables.put(PropsKeysHelper.VALUE_SUBJECT_NAME, "jsonSubject"); - variables.put(PropsKeysHelper.KEY_SUBJECT_NAME, "jsonSubject"); - variables.put(PropsKeysHelper.VALUE_SCHEMA, String.valueOf(parsedSchema)); - variables.put(PropsKeysHelper.KEY_SCHEMA, String.valueOf(parsedSchema)); - variables.putObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES, JsonSchemaFixturesConstants.SIMPLE_SCHEMA); - return variables; + if ("avro".equalsIgnoreCase(jmeterProps)) { + jmcx.setVariables(getVariablesAvro()); } - - @ParameterizedTest - @MethodSource("parametersForConfigureValueGeneratorTest") - void configureValueGeneratorTest(String jmeterProps) throws IOException { - Properties props = new Properties(); - props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, jmeterProps); - jmcx.setVariables(getVariablesAvro()); - var generator = SamplerUtil.configureValueGenerator(props); - Assertions.assertThat(generator.nextMessage()).isNotNull(); + if ("protobuf".equalsIgnoreCase(jmeterProps)) { + jmcx.setVariables(getVariablesProtobuf()); } - @ParameterizedTest - @MethodSource("parametersForConfigureKeyGeneratorTest") - void configureKeyGeneratorTest(String jmeterProps) throws IOException { - Properties props = new Properties(); - props.put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, "localhost:8081"); - props.put(PropsKeysHelper.KEY_SCHEMA_TYPE, jmeterProps); - - if (jmeterProps.equalsIgnoreCase("json")) - jmcx.setVariables(getVariablesJsonSchema()); - if (jmeterProps.equalsIgnoreCase("avro")) - jmcx.setVariables(getVariablesAvro()); - if (jmeterProps.equalsIgnoreCase("protobuf")) - jmcx.setVariables(getVariablesProtobuf()); - - var generator = SamplerUtil.configureKeyGenerator(props); - Assertions.assertThat(generator.nextMessage()).isNotNull(); - - - } + final var generator = SamplerUtil.configureKeyGenerator(props); + Assertions.assertThat(generator.nextMessage()).isNotNull(); + + } + + public JMeterVariables getVariablesJsonSchema() throws IOException { + + final File testFile = fileHelper.getFile("/jsonschema/basic.jcs"); + final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "JSON"); + + final var variables = new JMeterVariables(); + variables.put(PropsKeysHelper.KEY_SCHEMA_TYPE, "JSON"); + variables.put(PropsKeysHelper.VALUE_SUBJECT_NAME, "jsonSubject"); + variables.put(PropsKeysHelper.KEY_SUBJECT_NAME, "jsonSubject"); + variables.put(PropsKeysHelper.VALUE_SCHEMA, String.valueOf(parsedSchema)); + variables.put(PropsKeysHelper.KEY_SCHEMA, String.valueOf(parsedSchema)); + variables.putObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES, JsonSchemaFixturesConstants.SIMPLE_SCHEMA); + return variables; + } + + public JMeterVariables getVariablesProtobuf() throws IOException { + + final File testFile = fileHelper.getFile("/proto-files/easyTest.proto"); + final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(testFile, "PROTOBUF"); + final var variables = new JMeterVariables(); + variables.put(PropsKeysHelper.KEY_SCHEMA_TYPE, "protobuf"); + variables.put(PropsKeysHelper.VALUE_SUBJECT_NAME, "protobufSubject"); + variables.put(PropsKeysHelper.VALUE_SCHEMA, String.valueOf(parsedSchema)); + variables.put(PropsKeysHelper.KEY_SCHEMA, String.valueOf(parsedSchema)); + variables.put(PropsKeysHelper.KEY_SUBJECT_NAME, "protobufSubject"); + variables.putObject(PropsKeysHelper.KEY_SCHEMA_PROPERTIES, Arrays.asList( + SerializerTestFixture.createFieldValueMapping("street", "string"), + SerializerTestFixture.createFieldValueMapping("number[]", "int-array"), + SerializerTestFixture.createFieldValueMapping("zipcode", "long"))); + variables.putObject(PropsKeysHelper.VALUE_SCHEMA_PROPERTIES, Arrays.asList( + SerializerTestFixture.createFieldValueMapping("street", "string"), + SerializerTestFixture.createFieldValueMapping("number[]", "int-array"), + SerializerTestFixture.createFieldValueMapping("zipcode", "long"))); + return variables; + } } diff --git a/src/test/java/com/sngular/kloadgen/serializer/AvroDeserializerTest.java b/src/test/java/com/sngular/kloadgen/serializer/AvroDeserializerTest.java index d2680ab2..4fbf94b8 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/AvroDeserializerTest.java +++ b/src/test/java/com/sngular/kloadgen/serializer/AvroDeserializerTest.java @@ -1,23 +1,13 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_FILE_ISSUE; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_ISSUE; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_LOGICAL_TYPES; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_MAP; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_OPTIONAL_ENUM; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_OPTIONAL_MAP; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_SUBENTITY_ARRAY; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_UNION_RECORD; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_USER; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.readSchema; - import java.io.File; import java.util.List; import java.util.Map; import java.util.stream.Stream; import java.util.stream.Stream.Builder; +import javax.xml.bind.DatatypeConverter; + import com.sngular.kloadgen.common.SchemaTypeEnum; import com.sngular.kloadgen.model.FieldValueMapping; import com.sngular.kloadgen.processor.SchemaProcessor; @@ -26,7 +16,6 @@ import com.sngular.kloadgen.testutil.SchemaParseUtil; import com.sngular.kloadgen.util.PropsKeysHelper; import io.confluent.kafka.schemaregistry.ParsedSchema; -import javax.xml.bind.DatatypeConverter; import lombok.extern.slf4j.Slf4j; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -52,39 +41,39 @@ void setUp() { private static Stream getSchemaToTest() { - Builder builder = Stream.builder(); - - File testSubentityArrayFile = TEST_SUBENTITY_ARRAY.getFirst(); - builder.add(Arguments.arguments(Named.of(testSubentityArrayFile.getName(), testSubentityArrayFile), TEST_SUBENTITY_ARRAY.getSecond())); - File testIssueFile = TEST_ISSUE.getFirst(); - builder.add(Arguments.arguments(Named.of(testIssueFile.getName(), testIssueFile), TEST_ISSUE.getSecond())); - File testOptionalEnumFile = TEST_OPTIONAL_ENUM.getFirst(); - builder.add(Arguments.arguments(Named.of(testOptionalEnumFile.getName(), testOptionalEnumFile), TEST_OPTIONAL_ENUM.getSecond())); - File testFileIssueFile = TEST_FILE_ISSUE.getFirst(); - builder.add(Arguments.arguments(Named.of(testFileIssueFile.getName(), testFileIssueFile), TEST_FILE_ISSUE.getSecond())); - File testLogicalTypesFile = TEST_LOGICAL_TYPES.getFirst(); - builder.add(Arguments.arguments(Named.of(testLogicalTypesFile.getName(), testLogicalTypesFile), TEST_LOGICAL_TYPES.getSecond())); - File testMapFile = TEST_MAP.getFirst(); - builder.add(Arguments.arguments(Named.of(testMapFile.getName(), testMapFile), TEST_MAP.getSecond())); - File testNullOnOptionalFieldsFile = TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); + final Builder builder = Stream.builder(); + + final File testSubentityArrayFile = AvroSerializersTestFixture.TEST_SUBENTITY_ARRAY.getFirst(); + builder.add(Arguments.arguments(Named.of(testSubentityArrayFile.getName(), testSubentityArrayFile), AvroSerializersTestFixture.TEST_SUBENTITY_ARRAY.getSecond())); + final File testIssueFile = AvroSerializersTestFixture.TEST_ISSUE.getFirst(); + builder.add(Arguments.arguments(Named.of(testIssueFile.getName(), testIssueFile), AvroSerializersTestFixture.TEST_ISSUE.getSecond())); + final File testOptionalEnumFile = AvroSerializersTestFixture.TEST_OPTIONAL_ENUM.getFirst(); + builder.add(Arguments.arguments(Named.of(testOptionalEnumFile.getName(), testOptionalEnumFile), AvroSerializersTestFixture.TEST_OPTIONAL_ENUM.getSecond())); + final File testFileIssueFile = AvroSerializersTestFixture.TEST_FILE_ISSUE.getFirst(); + builder.add(Arguments.arguments(Named.of(testFileIssueFile.getName(), testFileIssueFile), AvroSerializersTestFixture.TEST_FILE_ISSUE.getSecond())); + final File testLogicalTypesFile = AvroSerializersTestFixture.TEST_LOGICAL_TYPES.getFirst(); + builder.add(Arguments.arguments(Named.of(testLogicalTypesFile.getName(), testLogicalTypesFile), AvroSerializersTestFixture.TEST_LOGICAL_TYPES.getSecond())); + final File testMapFile = AvroSerializersTestFixture.TEST_MAP.getFirst(); + builder.add(Arguments.arguments(Named.of(testMapFile.getName(), testMapFile), AvroSerializersTestFixture.TEST_MAP.getSecond())); + final File testNullOnOptionalFieldsFile = AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); builder.add( - Arguments.arguments(Named.of(testNullOnOptionalFieldsFile.getName(), testNullOnOptionalFieldsFile), TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); - File testOptionalMapFile = TEST_OPTIONAL_MAP.getFirst(); - builder.add(Arguments.arguments(Named.of(testOptionalMapFile.getName(), testOptionalMapFile), TEST_OPTIONAL_MAP.getSecond())); - File testNullOnOptionalFields = TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); - builder.add(Arguments.arguments(Named.of(testNullOnOptionalFields.getName(), testNullOnOptionalFields), TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); - File testUnionRecordFile = TEST_UNION_RECORD.getFirst(); - builder.add(Arguments.arguments(Named.of(testUnionRecordFile.getName(), testUnionRecordFile), TEST_UNION_RECORD.getSecond())); - File testUserFile = TEST_USER.getFirst(); - builder.add(Arguments.arguments(Named.of(testUserFile.getName(), testUserFile), TEST_USER.getSecond())); + Arguments.arguments(Named.of(testNullOnOptionalFieldsFile.getName(), testNullOnOptionalFieldsFile), AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); + final File testOptionalMapFile = AvroSerializersTestFixture.TEST_OPTIONAL_MAP.getFirst(); + builder.add(Arguments.arguments(Named.of(testOptionalMapFile.getName(), testOptionalMapFile), AvroSerializersTestFixture.TEST_OPTIONAL_MAP.getSecond())); + final File testNullOnOptionalFields = AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); + builder.add(Arguments.arguments(Named.of(testNullOnOptionalFields.getName(), testNullOnOptionalFields), AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); + final File testUnionRecordFile = AvroSerializersTestFixture.TEST_UNION_RECORD.getFirst(); + builder.add(Arguments.arguments(Named.of(testUnionRecordFile.getName(), testUnionRecordFile), AvroSerializersTestFixture.TEST_UNION_RECORD.getSecond())); + final File testUserFile = AvroSerializersTestFixture.TEST_USER.getFirst(); + builder.add(Arguments.arguments(Named.of(testUserFile.getName(), testUserFile), AvroSerializersTestFixture.TEST_USER.getSecond())); return builder.build(); } @ParameterizedTest @MethodSource("getSchemaToTest") - void deserialize(File schemaFile, List fieldValueMappings) throws Exception { - final var schemaStr = readSchema(schemaFile); + void deserialize(final File schemaFile, final List fieldValueMappings) throws Exception { + final var schemaStr = SerializerTestFixture.readSchema(schemaFile); final BaseSchemaMetadata confluentBaseSchemaMetadata = new BaseSchemaMetadata<>( ConfluentSchemaMetadata.parse(new io.confluent.kafka.schemaregistry.client.SchemaMetadata(1, 1, diff --git a/src/test/java/com/sngular/kloadgen/serializer/AvroSerializersTestFixture.java b/src/test/java/com/sngular/kloadgen/serializer/AvroSerializersTestFixture.java index a9cf10c6..db88d04d 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/AvroSerializersTestFixture.java +++ b/src/test/java/com/sngular/kloadgen/serializer/AvroSerializersTestFixture.java @@ -1,7 +1,5 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.createFieldValueMapping; - import java.io.File; import java.util.Arrays; import java.util.List; @@ -14,132 +12,135 @@ public final class AvroSerializersTestFixture { - static final List> serializerList = List.of( - new GenericAvroRecordSerializer<>(), - new GenericAvroRecordBinarySerializer<>() + static final List> SERIALIZER_LIST = List.of( + new GenericAvroRecordSerializer<>(), + new GenericAvroRecordBinarySerializer<>() ); static final Pair> TEST_SUBENTITY_ARRAY = new Pair<>( new FileHelper().getFile("/avro-files/avros-example-with-sub-entity-array-test.avsc"), Arrays.asList( - createFieldValueMapping("subEntity.anotherLevel.subEntityIntArray[2]", "int-array"), - createFieldValueMapping("subEntity.anotherLevel.subEntityRecordArray[2].name", "string"), - createFieldValueMapping("topLevelIntArray[3]", "int-array"), - createFieldValueMapping("topLevelRecordArray[3].name", "string")) + SerializerTestFixture.createFieldValueMapping("subEntity.anotherLevel.subEntityIntArray[2]", "int-array"), + SerializerTestFixture.createFieldValueMapping("subEntity.anotherLevel.subEntityRecordArray[2].name", "string"), + SerializerTestFixture.createFieldValueMapping("topLevelIntArray[3]", "int-array"), + SerializerTestFixture.createFieldValueMapping("topLevelRecordArray[3].name", "string")) ); static final Pair> TEST_EMBEDED_AVROS_EXAMPLE = new Pair<>( new FileHelper().getFile("/avro-files/embedded-avros-example-test.avsc"), Arrays.asList( - createFieldValueMapping("fieldMySchema.testInt_id", "int", "[100]"), - createFieldValueMapping("fieldMySchema.testLong", "long"), - createFieldValueMapping("fieldMySchema.fieldString", "string"), - createFieldValueMapping("timestamp", "long")) + SerializerTestFixture.createFieldValueMapping("fieldMySchema.testInt_id", "int", "[100]"), + SerializerTestFixture.createFieldValueMapping("fieldMySchema.testLong", "long"), + SerializerTestFixture.createFieldValueMapping("fieldMySchema.fieldString", "string"), + SerializerTestFixture.createFieldValueMapping("timestamp", "long")) ); static final Pair> TEST_ISSUE = new Pair<>( new FileHelper().getFile("/avro-files/issue.avsc"), Arrays.asList( - createFieldValueMapping("mainObject.arrayValue[].optional1", "string"), - createFieldValueMapping("mainObject.arrayValue[].optional2", "string"), - createFieldValueMapping("mainObject.arrayValue[].optional3", "string")) + SerializerTestFixture.createFieldValueMapping("mainObject.arrayValue[].optional1", "string"), + SerializerTestFixture.createFieldValueMapping("mainObject.arrayValue[].optional2", "string"), + SerializerTestFixture.createFieldValueMapping("mainObject.arrayValue[].optional3", "string")) ); static final Pair> TEST_OPTIONAL_ENUM = new Pair<>( new FileHelper().getFile("/avro-files/optionalEnum.avsc"), - Arrays.asList(createFieldValueMapping("aggregateAttribute.fruitList.fruits[].fruitType", "enum")) + Arrays.asList(SerializerTestFixture.createFieldValueMapping("aggregateAttribute.fruitList.fruits[].fruitType", "enum")) ); static final Pair> TEST_FILE_ISSUE = new Pair<>( new FileHelper().getFile("/avro-files/testFileIssue.avsc"), Arrays.asList( - createFieldValueMapping("timestamp", "long_timestamp-millis"), - createFieldValueMapping("queueId", "string"), - createFieldValueMapping("name", "string"), - createFieldValueMapping("description", "string"), - createFieldValueMapping("accountDefault", "boolean"), - createFieldValueMapping("accountId", "string")) + SerializerTestFixture.createFieldValueMapping("timestamp", "long_timestamp-millis"), + SerializerTestFixture.createFieldValueMapping("queueId", "string"), + SerializerTestFixture.createFieldValueMapping("name", "string"), + SerializerTestFixture.createFieldValueMapping("description", "string"), + SerializerTestFixture.createFieldValueMapping("accountDefault", "boolean"), + SerializerTestFixture.createFieldValueMapping("accountId", "string")) ); static final Pair> TEST_LOGICAL_TYPES = new Pair<>( new FileHelper().getFile("/avro-files/testLogicalTypes.avsc"), Arrays.asList( - createFieldValueMapping("Date", "int_date"), - createFieldValueMapping("TimeMillis", "int_time-millis"), - createFieldValueMapping("TimeMicros", "long_time-micros"), - createFieldValueMapping("TimestampMillis", "long_timestamp-millis"), - createFieldValueMapping("TimestampMicros", "long_timestamp-micros"), - createFieldValueMapping("LocalTimestampMillis", "long_local-timestamp-millis"), - createFieldValueMapping("LocalTimestampMicros", "long_local-timestamp-micros"), - createFieldValueMapping("UUID", "string_uuid"), - createFieldValueMapping("Decimal", "bytes_decimal"), - createFieldValueMapping("DecimalFixed", "fixed_decimal")) + SerializerTestFixture.createFieldValueMapping("Date", "int_date"), + SerializerTestFixture.createFieldValueMapping("TimeMillis", "int_time-millis"), + SerializerTestFixture.createFieldValueMapping("TimeMicros", "long_time-micros"), + SerializerTestFixture.createFieldValueMapping("TimestampMillis", "long_timestamp-millis"), + SerializerTestFixture.createFieldValueMapping("TimestampMicros", "long_timestamp-micros"), + SerializerTestFixture.createFieldValueMapping("LocalTimestampMillis", "long_local-timestamp-millis"), + SerializerTestFixture.createFieldValueMapping("LocalTimestampMicros", "long_local-timestamp-micros"), + SerializerTestFixture.createFieldValueMapping("UUID", "string_uuid"), + SerializerTestFixture.createFieldValueMapping("Decimal", "bytes_decimal"), + SerializerTestFixture.createFieldValueMapping("DecimalFixed", "fixed_decimal")) ); static final Pair> TEST_MAP = new Pair<>( new FileHelper().getFile("/avro-files/testMap.avsc"), Arrays.asList( - createFieldValueMapping("theMap[:][].otherType.addTypeId", "string"), - createFieldValueMapping("theMap[:][].otherType.name", "string"), - createFieldValueMapping("theMap[:][].otherType.otherField", "string"), - createFieldValueMapping("theMap[:][].addAmount", "bytes_decimal"), - createFieldValueMapping("theMap[:][].addCode", "string"), - createFieldValueMapping("theMap[:][].metadataMap[:]", "string-map"), - createFieldValueMapping("theMap[:][].metadataArray[]", "string-array"), - createFieldValueMapping("theMap[:][].metadataMapMap[:][:]", "string-map-map"), - createFieldValueMapping("theMap[:][].metadataArrayArray[][]", "string-array-array")) + SerializerTestFixture.createFieldValueMapping("theMap[:][].otherType.addTypeId", "string"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].otherType.name", "string"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].otherType.otherField", "string"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].addAmount", "bytes_decimal"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].addCode", "string"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].metadataMap[:]", "string-map"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].metadataArray[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].metadataMapMap[:][:]", "string-map-map"), + SerializerTestFixture.createFieldValueMapping("theMap[:][].metadataArrayArray[][]", "string-array-array")) ); static final Pair> TEST_NULL_ON_OPTIONAL_FIELDS = new Pair<>( new FileHelper().getFile("/avro-files/testNullOnOptionalFields.avsc"), Arrays.asList( - createFieldValueMapping("name", "string"), - createFieldValueMapping("favorite_number", "int"), - createFieldValueMapping("favorite_color", "string"), - createFieldValueMapping("emails[]", "string-array"), - createFieldValueMapping("phones[]", "int-array"), - createFieldValueMapping("friends[]", "string-array"), - createFieldValueMapping("favorite_cars[].brand", "string"), - createFieldValueMapping("favorite_cars[].power", "int"), - createFieldValueMapping("favorite_cars[].parts[]", "string-array"), - createFieldValueMapping("favorite_cars2[].brand2", "string"), - createFieldValueMapping("favorite_cars2[].power2", "int"), - createFieldValueMapping("favorite_cars2[].parts[]2", "string-array")) + SerializerTestFixture.createFieldValueMapping("name", "string"), + SerializerTestFixture.createFieldValueMapping("favorite_number", "int"), + SerializerTestFixture.createFieldValueMapping("favorite_color", "string"), + SerializerTestFixture.createFieldValueMapping("emails[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("phones[]", "int-array"), + SerializerTestFixture.createFieldValueMapping("friends[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("favorite_cars[].brand", "string"), + SerializerTestFixture.createFieldValueMapping("favorite_cars[].power", "int"), + SerializerTestFixture.createFieldValueMapping("favorite_cars[].parts[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("favorite_cars2[].brand2", "string"), + SerializerTestFixture.createFieldValueMapping("favorite_cars2[].power2", "int"), + SerializerTestFixture.createFieldValueMapping("favorite_cars2[].parts[]2", "string-array")) ); static final Pair> TEST_OPTIONAL_MAP = new Pair<>( new FileHelper().getFile("/avro-files/testOptionalMap.avsc"), Arrays.asList( - createFieldValueMapping("mapOfString[:]", "string-map"), - createFieldValueMapping("arrayOfString[]", "string-array"), - createFieldValueMapping("arrayOfMap[][:]", "string-map-array"), - createFieldValueMapping("mapOfArray[:][]", "int-array-map"), - createFieldValueMapping("mapOfArrayOfRecord[:][].name", "string"), - createFieldValueMapping("mapOfArrayOfRecord[:][].age", "int"), - createFieldValueMapping("arrayOfMapOfRecord[][:].name", "string"), - createFieldValueMapping("arrayOfMapOfRecord[][:].age", "int")) + SerializerTestFixture.createFieldValueMapping("mapOfString[:]", "string-map"), + SerializerTestFixture.createFieldValueMapping("arrayOfString[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("arrayOfMap[][:]", "string-map-array"), + SerializerTestFixture.createFieldValueMapping("mapOfArray[:][]", "int-array-map"), + SerializerTestFixture.createFieldValueMapping("mapOfArrayOfRecord[:][].name", "string"), + SerializerTestFixture.createFieldValueMapping("mapOfArrayOfRecord[:][].age", "int"), + SerializerTestFixture.createFieldValueMapping("arrayOfMapOfRecord[][:].name", "string"), + SerializerTestFixture.createFieldValueMapping("arrayOfMapOfRecord[][:].age", "int")) ); static final Pair> TEST_UNION_RECORD = new Pair<>( new FileHelper().getFile("/avro-files/testUnionRecord.avsc"), Arrays.asList( - createFieldValueMapping("validateInnerObject.attribute1", "string"), - createFieldValueMapping("validateInnerObject.attribute2", "string"), - createFieldValueMapping("products[].Price.price", "string"), - createFieldValueMapping("products[].Price.priceType", "string"), - createFieldValueMapping("products[].Price.currency", "string"), - createFieldValueMapping("products[].Price.discount", "string"), - createFieldValueMapping("products[].Price.validateInnerObject.attribute1", "string"), - createFieldValueMapping("products[].Price.validateInnerObject.attribute2", "string")) + SerializerTestFixture.createFieldValueMapping("validateInnerObject.attribute1", "string"), + SerializerTestFixture.createFieldValueMapping("validateInnerObject.attribute2", "string"), + SerializerTestFixture.createFieldValueMapping("products[].Price.price", "string"), + SerializerTestFixture.createFieldValueMapping("products[].Price.priceType", "string"), + SerializerTestFixture.createFieldValueMapping("products[].Price.currency", "string"), + SerializerTestFixture.createFieldValueMapping("products[].Price.discount", "string"), + SerializerTestFixture.createFieldValueMapping("products[].Price.validateInnerObject.attribute1", "string"), + SerializerTestFixture.createFieldValueMapping("products[].Price.validateInnerObject.attribute2", "string")) ); static final Pair> TEST_USER = new Pair<>( new FileHelper().getFile("/avro-files/userTest.avsc"), Arrays.asList( - createFieldValueMapping("id", "int"), - createFieldValueMapping("twitterAccounts[].status", "enum"), - createFieldValueMapping("twitterAccounts[].status2", "enum"), - createFieldValueMapping("toDoItems[].status3", "enum"), - createFieldValueMapping("toDoItems[].status4", "enum")) + SerializerTestFixture.createFieldValueMapping("id", "int"), + SerializerTestFixture.createFieldValueMapping("twitterAccounts[].status", "enum"), + SerializerTestFixture.createFieldValueMapping("twitterAccounts[].status2", "enum"), + SerializerTestFixture.createFieldValueMapping("toDoItems[].status3", "enum"), + SerializerTestFixture.createFieldValueMapping("toDoItems[].status4", "enum")) ); + + private AvroSerializersTestFixture() { + } } diff --git a/src/test/java/com/sngular/kloadgen/serializer/GenericRecordAvroSerializersTest.java b/src/test/java/com/sngular/kloadgen/serializer/GenericRecordAvroSerializersTest.java index 1e1ad528..4aa2fee8 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/GenericRecordAvroSerializersTest.java +++ b/src/test/java/com/sngular/kloadgen/serializer/GenericRecordAvroSerializersTest.java @@ -1,19 +1,5 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_EMBEDED_AVROS_EXAMPLE; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_FILE_ISSUE; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_ISSUE; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_LOGICAL_TYPES; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_MAP; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_OPTIONAL_ENUM; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_OPTIONAL_MAP; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_SUBENTITY_ARRAY; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_UNION_RECORD; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.TEST_USER; -import static com.sngular.kloadgen.serializer.AvroSerializersTestFixture.serializerList; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.readSchema; - import java.io.File; import java.util.List; import java.util.stream.Stream; @@ -40,42 +26,48 @@ public class GenericRecordAvroSerializersTest { private static Stream getSerializerAndSchemaToTest() { - Builder builder = Stream.builder(); - serializerList.forEach(serializer -> { - Named> serializerArgument = Named.of(serializer.getClass().getName(), serializer); - File testSubentityArrayFile = TEST_SUBENTITY_ARRAY.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testSubentityArrayFile.getName(), testSubentityArrayFile), TEST_SUBENTITY_ARRAY.getSecond())); - File testEmbededAvrosExampleFile = TEST_EMBEDED_AVROS_EXAMPLE.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testEmbededAvrosExampleFile.getName(), testEmbededAvrosExampleFile), TEST_EMBEDED_AVROS_EXAMPLE.getSecond())); - File testIssueFile = TEST_ISSUE.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testIssueFile.getName(), testIssueFile), TEST_ISSUE.getSecond())); - File testOptionalEnumFile = TEST_OPTIONAL_ENUM.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testOptionalEnumFile.getName(), testOptionalEnumFile), TEST_OPTIONAL_ENUM.getSecond())); - File testFileIssueFile = TEST_FILE_ISSUE.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testFileIssueFile.getName(), testFileIssueFile), TEST_FILE_ISSUE.getSecond())); - File testLogicalTypesFile = TEST_LOGICAL_TYPES.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testLogicalTypesFile.getName(), testLogicalTypesFile), TEST_LOGICAL_TYPES.getSecond())); - File testMapFile = TEST_MAP.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testMapFile.getName(), testMapFile), TEST_MAP.getSecond())); - File testNullOnOptionalFieldsFile = TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); - builder.add( - Arguments.arguments(serializerArgument, Named.of(testNullOnOptionalFieldsFile.getName(), testNullOnOptionalFieldsFile), TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); - File testOptionalMapFile = TEST_OPTIONAL_MAP.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testOptionalMapFile.getName(), testOptionalMapFile), TEST_OPTIONAL_MAP.getSecond())); - File testNullOnOptionalFields = TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testNullOnOptionalFields.getName(), testNullOnOptionalFields), TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); - File testUnionRecordFile = TEST_UNION_RECORD.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testUnionRecordFile.getName(), testUnionRecordFile), TEST_UNION_RECORD.getSecond())); - File testUserFile = TEST_USER.getFirst(); - builder.add(Arguments.arguments(serializerArgument, Named.of(testUserFile.getName(), testUserFile), TEST_USER.getSecond())); - }); + final Builder builder = Stream.builder(); + AvroSerializersTestFixture.SERIALIZER_LIST.forEach(serializer -> extracted(serializer, builder)); return builder.build(); } + private static void extracted(final Serializer serializer, final Builder builder) { + final Named> serializerArgument = Named.of(serializer.getClass().getName(), serializer); + final File testSubentityArrayFile = AvroSerializersTestFixture.TEST_SUBENTITY_ARRAY.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testSubentityArrayFile.getName(), testSubentityArrayFile), + AvroSerializersTestFixture.TEST_SUBENTITY_ARRAY.getSecond())); + final File testEmbededAvrosExampleFile = AvroSerializersTestFixture.TEST_EMBEDED_AVROS_EXAMPLE.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testEmbededAvrosExampleFile.getName(), testEmbededAvrosExampleFile), + AvroSerializersTestFixture.TEST_EMBEDED_AVROS_EXAMPLE.getSecond())); + final File testIssueFile = AvroSerializersTestFixture.TEST_ISSUE.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testIssueFile.getName(), testIssueFile), AvroSerializersTestFixture.TEST_ISSUE.getSecond())); + final File testOptionalEnumFile = AvroSerializersTestFixture.TEST_OPTIONAL_ENUM.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testOptionalEnumFile.getName(), testOptionalEnumFile), AvroSerializersTestFixture.TEST_OPTIONAL_ENUM.getSecond())); + final File testFileIssueFile = AvroSerializersTestFixture.TEST_FILE_ISSUE.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testFileIssueFile.getName(), testFileIssueFile), AvroSerializersTestFixture.TEST_FILE_ISSUE.getSecond())); + final File testLogicalTypesFile = AvroSerializersTestFixture.TEST_LOGICAL_TYPES.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testLogicalTypesFile.getName(), testLogicalTypesFile), AvroSerializersTestFixture.TEST_LOGICAL_TYPES.getSecond())); + final File testMapFile = AvroSerializersTestFixture.TEST_MAP.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testMapFile.getName(), testMapFile), AvroSerializersTestFixture.TEST_MAP.getSecond())); + final File testNullOnOptionalFieldsFile = AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); + builder.add( + Arguments.arguments(serializerArgument, Named.of(testNullOnOptionalFieldsFile.getName(), testNullOnOptionalFieldsFile), + AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); + final File testOptionalMapFile = AvroSerializersTestFixture.TEST_OPTIONAL_MAP.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testOptionalMapFile.getName(), testOptionalMapFile), AvroSerializersTestFixture.TEST_OPTIONAL_MAP.getSecond())); + final File testNullOnOptionalFields = AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testNullOnOptionalFields.getName(), testNullOnOptionalFields), + AvroSerializersTestFixture.TEST_NULL_ON_OPTIONAL_FIELDS.getSecond())); + final File testUnionRecordFile = AvroSerializersTestFixture.TEST_UNION_RECORD.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testUnionRecordFile.getName(), testUnionRecordFile), AvroSerializersTestFixture.TEST_UNION_RECORD.getSecond())); + final File testUserFile = AvroSerializersTestFixture.TEST_USER.getFirst(); + builder.add(Arguments.arguments(serializerArgument, Named.of(testUserFile.getName(), testUserFile), AvroSerializersTestFixture.TEST_USER.getSecond())); + } + @ParameterizedTest @MethodSource("getSerializerAndSchemaToTest") - void genericAvroRecordSerializerTest(Serializer serializer, File schemaFile, List fieldValueMappings) throws Exception { - final var schemaStr = readSchema(schemaFile); + void genericAvroRecordSerializerTest(final Serializer serializer, final File schemaFile, final List fieldValueMappings) throws Exception { + final var schemaStr = SerializerTestFixture.readSchema(schemaFile); final BaseSchemaMetadata confluentBaseSchemaMetadata = new BaseSchemaMetadata<>( ConfluentSchemaMetadata.parse(new io.confluent.kafka.schemaregistry.client.SchemaMetadata(1, 1, diff --git a/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTest.java b/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTest.java index f4746ab3..922f6093 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTest.java +++ b/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTest.java @@ -1,18 +1,5 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_BASIC; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_BASIC_ARRAY; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_BASIC_NUMBER; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_COLLECTIONS; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_COMPLEX_DEFINITIONS; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_COMPLEX_DOCUMENT; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_MAP; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_MEDIUM_DOCUMENT; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_MULTIPLE_TYPE; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_MULTIPLE_TYPE_SINGLE; -import static com.sngular.kloadgen.serializer.JsonSerializerTestFixture.TEST_NESTED_COLLECTIONS; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.readSchema; - import java.io.File; import java.util.List; import java.util.stream.Stream; @@ -36,41 +23,41 @@ public class JsonSerializerTest { private static final SchemaProcessor JSON_SCHEMA_PROCESSOR = new SchemaProcessor(); - private static final GenericJsonRecordSerializer serializer = new GenericJsonRecordSerializer(); + private static final GenericJsonRecordSerializer SERIALIZER = new GenericJsonRecordSerializer(); private static Stream getSchemaToTest() { - Builder builder = Stream.builder(); + final Builder builder = Stream.builder(); - File testBasicFile = TEST_BASIC.getFirst(); - builder.add(Arguments.arguments(Named.of(testBasicFile.getName(), testBasicFile), TEST_BASIC.getSecond())); - File testBasicArraysFile = TEST_BASIC_ARRAY.getFirst(); - builder.add(Arguments.arguments(Named.of(testBasicArraysFile.getName(), testBasicArraysFile), TEST_BASIC_ARRAY.getSecond())); - File testBasicNumberFile = TEST_BASIC_NUMBER.getFirst(); - builder.add(Arguments.arguments(Named.of(testBasicNumberFile.getName(), testBasicNumberFile), TEST_BASIC_NUMBER.getSecond())); - File testCollectionsFile = TEST_COLLECTIONS.getFirst(); - builder.add(Arguments.arguments(Named.of(testCollectionsFile.getName(), testCollectionsFile), TEST_COLLECTIONS.getSecond())); - File testComplexDefinitionsFile = TEST_COMPLEX_DEFINITIONS.getFirst(); - builder.add(Arguments.arguments(Named.of(testComplexDefinitionsFile.getName(), testComplexDefinitionsFile), TEST_COMPLEX_DEFINITIONS.getSecond())); - File testComplexDocumentFile = TEST_COMPLEX_DOCUMENT.getFirst(); - builder.add(Arguments.arguments(Named.of(testComplexDocumentFile.getName(), testComplexDocumentFile), TEST_COMPLEX_DOCUMENT.getSecond())); - File testMediumDocumentFile = TEST_MEDIUM_DOCUMENT.getFirst(); - builder.add(Arguments.arguments(Named.of(testMediumDocumentFile.getName(), testMediumDocumentFile), TEST_MEDIUM_DOCUMENT.getSecond())); - File testMultipleTypeFile = TEST_MULTIPLE_TYPE.getFirst(); - builder.add(Arguments.arguments(Named.of(testMultipleTypeFile.getName(), testMultipleTypeFile), TEST_MULTIPLE_TYPE.getSecond())); - File testMultipleTypeSingleFile = TEST_MULTIPLE_TYPE_SINGLE.getFirst(); - builder.add(Arguments.arguments(Named.of(testMultipleTypeSingleFile.getName(), testMultipleTypeSingleFile), TEST_MULTIPLE_TYPE_SINGLE.getSecond())); - File testNestedCollectionsFile = TEST_NESTED_COLLECTIONS.getFirst(); - builder.add(Arguments.arguments(Named.of(testNestedCollectionsFile.getName(), testNestedCollectionsFile), TEST_NESTED_COLLECTIONS.getSecond())); - File testMapFile = TEST_MAP.getFirst(); - builder.add(Arguments.arguments(Named.of(testMapFile.getName(), testMapFile), TEST_MAP.getSecond())); + final File testBasicFile = JsonSerializerTestFixture.TEST_BASIC.getFirst(); + builder.add(Arguments.arguments(Named.of(testBasicFile.getName(), testBasicFile), JsonSerializerTestFixture.TEST_BASIC.getSecond())); + final File testBasicArraysFile = JsonSerializerTestFixture.TEST_BASIC_ARRAY.getFirst(); + builder.add(Arguments.arguments(Named.of(testBasicArraysFile.getName(), testBasicArraysFile), JsonSerializerTestFixture.TEST_BASIC_ARRAY.getSecond())); + final File testBasicNumberFile = JsonSerializerTestFixture.TEST_BASIC_NUMBER.getFirst(); + builder.add(Arguments.arguments(Named.of(testBasicNumberFile.getName(), testBasicNumberFile), JsonSerializerTestFixture.TEST_BASIC_NUMBER.getSecond())); + final File testCollectionsFile = JsonSerializerTestFixture.TEST_COLLECTIONS.getFirst(); + builder.add(Arguments.arguments(Named.of(testCollectionsFile.getName(), testCollectionsFile), JsonSerializerTestFixture.TEST_COLLECTIONS.getSecond())); + final File testComplexDefinitionsFile = JsonSerializerTestFixture.TEST_COMPLEX_DEFINITIONS.getFirst(); + builder.add(Arguments.arguments(Named.of(testComplexDefinitionsFile.getName(), testComplexDefinitionsFile), JsonSerializerTestFixture.TEST_COMPLEX_DEFINITIONS.getSecond())); + final File testComplexDocumentFile = JsonSerializerTestFixture.TEST_COMPLEX_DOCUMENT.getFirst(); + builder.add(Arguments.arguments(Named.of(testComplexDocumentFile.getName(), testComplexDocumentFile), JsonSerializerTestFixture.TEST_COMPLEX_DOCUMENT.getSecond())); + final File testMediumDocumentFile = JsonSerializerTestFixture.TEST_MEDIUM_DOCUMENT.getFirst(); + builder.add(Arguments.arguments(Named.of(testMediumDocumentFile.getName(), testMediumDocumentFile), JsonSerializerTestFixture.TEST_MEDIUM_DOCUMENT.getSecond())); + final File testMultipleTypeFile = JsonSerializerTestFixture.TEST_MULTIPLE_TYPE.getFirst(); + builder.add(Arguments.arguments(Named.of(testMultipleTypeFile.getName(), testMultipleTypeFile), JsonSerializerTestFixture.TEST_MULTIPLE_TYPE.getSecond())); + final File testMultipleTypeSingleFile = JsonSerializerTestFixture.TEST_MULTIPLE_TYPE_SINGLE.getFirst(); + builder.add(Arguments.arguments(Named.of(testMultipleTypeSingleFile.getName(), testMultipleTypeSingleFile), JsonSerializerTestFixture.TEST_MULTIPLE_TYPE_SINGLE.getSecond())); + final File testNestedCollectionsFile = JsonSerializerTestFixture.TEST_NESTED_COLLECTIONS.getFirst(); + builder.add(Arguments.arguments(Named.of(testNestedCollectionsFile.getName(), testNestedCollectionsFile), JsonSerializerTestFixture.TEST_NESTED_COLLECTIONS.getSecond())); + final File testMapFile = JsonSerializerTestFixture.TEST_MAP.getFirst(); + builder.add(Arguments.arguments(Named.of(testMapFile.getName(), testMapFile), JsonSerializerTestFixture.TEST_MAP.getSecond())); return builder.build(); } @ParameterizedTest @MethodSource("getSchemaToTest") - void recordSerializersTestLogicalTypes(File schemaFile, List fieldValueMappings) throws Exception { - final var schemaStr = readSchema(schemaFile); + final void recordSerializersTestLogicalTypes(final File schemaFile, final List fieldValueMappings) throws Exception { + final var schemaStr = SerializerTestFixture.readSchema(schemaFile); final BaseSchemaMetadata confluentBaseSchemaMetadata = new BaseSchemaMetadata<>( ConfluentSchemaMetadata.parse(new io.confluent.kafka.schemaregistry.client.SchemaMetadata(1, 1, @@ -80,7 +67,7 @@ void recordSerializersTestLogicalTypes(File schemaFile, List JSON_SCHEMA_PROCESSOR.processSchema(SchemaTypeEnum.JSON, parsedSchema, confluentBaseSchemaMetadata, fieldValueMappings); final var objectNode = JSON_SCHEMA_PROCESSOR.next(); - final var message = serializer.serialize("the-topic", (ObjectNode) objectNode); + final var message = SERIALIZER.serialize("the-topic", (ObjectNode) objectNode); Assertions.assertThat(message).isNotNull(); } diff --git a/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTestFixture.java b/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTestFixture.java index 91e9baea..9e3da2ac 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTestFixture.java +++ b/src/test/java/com/sngular/kloadgen/serializer/JsonSerializerTestFixture.java @@ -1,7 +1,5 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.createFieldValueMapping; - import java.io.File; import java.util.Arrays; import java.util.List; @@ -10,102 +8,102 @@ import com.sngular.kloadgen.testutil.FileHelper; import org.apache.commons.math3.util.Pair; -public class JsonSerializerTestFixture { +final class JsonSerializerTestFixture { static final Pair> TEST_BASIC = new Pair<>( new FileHelper().getFile("/jsonschema/basic.jcs"), Arrays.asList( - createFieldValueMapping("firstName", "string"), - createFieldValueMapping("lastName", "string"), - createFieldValueMapping("age", "number")) + SerializerTestFixture.createFieldValueMapping("firstName", "string"), + SerializerTestFixture.createFieldValueMapping("lastName", "string"), + SerializerTestFixture.createFieldValueMapping("age", "number")) ); static final Pair> TEST_BASIC_ARRAY = new Pair<>( new FileHelper().getFile("/jsonschema/basic-array.jcs"), Arrays.asList( - createFieldValueMapping("fruits[]", "string-array"), - createFieldValueMapping("vegetables[].veggieName", "string"), - createFieldValueMapping("vegetables[].veggieLike", "boolean")) + SerializerTestFixture.createFieldValueMapping("fruits[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("vegetables[].veggieName", "string"), + SerializerTestFixture.createFieldValueMapping("vegetables[].veggieLike", "boolean")) ); static final Pair> TEST_BASIC_NUMBER = new Pair<>( new FileHelper().getFile("/jsonschema/basic-number.jcs"), Arrays.asList( - createFieldValueMapping("latitude", "number"), - createFieldValueMapping("longitude", "number")) + SerializerTestFixture.createFieldValueMapping("latitude", "number"), + SerializerTestFixture.createFieldValueMapping("longitude", "number")) ); static final Pair> TEST_COLLECTIONS = new Pair<>( new FileHelper().getFile("/jsonschema/collections.jcs"), Arrays.asList( - createFieldValueMapping("mapOfStrings[:]", "string-map"), - createFieldValueMapping("arrayOfObjectsOfBasicTypes[].stringOfObject", "string"), - createFieldValueMapping("arrayOfObjectsOfBasicTypes[].numberOfObject", "number"), - createFieldValueMapping("objectOfCollectionsOfBasicTypes.arrayOfStrings[]", "string-array"), - createFieldValueMapping("objectOfCollectionsOfBasicTypes.mapOfIntegers[:]", "number-map"), - createFieldValueMapping("objectOfCollectionsOfBasicTypes.stringControl", "string"), - createFieldValueMapping("objectOfCollectionsOfObject.stringControl", "string"), - createFieldValueMapping("objectOfCollectionsOfObject.arrayOfObjectsPerson[].namePerson", "string"), - createFieldValueMapping("objectOfCollectionsOfObject.arrayOfObjectsPerson[].phonePerson", "number"), - createFieldValueMapping("objectOfCollectionsOfObject.mapOfObjectsDog[:].nameDog", "string"), - createFieldValueMapping("objectOfCollectionsOfObject.mapOfObjectsDog[:].vetData.dogId", "number"), - createFieldValueMapping("objectOfCollectionsOfObject.mapOfObjectsDog[:].vetData.breedName", "string")) + SerializerTestFixture.createFieldValueMapping("mapOfStrings[:]", "string-map"), + SerializerTestFixture.createFieldValueMapping("arrayOfObjectsOfBasicTypes[].stringOfObject", "string"), + SerializerTestFixture.createFieldValueMapping("arrayOfObjectsOfBasicTypes[].numberOfObject", "number"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfBasicTypes.arrayOfStrings[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfBasicTypes.mapOfIntegers[:]", "number-map"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfBasicTypes.stringControl", "string"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfObject.stringControl", "string"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfObject.arrayOfObjectsPerson[].namePerson", "string"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfObject.arrayOfObjectsPerson[].phonePerson", "number"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfObject.mapOfObjectsDog[:].nameDog", "string"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfObject.mapOfObjectsDog[:].vetData.dogId", "number"), + SerializerTestFixture.createFieldValueMapping("objectOfCollectionsOfObject.mapOfObjectsDog[:].vetData.breedName", "string")) ); static final Pair> TEST_COMPLEX_DEFINITIONS = new Pair<>( new FileHelper().getFile("/jsonschema/complex-definitions.jcs"), Arrays.asList( - createFieldValueMapping("objectOfDefinitions.stringControl", "string"), - createFieldValueMapping("objectOfDefinitions.arrayOfStrings[]", "string-array"), - createFieldValueMapping("objectOfDefinitions.mapOfStrings[:]", "string-map"), - createFieldValueMapping("arrayOfObjects[].stringOfObject", "string"), - createFieldValueMapping("arrayOfObjects[].numberOfObject", "number"), - createFieldValueMapping("mapOfObjects[:].arrayOfInternalObject[]", "string-array"), - createFieldValueMapping("mapOfMaps[:][:].stringControlObject", "string"), - createFieldValueMapping("mapOfMaps[:][:].arrayOfArraysOfStrings[][]", "string-array-array")) + SerializerTestFixture.createFieldValueMapping("objectOfDefinitions.stringControl", "string"), + SerializerTestFixture.createFieldValueMapping("objectOfDefinitions.arrayOfStrings[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("objectOfDefinitions.mapOfStrings[:]", "string-map"), + SerializerTestFixture.createFieldValueMapping("arrayOfObjects[].stringOfObject", "string"), + SerializerTestFixture.createFieldValueMapping("arrayOfObjects[].numberOfObject", "number"), + SerializerTestFixture.createFieldValueMapping("mapOfObjects[:].arrayOfInternalObject[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("mapOfMaps[:][:].stringControlObject", "string"), + SerializerTestFixture.createFieldValueMapping("mapOfMaps[:][:].arrayOfArraysOfStrings[][]", "string-array-array")) ); static final Pair> TEST_COMPLEX_DOCUMENT = new Pair<>( new FileHelper().getFile("/jsonschema/complex-document.jcs"), Arrays.asList( - createFieldValueMapping("_id", "string"), - createFieldValueMapping("userId", "number"), - createFieldValueMapping("storeId", "number"), - createFieldValueMapping("snapshotId", "string"), - createFieldValueMapping("addressId", "string"), - createFieldValueMapping("addressLine", "string"), - createFieldValueMapping("alias", "string"), - createFieldValueMapping("contactInformation.email", "string"), - createFieldValueMapping("contactInformation.firstName", "string"), - createFieldValueMapping("contactInformation.middleName", "string"), - createFieldValueMapping("contactInformation.lastName", "string"), - createFieldValueMapping("contactInformation.honorific", "string"), - createFieldValueMapping("contactInformation.phones[].prefix", "string"), - createFieldValueMapping("contactInformation.phones[].number", "string"), - createFieldValueMapping("countryCode", "string"), - createFieldValueMapping("location.streetName", "string"), - createFieldValueMapping("location.streetNumber", "string"), - createFieldValueMapping("location.floor", "string"), - createFieldValueMapping("location.door", "string"), - createFieldValueMapping("location.doorCode", "string"), - createFieldValueMapping("location.zipCode", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level1.code", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level1.freeForm", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level2.code", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level2.freeForm", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level3.code", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level3.freeForm", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level4.code", "string"), - createFieldValueMapping("geopoliticalSubdivisions.level4.freeForm", "string"), - createFieldValueMapping("_metadata.createdAt", "timestamp"), - createFieldValueMapping("_metadata.createdBy", "string"), - createFieldValueMapping("_metadata.lastUpdatedAt", "timestamp"), - createFieldValueMapping("_metadata.lastUpdatedBy", "string"), - createFieldValueMapping("_metadata.deletedAt", "timestamp"), - createFieldValueMapping("_metadata.projectVersion", "string"), - createFieldValueMapping("_metadata.projectName", "string"), - createFieldValueMapping("_metadata.deletedBy", "string"), - createFieldValueMapping("_metadata.schema", "number"), + SerializerTestFixture.createFieldValueMapping("_id", "string"), + SerializerTestFixture.createFieldValueMapping("userId", "number"), + SerializerTestFixture.createFieldValueMapping("storeId", "number"), + SerializerTestFixture.createFieldValueMapping("snapshotId", "string"), + SerializerTestFixture.createFieldValueMapping("addressId", "string"), + SerializerTestFixture.createFieldValueMapping("addressLine", "string"), + SerializerTestFixture.createFieldValueMapping("alias", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.email", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.firstName", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.middleName", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.lastName", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.honorific", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.phones[].prefix", "string"), + SerializerTestFixture.createFieldValueMapping("contactInformation.phones[].number", "string"), + SerializerTestFixture.createFieldValueMapping("countryCode", "string"), + SerializerTestFixture.createFieldValueMapping("location.streetName", "string"), + SerializerTestFixture.createFieldValueMapping("location.streetNumber", "string"), + SerializerTestFixture.createFieldValueMapping("location.floor", "string"), + SerializerTestFixture.createFieldValueMapping("location.door", "string"), + SerializerTestFixture.createFieldValueMapping("location.doorCode", "string"), + SerializerTestFixture.createFieldValueMapping("location.zipCode", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level1.code", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level1.freeForm", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level2.code", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level2.freeForm", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level3.code", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level3.freeForm", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level4.code", "string"), + SerializerTestFixture.createFieldValueMapping("geopoliticalSubdivisions.level4.freeForm", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.createdAt", "timestamp"), + SerializerTestFixture.createFieldValueMapping("_metadata.createdBy", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.lastUpdatedAt", "timestamp"), + SerializerTestFixture.createFieldValueMapping("_metadata.lastUpdatedBy", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.deletedAt", "timestamp"), + SerializerTestFixture.createFieldValueMapping("_metadata.projectVersion", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.projectName", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.deletedBy", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.schema", "number"), FieldValueMapping.builder().fieldName("_entity").fieldType("enum").valueLength(0).fieldValueList("[AddressSnapshot]").required(true) .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("_class").fieldType("enum").valueLength(0).fieldValueList("[AddressSnapshot]").required(true) @@ -119,21 +117,21 @@ public class JsonSerializerTestFixture { .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("_entity").fieldType("enum").valueLength(0).fieldValueList("[OrderPublicDetailDocument]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("_metadata.createdAt", "number"), - createFieldValueMapping("_metadata.createdBy", "string"), - createFieldValueMapping("_metadata.deletedAt", "number"), - createFieldValueMapping("_metadata.deletedBy", "string"), - createFieldValueMapping("_metadata.lastUpdatedAt", "number"), - createFieldValueMapping("_metadata.lastUpdatedBy", "string"), - createFieldValueMapping("_metadata.projectName", "string"), - createFieldValueMapping("_metadata.projectVersion", "string"), - createFieldValueMapping("_metadata.schema", "number"), - createFieldValueMapping("orderId", "string"), - createFieldValueMapping("storeId", "number"), - createFieldValueMapping("paymentId", "string"), - createFieldValueMapping("parentId", "string"), - createFieldValueMapping("externalId", "string"), - createFieldValueMapping("editorId", "number"), + SerializerTestFixture.createFieldValueMapping("_metadata.createdAt", "number"), + SerializerTestFixture.createFieldValueMapping("_metadata.createdBy", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.deletedAt", "number"), + SerializerTestFixture.createFieldValueMapping("_metadata.deletedBy", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.lastUpdatedAt", "number"), + SerializerTestFixture.createFieldValueMapping("_metadata.lastUpdatedBy", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.projectName", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.projectVersion", "string"), + SerializerTestFixture.createFieldValueMapping("_metadata.schema", "number"), + SerializerTestFixture.createFieldValueMapping("orderId", "string"), + SerializerTestFixture.createFieldValueMapping("storeId", "number"), + SerializerTestFixture.createFieldValueMapping("paymentId", "string"), + SerializerTestFixture.createFieldValueMapping("parentId", "string"), + SerializerTestFixture.createFieldValueMapping("externalId", "string"), + SerializerTestFixture.createFieldValueMapping("editorId", "number"), FieldValueMapping.builder().fieldName("type").fieldType("enum").valueLength(0) .fieldValueList("[REGULAR, REPLACEMENT, EXCHANGE, REPOSITION, ONLINE_EXCHANGE, STORE_EXCHANGE]").required(true) .isAncestorRequired(true).build(), @@ -151,111 +149,113 @@ public class JsonSerializerTestFixture { .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("manualReviewStatus").fieldType("enum").valueLength(0).fieldValueList("[NA, PENDING, ACCEPTED, REFUSED]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("customerId", "number"), + SerializerTestFixture.createFieldValueMapping("customerId", "number"), FieldValueMapping.builder().fieldName("customerType").fieldType("enum").valueLength(0).fieldValueList("[REGISTERED, GUEST]").required(true) .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("guestAction").fieldType("enum").valueLength(0).fieldValueList("[NO_ACTION, CONVERSION, LINK_ACCOUNT]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("locale", "string"), - createFieldValueMapping("preorder", "boolean"), - createFieldValueMapping("fastSint", "boolean"), - createFieldValueMapping("pvpTaxesIncluded", "boolean"), - createFieldValueMapping("placedDatetime", "string"), - createFieldValueMapping("cancelledDatetime", "string"), - createFieldValueMapping("lastUpdateDatetime", "string"), - createFieldValueMapping("snapshotDatetime", "string"), - createFieldValueMapping("duty.amount.value", "number"), - createFieldValueMapping("duty.amount.currency", "string"), - createFieldValueMapping("duty.amount.exponent", "number"), - createFieldValueMapping("duty.confirmedAmount.value", "number"), - createFieldValueMapping("duty.confirmedAmount.currency", "string"), - createFieldValueMapping("duty.confirmedAmount.exponent", "number"), + SerializerTestFixture.createFieldValueMapping("locale", "string"), + SerializerTestFixture.createFieldValueMapping("preorder", "boolean"), + SerializerTestFixture.createFieldValueMapping("fastSint", "boolean"), + SerializerTestFixture.createFieldValueMapping("pvpTaxesIncluded", "boolean"), + SerializerTestFixture.createFieldValueMapping("placedDatetime", "string"), + SerializerTestFixture.createFieldValueMapping("cancelledDatetime", "string"), + SerializerTestFixture.createFieldValueMapping("lastUpdateDatetime", "string"), + SerializerTestFixture.createFieldValueMapping("snapshotDatetime", "string"), + SerializerTestFixture.createFieldValueMapping("duty.amount.value", "number"), + SerializerTestFixture.createFieldValueMapping("duty.amount.currency", "string"), + SerializerTestFixture.createFieldValueMapping("duty.amount.exponent", "number"), + SerializerTestFixture.createFieldValueMapping("duty.confirmedAmount.value", "number"), + SerializerTestFixture.createFieldValueMapping("duty.confirmedAmount.currency", "string"), + SerializerTestFixture.createFieldValueMapping("duty.confirmedAmount.exponent", "number"), FieldValueMapping.builder().fieldName("billing.status").fieldType("enum").valueLength(0).fieldValueList("[NOT_APPLIED, PENDING, BILLED]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("billing.addressId", "string"), - createFieldValueMapping("origin.systemCode", "string"), + SerializerTestFixture.createFieldValueMapping("billing.addressId", "string"), + SerializerTestFixture.createFieldValueMapping("origin.systemCode", "string"), FieldValueMapping.builder().fieldName("origin.systemType").fieldType("enum").valueLength(0).fieldValueList("[CHECKOUT, MPS, BACKOFFICE, STORE]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("origin.systemUser", "string"), - createFieldValueMapping("origin.systemDeviceId", "string"), - createFieldValueMapping("origin.deviceType", "string"), - createFieldValueMapping("origin.deviceUserAgent", "string"), - createFieldValueMapping("orderItems[].id", "string"), - createFieldValueMapping("orderItems[].partNumber", "string"), + SerializerTestFixture.createFieldValueMapping("origin.systemUser", "string"), + SerializerTestFixture.createFieldValueMapping("origin.systemDeviceId", "string"), + SerializerTestFixture.createFieldValueMapping("origin.deviceType", "string"), + SerializerTestFixture.createFieldValueMapping("origin.deviceUserAgent", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].id", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].partNumber", "string"), FieldValueMapping.builder().fieldName("orderItems[].type").fieldType("enum").valueLength(0) .fieldValueList("[PRODUCT, VIRTUAL_GIFT_CARD, PHYSICAL_GIFT_CARD, SHIPPING_COST, SERVICE]").required(true) .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("orderItems[].status").fieldType("enum").valueLength(0).fieldValueList("[RECEIVED, PREPARING, SHIPPED, CLOSED, CANCELLED]") .required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("orderItems[].statusReason", "string"), - createFieldValueMapping("orderItems[].statusDatetime", "string"), - createFieldValueMapping("orderItems[].pvpAmount.value", "number"), - createFieldValueMapping("orderItems[].pvpAmount.currency", "string"), - createFieldValueMapping("orderItems[].pvpAmount.exponent", "number"), - createFieldValueMapping("orderItems[].taxesAmount.value", "number"), - createFieldValueMapping("orderItems[].taxesAmount.currency", "string"), - createFieldValueMapping("orderItems[].taxesAmount.exponent", "number"), - createFieldValueMapping("orderItems[].taxesPercentage", "number"), - createFieldValueMapping("orderItems[].initialMinimumDate", "string"), - createFieldValueMapping("orderItems[].initialMaximumDate", "string"), - createFieldValueMapping("orderItems[].adjustment.value", "number"), - createFieldValueMapping("orderItems[].adjustment.currency", "string"), - createFieldValueMapping("orderItems[].adjustment.exponent", "number"), - createFieldValueMapping("orderItems[].product.catalogEntryId", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].statusReason", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].statusDatetime", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].pvpAmount.value", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].pvpAmount.currency", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].pvpAmount.exponent", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].taxesAmount.value", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].taxesAmount.currency", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].taxesAmount.exponent", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].taxesPercentage", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].initialMinimumDate", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].initialMaximumDate", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].adjustment.value", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].adjustment.currency", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].adjustment.exponent", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].product.catalogEntryId", "number"), FieldValueMapping.builder().fieldName("orderItems[].product.stockMode").fieldType("enum").valueLength(0) .fieldValueList("[UNKNOWN, NORMAL, TRANSIT_PRESALE, VIRTUALSTOCK_PRESALE, SOD_PRESALE, PREORDER]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("orderItems[].virtualGiftCard.catalogEntryId", "number"), - createFieldValueMapping("orderItems[].virtualGiftCard.senderName", "string"), - createFieldValueMapping("orderItems[].virtualGiftCard.message", "string"), - createFieldValueMapping("orderItems[].virtualGiftCard.style", "string"), - createFieldValueMapping("orderItems[].virtualGiftCard.receiverName", "string"), - createFieldValueMapping("orderItems[].virtualGiftCard.deliveryDate", "string"), - createFieldValueMapping("orderItems[].physicalGiftCard.catalogEntryId", "number"), - createFieldValueMapping("orderItems[].physicalGiftCard.senderName", "string"), - createFieldValueMapping("orderItems[].physicalGiftCard.message", "string"), - createFieldValueMapping("orderItems[].physicalGiftCard.receiverPhonePrefix", "string"), - createFieldValueMapping("orderItems[].physicalGiftCard.receiverPhoneNumber", "string")) + SerializerTestFixture.createFieldValueMapping("orderItems[].virtualGiftCard.catalogEntryId", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].virtualGiftCard.senderName", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].virtualGiftCard.message", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].virtualGiftCard.style", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].virtualGiftCard.receiverName", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].virtualGiftCard.deliveryDate", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].physicalGiftCard.catalogEntryId", "number"), + SerializerTestFixture.createFieldValueMapping("orderItems[].physicalGiftCard.senderName", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].physicalGiftCard.message", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].physicalGiftCard.receiverPhonePrefix", "string"), + SerializerTestFixture.createFieldValueMapping("orderItems[].physicalGiftCard.receiverPhoneNumber", "string")) ); static final Pair> TEST_MULTIPLE_TYPE = new Pair<>( new FileHelper().getFile("/jsonschema/multiple-type.jcs"), Arrays.asList( - createFieldValueMapping("id", "number"), - createFieldValueMapping("version", "number"), - createFieldValueMapping("dtype", "string"), - createFieldValueMapping("timestamp", "string"), - createFieldValueMapping("event_type", "string")) + SerializerTestFixture.createFieldValueMapping("id", "number"), + SerializerTestFixture.createFieldValueMapping("version", "number"), + SerializerTestFixture.createFieldValueMapping("dtype", "string"), + SerializerTestFixture.createFieldValueMapping("timestamp", "string"), + SerializerTestFixture.createFieldValueMapping("event_type", "string")) ); static final Pair> TEST_MULTIPLE_TYPE_SINGLE = new Pair<>( new FileHelper().getFile("/jsonschema/multiple-type-single.jcs"), Arrays.asList( - createFieldValueMapping("id", "number")) + SerializerTestFixture.createFieldValueMapping("id", "number")) ); static final Pair> TEST_NESTED_COLLECTIONS = new Pair<>( new FileHelper().getFile("/jsonschema/nested-collections.jcs"), Arrays.asList( - createFieldValueMapping("arrayOfMapsOfObjects[][:].stringObject", "string"), - createFieldValueMapping("arrayOfMapsOfObjects[][:].numberObject", "number"), - createFieldValueMapping("arrayOfArraysOfStrings[][]", "string-array-array"), - createFieldValueMapping("mapOfArraysOfStrings[:][]", "string-array-map"), - createFieldValueMapping("mapOfMapsOfObjects[:][:].name4Object", "string"), - createFieldValueMapping("mapOfMapsOfObjects[:][:].number4Object", "number"), - createFieldValueMapping("mapOfObjectsOfCollections[:].arrayOfMapsOfObject[][:].stringControl", "string"), - createFieldValueMapping("mapOfObjectsOfCollections[:].arrayOfMapsOfObject[][:].numberControl", "number")) + SerializerTestFixture.createFieldValueMapping("arrayOfMapsOfObjects[][:].stringObject", "string"), + SerializerTestFixture.createFieldValueMapping("arrayOfMapsOfObjects[][:].numberObject", "number"), + SerializerTestFixture.createFieldValueMapping("arrayOfArraysOfStrings[][]", "string-array-array"), + SerializerTestFixture.createFieldValueMapping("mapOfArraysOfStrings[:][]", "string-array-map"), + SerializerTestFixture.createFieldValueMapping("mapOfMapsOfObjects[:][:].name4Object", "string"), + SerializerTestFixture.createFieldValueMapping("mapOfMapsOfObjects[:][:].number4Object", "number"), + SerializerTestFixture.createFieldValueMapping("mapOfObjectsOfCollections[:].arrayOfMapsOfObject[][:].stringControl", "string"), + SerializerTestFixture.createFieldValueMapping("mapOfObjectsOfCollections[:].arrayOfMapsOfObject[][:].numberControl", "number")) ); static final Pair> TEST_MAP = new Pair<>( new FileHelper().getFile("/jsonschema/test-map.jcs"), Arrays.asList( - createFieldValueMapping("firstName", "string"), - createFieldValueMapping("lastName", "string"), - createFieldValueMapping("age", "number"), - createFieldValueMapping("testMap.itemType[:]", "number-map"), - createFieldValueMapping("testMap.itemTipo[:]", "string-map")) + SerializerTestFixture.createFieldValueMapping("firstName", "string"), + SerializerTestFixture.createFieldValueMapping("lastName", "string"), + SerializerTestFixture.createFieldValueMapping("age", "number"), + SerializerTestFixture.createFieldValueMapping("testMap.itemType[:]", "number-map"), + SerializerTestFixture.createFieldValueMapping("testMap.itemTipo[:]", "string-map")) ); + private JsonSerializerTestFixture() { + } } diff --git a/src/test/java/com/sngular/kloadgen/serializer/ProtobufSerializerTest.java b/src/test/java/com/sngular/kloadgen/serializer/ProtobufSerializerTest.java index c53f7b72..79e16c1d 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/ProtobufSerializerTest.java +++ b/src/test/java/com/sngular/kloadgen/serializer/ProtobufSerializerTest.java @@ -1,21 +1,9 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_COMPLETE_PROTO; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_COMPLEX; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_DATE_TIME; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_DEVE; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_EASY; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_EMBEDDED_TYPE; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_ENUM; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_GOOGLE_TYPES; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_ISSUE_311; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_MAP; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_ONE_OF; -import static com.sngular.kloadgen.serializer.ProtobuffSerializerTestFixture.TEST_PROVIDED; - import java.io.File; import java.io.IOException; import java.util.List; +import java.util.Locale; import java.util.stream.Stream; import java.util.stream.Stream.Builder; @@ -25,8 +13,13 @@ import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata; import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentSchemaMetadata; import com.sngular.kloadgen.testutil.SchemaParseUtil; +import com.sngular.kloadgen.util.SchemaRegistryKeyHelper; import io.confluent.kafka.schemaregistry.ParsedSchema; import lombok.extern.slf4j.Slf4j; +import org.apache.jmeter.threads.JMeterContext; +import org.apache.jmeter.threads.JMeterContextService; +import org.apache.jmeter.threads.JMeterVariables; +import org.apache.jmeter.util.JMeterUtils; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Named; @@ -42,42 +35,49 @@ class ProtobufSerializerTest { @BeforeEach void setUp() { protobufSerializer = new ProtobufSerializer(); + final File file = new File("src/test/resources"); + final String absolutePath = file.getAbsolutePath(); + JMeterUtils.loadJMeterProperties(absolutePath + "/kloadgen.properties"); + final JMeterContext jmcx = JMeterContextService.getContext(); + jmcx.setVariables(new JMeterVariables()); + JMeterUtils.setLocale(Locale.ENGLISH); + JMeterContextService.getContext().getProperties().put(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, "CONFLUENT"); } private static Stream getSchemaToTest() { - Builder builder = Stream.builder(); + final Builder builder = Stream.builder(); - File testCompleteProtoFile = TEST_COMPLETE_PROTO.getFirst(); - builder.add(Arguments.arguments(Named.of(testCompleteProtoFile.getName(), testCompleteProtoFile), TEST_COMPLETE_PROTO.getSecond())); - File testComplexFile = TEST_COMPLEX.getFirst(); - builder.add(Arguments.arguments(Named.of(testComplexFile.getName(), testComplexFile), TEST_COMPLEX.getSecond())); - File testDateTimeFile = TEST_DATE_TIME.getFirst(); - builder.add(Arguments.arguments(Named.of(testDateTimeFile.getName(), testDateTimeFile), TEST_DATE_TIME.getSecond())); - File testDeveFile = TEST_DEVE.getFirst(); - builder.add(Arguments.arguments(Named.of(testDeveFile.getName(), testDeveFile), TEST_DEVE.getSecond())); - File testEasyFile = TEST_EASY.getFirst(); - builder.add(Arguments.arguments(Named.of(testEasyFile.getName(), testEasyFile), TEST_EASY.getSecond())); - File testEmbeddedTypeFile = TEST_EMBEDDED_TYPE.getFirst(); - builder.add(Arguments.arguments(Named.of(testEmbeddedTypeFile.getName(), testEmbeddedTypeFile), TEST_EMBEDDED_TYPE.getSecond())); - File testEnumFile = TEST_ENUM.getFirst(); - builder.add(Arguments.arguments(Named.of(testEnumFile.getName(), testEnumFile), TEST_ENUM.getSecond())); - File testGoogleTypesFile = TEST_GOOGLE_TYPES.getFirst(); - builder.add(Arguments.arguments(Named.of(testGoogleTypesFile.getName(), testGoogleTypesFile), TEST_GOOGLE_TYPES.getSecond())); - File testIssue311File = TEST_ISSUE_311.getFirst(); - builder.add(Arguments.arguments(Named.of(testIssue311File.getName(), testIssue311File), TEST_ISSUE_311.getSecond())); - File testMapFile = TEST_MAP.getFirst(); - builder.add(Arguments.arguments(Named.of(testMapFile.getName(), testMapFile), TEST_MAP.getSecond())); - File testOneOfFile = TEST_ONE_OF.getFirst(); - builder.add(Arguments.arguments(Named.of(testOneOfFile.getName(), testOneOfFile), TEST_ONE_OF.getSecond())); - File testProvidedFile = TEST_PROVIDED.getFirst(); - builder.add(Arguments.arguments(Named.of(testProvidedFile.getName(), testProvidedFile), TEST_PROVIDED.getSecond())); + final File testCompleteProtoFile = ProtobuffSerializerTestFixture.TEST_COMPLETE_PROTO.getFirst(); + builder.add(Arguments.arguments(Named.of(testCompleteProtoFile.getName(), testCompleteProtoFile), ProtobuffSerializerTestFixture.TEST_COMPLETE_PROTO.getSecond())); + final File testComplexFile = ProtobuffSerializerTestFixture.TEST_COMPLEX.getFirst(); + builder.add(Arguments.arguments(Named.of(testComplexFile.getName(), testComplexFile), ProtobuffSerializerTestFixture.TEST_COMPLEX.getSecond())); + final File testDateTimeFile = ProtobuffSerializerTestFixture.TEST_DATE_TIME.getFirst(); + builder.add(Arguments.arguments(Named.of(testDateTimeFile.getName(), testDateTimeFile), ProtobuffSerializerTestFixture.TEST_DATE_TIME.getSecond())); + final File testDeveFile = ProtobuffSerializerTestFixture.TEST_DEVE.getFirst(); + builder.add(Arguments.arguments(Named.of(testDeveFile.getName(), testDeveFile), ProtobuffSerializerTestFixture.TEST_DEVE.getSecond())); + final File testEasyFile = ProtobuffSerializerTestFixture.TEST_EASY.getFirst(); + builder.add(Arguments.arguments(Named.of(testEasyFile.getName(), testEasyFile), ProtobuffSerializerTestFixture.TEST_EASY.getSecond())); + final File testEmbeddedTypeFile = ProtobuffSerializerTestFixture.TEST_EMBEDDED_TYPE.getFirst(); + builder.add(Arguments.arguments(Named.of(testEmbeddedTypeFile.getName(), testEmbeddedTypeFile), ProtobuffSerializerTestFixture.TEST_EMBEDDED_TYPE.getSecond())); + final File testEnumFile = ProtobuffSerializerTestFixture.TEST_ENUM.getFirst(); + builder.add(Arguments.arguments(Named.of(testEnumFile.getName(), testEnumFile), ProtobuffSerializerTestFixture.TEST_ENUM.getSecond())); + final File testGoogleTypesFile = ProtobuffSerializerTestFixture.TEST_GOOGLE_TYPES.getFirst(); + builder.add(Arguments.arguments(Named.of(testGoogleTypesFile.getName(), testGoogleTypesFile), ProtobuffSerializerTestFixture.TEST_GOOGLE_TYPES.getSecond())); + final File testIssue311File = ProtobuffSerializerTestFixture.TEST_ISSUE_311.getFirst(); + builder.add(Arguments.arguments(Named.of(testIssue311File.getName(), testIssue311File), ProtobuffSerializerTestFixture.TEST_ISSUE_311.getSecond())); + final File testMapFile = ProtobuffSerializerTestFixture.TEST_MAP.getFirst(); + builder.add(Arguments.arguments(Named.of(testMapFile.getName(), testMapFile), ProtobuffSerializerTestFixture.TEST_MAP.getSecond())); + final File testOneOfFile = ProtobuffSerializerTestFixture.TEST_ONE_OF.getFirst(); + builder.add(Arguments.arguments(Named.of(testOneOfFile.getName(), testOneOfFile), ProtobuffSerializerTestFixture.TEST_ONE_OF.getSecond())); + final File testProvidedFile = ProtobuffSerializerTestFixture.TEST_PROVIDED.getFirst(); + builder.add(Arguments.arguments(Named.of(testProvidedFile.getName(), testProvidedFile), ProtobuffSerializerTestFixture.TEST_PROVIDED.getSecond())); return builder.build(); } @ParameterizedTest @MethodSource("getSchemaToTest") - void serialize(File schemaFile, List fieldValueMappings) throws IOException { + void serialize(final File schemaFile, final List fieldValueMappings) throws IOException { final ParsedSchema parsedSchema = SchemaParseUtil.getParsedSchema(schemaFile, "Protobuf"); final SchemaProcessor protobufSchemaProcessor = new SchemaProcessor(); final BaseSchemaMetadata confluentBaseSchemaMetadata = diff --git a/src/test/java/com/sngular/kloadgen/serializer/ProtobuffSerializerTestFixture.java b/src/test/java/com/sngular/kloadgen/serializer/ProtobuffSerializerTestFixture.java index 67276d8a..136a8ca7 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/ProtobuffSerializerTestFixture.java +++ b/src/test/java/com/sngular/kloadgen/serializer/ProtobuffSerializerTestFixture.java @@ -1,7 +1,5 @@ package com.sngular.kloadgen.serializer; -import static com.sngular.kloadgen.serializer.SerializerTestFixture.createFieldValueMapping; - import java.io.File; import java.util.Arrays; import java.util.List; @@ -15,16 +13,16 @@ public class ProtobuffSerializerTestFixture { static final Pair> TEST_COMPLETE_PROTO = new Pair<>( new FileHelper().getFile("/proto-files/completeProto.proto"), Arrays.asList( - createFieldValueMapping("name", "string"), - createFieldValueMapping("id", "int"), - createFieldValueMapping("addressesArray[].id[]", "string-array"), - createFieldValueMapping("addressesArray[].zipcode", "long"), - createFieldValueMapping("addressesDot[].id[]", "string-array"), - createFieldValueMapping("addressesDot[].zipcode", "long"), - createFieldValueMapping("addressesMap[:].id[]", "string-array"), - createFieldValueMapping("addressesMap[:].zipcode", "long"), - createFieldValueMapping("addressesNoDotMap[:].id[]", "string-array"), - createFieldValueMapping("addressesNoDotMap[:].zipcode", "long"), + SerializerTestFixture.createFieldValueMapping("name", "string"), + SerializerTestFixture.createFieldValueMapping("id", "int"), + SerializerTestFixture.createFieldValueMapping("addressesArray[].id[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("addressesArray[].zipcode", "long"), + SerializerTestFixture.createFieldValueMapping("addressesDot[].id[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("addressesDot[].zipcode", "long"), + SerializerTestFixture.createFieldValueMapping("addressesMap[:].id[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("addressesMap[:].zipcode", "long"), + SerializerTestFixture.createFieldValueMapping("addressesNoDotMap[:].id[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("addressesNoDotMap[:].zipcode", "long"), FieldValueMapping.builder().fieldName("phones[]").fieldType("enum").valueLength(0).fieldValueList("[MOBILE, HOME, WORK]").required(true) .isAncestorRequired(true).build()) ); @@ -32,100 +30,100 @@ public class ProtobuffSerializerTestFixture { static final Pair> TEST_COMPLEX = new Pair<>( new FileHelper().getFile("/proto-files/complexTest.proto"), Arrays.asList( - createFieldValueMapping("phone_types[].phone", "long"), - createFieldValueMapping("phone_types[].principal", "boolean"), - createFieldValueMapping("name", "string"), - createFieldValueMapping("age", "int"), - createFieldValueMapping("address[].street[]", "string-array"), - createFieldValueMapping("address[].number_street", "int"), - createFieldValueMapping("pets[:].pet_name", "string"), - createFieldValueMapping("pets[:].pet_age", "int"), - createFieldValueMapping("pets[:].owner", "string"), - createFieldValueMapping("descriptors[:]", "string-map"), - createFieldValueMapping("dates[]", "string-array"), - createFieldValueMapping("response", "string"), - createFieldValueMapping("presents[:].options[]", "string-array")) + SerializerTestFixture.createFieldValueMapping("phone_types[].phone", "long"), + SerializerTestFixture.createFieldValueMapping("phone_types[].principal", "boolean"), + SerializerTestFixture.createFieldValueMapping("name", "string"), + SerializerTestFixture.createFieldValueMapping("age", "int"), + SerializerTestFixture.createFieldValueMapping("address[].street[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("address[].number_street", "int"), + SerializerTestFixture.createFieldValueMapping("pets[:].pet_name", "string"), + SerializerTestFixture.createFieldValueMapping("pets[:].pet_age", "int"), + SerializerTestFixture.createFieldValueMapping("pets[:].owner", "string"), + SerializerTestFixture.createFieldValueMapping("descriptors[:]", "string-map"), + SerializerTestFixture.createFieldValueMapping("dates[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("response", "string"), + SerializerTestFixture.createFieldValueMapping("presents[:].options[]", "string-array")) ); static final Pair> TEST_DATE_TIME = new Pair<>( new FileHelper().getFile("/proto-files/dateTimeTest.proto"), Arrays.asList( - createFieldValueMapping("incident_date", ".google.type.Date"), - createFieldValueMapping("incident_time", ".google.type.TimeOfDay")) + SerializerTestFixture.createFieldValueMapping("incident_date", ".google.type.Date"), + SerializerTestFixture.createFieldValueMapping("incident_time", ".google.type.TimeOfDay")) ); static final Pair> TEST_DEVE = new Pair<>( new FileHelper().getFile("/proto-files/deveTest.proto"), Arrays.asList( - createFieldValueMapping("load_type", "string"), + SerializerTestFixture.createFieldValueMapping("load_type", "string"), FieldValueMapping.builder().fieldName("shipment.carrier_identifier.type").fieldType("enum").valueLength(0) .fieldValueList("[CARRIER_IDENTIFIER_TYPE_UNSPECIFIED, CARRIER_IDENTIFIER_TYPE_DOT_NUMBER]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("shipment.carrier_identifier.value", "string"), + SerializerTestFixture.createFieldValueMapping("shipment.carrier_identifier.value", "string"), FieldValueMapping.builder().fieldName("shipment.shipment_identifiers[].type").fieldType("enum").valueLength(0) .fieldValueList("[SHIPMENT_IDENTIFIER_TYPE_UNSPECIFIED, SHIPMENT_IDENTIFIER_TYPE_BILL_OF_LADING, SHIPMENT_IDENTIFIER_TYPE_ORDER]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("shipment.shipment_identifiers[].value", "string"), + SerializerTestFixture.createFieldValueMapping("shipment.shipment_identifiers[].value", "string"), FieldValueMapping.builder().fieldName("shipment.equipment_identifiers[].type").fieldType("enum").valueLength(0) .fieldValueList( - "[EQUIPMENT_IDENTIFIER_TYPE_UNSPECIFIED, EQUIPMENT_IDENTIFIER_TYPE_MOBILE_PHONE_NUMBER, EQUIPMENT_IDENTIFIER_TYPE_VEHICLE_ID, " + - "EQUIPMENT_IDENTIFIER_TYPE_LICENSE_PLATE, EQUIPMENT_IDENTIFIER_TYPE_SENSITECH_DEVICE_ID, EQUIPMENT_IDENTIFIER_TYPE_EMERSON_DEVICE_ID, " + - "EQUIPMENT_IDENTIFIER_TYPE_TIVE_DEVICE_ID, EQUIPMENT_IDENTIFIER_TYPE_CONTAINER_ID]") + "[EQUIPMENT_IDENTIFIER_TYPE_UNSPECIFIED, EQUIPMENT_IDENTIFIER_TYPE_MOBILE_PHONE_NUMBER, EQUIPMENT_IDENTIFIER_TYPE_VEHICLE_ID, " + + "EQUIPMENT_IDENTIFIER_TYPE_LICENSE_PLATE, EQUIPMENT_IDENTIFIER_TYPE_SENSITECH_DEVICE_ID, EQUIPMENT_IDENTIFIER_TYPE_EMERSON_DEVICE_ID, " + + "EQUIPMENT_IDENTIFIER_TYPE_TIVE_DEVICE_ID, EQUIPMENT_IDENTIFIER_TYPE_CONTAINER_ID]") .required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("shipment.equipment_identifiers[].value", "string"), - createFieldValueMapping("shipment.attributes[]", "string-array"), - createFieldValueMapping("latest_status_update.timestamp", ".google.protobuf.Timestamp"), + SerializerTestFixture.createFieldValueMapping("shipment.equipment_identifiers[].value", "string"), + SerializerTestFixture.createFieldValueMapping("shipment.attributes[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.timestamp", ".google.protobuf.Timestamp"), FieldValueMapping.builder().fieldName("latest_status_update.status_code").fieldType("enum").valueLength(0) .fieldValueList( - "[STATUS_UPDATE_CODE_UNSPECIFIED, STATUS_UPDATE_CODE_DISPATCHED, STATUS_UPDATE_CODE_IN_TRANSIT, STATUS_UPDATE_CODE_AT_STOP, " + - "STATUS_UPDATE_CODE_COMPLETED, STATUS_UPDATE_CODE_TRACKING_FAILED, STATUS_UPDATE_CODE_INFO, STATUS_UPDATE_CODE_DELETED]") + "[STATUS_UPDATE_CODE_UNSPECIFIED, STATUS_UPDATE_CODE_DISPATCHED, STATUS_UPDATE_CODE_IN_TRANSIT, STATUS_UPDATE_CODE_AT_STOP, " + + "STATUS_UPDATE_CODE_COMPLETED, STATUS_UPDATE_CODE_TRACKING_FAILED, STATUS_UPDATE_CODE_INFO, STATUS_UPDATE_CODE_DELETED]") .required(true) .isAncestorRequired(true).build(), FieldValueMapping.builder().fieldName("latest_status_update.status_reason").fieldType("enum").valueLength(0) .fieldValueList( - "[STATUS_UPDATE_REASON_UNSPECIFIED, STATUS_UPDATE_REASON_PENDING_TRACKING_METHOD, STATUS_UPDATE_REASON_SCHEDULED, " + - "STATUS_UPDATE_REASON_PENDING_APPROVAL, STATUS_UPDATE_REASON_ACQUIRING_LOCATION, STATUS_UPDATE_REASON_PENDING_CARRIER, " + - "STATUS_UPDATE_REASON_IN_MOTION, STATUS_UPDATE_REASON_IDLE, STATUS_UPDATE_REASON_APPROVAL_DENIED, STATUS_UPDATE_REASON_TIMED_OUT, " + - "STATUS_UPDATE_REASON_CANCELED, STATUS_UPDATE_REASON_DEPARTED_FINAL_STOP, STATUS_UPDATE_REASON_ARRIVED_FINAL_STOP, " + - "STATUS_UPDATE_REASON_ARRIVED_FAILED_TO_ACQUIRE_LOCATION, STATUS_UPDATE_REASON_INFO]") + "[STATUS_UPDATE_REASON_UNSPECIFIED, STATUS_UPDATE_REASON_PENDING_TRACKING_METHOD, STATUS_UPDATE_REASON_SCHEDULED, " + + "STATUS_UPDATE_REASON_PENDING_APPROVAL, STATUS_UPDATE_REASON_ACQUIRING_LOCATION, STATUS_UPDATE_REASON_PENDING_CARRIER, " + + "STATUS_UPDATE_REASON_IN_MOTION, STATUS_UPDATE_REASON_IDLE, STATUS_UPDATE_REASON_APPROVAL_DENIED, STATUS_UPDATE_REASON_TIMED_OUT, " + + "STATUS_UPDATE_REASON_CANCELED, STATUS_UPDATE_REASON_DEPARTED_FINAL_STOP, STATUS_UPDATE_REASON_ARRIVED_FINAL_STOP, " + + "STATUS_UPDATE_REASON_ARRIVED_FAILED_TO_ACQUIRE_LOCATION, STATUS_UPDATE_REASON_INFO]") .required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("latest_status_update.geo_coordinates.latitude", "double"), - createFieldValueMapping("latest_status_update.geo_coordinates.longitude", "double"), - createFieldValueMapping("latest_status_update.address.postal_code", "string"), - createFieldValueMapping("latest_status_update.address.address_lines[]", "string-array"), - createFieldValueMapping("latest_status_update.address.city", "string"), - createFieldValueMapping("latest_status_update.address.state", "string"), - createFieldValueMapping("latest_status_update.address.country", "string"), - createFieldValueMapping("latest_status_update.stop_number", "int"), - createFieldValueMapping("latest_stop_statuses[].stop_number", "int"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.geo_coordinates.latitude", "double"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.geo_coordinates.longitude", "double"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.address.postal_code", "string"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.address.address_lines[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.address.city", "string"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.address.state", "string"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.address.country", "string"), + SerializerTestFixture.createFieldValueMapping("latest_status_update.stop_number", "int"), + SerializerTestFixture.createFieldValueMapping("latest_stop_statuses[].stop_number", "int"), FieldValueMapping.builder().fieldName("latest_stop_statuses[].status_code").fieldType("enum").valueLength(0) .fieldValueList( "[STOP_STATUS_CODE_UNSPECIFIED, STOP_STATUS_CODE_UNKNOWN, STOP_STATUS_CODE_EN_ROUTE, STOP_STATUS_CODE_ARRIVED, STOP_STATUS_CODE_DEPARTED]") .required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("latest_stop_statuses[].arrival_estimate.estimated_arrival_window.start_date_time", "google.protobuf.Timestamp"), - createFieldValueMapping("latest_stop_statuses[].arrival_estimate.estimated_arrival_window.end_date_time", "google.protobuf.Timestamp"), - createFieldValueMapping("latest_stop_statuses[].arrival_estimate.last_calculated_date_time", "google.protobuf.Timestamp"), + SerializerTestFixture.createFieldValueMapping("latest_stop_statuses[].arrival_estimate.estimated_arrival_window.start_date_time", "google.protobuf.Timestamp"), + SerializerTestFixture.createFieldValueMapping("latest_stop_statuses[].arrival_estimate.estimated_arrival_window.end_date_time", "google.protobuf.Timestamp"), + SerializerTestFixture.createFieldValueMapping("latest_stop_statuses[].arrival_estimate.last_calculated_date_time", "google.protobuf.Timestamp"), FieldValueMapping.builder().fieldName("latest_stop_statuses[].arrival_code").fieldType("enum").valueLength(0) .fieldValueList("[ARRIVAL_CODE_UNSPECIFIED, ARRIVAL_CODE_UNKNOWN, ARRIVAL_CODE_EARLY, ARRIVAL_CODE_ON_TIME, ARRIVAL_CODE_LATE]").required(true) .isAncestorRequired(true).build(), - createFieldValueMapping("latest_stop_statuses[].additional_appointment_window_statuses[]", "string-array")) + SerializerTestFixture.createFieldValueMapping("latest_stop_statuses[].additional_appointment_window_statuses[]", "string-array")) ); static final Pair> TEST_EASY = new Pair<>( new FileHelper().getFile("/proto-files/easyTest.proto"), Arrays.asList( - createFieldValueMapping("street", "string"), - createFieldValueMapping("number[]", "int-array"), - createFieldValueMapping("zipcode", "long")) + SerializerTestFixture.createFieldValueMapping("street", "string"), + SerializerTestFixture.createFieldValueMapping("number[]", "int-array"), + SerializerTestFixture.createFieldValueMapping("zipcode", "long")) ); static final Pair> TEST_EMBEDDED_TYPE = new Pair<>( new FileHelper().getFile("/proto-files/embeddedTypeTest.proto"), Arrays.asList( - createFieldValueMapping("phones.addressesPhone[:].id[]", "string-array"), + SerializerTestFixture.createFieldValueMapping("phones.addressesPhone[:].id[]", "string-array"), FieldValueMapping.builder().fieldName("phones.phoneType").fieldType("enum").valueLength(0) .fieldValueList("[MOBILE, HOME, WORK]").required(true) .isAncestorRequired(true).build()) @@ -148,84 +146,87 @@ public class ProtobuffSerializerTestFixture { static final Pair> TEST_GOOGLE_TYPES = new Pair<>( new FileHelper().getFile("/proto-files/googleTypesTest.proto"), Arrays.asList( - createFieldValueMapping("id", "Int32Value"), - createFieldValueMapping("occurrence_id", "StringValue"), - createFieldValueMapping("load_number", "StringValue")) + SerializerTestFixture.createFieldValueMapping("id", "Int32Value"), + SerializerTestFixture.createFieldValueMapping("occurrence_id", "StringValue"), + SerializerTestFixture.createFieldValueMapping("load_number", "StringValue")) ); static final Pair> TEST_ISSUE_311 = new Pair<>( new FileHelper().getFile("/proto-files/issue311Test.proto"), Arrays.asList( - createFieldValueMapping("order_id", "int"), - createFieldValueMapping("order_number", "string"), - createFieldValueMapping("customer_account.billing_party.party_id", "string"), - createFieldValueMapping("customer_account.billing_party.address.address_line_one", "string"), - createFieldValueMapping("customer_account.billing_party.address.address_line_two", "string"), - createFieldValueMapping("customer_account.billing_party.party_contact[].contact_id", "int"), - createFieldValueMapping("customer_account.billing_party.party_contact[].contact_name", "string"), - createFieldValueMapping("details.in_details.customs_details.party.party_address.address_line_one", "string"), - createFieldValueMapping("details.in_details.customs_details.party.party_address.address_line_two", "string"), - createFieldValueMapping("details.in_details.customs_details.party.party_contact[].contact_id", "int"), - createFieldValueMapping("details.in_details.customs_details.party.party_contact[].contact_name", "string"), - createFieldValueMapping("details.in_details.customs_details.party.p_contact[].contact_id", "int"), - createFieldValueMapping("details.in_details.customs_details.party.p_contact[].contact_name", "string")) + SerializerTestFixture.createFieldValueMapping("order_id", "int"), + SerializerTestFixture.createFieldValueMapping("order_number", "string"), + SerializerTestFixture.createFieldValueMapping("customer_account.billing_party.party_id", "string"), + SerializerTestFixture.createFieldValueMapping("customer_account.billing_party.address.address_line_one", "string"), + SerializerTestFixture.createFieldValueMapping("customer_account.billing_party.address.address_line_two", "string"), + SerializerTestFixture.createFieldValueMapping("customer_account.billing_party.party_contact[].contact_id", "int"), + SerializerTestFixture.createFieldValueMapping("customer_account.billing_party.party_contact[].contact_name", "string"), + SerializerTestFixture.createFieldValueMapping("details.in_details.customs_details.party.party_address.address_line_one", "string"), + SerializerTestFixture.createFieldValueMapping("details.in_details.customs_details.party.party_address.address_line_two", "string"), + SerializerTestFixture.createFieldValueMapping("details.in_details.customs_details.party.party_contact[].contact_id", "int"), + SerializerTestFixture.createFieldValueMapping("details.in_details.customs_details.party.party_contact[].contact_name", "string"), + SerializerTestFixture.createFieldValueMapping("details.in_details.customs_details.party.p_contact[].contact_id", "int"), + SerializerTestFixture.createFieldValueMapping("details.in_details.customs_details.party.p_contact[].contact_name", "string")) ); static final Pair> TEST_MAP = new Pair<>( new FileHelper().getFile("/proto-files/mapTest.proto"), Arrays.asList( - createFieldValueMapping("name[:]", "string-map"), - createFieldValueMapping("addresses[:].street", "string"), - createFieldValueMapping("addresses[:].number", "int"), - createFieldValueMapping("addresses[:].zipcode", "int"), - createFieldValueMapping("addressesNoDot[:].street", "string"), - createFieldValueMapping("addressesNoDot[:].number", "int"), - createFieldValueMapping("addressesNoDot[:].zipcode", "int")) + SerializerTestFixture.createFieldValueMapping("name[:]", "string-map"), + SerializerTestFixture.createFieldValueMapping("addresses[:].street", "string"), + SerializerTestFixture.createFieldValueMapping("addresses[:].number", "int"), + SerializerTestFixture.createFieldValueMapping("addresses[:].zipcode", "int"), + SerializerTestFixture.createFieldValueMapping("addressesNoDot[:].street", "string"), + SerializerTestFixture.createFieldValueMapping("addressesNoDot[:].number", "int"), + SerializerTestFixture.createFieldValueMapping("addressesNoDot[:].zipcode", "int")) ); static final Pair> TEST_ONE_OF = new Pair<>( new FileHelper().getFile("/proto-files/oneOfTest.proto"), Arrays.asList( - createFieldValueMapping("type.street", "string"), - createFieldValueMapping("type.number", "int"), - createFieldValueMapping("type.test", "string"), - createFieldValueMapping("optionString", "string")) + SerializerTestFixture.createFieldValueMapping("type.street", "string"), + SerializerTestFixture.createFieldValueMapping("type.number", "int"), + SerializerTestFixture.createFieldValueMapping("type.test", "string"), + SerializerTestFixture.createFieldValueMapping("optionString", "string")) ); static final Pair> TEST_PROVIDED = new Pair<>( new FileHelper().getFile("/proto-files/providedTest.proto"), Arrays.asList( - createFieldValueMapping("id", "int"), - createFieldValueMapping("occurrence_id", "string"), - createFieldValueMapping("load_number", "string"), - createFieldValueMapping("claim_type.code", "string"), - createFieldValueMapping("claim_type.description", "string"), - createFieldValueMapping("collision_type.code", "string"), - createFieldValueMapping("collision_type.description", "string"), - createFieldValueMapping("incident_cause_type.code", "string"), - createFieldValueMapping("incident_cause_type.description", "string"), - createFieldValueMapping("incident_type.code", "string"), - createFieldValueMapping("incident_type.description", "string"), - createFieldValueMapping("review_status_type.code", "string"), - createFieldValueMapping("review_status_type.description", "string"), - createFieldValueMapping("incident_latitude", "double"), - createFieldValueMapping("incident_longitude", "double"), - createFieldValueMapping("incident_date", "string"), - createFieldValueMapping("incident_time", "google.protobuf.Timestamp"), - createFieldValueMapping("incident_city", "string"), - createFieldValueMapping("incident_state", "string"), - createFieldValueMapping("location_description", "string"), - createFieldValueMapping("incident_equipment_details[].equipment_number", "string"), - createFieldValueMapping("incident_equipment_details[].equipment_type", "string"), - createFieldValueMapping("incident_equipment_details[].equipment_prefix", "string"), - createFieldValueMapping("driver.driver_id", "int"), - createFieldValueMapping("driver.driver_first_name", "string"), - createFieldValueMapping("driver.driver_last_name", "string"), - createFieldValueMapping("dot_accident_indicator", "string"), - createFieldValueMapping("drug_test_required_indicator", "string"), - createFieldValueMapping("hazardous_material_indicator", "string"), - createFieldValueMapping("preventable_indicator", "string"), - createFieldValueMapping("report_by_name", "string"), - createFieldValueMapping("create_user_id", "string")) + SerializerTestFixture.createFieldValueMapping("id", "int"), + SerializerTestFixture.createFieldValueMapping("occurrence_id", "string"), + SerializerTestFixture.createFieldValueMapping("load_number", "string"), + SerializerTestFixture.createFieldValueMapping("claim_type.code", "string"), + SerializerTestFixture.createFieldValueMapping("claim_type.description", "string"), + SerializerTestFixture.createFieldValueMapping("collision_type.code", "string"), + SerializerTestFixture.createFieldValueMapping("collision_type.description", "string"), + SerializerTestFixture.createFieldValueMapping("incident_cause_type.code", "string"), + SerializerTestFixture.createFieldValueMapping("incident_cause_type.description", "string"), + SerializerTestFixture.createFieldValueMapping("incident_type.code", "string"), + SerializerTestFixture.createFieldValueMapping("incident_type.description", "string"), + SerializerTestFixture.createFieldValueMapping("review_status_type.code", "string"), + SerializerTestFixture.createFieldValueMapping("review_status_type.description", "string"), + SerializerTestFixture.createFieldValueMapping("incident_latitude", "double"), + SerializerTestFixture.createFieldValueMapping("incident_longitude", "double"), + SerializerTestFixture.createFieldValueMapping("incident_date", "string"), + SerializerTestFixture.createFieldValueMapping("incident_time", "google.protobuf.Timestamp"), + SerializerTestFixture.createFieldValueMapping("incident_city", "string"), + SerializerTestFixture.createFieldValueMapping("incident_state", "string"), + SerializerTestFixture.createFieldValueMapping("location_description", "string"), + SerializerTestFixture.createFieldValueMapping("incident_equipment_details[].equipment_number", "string"), + SerializerTestFixture.createFieldValueMapping("incident_equipment_details[].equipment_type", "string"), + SerializerTestFixture.createFieldValueMapping("incident_equipment_details[].equipment_prefix", "string"), + SerializerTestFixture.createFieldValueMapping("driver.driver_id", "int"), + SerializerTestFixture.createFieldValueMapping("driver.driver_first_name", "string"), + SerializerTestFixture.createFieldValueMapping("driver.driver_last_name", "string"), + SerializerTestFixture.createFieldValueMapping("dot_accident_indicator", "string"), + SerializerTestFixture.createFieldValueMapping("drug_test_required_indicator", "string"), + SerializerTestFixture.createFieldValueMapping("hazardous_material_indicator", "string"), + SerializerTestFixture.createFieldValueMapping("preventable_indicator", "string"), + SerializerTestFixture.createFieldValueMapping("report_by_name", "string"), + SerializerTestFixture.createFieldValueMapping("create_user_id", "string")) ); + + protected ProtobuffSerializerTestFixture() { + } } diff --git a/src/test/java/com/sngular/kloadgen/serializer/SerializerTestFixture.java b/src/test/java/com/sngular/kloadgen/serializer/SerializerTestFixture.java index 3e7ec09f..ae6827a7 100644 --- a/src/test/java/com/sngular/kloadgen/serializer/SerializerTestFixture.java +++ b/src/test/java/com/sngular/kloadgen/serializer/SerializerTestFixture.java @@ -8,7 +8,10 @@ import com.sngular.kloadgen.model.FieldValueMapping; -public class SerializerTestFixture { +public final class SerializerTestFixture { + + private SerializerTestFixture() { + } public static FieldValueMapping createFieldValueMapping(final String name, final String fieldType) { return FieldValueMapping.builder().fieldName(name).fieldType(fieldType).valueLength(0).fieldValueList("[]").required(true) @@ -20,7 +23,7 @@ public static FieldValueMapping createFieldValueMapping(final String name, final .isAncestorRequired(true).build(); } - static String readSchema(final File file) throws IOException { + public static String readSchema(final File file) throws IOException { final StringBuilder contentBuilder = new StringBuilder(); try (Stream stream = Files.lines(file.toPath(), StandardCharsets.UTF_8)) { diff --git a/src/test/java/com/sngular/kloadgen/testutil/SchemaParseUtil.java b/src/test/java/com/sngular/kloadgen/testutil/SchemaParseUtil.java index 21d2450e..be52e15c 100644 --- a/src/test/java/com/sngular/kloadgen/testutil/SchemaParseUtil.java +++ b/src/test/java/com/sngular/kloadgen/testutil/SchemaParseUtil.java @@ -17,33 +17,26 @@ public final class SchemaParseUtil { private SchemaParseUtil() { } + public static ParsedSchema getParsedSchema(final File schema, final String type) throws IOException { + return getParsedSchema(new String(Files.readAllBytes(schema.toPath())), type); + } + public static ParsedSchema getParsedSchema(final String schema, final String type) { - SchemaTypeEnum schemaType = EnumUtils.isValidEnum(SchemaTypeEnum.class, type.toUpperCase()) ? - SchemaTypeEnum.valueOf(type.toUpperCase()) : null; - ParsedSchema schemaParsed = null; + final SchemaTypeEnum schemaType = EnumUtils.isValidEnum(SchemaTypeEnum.class, type.toUpperCase()) + ? SchemaTypeEnum.valueOf(type.toUpperCase()) : null; + final ParsedSchema schemaParsed; try { - switch (schemaType) { - case JSON: - schemaParsed = new JsonSchema(schema); - break; - case AVRO: - schemaParsed = new AvroSchema(schema); - break; - case PROTOBUF: - schemaParsed = new ProtobufSchema(schema); - break; - default: - throw new KLoadGenException("Unsupported schema type"); - } - } catch (KLoadGenException e) { + assert schemaType != null; + schemaParsed = switch (schemaType) { + case JSON -> new JsonSchema(schema); + case AVRO -> new AvroSchema(schema); + case PROTOBUF -> new ProtobufSchema(schema); + }; + } catch (final KLoadGenException e) { final String logMsg = "Please, make sure that the schema sources fed are correct"; throw new KLoadGenException("Error obtaining object creator factory. " + logMsg); } - return schemaParsed; - } - - public static ParsedSchema getParsedSchema(final File schema, final String type) throws IOException { - return getParsedSchema(new String(Files.readAllBytes(schema.toPath())), type); + return schemaParsed; } } diff --git a/src/test/resources/proto-files/complexTest.proto b/src/test/resources/proto-files/complexTest.proto index dea0058f..4c10d313 100644 --- a/src/test/resources/proto-files/complexTest.proto +++ b/src/test/resources/proto-files/complexTest.proto @@ -8,7 +8,6 @@ option java_outer_classname = "Protos"; import "google/protobuf/timestamp.proto"; import "google/protobuf/descriptor.proto"; -import "google/protobuf/compiler/plugin.proto"; message Test { repeated PhoneTypes phone_types = 1;