Skip to content

Commit

Permalink
Uncomment test and fixes to use it plus checkstyle
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoSngular committed Nov 15, 2023
1 parent dd0d794 commit dad5fcf
Show file tree
Hide file tree
Showing 20 changed files with 75 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import com.sngular.kloadgen.exception.KLoadGenException;
import com.sngular.kloadgen.extractor.extractors.avro.AvroExtractor;
import com.sngular.kloadgen.extractor.extractors.json.JsonExtractor;
import com.sngular.kloadgen.extractor.extractors.protobuff.ProtobuffExtractor;
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;
Expand All @@ -26,15 +26,15 @@ public final class ExtractorFactory {

private static JsonExtractor jsonExtractor = new JsonExtractor();

private static ProtobuffExtractor protobuffExtractor = new ProtobuffExtractor();
private static ProtobufExtractor protobufExtractor = new ProtobufExtractor();

private ExtractorFactory() {
}

public static void configExtractorFactory(final AvroExtractor avroExtractor, final JsonExtractor jsonExtractor, final ProtobuffExtractor protobuffExtractor) {
public static void configExtractorFactory(final AvroExtractor avroExtractor, final JsonExtractor jsonExtractor, final ProtobufExtractor protobufExtractor) {
ExtractorFactory.avroExtractor = avroExtractor;
ExtractorFactory.jsonExtractor = jsonExtractor;
ExtractorFactory.protobuffExtractor = protobuffExtractor;
ExtractorFactory.protobufExtractor = protobufExtractor;
}

public static ExtractorRegistry getExtractor(final String schemaType) {
Expand All @@ -43,7 +43,7 @@ public static ExtractorRegistry getExtractor(final String schemaType) {
final ExtractorRegistry response = switch (SchemaTypeEnum.valueOf(schemaType.toUpperCase())) {
case JSON -> jsonExtractor;
case AVRO -> avroExtractor;
case PROTOBUF -> protobuffExtractor;
case PROTOBUF -> protobufExtractor;
default -> throw new KLoadGenException(String.format("Schema type not supported %s", schemaType));
};
return response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sngular.kloadgen.extractor.extractors.protobuff;
package com.sngular.kloadgen.extractor.extractors.protobuf;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sngular.kloadgen.extractor.extractors.protobuff;
package com.sngular.kloadgen.extractor.extractors.protobuf;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sngular.kloadgen.extractor.extractors.protobuff;
package com.sngular.kloadgen.extractor.extractors.protobuf;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sngular.kloadgen.extractor.extractors.protobuff;
package com.sngular.kloadgen.extractor.extractors.protobuf;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,7 +10,7 @@
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;

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

private static final Map<SchemaRegistryEnum, Extractor> SCHEMA_REGISTRY_MAP = Map.of(SchemaRegistryEnum.CONFLUENT, new ProtoBufConfluentExtractor(),
SchemaRegistryEnum.APICURIO, new ProtoBufApicurioExtractor());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sngular.kloadgen.schemaregistry.adapter.impl;

import com.sngular.kloadgen.parsedschema.IParsedSchema;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema;
import lombok.Getter;
Expand All @@ -16,7 +17,7 @@ public final class ConfluentAbstractParsedSchemaMetadata extends AbstractParsedS

private Object rawSchema;

private ConfluentAbstractParsedSchemaMetadata(final ParsedSchema parsedSchema) {
private ConfluentAbstractParsedSchemaMetadata(final IParsedSchema parsedSchema) {
this.schemaType = parsedSchema.schemaType();
this.name = parsedSchema.name();
this.canonicalString = parsedSchema.canonicalString();
Expand All @@ -35,11 +36,7 @@ public ConfluentAbstractParsedSchemaMetadata(final ProtobufSchema schema) {
this.canonicalString = schema.canonicalString();
}

public ConfluentAbstractParsedSchemaMetadata() {

}

public static AbstractParsedSchemaAdapter parse(final ParsedSchema parsedSchema) {
public static AbstractParsedSchemaAdapter parse(final IParsedSchema parsedSchema) {
return new ConfluentAbstractParsedSchemaMetadata(parsedSchema);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,12 @@ public String getSchemaRegistryUrlKey() {

@Override
public void setSchemaRegistryClient(final String url, final Map<String, ?> 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<String, ?> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@

import com.sngular.kloadgen.common.SchemaRegistryEnum;
import com.sngular.kloadgen.exception.KLoadGenException;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioAbstractParsedSchemaMetadata;
import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseParsedSchema;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentAbstractParsedSchemaMetadata;
import com.sngular.kloadgen.testutil.FileHelper;
import com.sngular.kloadgen.testutil.ParsedSchemaUtil;
import com.sngular.kloadgen.util.JMeterHelper;
import com.sngular.kloadgen.util.SchemaRegistryKeyHelper;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
Expand Down Expand Up @@ -105,7 +106,7 @@ void testFlatPropertiesListWithProtobuf() throws IOException {
properties.setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryEnum.CONFLUENT.toString());

final ParsedSchema parsedSchema = new ParsedSchema(testFile, "PROTOBUF");
final BaseParsedSchema baseParsedSchema = new BaseParsedSchema<>(ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema));
final BaseParsedSchema 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");
Expand All @@ -121,7 +122,7 @@ void testReadSchemaFile() throws IOException {
Assertions.assertThat(result).isNotNull();
}

/*@Test
@Test
@DisplayName("Test flatPropertiesList throws exception schema type not supported")
void testFlatPropertiesListWithException() {

Expand All @@ -136,7 +137,7 @@ void testFlatPropertiesListWithException() {
.isThrownBy(() -> SchemaExtractor.flatPropertiesList("exceptionSubject")
).withMessage(String.format("Schema type not supported %s", parsedSchema.schemaType()));

}*/
}

@Test
@DisplayName("Test flatPropertiesList with AVRO and CONFLUENT")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@
import com.sngular.kloadgen.extractor.extractors.avro.AvroConfluentExtractor;
import com.sngular.kloadgen.extractor.extractors.avro.AvroExtractor;
import com.sngular.kloadgen.extractor.extractors.json.JsonExtractor;
import com.sngular.kloadgen.extractor.extractors.protobuff.ProtobuffExtractor;
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.BaseParsedSchema;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentAbstractParsedSchemaMetadata;
import com.sngular.kloadgen.testutil.FileHelper;
import com.sngular.kloadgen.util.JMeterHelper;
import com.sngular.kloadgen.util.SchemaRegistryKeyHelper;
import com.sngular.kloadgen.parsedschema.ParsedSchema;

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

import org.apache.avro.Schema;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
Expand All @@ -22,26 +29,13 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

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

import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.when;
import org.mockito.*;

class ExtractorFactoryTest {

private static final JsonExtractor jsonExtractor = new JsonExtractor();
private static final JsonExtractor JSON_EXTRACTOR = new JsonExtractor();

private static final ProtobuffExtractor protobuffExtractor = new ProtobuffExtractor();
private static final ProtobufExtractor PROTOBUF_EXTRACTOR = new ProtobufExtractor();

@Captor
private ArgumentCaptor<Schema> argumentCaptor = ArgumentCaptor.forClass(Schema.class);
Expand All @@ -52,7 +46,7 @@ class ExtractorFactoryTest {

private final Extractor<Schema> avroConfluentExtractor = new AvroConfluentExtractor();

private MockedStatic<JMeterHelper> jMeterHelperMockedStatic;
private MockedStatic<JMeterHelper> jMETER_HELPER_MOCKED_STATIC;

private MockedStatic<JMeterContextService> jMeterContextServiceMockedStatic;

Expand All @@ -65,36 +59,36 @@ public void init() {
jmcx.setVariables(new JMeterVariables());
JMeterUtils.setLocale(Locale.ENGLISH);

jMeterHelperMockedStatic = Mockito.mockStatic(JMeterHelper.class);
jMETER_HELPER_MOCKED_STATIC = Mockito.mockStatic(JMeterHelper.class);
jMeterContextServiceMockedStatic = Mockito.mockStatic(JMeterContextService.class, Answers.RETURNS_DEEP_STUBS);
argumentCaptor = ArgumentCaptor.forClass(Schema.class);
}

@AfterEach
public void tearDown() {
properties.clear();
jMeterHelperMockedStatic.close();
jMETER_HELPER_MOCKED_STATIC.close();
jMeterContextServiceMockedStatic.close();
}

@Test
void flatPropertiesList() throws IOException {
AvroExtractor avroExtractor = Mockito.mock(AvroExtractor.class);
ExtractorFactory.configExtractorFactory(avroExtractor, jsonExtractor, protobuffExtractor);
final AvroExtractor avroExtractor = Mockito.mock(AvroExtractor.class);
ExtractorFactory.configExtractorFactory(avroExtractor, JSON_EXTRACTOR, PROTOBUF_EXTRACTOR);

final File testFile = fileHelper.getFile("/avro-files/embedded-avros-example-test.avsc");
properties.setProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME, SchemaRegistryEnum.CONFLUENT.toString());

final ParsedSchema parsedSchema = new ParsedSchema(testFile, "AVRO");
final var baseParsedSchema = new BaseParsedSchema<>(ConfluentAbstractParsedSchemaMetadata.parse(parsedSchema));

jMeterHelperMockedStatic.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema);
jMETER_HELPER_MOCKED_STATIC.when(() -> JMeterHelper.getParsedSchema(Mockito.anyString(), Mockito.any(Properties.class))).thenReturn(baseParsedSchema);
jMeterContextServiceMockedStatic.when(() -> JMeterContextService.getContext().getProperties()).thenReturn(properties);

final Schema schema = new Schema.Parser().parse(testFile);
final List<FieldValueMapping> fieldValueMappingList = avroConfluentExtractor.processSchema(schema);

when(avroExtractor.processSchema(new ParsedSchema(argumentCaptor.capture(), ""), isA(SchemaRegistryEnum.class))).thenReturn(fieldValueMappingList);
Mockito.when(avroExtractor.processSchema(new ParsedSchema(argumentCaptor.capture(), ""), ArgumentMatchers.isA(SchemaRegistryEnum.class))).thenReturn(fieldValueMappingList);
final var result = SchemaExtractor.flatPropertiesList(schema.getName());

Assertions.assertThat(result).isNotNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sngular.kloadgen.extractor.extractors.protobuff;
package com.sngular.kloadgen.extractor.extractors.protobuf;

import java.io.File;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.sngular.kloadgen.extractor.extractors.protobuff;
package com.sngular.kloadgen.extractor.extractors.protobuf;

import java.io.File;
import java.util.List;
Expand All @@ -8,7 +8,6 @@
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.testutil.FileHelper;
import io.confluent.kafka.schemaregistry.protobuf.ProtobufSchema;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
import com.sngular.kloadgen.common.SchemaTypeEnum;
import com.sngular.kloadgen.exception.KLoadGenException;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.processor.fixture.AvroSchemaFixturesConstants;
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.parsedschema.ParsedSchema;
import org.apache.avro.LogicalTypes;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
import java.util.List;

import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.testutil.FileHelper;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.testutil.FileHelper;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
import java.util.stream.Stream;

import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.processor.fixture.JsonSchemaFixturesConstants;
import com.sngular.kloadgen.serializer.SerializerTestFixture;
import com.sngular.kloadgen.testutil.FileHelper;
import com.sngular.kloadgen.util.PropsKeysHelper;
import com.sngular.kloadgen.util.SchemaRegistryKeyHelper;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

import com.sngular.kloadgen.common.SchemaTypeEnum;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.processor.SchemaProcessor;
import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentSchemaMetadata;
import com.sngular.kloadgen.util.PropsKeysHelper;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

import com.sngular.kloadgen.common.SchemaTypeEnum;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.processor.SchemaProcessor;
import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentSchemaMetadata;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import org.apache.avro.generic.GenericRecord;
import org.apache.kafka.common.serialization.Serializer;
import org.assertj.core.api.Assertions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sngular.kloadgen.common.SchemaTypeEnum;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.processor.SchemaProcessor;
import com.sngular.kloadgen.schemaregistry.adapter.impl.BaseSchemaMetadata;
import com.sngular.kloadgen.schemaregistry.adapter.impl.ConfluentSchemaMetadata;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down
Loading

0 comments on commit dad5fcf

Please sign in to comment.