Skip to content

Commit

Permalink
confluentinc#1914 - register all files in outputDirectory if no subje…
Browse files Browse the repository at this point in the history
…cts provided
  • Loading branch information
mac2000 committed Jun 15, 2021
1 parent 8ea8ac5 commit 228495c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.common.base.Preconditions;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.apache.commons.compress.utils.FileNameUtils;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.Parameter;
Expand All @@ -29,13 +31,15 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import io.confluent.kafka.schemaregistry.ParsedSchema;
import io.confluent.kafka.schemaregistry.avro.AvroSchema;
Expand All @@ -46,6 +50,9 @@ public abstract class UploadSchemaRegistryMojo extends SchemaRegistryMojo {

public static final String PERCENT_REPLACEMENT = "_x";

@Parameter(required = false)
File outputDirectory;

@Parameter(required = true)
Map<String, File> subjects = new HashMap<>();

Expand Down Expand Up @@ -75,6 +82,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {
errors = 0;
failures = 0;

if (subjects.size() == 0 && outputDirectory != null) {
subjects = Arrays.stream(outputDirectory.listFiles()).collect(Collectors.toMap(f -> FileNameUtils.getBaseName(f.getName()), f -> f));
}

for (String subject : subjects.keySet()) {
processSubject(subject, false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,31 @@ public void missingSchemas() throws IOException, MojoFailureException, MojoExecu
this.mojo.subjects = subjectToFile;
this.mojo.execute();
}

@Test
public void shouldTakeSchemasFromOutputDirectoryIfNoSubjectsProvided() throws IOException, MojoFailureException, MojoExecutionException {
Map<String, Integer> expectedVersions = new LinkedHashMap<>();

Map<String, File> subjectToFile = new LinkedHashMap<>();
int version = 1;
for (int i = 0; i < 2; i++) {
String keySubject = String.format("TestSubject%03d-key", i);
String valueSubject = String.format("TestSubject%03d-value", i);
Schema keySchema = Schema.create(Schema.Type.STRING);
Schema valueSchema = Schema.createUnion(Arrays.asList(Schema.create(Schema.Type.STRING), Schema.create(Schema.Type.NULL)));
File keySchemaFile = new File(this.tempDirectory, keySubject + ".avsc");
File valueSchemaFile = new File(this.tempDirectory, valueSubject + ".avsc");
writeSchema(keySchemaFile, keySchema);
writeSchema(valueSchemaFile, valueSchema);
subjectToFile.put(keySubject, keySchemaFile);
expectedVersions.put(keySubject, version);
subjectToFile.put(valueSubject, valueSchemaFile);
expectedVersions.put(valueSubject, version);
}

this.mojo.outputDirectory = this.tempDirectory;
this.mojo.execute();

Assert.assertThat(this.mojo.schemaVersions, IsEqual.equalTo(expectedVersions));
}
}

0 comments on commit 228495c

Please sign in to comment.