Skip to content

Commit

Permalink
Merge pull request #24 from spyrkob/SET-813_add_latest_existing_merge…
Browse files Browse the repository at this point in the history
…_strategy

Add LATEST_EXISTING manifest merge strategy
  • Loading branch information
spyrkob authored Sep 2, 2024
2 parents e4ec5a4 + 41f3a12 commit 048dc20
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Integer call() throws Exception {
}

public static ChannelManifest merge(ChannelManifest manifestOne, ChannelManifest manifestTwo,
VersionMergeStrategy.Strategies mergeStrategy,
VersionMergeStrategy mergeStrategy,
String mergedManifestName, String mergedManifestId) {
Objects.requireNonNull(manifestOne);
Objects.requireNonNull(manifestTwo);
Expand All @@ -62,14 +62,25 @@ public static ChannelManifest merge(ChannelManifest manifestOne, ChannelManifest
Set<Stream> merged = new TreeSet<>(streamsOne);
for (Stream s : streamsTwo) {
final String key = s.getGroupId() + ":" + s.getArtifactId();
final String versionOne;
final String versionTwo = s.getVersion();
if (!presentKeys.containsKey(key)) {
merged.add(s);
versionOne = null;
} else {
final String version = mergeStrategy.merge(presentKeys.get(key).getVersion(), s.getVersion());
if (!version.equals(presentKeys.get(key).getVersion())) {
versionOne = presentKeys.get(key).getVersion();
}

final String version = mergeStrategy.merge(versionOne, versionTwo);
final Stream streamOne = presentKeys.get(key);
if (version == null) {
if (streamOne != null) {
merged.remove(streamOne);
}
} else if (!version.equals(versionOne)) {
if (streamOne != null) {
merged.remove(presentKeys.get(key));
merged.add(s);
}
merged.add(s);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
public interface VersionMergeStrategy {
enum Strategies implements VersionMergeStrategy {
LATEST(new LatestMergeStrategy()),
FIRST(new FirstMergeStrategy());
FIRST(new FirstMergeStrategy()),
LATEST_EXISTING(new LatestExistingMergeStrategy());

private final VersionMergeStrategy mergeStrategy;

Expand All @@ -24,18 +25,31 @@ class FirstMergeStrategy implements VersionMergeStrategy {

@Override
public String merge(String v1, String v2) {
return v1;
return v1 == null ? v2 : v1;
}
}

class LatestMergeStrategy implements VersionMergeStrategy {

@Override
public String merge(String v1, String v2) {
if (v1 == null || v2 == null) {
return v1 == null ? v2 : v1;
}
if (VersionMatcher.COMPARATOR.compare(v2, v1) > 0) {
return v2;
} else {
return v1;
}
}
}

class LatestExistingMergeStrategy extends LatestMergeStrategy {
@Override
public String merge(String v1, String v2) {
if (v1 == null) {
return null;
}
return super.merge(v1, v2);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.wildfly.prospero.extras.manifest.merge;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.jupiter.api.Test;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.Stream;

class ManifestMergeCommandTest {

protected static final ChannelManifest MANIFEST_ONE = new ChannelManifest(null, null, null, List.of(new Stream("org.test", "test-one", "1.0.0")));
protected static final ChannelManifest MANIFEST_TWO = new ChannelManifest(null, null, null, List.of(new Stream("org.test", "test-one", "1.1.0")));

@Test
public void pickFirstVersion() throws Exception {


final VersionMergeStrategy strategy = (v1, v2) -> v1;
final ChannelManifest merged = ManifestMergeCommand.merge(MANIFEST_ONE, MANIFEST_TWO, strategy, null, null);

assertThat(merged.getStreams())
.containsOnly(new Stream("org.test", "test-one", "1.0.0"));

}

@Test
public void pickSecondVersion() throws Exception {
final VersionMergeStrategy strategy = (v1, v2) -> v2;
final ChannelManifest merged = ManifestMergeCommand.merge(MANIFEST_ONE, MANIFEST_TWO, strategy, null, null);

assertThat(merged.getStreams())
.containsOnly(new Stream("org.test", "test-one", "1.1.0"));

}

@Test
public void rejectStream() throws Exception {
// if the strategy returns null, the stream should be removed
final VersionMergeStrategy strategy = (v1, v2) -> null;
final ChannelManifest merged = ManifestMergeCommand.merge(MANIFEST_ONE, MANIFEST_TWO, strategy, null, null);

assertThat(merged.getStreams())
.isEmpty();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,25 @@ public void testLatestVersionMerge() {
final VersionMergeStrategy latest = new LatestMergeStrategy();
assertEquals("1.2.4", latest.merge("1.2.3", "1.2.4"));
assertEquals("1.2.4", latest.merge("1.2.4", "1.2.3"));
assertEquals("1.2.4", latest.merge("1.2.4", null));
assertEquals("1.2.4", latest.merge(null, "1.2.4"));
}

@Test
public void testFirstVersionMerge() {
final VersionMergeStrategy first = new FirstMergeStrategy();
assertEquals("1.2.3", first.merge("1.2.3", "1.2.4"));
assertEquals("1.2.4", first.merge("1.2.4", "1.2.3"));
assertEquals("1.2.4", first.merge("1.2.4", null));
assertEquals("1.2.4", first.merge(null, "1.2.4"));
}

@Test
public void testLatestExistingVersionMerge() {
final VersionMergeStrategy strategy = new LatestExistingMergeStrategy();
assertEquals("1.2.4", strategy.merge("1.2.3", "1.2.4"));
assertEquals("1.2.4", strategy.merge("1.2.4", "1.2.3"));
assertEquals("1.2.4", strategy.merge("1.2.4", null));
assertNull(strategy.merge(null, "1.2.4"));
}
}

0 comments on commit 048dc20

Please sign in to comment.