Skip to content

Commit

Permalink
#332 Fix Protobuf Apicurio Extractor.
Browse files Browse the repository at this point in the history
  • Loading branch information
jemacineiras committed Jan 22, 2024
1 parent e9cefa8 commit 00250b9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@

import com.sngular.kloadgen.extractor.extractors.Extractor;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.squareup.wire.schema.internal.parser.ProtoFileElement;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import io.apicurio.registry.utils.protobuf.schema.ProtobufSchema;

public class ProtoBufApicurioExtractor extends AbstractProtoFileExtractor implements Extractor<ProtoFileElement> {
public class ProtoBufApicurioExtractor extends AbstractProtoFileExtractor implements Extractor<ParsedSchema> {

public final List<FieldValueMapping> processSchema(final ProtoFileElement schemaReceived) {
return processSchemaDefault(schemaReceived);
public final List<FieldValueMapping> processSchema(final ParsedSchema schemaReceived) {
return processSchemaDefault(((ProtobufSchema) schemaReceived.schema()).getProtoFileElement());
}

public final List<String> getSchemaNameList(final String schema) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ParsedSchema(final T schema) {
this.schema = schema;
this.schemaType = switch (this.schema.getClass().getSimpleName()) {
case "Schema", "AvroSchema", "UnionSchema", "RecordSchema" -> "AVRO";
case "ProtoBuf", "ProtoFileElement" -> "PROTOBUF";
case "ProtoBuf", "ProtobufSchema", "ProtoFileElement" -> "PROTOBUF";
case "JsonSchema", "ObjectSchema" -> "JSON";
default -> throw new KLoadGenException(String.format("Need to specify schemaType for %s", this.schema.getClass().getSimpleName()));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

import com.sngular.kloadgen.extractor.extractors.Extractor;
import com.sngular.kloadgen.model.FieldValueMapping;
import com.sngular.kloadgen.parsedschema.ParsedSchema;
import com.sngular.kloadgen.testutil.FileHelper;
import com.squareup.wire.schema.Location;
import com.squareup.wire.schema.internal.parser.ProtoFileElement;
import com.squareup.wire.schema.internal.parser.ProtoParser;
import io.apicurio.registry.utils.protobuf.schema.FileDescriptorUtils;
import io.apicurio.registry.utils.protobuf.schema.ProtobufSchema;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterContextService;
import org.apache.jmeter.threads.JMeterVariables;
Expand All @@ -23,7 +26,7 @@ class ProtobufApicurioExtractorTest {

private final FileHelper fileHelper = new FileHelper();

private final Extractor<ProtoFileElement> protoBufApicurioExtractor = new ProtoBufApicurioExtractor();
private final Extractor<ParsedSchema> protoBufApicurioExtractor = new ProtoBufApicurioExtractor();

private final Location location = Location.get("", "");

Expand All @@ -42,8 +45,9 @@ public void setUp() {
@DisplayName("Test Extractor with simple proto file")
void testFlatProperties() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/easyTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));

Assertions.assertThat(fieldValueMappingList)
.hasSize(3)
Expand All @@ -59,8 +63,9 @@ void testFlatProperties() throws Exception {
@DisplayName("Test Extractor with data structure map and array")
void testEmbeddedTypes() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/embeddedTypeTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(2)
.containsExactlyInAnyOrder(
Expand All @@ -74,8 +79,9 @@ void testEmbeddedTypes() throws Exception {
@DisplayName("Test Extractor with data structure enums and collections")
void testEnumType() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/enumTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(3)
.containsExactlyInAnyOrder(
Expand All @@ -92,8 +98,9 @@ void testEnumType() throws Exception {
@DisplayName("Test Extractor with data structure Any of")
void testOneOfsType() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/oneOfTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(4)
.contains(
Expand All @@ -112,8 +119,9 @@ void testOneOfsType() throws Exception {
@DisplayName("Test Extractor with complex structure")
void testComplexProto() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/complexTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(13)
.containsExactlyInAnyOrder(
Expand All @@ -137,8 +145,9 @@ void testComplexProto() throws Exception {
@DisplayName("Test Extractor with real proto")
void testProvided() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/providedTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(32)
.containsExactlyInAnyOrder(
Expand Down Expand Up @@ -181,8 +190,9 @@ void testProvided() throws Exception {
@DisplayName("Test Extractor with data structure maps")
void testMap() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/mapTest.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(7)
.containsExactlyInAnyOrder(
Expand All @@ -200,8 +210,9 @@ void testMap() throws Exception {
@DisplayName("Test Extractor with multi types")
void completeTest() throws Exception {
final String testFile = fileHelper.getContent("/proto-files/completeProto.proto");
final ProtoFileElement schema = ProtoParser.Companion.parse(location, testFile);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(schema);
final ProtoFileElement protoFileElement = ProtoParser.Companion.parse(location, testFile);
final ProtobufSchema schema = new ProtobufSchema(FileDescriptorUtils.protoFileToFileDescriptor(protoFileElement), protoFileElement);
final List<FieldValueMapping> fieldValueMappingList = protoBufApicurioExtractor.processSchema(new ParsedSchema(schema));
Assertions.assertThat(fieldValueMappingList)
.hasSize(11)
.containsExactlyInAnyOrder(
Expand Down

0 comments on commit 00250b9

Please sign in to comment.