From e78d5702a23e62615d5e25ae7e923c2db461fdf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=20Enrique=20Garc=C3=ADa=20Maci=C3=B1eiras?= Date: Thu, 19 Oct 2023 09:07:03 +0200 Subject: [PATCH] #332 Fix checkstyle issues --- pom.xml | 4 +- .../asyncapi/AsyncApiExtractorImpl.java | 12 ++--- .../extractors/ExtractorFactory.java | 50 +++++++------------ .../avro/AvroConfluentExtractor.java | 7 +-- .../kloadgen/sampler/AsyncApiSampler.java | 4 +- .../sampler/gui/AsyncApiSamplerGui.java | 25 ++++------ .../impl/ConfluentParsedSchemaMetadata.java | 19 +++---- ...es => AsyncApiSamplerResources.properties} | 0 .../avro/AvroConfluentExtractorTest.java | 19 +++---- 9 files changed, 60 insertions(+), 80 deletions(-) rename src/main/resources/com/sngular/kloadgen/sampler/{asyncapisampler.properties => AsyncApiSamplerResources.properties} (100%) diff --git a/pom.xml b/pom.xml index 74286d73..5ad1f706 100644 --- a/pom.xml +++ b/pom.xml @@ -305,7 +305,7 @@ 5.8.2 7.1.1 2.4.1.Final - 3.1.0 + 3.5.1 1.18.22 0.9.5 4.2.0 @@ -464,7 +464,7 @@ com.github.everit-org.json-schema org.everit.json.schema - 1.14.0 + 1.14.2 diff --git a/src/main/java/com/sngular/kloadgen/extractor/asyncapi/AsyncApiExtractorImpl.java b/src/main/java/com/sngular/kloadgen/extractor/asyncapi/AsyncApiExtractorImpl.java index 1c536f8b..6a91e9ae 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/asyncapi/AsyncApiExtractorImpl.java +++ b/src/main/java/com/sngular/kloadgen/extractor/asyncapi/AsyncApiExtractorImpl.java @@ -40,7 +40,7 @@ public class AsyncApiExtractorImpl implements ApiExtractor { @Override public final AsyncApiFile processFile(final File apiFile) { - AsyncApiFile asyncApiFile = null; + AsyncApiFile asyncApiFile; try { final JsonNode openApi = om.readTree(apiFile); asyncApiFile = processNode(openApi); @@ -80,7 +80,7 @@ private AsyncApiSchema mapNodeToSchema(final Map components, f final var message = ApiTool.findNodeValue(node, "message"); if (ApiTool.hasNode(message, "bindings")) { builder.key(true); - // builder.keyType(ApiTool.findValue(message, "key")); + // builder.keyType(ApiTool.findValue(message, "key")); } builder.model(messageToFieldList(message, components, antiLoopList)); return builder.build(); @@ -138,7 +138,7 @@ private List processPayload(final JsonNode finalPayload, fina .fieldName(propertyName) .fieldType(getType(propertyDef)) .fieldValueList(hasValues(propertyDef)) - .required(ApiTool.checkIfRequired(propertyDef, property.getKey())) + .required(ApiTool.checkIfRequired(finalPayload, property.getKey())) .constraints(getConstraints(propertyDef)) .build()); } @@ -172,8 +172,8 @@ private String getType(final JsonNode finalPayload) { return type; } - private Map getConstraints(JsonNode finalPayload) { - Map constraints = new EnumMap<>(ConstraintTypeEnum.class); + private Map getConstraints(final JsonNode finalPayload) { + final Map constraints = new EnumMap<>(ConstraintTypeEnum.class); if (ApiTool.hasNode(finalPayload, "minLength")) { constraints.put(ConstraintTypeEnum.MIN_LENGTH, ApiTool.getNodeAsString(finalPayload, "minLength")); } @@ -199,7 +199,7 @@ private Map getConstraints(JsonNode finalPayload) { return constraints; } - private String hasValues(JsonNode propertyDef) { + private String hasValues(final JsonNode propertyDef) { String values = null; if (ApiTool.isEnum(propertyDef)) { values = String.join(",", ApiTool.getEnumValues(propertyDef)); 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..15193f05 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/ExtractorFactory.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/ExtractorFactory.java @@ -21,41 +21,31 @@ import org.apache.jmeter.threads.JMeterContextService; public final class ExtractorFactory { - private static AvroExtractor avroExtractor = new AvroExtractor(); + private static final AvroExtractor avroExtractor = new AvroExtractor(); - private static JsonExtractor jsonExtractor = new JsonExtractor(); + private static final JsonExtractor jsonExtractor = new JsonExtractor(); - private static ProtobuffExtractor protobuffExtractor = new ProtobuffExtractor(); + private static final ProtobuffExtractor protobuffExtractor = new ProtobuffExtractor(); private ExtractorFactory() { } public static ExtractorRegistry getExtractor(final String schemaType, final String schemaRegistryEnum) { - SchemaRegistryEnum registryEnum = getSchemaRegistry(schemaRegistryEnum); - if (schemaType != null && EnumUtils.isValidEnum(SchemaTypeEnum.class, schemaType.toUpperCase())) { - final ExtractorRegistry response; - switch (SchemaTypeEnum.valueOf(schemaType.toUpperCase())) { - case JSON: - response = jsonExtractor; - break; - case AVRO: - response = avroExtractor; - break; - case PROTOBUF: - response = protobuffExtractor; - break; - default: - throw new KLoadGenException(String.format("Schema type not supported %s", schemaType)); - } + final ExtractorRegistry response = switch (SchemaTypeEnum.valueOf(schemaType.toUpperCase())) { + case JSON -> jsonExtractor; + case AVRO -> avroExtractor; + case PROTOBUF -> protobuffExtractor; + default -> throw new KLoadGenException(String.format("Schema type not supported %s", schemaType)); + }; return response; } else { throw new KLoadGenException(String.format("Schema type not supported %s", schemaType)); } } - 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,9 +57,8 @@ 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 String schemaType = parsedSchemaAdapter.getType(); List attributeList = new ArrayList<>(); SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registryName.toUpperCase()); @@ -77,17 +66,12 @@ public static Pair> flatPropertiesList(final Str Object schema = null; 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 -> ((ApicurioParsedSchemaMetadata) parsedSchemaAdapter).getSchema(); + case CONFLUENT -> parsedSchemaAdapter.getRawSchema(); + default -> throw new KLoadGenException("Schema Registry Type nos supported " + registryName.toUpperCase()); + }; + attributeList = getExtractor(schemaType, registryName.toUpperCase()).processSchema(schema, schemaRegistryEnum); } return Pair.of(schemaType, attributeList); } diff --git a/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractor.java b/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractor.java index 7405c3ab..4dc1513b 100644 --- a/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractor.java +++ b/src/main/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractor.java @@ -5,11 +5,12 @@ import com.sngular.kloadgen.extractor.extractors.Extractor; import com.sngular.kloadgen.model.FieldValueMapping; import io.confluent.kafka.schemaregistry.avro.AvroSchema; +import org.apache.avro.Schema; -public class AvroConfluentExtractor extends AbstractAvroFileExtractor implements Extractor { +public class AvroConfluentExtractor extends AbstractAvroFileExtractor implements Extractor { - public final List processSchema(final AvroSchema schema) { - return this.processSchemaDefault(schema.rawSchema()); + public final List processSchema(final Schema schema) { + return this.processSchemaDefault(schema); } public final List getSchemaNameList(final String schema) { diff --git a/src/main/java/com/sngular/kloadgen/sampler/AsyncApiSampler.java b/src/main/java/com/sngular/kloadgen/sampler/AsyncApiSampler.java index 7ccd35b1..8843eed4 100644 --- a/src/main/java/com/sngular/kloadgen/sampler/AsyncApiSampler.java +++ b/src/main/java/com/sngular/kloadgen/sampler/AsyncApiSampler.java @@ -142,9 +142,9 @@ public final List getSchemaFieldConfiguration() { .builder() .fieldName(elementProperty.getElement().getPropertyAsString("fieldName")) .fieldType(elementProperty.getElement().getPropertyAsString("fieldType")) - .required(elementProperty.getElement().getPropertyAsBoolean("required", false)) + .required(elementProperty.getElement().getPropertyAsBoolean("required", true)) .valueLength(elementProperty.getElement().getPropertyAsInt("valueLength", 0)) - .fieldValueList(elementProperty.getElement().getPropertyAsString("fieldValueList")) + .fieldValueList(elementProperty.getElement().getPropertyAsString("fieldValuesList")) .build()); } return propertyList; diff --git a/src/main/java/com/sngular/kloadgen/sampler/gui/AsyncApiSamplerGui.java b/src/main/java/com/sngular/kloadgen/sampler/gui/AsyncApiSamplerGui.java index d39b52f9..f0ec87fc 100644 --- a/src/main/java/com/sngular/kloadgen/sampler/gui/AsyncApiSamplerGui.java +++ b/src/main/java/com/sngular/kloadgen/sampler/gui/AsyncApiSamplerGui.java @@ -42,7 +42,6 @@ import com.sngular.kloadgen.exception.KLoadGenException; import com.sngular.kloadgen.extractor.ApiExtractor; import com.sngular.kloadgen.extractor.asyncapi.AsyncApiExtractorImpl; -import com.sngular.kloadgen.extractor.model.AsyncApiAbstract; import com.sngular.kloadgen.extractor.model.AsyncApiFile; import com.sngular.kloadgen.extractor.model.AsyncApiSR; import com.sngular.kloadgen.extractor.model.AsyncApiSchema; @@ -55,7 +54,6 @@ import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.jmeter.config.Argument; -import org.apache.jmeter.gui.TestElementMetadata; import org.apache.jmeter.samplers.gui.AbstractSamplerGui; import org.apache.jmeter.testelement.TestElement; import org.apache.jmeter.testelement.property.JMeterProperty; @@ -63,7 +61,6 @@ import org.apache.jorphan.gui.JLabeledTextField; import org.apache.kafka.clients.producer.ProducerConfig; -@TestElementMetadata(labelResource = "async_api_sampler") public final class AsyncApiSamplerGui extends AbstractSamplerGui { private final JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory()); @@ -185,7 +182,7 @@ private void actionFileChooser(final ActionEvent event) { asyncApiFileTextField.setText(apiFile.getAbsolutePath()); asyncApiFile = asyncApiExtractor.processFile(apiFile); populateData(); - } catch (KLoadGenException ex) { + } catch (final KLoadGenException ex) { JOptionPane.showMessageDialog(mainPanel, "Error has occurred: " + ex.getMessage(), "Weird Error", JOptionPane.ERROR_MESSAGE); } } @@ -230,7 +227,7 @@ private Collection> prepareSRServer(final AsyncApiSR schema final var propertyList = new ArrayList>(); propertyList.add(Pair.of(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_URL, srPropoMap.get("url"))); propertyList.add(Pair.of(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, srPropoMap.get("type"))); - SchemaRegistryProperties.DEFAULTS.forEach(jMeterProperty -> propertyList.add(Pair.of(jMeterProperty.getName(), jMeterProperty.getPropertyValue()))); + SchemaRegistryProperties.DEFAULTS.forEach(jmeterproperty -> propertyList.add(Pair.of(jmeterproperty.getName(), jmeterproperty.getPropertyValue()))); return propertyList; } @@ -242,14 +239,14 @@ private Collection> prepareServer(final AsyncApiServer asyn CollectionUtils.select(SamplerUtil .getCommonProducerDefaultParameters(), property -> !property.getName().equalsIgnoreCase(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG)) - .forEach(jMeterProperty -> extractArgument(jMeterProperty, propertyList)); + .forEach(jmeterproperty -> extractArgument(jmeterproperty, propertyList)); } return propertyList; } - private static boolean extractArgument(final JMeterProperty jMeterProperty, final ArrayList> propertyList) { - final var argument = (Argument) jMeterProperty.getObjectValue(); - return propertyList.add(Pair.of(argument.getPropertyAsString("Argument.name"), argument.getPropertyAsString("Argument.value"))); + private static void extractArgument(final JMeterProperty jmeterproperty, final ArrayList> propertyList) { + final var argument = (Argument) jmeterproperty.getObjectValue(); + propertyList.add(Pair.of(argument.getPropertyAsString("Argument.name"), argument.getPropertyAsString("Argument.value"))); } private void init() { @@ -311,7 +308,7 @@ private JTabbedPane createAsyncApiTabs() { return tabbedPanel; } - private void fillTable(final DefaultTableModel schemaFields, final Collection> schemaData) { + private void fillTable(final DefaultTableModel schemaFields, final Collection> schemaData) { if (Objects.nonNull(schemaData)) { final var count = schemaFields.getRowCount(); for (var i = 0; i < count; i++) { @@ -321,7 +318,7 @@ private void fillTable(final DefaultTableModel sche } } - private void fillTable(final DefaultTableModel schemaFields, final Object[] schemaData) { + private void fillTable(final DefaultTableModel schemaFields, final Object[] schemaData) { if (Objects.nonNull(schemaData)) { final var count = schemaFields.getRowCount(); for (var i = 0; i < count; i++) { @@ -333,7 +330,7 @@ private void fillTable(final DefaultTableModel sche } } - private void fillTable(final DefaultTableModel schemaFields, final List schemaData) { + private void fillTable(final DefaultTableModel schemaFields, final List schemaData) { if (Objects.nonNull(schemaData)) { final var count = schemaFields.getRowCount(); for (var i = 0; i < count; i++) { @@ -395,10 +392,10 @@ private void topicComboActionListener(final ActionEvent event) { } } - private void registryComboActionListener(ActionEvent actionEvent) { + private void registryComboActionListener(final ActionEvent actionEvent) { } - private void serverChooseActionListener(ActionEvent actionEvent) { + private void serverChooseActionListener(final ActionEvent actionEvent) { final JComboBox serverChoose = (JComboBox)actionEvent.getSource(); if (Objects.nonNull(serverChoose)) { fillTable(brokerFieldModel, prepareServer((AsyncApiServer) serverChoose.getSelectedItem())); diff --git a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java index a24ea866..799baf16 100644 --- a/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java +++ b/src/main/java/com/sngular/kloadgen/schemaregistry/adapter/impl/ConfluentParsedSchemaMetadata.java @@ -14,31 +14,27 @@ public class ConfluentParsedSchemaMetadata extends ParsedSchemaAdapter { private String canonicalString; - private Object rawSchema; + private Object rawSchema; - private ConfluentParsedSchemaMetadata(ParsedSchema parsedSchema){ + private ConfluentParsedSchemaMetadata(final ParsedSchema parsedSchema) { this.schemaType = parsedSchema.schemaType(); this.name = parsedSchema.name(); this.canonicalString = parsedSchema.canonicalString(); this.rawSchema = parsedSchema.rawSchema(); } - private ConfluentParsedSchemaMetadata(Schema schema){ + private ConfluentParsedSchemaMetadata(final Schema schema) { this.schemaType = schema.getType().getName(); this.name = schema.getName(); } - public ConfluentParsedSchemaMetadata(ProtobufSchema schema){ + public ConfluentParsedSchemaMetadata(final ProtobufSchema schema) { this.schemaType = schema.schemaType(); this.name = schema.name(); this.rawSchema = schema.rawSchema(); this.canonicalString = schema.canonicalString(); } - public ConfluentParsedSchemaMetadata() { - - } - public static ParsedSchemaAdapter parse(final ParsedSchema parsedSchema) { return new ConfluentParsedSchemaMetadata(parsedSchema); } @@ -46,17 +42,18 @@ public static ParsedSchemaAdapter parse(final ParsedSchema parsedSchema) { public static ParsedSchemaAdapter parse(final Schema schema) { return new ConfluentParsedSchemaMetadata(schema); } - public static ParsedSchemaAdapter parse (final ProtobufSchema schema){ + + public static ParsedSchemaAdapter parse (final ProtobufSchema schema) { return new ConfluentParsedSchemaMetadata(schema); } @Override - public String getType() { + public final String getType() { return this.schemaType; } @Override - public Object getRawSchema() { + public final Object getRawSchema() { return this.rawSchema; } } diff --git a/src/main/resources/com/sngular/kloadgen/sampler/asyncapisampler.properties b/src/main/resources/com/sngular/kloadgen/sampler/AsyncApiSamplerResources.properties similarity index 100% rename from src/main/resources/com/sngular/kloadgen/sampler/asyncapisampler.properties rename to src/main/resources/com/sngular/kloadgen/sampler/AsyncApiSamplerResources.properties diff --git a/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractorTest.java b/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractorTest.java index 16c6d67c..6977c561 100644 --- a/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractorTest.java +++ b/src/test/java/com/sngular/kloadgen/extractor/extractors/avro/AvroConfluentExtractorTest.java @@ -6,6 +6,7 @@ import com.sngular.kloadgen.model.FieldValueMapping; import com.sngular.kloadgen.testutil.FileHelper; import io.confluent.kafka.schemaregistry.avro.AvroSchema; +import org.apache.avro.Schema; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -15,7 +16,7 @@ class AvroConfluentExtractorTest { private final FileHelper fileHelper = new FileHelper(); - private final Extractor avroConfluentExtractor = new AvroConfluentExtractor(); + private final Extractor avroConfluentExtractor = new AvroConfluentExtractor(); @Test @@ -24,7 +25,7 @@ void testFlatPropertiesEmbeddedAvros() throws Exception { final String testFile = fileHelper.getContent("/avro-files/embedded-avros-example-test.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(4) @@ -45,7 +46,7 @@ void testOptionalEnum() throws Exception { final String testFile = fileHelper.getContent("/avro-files/optionalEnum.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(1) @@ -68,7 +69,7 @@ void testFlatPropertiesOptionalMapArray() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testOptionalMap.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(8) @@ -97,7 +98,7 @@ void testFlatPropertiesOptionalMapArray() throws Exception { void testFlatPropertiesMap() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testMap.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(9) @@ -129,7 +130,7 @@ void testFlatPropertiesLogicalTypes() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testLogicalTypes.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(10) @@ -159,7 +160,7 @@ void testFlatPropertiesOptionalArray() throws Exception { final String testFile = fileHelper.getContent("/avro-files/issue.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(3) @@ -179,7 +180,7 @@ void testFlatPropertiesUnionRecordAvros() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testUnionRecord.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(8) @@ -209,7 +210,7 @@ void testFlatPropertiesRecordUnionReverseOrder() throws Exception { final String testFile = fileHelper.getContent("/avro-files/testUnionReverseOrder.avsc"); final AvroSchema schema = new AvroSchema(testFile); - final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema); + final List fieldValueMappingList = avroConfluentExtractor.processSchema(schema.rawSchema()); Assertions.assertThat(fieldValueMappingList) .hasSize(5)