Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1914 - register all files in outputDirectory if no subjects provided #1915

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,7 +50,10 @@ public abstract class UploadSchemaRegistryMojo extends SchemaRegistryMojo {

public static final String PERCENT_REPLACEMENT = "_x";

@Parameter(required = true)
@Parameter(required = false)
File outputDirectory;

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

@Parameter(required = false)
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));
}
}