Skip to content

Commit 3922919

Browse files
josemenvegsngularjemacineirasjg2devSrsonatype-lift[bot]
authored
code refactor (#375)
* code refactor * checkstyle revisited * Fixing the code * correcting errors, uncouple the ways to extract the formatting from the Schema Registry, added tests * code review refactor * code review * Update src/main/java/com/sngular/kloadgen/extractor/extractors/protobuff/AbstractProtoFileExtractor.java Co-authored-by: sonatype-lift[bot] <37194012+sonatype-lift[bot]@users.noreply.github.com> * bump version --------- Co-authored-by: Jose Enrique García Maciñeiras <joseenrique.garcia@sngular.com> Co-authored-by: Julian Gomez <julian.gomez@sngular.com> Co-authored-by: sonatype-lift[bot] <37194012+sonatype-lift[bot]@users.noreply.github.com>
1 parent a5f8a56 commit 3922919

File tree

82 files changed

+1689
-867
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+1689
-867
lines changed

pom-maven-central.xml

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<artifactId>kloadgen</artifactId>
99

10-
<version>5.6.3</version>
10+
<version>5.6.4</version>
1111

1212
<name>KLoadGen</name>
1313
<description>Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial
@@ -257,6 +257,28 @@
257257
</roles>
258258
<timezone>Europe/Madrid</timezone>
259259
</developer>
260+
<developer>
261+
<id>josemenvegsngular</id>
262+
<name>José María Méndez Vega</name>
263+
<email>josemaria.mendez@sngular.com</email>
264+
<organization>Sngular</organization>
265+
<organizationUrl>https://www.sngular.com</organizationUrl>
266+
<roles>
267+
<role>Developer</role>
268+
</roles>
269+
<timezone>Europe/Madrid</timezone>
270+
</developer>
271+
<developer>
272+
<id>jg2devSr</id>
273+
<name>Julian Gomez Gomez</name>
274+
<email>julian.gomez@sngular.com</email>
275+
<organization>Sngular</organization>
276+
<organizationUrl>https://www.sngular.com</organizationUrl>
277+
<roles>
278+
<role>Senior Backend Developer</role>
279+
</roles>
280+
<timezone>Europe/Madrid</timezone>
281+
</developer>
260282
</developers>
261283

262284
<scm>

pom.xml

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
<artifactId>kloadgen</artifactId>
99

10-
<version>5.6.3</version>
10+
<version>5.6.4</version>
1111

1212
<name>KLoadGen</name>
1313
<description>Load Generation Jmeter plugin for Kafka Cluster. Supporting AVRO, JSON Schema and Protobuf schema types. Generate Artificial
@@ -246,6 +246,28 @@
246246
</roles>
247247
<timezone>Europe/Madrid</timezone>
248248
</developer>
249+
<developer>
250+
<id>josemenvegsngular</id>
251+
<name>José María Méndez Vega</name>
252+
<email>josemaria.mendez@sngular.com</email>
253+
<organization>Sngular</organization>
254+
<organizationUrl>https://www.sngular.com</organizationUrl>
255+
<roles>
256+
<role>Developer</role>
257+
</roles>
258+
<timezone>Europe/Madrid</timezone>
259+
</developer>
260+
<developer>
261+
<id>jg2devSr</id>
262+
<name>Julian Gomez Gomez</name>
263+
<email>julian.gomez@sngular.com</email>
264+
<organization>Sngular</organization>
265+
<organizationUrl>https://www.sngular.com</organizationUrl>
266+
<roles>
267+
<role>Senior Backend Developer</role>
268+
</roles>
269+
<timezone>Europe/Madrid</timezone>
270+
</developer>
249271
</developers>
250272

251273
<scm>
@@ -291,7 +313,7 @@
291313
<rest-assured.version>4.5.0</rest-assured.version>
292314
<slf4j-api.version>2.0.0-alpha1</slf4j-api.version>
293315
<wiremock-junit5.version>1.3.1</wiremock-junit5.version>
294-
<wiremock.version>2.32.0</wiremock.version>
316+
<wiremock.version>2.35.0</wiremock.version>
295317
</properties>
296318

297319
<dependencies>

schema_registry_docker/docker-compose-noauth.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ services:
3535
ports:
3636
- '8080:8080'
3737
environment:
38-
AUTH_ENABLED: false
38+
AUTH_ENABLED: "false"
3939
kafka-manager:
4040
image: iunera/cmak
4141
depends_on:

src/main/java/com/sngular/kloadgen/config/schemaregistry/SchemaRegistryConfigElement.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
import java.util.Objects;
1717

1818
import com.sngular.kloadgen.model.PropertyMapping;
19-
import com.sngular.kloadgen.sampler.schemaregistry.SchemaRegistryAdapter;
20-
import com.sngular.kloadgen.sampler.schemaregistry.SchemaRegistryManagerFactory;
19+
import com.sngular.kloadgen.schemaregistry.SchemaRegistryAdapter;
20+
import com.sngular.kloadgen.schemaregistry.SchemaRegistryManagerFactory;
2121
import com.sngular.kloadgen.util.JMeterHelper;
2222
import com.sngular.kloadgen.util.ProducerKeysHelper;
2323
import com.sngular.kloadgen.util.SchemaRegistryKeyHelper;

src/main/java/com/sngular/kloadgen/extractor/SchemaExtractor.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,58 @@
22

33
import java.io.File;
44
import java.io.IOException;
5+
import java.nio.charset.StandardCharsets;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
58
import java.util.List;
9+
import java.util.stream.Stream;
610

11+
import com.sngular.kloadgen.common.SchemaRegistryEnum;
12+
import com.sngular.kloadgen.extractor.extractors.ExtractorFactory;
713
import com.sngular.kloadgen.model.FieldValueMapping;
814
import io.confluent.kafka.schemaregistry.ParsedSchema;
9-
import io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException;
1015
import org.apache.commons.lang3.tuple.Pair;
1116

12-
public interface SchemaExtractor {
17+
public class SchemaExtractor {
1318

14-
Pair<String, List<FieldValueMapping>> flatPropertiesList(String subjectName) throws IOException, RestClientException;
19+
private SchemaRegistryEnum type;
1520

16-
List<FieldValueMapping> flatPropertiesList(ParsedSchema parserSchema);
21+
public SchemaExtractor() {
22+
}
1723

18-
ParsedSchema schemaTypesList(File schemaFile, String schemaType) throws IOException;
24+
public SchemaRegistryEnum getType(){
25+
return type;
26+
}
27+
28+
public void setType(SchemaRegistryEnum type){
29+
this.type = type;
30+
}
31+
32+
public static Pair<String, List<FieldValueMapping>> flatPropertiesList(final String subjectName) {
33+
return ExtractorFactory.flatPropertiesList(subjectName);
34+
}
35+
36+
public static List<FieldValueMapping> flatPropertiesList(final ParsedSchema parserSchema) {
37+
return ExtractorFactory.getExtractor(parserSchema.schemaType(), "CONFLUENT").processSchema(parserSchema, SchemaRegistryEnum.CONFLUENT);
38+
}
39+
40+
public static List<String> schemaTypesList(final File schemaFile, final String schemaType, String registry) throws IOException {
41+
return ExtractorFactory.getExtractor(schemaType, registry).getSchemaNameList(readLineByLine(schemaFile.getPath()),
42+
ExtractorFactory.getSchemaRegistry(registry));
43+
}
44+
45+
private static String readLineByLine(final String filePath) throws IOException {
46+
final StringBuilder contentBuilder = new StringBuilder();
47+
48+
try (Stream<String> stream = Files.lines(Paths.get(filePath), StandardCharsets.UTF_8)) {
49+
stream.forEach(s -> contentBuilder.append(s).append("\n"));
50+
}
51+
52+
return contentBuilder.toString();
53+
}
54+
55+
private static List<FieldValueMapping> processSchema(final ParsedSchema schema) {
56+
return ExtractorFactory.getExtractor(schema.schemaType(),SchemaRegistryEnum.CONFLUENT.name()).processSchema(schema.rawSchema().toString(), SchemaRegistryEnum.CONFLUENT);
57+
}
1958

2059
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.sngular.kloadgen.extractor.extractors;
2+
3+
import java.util.List;
4+
5+
import com.sngular.kloadgen.model.FieldValueMapping;
6+
7+
public interface Extractor<T> {
8+
List<FieldValueMapping> processSchema(final T schema);
9+
10+
List<String> getSchemaNameList(final String schema);
11+
12+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package com.sngular.kloadgen.extractor.extractors;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.Objects;
6+
import java.util.Properties;
7+
8+
import com.sngular.kloadgen.common.SchemaRegistryEnum;
9+
import com.sngular.kloadgen.common.SchemaTypeEnum;
10+
import com.sngular.kloadgen.exception.KLoadGenException;
11+
import com.sngular.kloadgen.extractor.extractors.avro.AvroExtractor;
12+
import com.sngular.kloadgen.extractor.extractors.json.JsonExtractor;
13+
import com.sngular.kloadgen.extractor.extractors.protobuff.ProtobuffExtractor;
14+
import com.sngular.kloadgen.model.FieldValueMapping;
15+
import com.sngular.kloadgen.schemaregistry.adapter.impl.ApicurioParsedSchemaMetadata;
16+
import com.sngular.kloadgen.schemaregistry.adapter.impl.ParsedSchemaAdapter;
17+
import com.sngular.kloadgen.util.JMeterHelper;
18+
import com.sngular.kloadgen.util.SchemaRegistryKeyHelper;
19+
import org.apache.commons.lang3.EnumUtils;
20+
import org.apache.commons.lang3.tuple.Pair;
21+
import org.apache.jmeter.threads.JMeterContextService;
22+
23+
public final class ExtractorFactory {
24+
private static AvroExtractor avroExtractor = new AvroExtractor();
25+
26+
private static JsonExtractor jsonExtractor = new JsonExtractor();
27+
28+
private static ProtobuffExtractor protobuffExtractor = new ProtobuffExtractor();
29+
30+
private ExtractorFactory() {
31+
}
32+
33+
public static ExtractorRegistry getExtractor(final String schemaType, final String schemaRegistryEnum) {
34+
35+
SchemaRegistryEnum registryEnum = getSchemaRegistry(schemaRegistryEnum);
36+
37+
if (schemaType != null && EnumUtils.isValidEnum(SchemaTypeEnum.class, schemaType.toUpperCase())) {
38+
final ExtractorRegistry response;
39+
switch (SchemaTypeEnum.valueOf(schemaType.toUpperCase())) {
40+
case JSON:
41+
response = jsonExtractor;
42+
break;
43+
case AVRO:
44+
response = avroExtractor;
45+
break;
46+
case PROTOBUF:
47+
response = protobuffExtractor;
48+
break;
49+
default:
50+
throw new KLoadGenException(String.format("Schema type not supported %s", schemaType));
51+
}
52+
return response;
53+
} else {
54+
throw new KLoadGenException(String.format("Schema type not supported %s", schemaType));
55+
}
56+
}
57+
58+
public static SchemaRegistryEnum getSchemaRegistry(String schemaRegistryEnum) {
59+
if (schemaRegistryEnum != null && EnumUtils.isValidEnum(SchemaRegistryEnum.class, schemaRegistryEnum.toUpperCase())) {
60+
return SchemaRegistryEnum.valueOf(schemaRegistryEnum.toUpperCase());
61+
} else {
62+
throw new KLoadGenException(String.format("Schema Registry type not supported %s", schemaRegistryEnum));
63+
}
64+
}
65+
66+
public static Pair<String, List<FieldValueMapping>> flatPropertiesList(final String subjectName) {
67+
final Properties properties = JMeterContextService.getContext().getProperties();
68+
final var schemaParsed = JMeterHelper.getParsedSchema(subjectName, properties);
69+
final String registryName = properties.getProperty(SchemaRegistryKeyHelper.SCHEMA_REGISTRY_NAME);
70+
String schemaType = null;
71+
final ParsedSchemaAdapter parsedSchemaAdapter = schemaParsed.getParsedSchemaAdapter();
72+
schemaType = parsedSchemaAdapter.getType();
73+
74+
List<FieldValueMapping> attributeList = new ArrayList<>();
75+
SchemaRegistryEnum schemaRegistryEnum = SchemaRegistryEnum.valueOf(registryName.toUpperCase());
76+
77+
Object schema = null;
78+
if (Objects.nonNull(registryName)) {
79+
//TODO change parser
80+
switch (schemaRegistryEnum) {
81+
case APICURIO:
82+
schema = ((ApicurioParsedSchemaMetadata) parsedSchemaAdapter).getSchema();
83+
break;
84+
case CONFLUENT:
85+
schema = parsedSchemaAdapter.getRawSchema();
86+
break;
87+
default:
88+
throw new KLoadGenException("Schema Registry Type nos supported " + registryName.toUpperCase());
89+
}
90+
attributeList = getExtractor(schemaType,registryName.toUpperCase()).processSchema(schema, schemaRegistryEnum);
91+
}
92+
return Pair.of(schemaType, attributeList);
93+
}
94+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.sngular.kloadgen.extractor.extractors;
2+
3+
import java.util.List;
4+
5+
import com.sngular.kloadgen.common.SchemaRegistryEnum;
6+
import com.sngular.kloadgen.model.FieldValueMapping;
7+
8+
public interface ExtractorRegistry<T> {
9+
List<FieldValueMapping> processSchema(final T schema, SchemaRegistryEnum registry);
10+
11+
List<String> getSchemaNameList(final String schema, SchemaRegistryEnum registryEnum);
12+
13+
}

0 commit comments

Comments
 (0)