Skip to content

Commit

Permalink
#384 Refactor Extractor. Fix some Codacy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Nov 27, 2023
1 parent f1bf5ec commit 3754b7c
Show file tree
Hide file tree
Showing 71 changed files with 1,111 additions and 1,304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.sngular.kloadgen.common.SchemaRegistryEnum;
import com.sngular.kloadgen.extractor.extractors.ExtractorFactory;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.parsedschema.AbstractParsedSchema;
import lombok.Getter;
import org.apache.commons.lang3.tuple.Pair;

Expand All @@ -30,7 +30,7 @@ public static Pair<String, List<FieldValueMapping>> flatPropertiesList(final Str
return ExtractorFactory.flatPropertiesList(subjectName);
}

public static List<FieldValueMapping> flatPropertiesList(final ParsedSchema parserSchema) {
public static List<FieldValueMapping> flatPropertiesList(final AbstractParsedSchema<?> parserSchema) {
return ExtractorFactory.getExtractor(parserSchema.schemaType()).processSchema(parserSchema, SchemaRegistryEnum.CONFLUENT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Properties;

import com.sngular.kloadgen.common.SchemaRegistryEnum;
Expand All @@ -12,12 +11,10 @@
import com.sngular.kloadgen.extractor.extractors.json.JsonExtractor;
import com.sngular.kloadgen.extractor.extractors.protobuf.ProtobufExtractor;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.schemaregistry.adapter.impl.AbstractParsedSchemaAdapter;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioParsedSchemaMetadata;
import com.sngular.kloadgen.util.JMeterHelper;
import com.sngular.kloadgen.util.SchemaRegistryKeyHelper;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.jmeter.threads.JMeterContextService;

Expand All @@ -37,14 +34,13 @@ public static void configExtractorFactory(final AvroExtractor avroExtractor, fin
ExtractorFactory.protobufExtractor = protobufExtractor;
}

public static ExtractorRegistry getExtractor(final String schemaType) {
public static ExtractorRegistry<?> getExtractor(final String schemaType) {

if (schemaType != null && EnumUtils.isValidEnum(SchemaTypeEnum.class, schemaType.toUpperCase())) {
return switch (SchemaTypeEnum.valueOf(schemaType.toUpperCase())) {
case JSON -> jsonExtractor;
case AVRO -> avroExtractor;
case PROTOBUF -> protobufExtractor;
default -> throw new KLoadGenException(String.format("Schema type not supported %s", schemaType));
};
} else {
throw new KLoadGenException(String.format("Schema type not supported %s", schemaType));
Expand All @@ -55,20 +51,13 @@ public static Pair<String, List<FieldValueMapping>> 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);
final AbstractParsedSchemaAdapter parsedSchemaAdapter = schemaParsed.getParsedSchemaAdapter();
final String schemaType = parsedSchemaAdapter.getType();
final String schemaType = schemaParsed.getSchemaType();

final List<FieldValueMapping> attributeList = new ArrayList<>();
final SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registryName.toUpperCase());

final Object schema;
if (Objects.nonNull(registryName)) {
//TODO change parser
schema = switch (schemaRegistryEnum) {
case APICURIO -> ((ApicurioParsedSchemaMetadata) parsedSchemaAdapter).getSchema();
case CONFLUENT -> parsedSchemaAdapter.getRawSchema();
};
attributeList.addAll(getExtractor(schemaType).processSchema(new ParsedSchema(schema), schemaRegistryEnum));
if (StringUtils.isNotEmpty(registryName)) {
attributeList.addAll(getExtractor(schemaType).processSchema(schemaParsed, schemaRegistryEnum));
}
return Pair.of(schemaType, attributeList);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

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

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

ParsedSchema processSchema(final String fileContent);
List<FieldValueMapping> processSchema(final AbstractParsedSchema<?> schema, SchemaRegistryEnum registry);

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 @@ -7,19 +7,21 @@
import com.sngular.kloadgen.extractor.extractors.Extractor;
import com.sngular.kloadgen.extractor.extractors.ExtractorRegistry;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.parsedschema.AbstractParsedSchema;
import com.sngular.kloadgen.parsedschema.AvroParsedSchema;
import org.apache.avro.Schema;

public class AvroExtractor<T extends ParsedSchema> implements ExtractorRegistry<T> {
public class AvroExtractor implements ExtractorRegistry<AvroParsedSchema> {

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

public final List<FieldValueMapping> processSchema(final T schema, final SchemaRegistryEnum registryEnum) {
return SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schema.rawSchema());
public final List<FieldValueMapping> processSchema(final AbstractParsedSchema<?> schema, final SchemaRegistryEnum registryEnum) {
return SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schema.getSchema());
}

public final ParsedSchema processSchema(final String fileContent) {
return new ParsedSchema(fileContent, "AVRO");
public final AvroParsedSchema processSchema(final String fileContent) {
return new AvroParsedSchema(null, new Schema.Parser().parse(fileContent));
}

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 @@ -7,21 +7,21 @@
import com.sngular.kloadgen.extractor.extractors.Extractor;
import com.sngular.kloadgen.extractor.extractors.ExtractorRegistry;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.parsedschema.AbstractParsedSchema;
import com.sngular.kloadgen.parsedschema.JsonParsedSchema;


public class JsonExtractor<T extends ParsedSchema> implements ExtractorRegistry<T> {
public class JsonExtractor implements ExtractorRegistry<JsonParsedSchema> {

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

public final List<FieldValueMapping> processSchema(final T schemaReceived, final SchemaRegistryEnum registryEnum) {
return SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schemaReceived.rawSchema().toString());
public final List<FieldValueMapping> processSchema(final AbstractParsedSchema<?> schemaReceived, final SchemaRegistryEnum registryEnum) {
return SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schemaReceived.getRawSchema().toString());
}


public final ParsedSchema processSchema(final String fileContent) {
return new ParsedSchema(fileContent, "JSON");
public final JsonParsedSchema processSchema(final String fileContent) {
return new JsonParsedSchema(null, fileContent);
}

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 @@ -4,14 +4,14 @@

import com.sngular.kloadgen.extractor.extractors.Extractor;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.parsedschema.AbstractParsedSchema;
import com.squareup.wire.schema.internal.parser.ProtoFileElement;
import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema;

public class ProtoBufConfluentExtractor extends AbstractProtoFileExtractor implements Extractor<ParsedSchema> {
public class ProtoBufConfluentExtractor extends AbstractProtoFileExtractor implements Extractor<AbstractParsedSchema<ProtoFileElement>> {

public final List<FieldValueMapping> processSchema(final ParsedSchema schemaReceived) {
return processSchemaDefault((ProtoFileElement) schemaReceived.rawSchema());
public final List<FieldValueMapping> processSchema(final AbstractParsedSchema<ProtoFileElement> schemaReceived) {
return processSchemaDefault(schemaReceived.getRawSchema());
}

public final List<String> getSchemaNameList(final String schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@
import com.sngular.kloadgen.extractor.extractors.Extractor;
import com.sngular.kloadgen.extractor.extractors.ExtractorRegistry;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.parsedschema.AbstractParsedSchema;
import com.sngular.kloadgen.parsedschema.ProtobufParsedSchema;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.internal.parser.ProtoParser;

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

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 T schemaReceived, final SchemaRegistryEnum registryEnum) {
public final List<FieldValueMapping> processSchema(final AbstractParsedSchema<?> schemaReceived, final SchemaRegistryEnum registryEnum) {
return new ArrayList<FieldValueMapping>(SCHEMA_REGISTRY_MAP.get(registryEnum).processSchema(schemaReceived));
}

public final ParsedSchema processSchema(final String fileContent) {
return (T) new ParsedSchema(fileContent, "PROTOBUF");
public final ProtobufParsedSchema processSchema(final String fileContent) {
final var protoDescriptor = new ProtoParser(Location.get("/"), fileContent.toCharArray());
return new ProtobufParsedSchema(null, protoDescriptor.readProtoFile());
}

public final List<String> getSchemaNameList(final String schema, final SchemaRegistryEnum registryEnum) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.sngular.kloadgen.parsedschema;

import lombok.Getter;

@Getter
public abstract class AbstractParsedSchema<T> {

private final T schema;
private final String schemaType;
private final String name;

public AbstractParsedSchema(final String name, final String type, final T schema) {
this.name = name;
this.schemaType = type;
this.schema = schema;
}

public final T schema() {
return this.schema;
}

public final String schemaType() {
return this.schemaType;
}

public abstract T getRawSchema();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sngular.kloadgen.parsedschema;

import org.apache.avro.Schema;

public class AvroParsedSchema extends AbstractParsedSchema<Schema> {

public AvroParsedSchema(final String name, final Schema schema) {
super(name, "AVRO", schema);
}

@Override
public final Schema getRawSchema() {
return getSchema();
}
}
13 changes: 0 additions & 13 deletions src/main/java/com/sngular/kloadgen/parsedschema/IParsedSchema.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.sngular.kloadgen.parsedschema;

public class JsonParsedSchema extends AbstractParsedSchema<String> {

public JsonParsedSchema(final String name, final String schema) {
super(name, "JSON", schema);
}

@Override
public final String getRawSchema() {
return getSchema();
}
}
Loading

0 comments on commit 3754b7c

Please sign in to comment.