Skip to content

Commit

Permalink
#384 fix style codacy
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Nov 24, 2023
1 parent 5413095 commit 06d0dde
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

import com.sngular.kloadgen.common.SchemaRegistryEnum;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;

public interface ExtractorRegistry<T> {
public interface ExtractorRegistry<T extends ParsedSchema> {
List<FieldValueMapping> processSchema(final T schema, SchemaRegistryEnum registry);

Object processSchema(final String fileContent);
T processSchema(final String fileContent);

List<String> getSchemaNameList(final String schema, SchemaRegistryEnum registryEnum);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;

public class ProtobuffExtractor<T> implements ExtractorRegistry<T> {
public class ProtobufExtractor<T extends ParsedSchema> implements ExtractorRegistry<T> {

private static final Map<SchemaRegistryEnum, Extractor> SCHEMA_REGISTRY_MAP = Map.of(SchemaRegistryEnum.CONFLUENT, new ProtoBufConfluentExtractor(),
SchemaRegistryEnum.APICURIO, new ProtoBufApicurioExtractor());

public final List<FieldValueMapping> processSchema(final ParsedSchema schemaReceived, final SchemaRegistryEnum registryEnum) {
public final List<FieldValueMapping> processSchema(final T schemaReceived, final SchemaRegistryEnum registryEnum) {
return new ArrayList<FieldValueMapping>(SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schemaReceived));
}

public final ParsedSchema processSchema(final String fileContent) {
return new ParsedSchema(fileContent, "PROTOBUF");
public final T processSchema(final String fileContent) {
return (T) new ParsedSchema(fileContent, "PROTOBUF");
}

public final List<String> getSchemaNameList(final String schema, final SchemaRegistryEnum registryEnum) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.apache.jorphan.gui.JLabeledTextField;
import org.apache.kafka.clients.producer.ProducerConfig;

@SuppressWarnings("checkstyle:ClassDataAbstractionCoupling")
public final class AsyncApiSamplerGui extends AbstractSamplerGui {

private final JFileChooser fileChooser = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
Expand All @@ -81,8 +82,6 @@ public final class AsyncApiSamplerGui extends AbstractSamplerGui {

private JComboBox<AsyncApiSchema> topicComboBox;

private JComboBox<AsyncApiSR> registryComboBox;

private AsyncApiFile asyncApiFile;

public AsyncApiSamplerGui() {
Expand Down Expand Up @@ -362,10 +361,10 @@ private JPanel createRegistryTab() {
final JPanel registryUrlPanel = new JPanel();
registryUrlPanel.setLayout(new BorderLayout(0, 0));
registryUrlPanel.add(new JLabeledTextField("Schema Registry URL"));
registryComboBox = new JComboBox<>();
final JComboBox<AsyncApiSR> registryComboBox = new JComboBox<>();
registryComboBox.setRenderer(new AsyncApiSRRenderer());
registryComboBox.addActionListener(this::registryComboActionListener);
registryUrlPanel.add(this.registryComboBox, BorderLayout.NORTH);
registryUrlPanel.add(registryComboBox, BorderLayout.NORTH);
registryUrlPanel.add(new JScrollPane(new JTable(schemaRegistryFieldModel)), BorderLayout.CENTER);
return registryUrlPanel;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.sngular.kloadgen.extractor.asyncapi;

import java.io.File;
import java.io.IOException;
import java.util.Locale;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
Expand All @@ -9,56 +13,50 @@
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
import org.apache.jmeter.util.JMeterUtils;
import static org.assertj.core.api.Assertions.*;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class AsyncApiExtractorImplTest {
private FileHelper fileHelper = new FileHelper();
private final ObjectMapper om = new ObjectMapper(new YAMLFactory());

@BeforeEach
public final void setUp() {
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);
}

@Test
@DisplayName("Should extract asyncapi file")
void testExtractFile() throws IOException {
final File testFile = fileHelper.getFile("/asyncapi/event-api.yml");
final AsyncApiFile asapfle = new AsyncApiExtractorImpl().processFile(testFile);

assertNotNull(asapfle);
assertEquals("{user_signedup=user_signedup}",asapfle.getApiSchemaList().toString());
assertEquals("{production=AsyncApiServer(name=production, url=mqtt://test.mosquitto.org, protocol=mqtt, description=Test MQTT broker)}",
asapfle.getApiServerMap().toString());
}
@Test
@DisplayName("Should extract basic asyncapi schema")
void testExtractAsyncapiSchema() throws IOException {
final File testFile = fileHelper.getFile("/asyncapi/event-api.yml");
final JsonNode openApi = om.readTree(testFile);
final AsyncApiFile asapfle = new AsyncApiExtractorImpl().processNode(openApi);
JsonNode propertiesSchema = asapfle.getAsyncApiFileNode().path("components").path("schemas").path("userSignedUpPayload").path("properties");
Assertions.assertThat(asapfle).isNotNull();
assertThat(propertiesSchema)
.hasSize(4)
.isSubsetOf(openApi.get("components").get("schemas").get("userSignedUpPayload").get("properties"));
}
private final ObjectMapper om = new ObjectMapper(new YAMLFactory());

private final FileHelper fileHelper = new FileHelper();

@BeforeEach
public final void setUp() {
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);
}

@Test
@DisplayName("Should extract asyncapi file")
void testExtractFile() throws IOException {
final File testFile = fileHelper.getFile("/asyncapi/event-api.yml");
final AsyncApiFile asapfle = new AsyncApiExtractorImpl().processFile(testFile);

org.junit.jupiter.api.Assertions.assertNotNull(asapfle);
org.junit.jupiter.api.Assertions.assertEquals("{user_signedup=user_signedup}", asapfle.getApiSchemaList().toString());
org.junit.jupiter.api.Assertions.assertEquals("{production=AsyncApiServer(name=production, url=mqtt://test.mosquitto.org, protocol=mqtt, description=Test MQTT broker)}",
asapfle.getApiServerMap().toString());
}

@Test
@DisplayName("Should extract basic asyncapi schema")
void testExtractAsyncapiSchema() throws IOException {
final File testFile = fileHelper.getFile("/asyncapi/event-api.yml");
final JsonNode openApi = om.readTree(testFile);
final AsyncApiFile asapfle = new AsyncApiExtractorImpl().processNode(openApi);
final JsonNode propertiesSchema = asapfle.getAsyncApiFileNode().path("components").path("schemas").path("userSignedUpPayload").path("properties");
Assertions.assertThat(asapfle).isNotNull();
Assertions.assertThat(propertiesSchema)
.hasSize(4)
.isSubsetOf(openApi.get("components").get("schemas").get("userSignedUpPayload").get("properties"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private static Stream<Object> parametersForTestBasicStructure() {
return Stream.of(Arguments.of(JsonSchemaFixturesConstants.SIMPLE_SCHEMA_REQUIRED, JsonSchemaFixturesConstants.SIMPLE_SCHEMA_REQUIRED_EXPECTED));
}

private static Stream<Object> parametersForTestOptionalFieldNonEmpty(){
private static Stream<Object> parametersForTestOptionalFieldNonEmpty() {
return Stream.of(Arguments.of(JsonSchemaFixturesConstants.SIMPLE_SCHEMA_NONREQUIRED, JsonSchemaFixturesConstants.SIMPLE_SCHEMA_NONREQUIRED_EXPECTED));
}

Expand Down Expand Up @@ -125,11 +125,11 @@ final void testNestedComplexLevels() {
@ParameterizedTest
@DisplayName("Should process non-required fields if it is not empty")
@MethodSource("parametersForTestOptionalFieldNonEmpty")
final void testOptionalFieldNonEmpty(final List<FieldValueMapping> schemaAsJson, final String expected){
final void testOptionalFieldNonEmpty(final List<FieldValueMapping> schemaAsJson, final String expected) {

final SchemaProcessor jsonSchemaProcessor = new SchemaProcessor();

jsonSchemaProcessor.processSchema(SchemaTypeEnum.JSON,null,null,schemaAsJson);
jsonSchemaProcessor.processSchema(SchemaTypeEnum.JSON, null, null, schemaAsJson);
final ObjectNode message = (ObjectNode) jsonSchemaProcessor.next();

Assertions.assertThat(message.toString()).isEqualTo(expected);
Expand Down

This file was deleted.

0 comments on commit 06d0dde

Please sign in to comment.