-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from spyrkob/manifest_subtract
Add manifest-subtract operation
- Loading branch information
Showing
6 changed files
with
247 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/main/java/org/wildfly/prospero/extras/manifest/subtract/ManifestSubtractCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package org.wildfly.prospero.extras.manifest.subtract; | ||
|
||
import org.wildfly.channel.ChannelManifest; | ||
import org.wildfly.channel.ChannelManifestMapper; | ||
import org.wildfly.channel.Stream; | ||
import org.wildfly.prospero.extras.ReturnCodes; | ||
import org.wildfly.prospero.extras.shared.CommandWithHelp; | ||
import picocli.CommandLine; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
@CommandLine.Command(name = "manifest-subtract") | ||
public class ManifestSubtractCommand extends CommandWithHelp { | ||
|
||
private static final Pattern EXCLUSION_PATTERN = Pattern.compile("[\\w-_.*]*:[\\w-_.*]*"); | ||
|
||
@CommandLine.Parameters(index = "0", descriptionKey = "parameterOne") | ||
Path manifestOne; | ||
|
||
@CommandLine.Parameters(index = "1", descriptionKey = "parameterTwo") | ||
Path manifestTwo; | ||
|
||
@CommandLine.Option(names = "--exclude", split = ",") | ||
List<String> exclusions = Collections.emptyList(); | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
final ChannelManifest manifestOne = ChannelManifestMapper.from(this.manifestOne.toUri().toURL()); | ||
final ChannelManifest manifestTwo = ChannelManifestMapper.from(this.manifestTwo.toUri().toURL()); | ||
|
||
final ChannelManifest res = subtract(manifestOne, manifestTwo, exclusions); | ||
|
||
System.out.println(ChannelManifestMapper.toYaml(res)); | ||
|
||
return ReturnCodes.SUCCESS; | ||
} | ||
|
||
public static ChannelManifest subtract(ChannelManifest manifestOne, ChannelManifest manifestTwo, List<String> exclusions) { | ||
Objects.requireNonNull(manifestOne); | ||
Objects.requireNonNull(manifestTwo); | ||
Objects.requireNonNull(exclusions); | ||
|
||
final Optional<String> illegalPattern = exclusions.stream() | ||
.filter(e -> !EXCLUSION_PATTERN.matcher(e).matches()) | ||
.findFirst(); | ||
if (illegalPattern.isPresent()) { | ||
throw new IllegalArgumentException("Exclusion [" + illegalPattern.get() + "] has invalid format ([\\w-_.*]*:[\\w-_.*]*)."); | ||
} | ||
|
||
final Set<String> groupExclusions = exclusions.stream() | ||
.map(String::trim) | ||
.filter(e -> e.endsWith(":*")) | ||
.map(e -> e.substring(0, e.length() - 2)) | ||
.collect(Collectors.toSet()); | ||
|
||
final Set<String> groupArtifactExclusions = exclusions.stream() | ||
.map(String::trim) | ||
.filter(e -> !e.endsWith(":*")) | ||
.collect(Collectors.toSet()); | ||
|
||
final Set<String> streamKeys = manifestTwo.getStreams().stream() | ||
.filter(s -> !groupExclusions.contains(s.getGroupId())) | ||
.filter(s -> !groupArtifactExclusions.contains(s.getGroupId() + ":" + s.getArtifactId())) | ||
.map(s -> getKey(s)) | ||
.collect(Collectors.toSet()); | ||
|
||
|
||
final List<Stream> filteredStreams = manifestOne.getStreams().stream() | ||
.filter(s -> !streamKeys.contains(getKey(s))) | ||
.collect(Collectors.toList()); | ||
|
||
|
||
return new ChannelManifest(manifestOne.getSchemaVersion(), manifestOne.getName(), manifestOne.getId(), | ||
manifestOne.getDescription(), manifestOne.getManifestRequirements(), filteredStreams); | ||
} | ||
|
||
private static String getKey(Stream s) { | ||
return s.getGroupId() + ":" + s.getArtifactId(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/test/java/org/wildfly/prospero/extras/manifest/subtract/SubtractCommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package org.wildfly.prospero.extras.manifest.subtract; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.wildfly.channel.ChannelManifest; | ||
import org.wildfly.channel.Stream; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
|
||
class SubtractCommandTest { | ||
|
||
@Test | ||
public void testSubtractTwoEmptySetsProducesEmptySet() throws Exception { | ||
final ChannelManifest res = callSubtract(new ChannelManifest("", "", "", Collections.emptyList()), getChannelManifest()); | ||
|
||
assertThat(res.getStreams()) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testSubtractEmptySetProducesEmptySet() throws Exception { | ||
final ChannelManifest c1 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest res = callSubtract(c1, getChannelManifest()); | ||
|
||
assertThat(res.getStreams()) | ||
.containsAll(c1.getStreams()); | ||
} | ||
|
||
@Test | ||
public void testSubtractFromEmptySetProducesOriginalSet() throws Exception { | ||
final ChannelManifest c1 = getChannelManifest(); | ||
final ChannelManifest c2 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest res = callSubtract(c1, c2); | ||
|
||
assertThat(res.getStreams()) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testRemoveCommonStream() { | ||
final ChannelManifest c1 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest c2 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest res = callSubtract(c1, c2); | ||
|
||
assertThat(res.getStreams()) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testRemoveCommonStreamLivesUnique() { | ||
final ChannelManifest c1 = getChannelManifest( | ||
new Stream("foo", "bar", "1.1"), | ||
new Stream("unique", "one", "1.1") | ||
); | ||
final ChannelManifest c2 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest res = callSubtract(c1, c2); | ||
|
||
assertThat(res.getStreams()) | ||
.containsOnly(new Stream("unique", "one", "1.1")); | ||
} | ||
|
||
@Test | ||
public void testRemoveCommonStreamIgnoresVersion() { | ||
final ChannelManifest c1 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest c2 = getChannelManifest(new Stream("foo", "bar", "1.2")); | ||
final ChannelManifest res = callSubtract(c1, c2); | ||
|
||
assertThat(res.getStreams()) | ||
.isEmpty(); | ||
} | ||
|
||
@Test | ||
public void testExcludeStreamsByGroupArtifact() { | ||
final ChannelManifest c1 = getChannelManifest( | ||
new Stream("foo", "bar", "1.1"), | ||
new Stream("other", "one", "1.1")); | ||
final ChannelManifest c2 = getChannelManifest(new Stream("foo", "bar", "1.1")); | ||
final ChannelManifest res = callSubtract(c1, c2, "foo:bar"); | ||
|
||
assertThat(res.getStreams()) | ||
.containsAll(c1.getStreams()); | ||
} | ||
|
||
@Test | ||
public void testExcludeStreamsByGroup() { | ||
final ChannelManifest c1 = getChannelManifest( | ||
new Stream("foo", "bar", "1.1"), | ||
new Stream("foo", "other", "1.1")); | ||
final ChannelManifest c2 = getChannelManifest( | ||
new Stream("foo", "bar", "1.1"), | ||
new Stream("foo", "other", "1.1") | ||
); | ||
final ChannelManifest res = callSubtract(c1, c2, "foo:*"); | ||
|
||
assertThat(res.getStreams()) | ||
.containsAll(c1.getStreams()); | ||
} | ||
|
||
@Test | ||
public void testInvalidExclusionPattern() { | ||
final ChannelManifest c1 = getChannelManifest(); | ||
final ChannelManifest c2 = getChannelManifest(); | ||
|
||
assertThatThrownBy(()->callSubtract(c1, c2, "foo")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
|
||
assertThatThrownBy(()->callSubtract(c1, c2, "foo:bar:aaa")) | ||
.isInstanceOf(IllegalArgumentException.class); | ||
|
||
callSubtract(c1, c2, "org.test:bar-aaa122"); | ||
} | ||
|
||
private static ChannelManifest callSubtract(ChannelManifest c1, ChannelManifest c2, String ... exclusions) { | ||
return ManifestSubtractCommand.subtract(c1, c2, Arrays.asList(exclusions)); | ||
} | ||
|
||
private static ChannelManifest getChannelManifest() { | ||
return new ChannelManifest("", "", "", Collections.emptyList()); | ||
} | ||
|
||
private static ChannelManifest getChannelManifest(Stream... stream) { | ||
return new ChannelManifest("", "", "", Arrays.asList(stream)); | ||
} | ||
} |