From 3fe892be116bf3cfca85d2dba288c8d6171c84f4 Mon Sep 17 00:00:00 2001 From: moulalis Date: Tue, 29 Aug 2023 18:09:34 +0530 Subject: [PATCH] [issue:411] When displaying the update candidates, display the name of the channel the update was found in --- .../org/wildfly/prospero/cli/CliConsole.java | 12 ++-- .../wildfly/prospero/cli/CliConsoleTest.java | 70 +++++++++++++++++++ .../wildfly/prospero/api/ArtifactChange.java | 16 +++++ .../prospero/updates/UpdateFinder.java | 12 ++-- 4 files changed, 102 insertions(+), 8 deletions(-) create mode 100644 prospero-cli/src/test/java/org/wildfly/prospero/cli/CliConsoleTest.java diff --git a/prospero-cli/src/main/java/org/wildfly/prospero/cli/CliConsole.java b/prospero-cli/src/main/java/org/wildfly/prospero/cli/CliConsole.java index 202457b19..6d7b14645 100644 --- a/prospero-cli/src/main/java/org/wildfly/prospero/cli/CliConsole.java +++ b/prospero-cli/src/main/java/org/wildfly/prospero/cli/CliConsole.java @@ -160,9 +160,11 @@ public void updatesFound(List artifactUpdates) { final Optional newVersion = artifactUpdate.getNewVersion(); final Optional oldVersion = artifactUpdate.getOldVersion(); final String artifactName = artifactUpdate.getArtifactName(); + final String channelName = artifactUpdate.getChannelName().map(name -> "[" + name + "]") + .orElse(""); - getStdOut().printf(" %s%-50s %-20s ==> %-20s%n", artifactUpdate.isDowngrade()?"[*]":"", artifactName, oldVersion.orElse("[]"), - newVersion.orElse("[]")); + getStdOut().printf(" %s%-50s %-20s ==> %-20s %-20s%n", artifactUpdate.isDowngrade()?"[*]":"", artifactName, oldVersion.orElse("[]"), + newVersion.orElse("[]"), channelName); } if (artifactUpdates.stream().anyMatch(ArtifactChange::isDowngrade)) { @@ -180,9 +182,11 @@ public void changesFound(List artifactUpdates) { final Optional newVersion = artifactUpdate.getNewVersion(); final Optional oldVersion = artifactUpdate.getOldVersion(); final String artifactName = artifactUpdate.getArtifactName(); + final String channelName = artifactUpdate.getChannelName().map(name -> "[" + name + "]") + .orElse(""); - getStdOut().printf(" %-50s %-20s ==> %-20s%n", artifactName, oldVersion.orElse("[]"), - newVersion.orElse("[]")); + getStdOut().printf(" %-50s %-20s ==> %-20s%n %-20s%n", artifactName, oldVersion.orElse("[]"), + newVersion.orElse("[]"),channelName); } } } diff --git a/prospero-cli/src/test/java/org/wildfly/prospero/cli/CliConsoleTest.java b/prospero-cli/src/test/java/org/wildfly/prospero/cli/CliConsoleTest.java new file mode 100644 index 000000000..4b9b40248 --- /dev/null +++ b/prospero-cli/src/test/java/org/wildfly/prospero/cli/CliConsoleTest.java @@ -0,0 +1,70 @@ +/* + * Copyright 2023 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.wildfly.prospero.cli; + +import org.eclipse.aether.artifact.DefaultArtifact; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wildfly.prospero.api.ArtifactChange; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CliConsoleTest extends AbstractConsoleTest { + private CliConsole cliConsole; + + private ByteArrayOutputStream outputStream; + + @Before + public void setUp() { + cliConsole = new CliConsole(); + outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + } + + @Test + public void testUpdatesFoundWithUpdates_ArtifactChange_update() { + List artifactChanges = new ArrayList<>(); + ArtifactChange artifactChange = ArtifactChange.updated(new DefaultArtifact("test.group", "test-artifact2", "jar", "2.0.0"), new DefaultArtifact("test.group", "test-artifact2", "jar", "2.1.0"), "[channel-1]"); + + artifactChanges.add(artifactChange); + cliConsole.updatesFound(artifactChanges); + String capturedOutput = outputStream.toString(); + + assertThat(capturedOutput.contains(String.format( + " %s%-50s %-20s ==> %-20s ==> %-20s%n", + "", + artifactChange.getArtifactName(), + artifactChange.getOldVersion().orElse("[]"), + artifactChange.getNewVersion().orElse("[]"), + artifactChange.getChannelName() + ))); + } + + @After + public void destory() throws IOException { + outputStream.close(); + cliConsole = null; + } +} diff --git a/prospero-common/src/main/java/org/wildfly/prospero/api/ArtifactChange.java b/prospero-common/src/main/java/org/wildfly/prospero/api/ArtifactChange.java index 61c70ceb1..cc550eaae 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/api/ArtifactChange.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/api/ArtifactChange.java @@ -24,6 +24,8 @@ import java.util.Optional; public class ArtifactChange extends Diff { + private String channelName; + public static ArtifactChange added(Artifact newVersion) { Objects.requireNonNull(newVersion); return new ArtifactChange(toGav(newVersion), null, newVersion.getVersion()); @@ -39,11 +41,21 @@ public static ArtifactChange updated(Artifact oldVersion, Artifact newVersion) { Objects.requireNonNull(newVersion); return new ArtifactChange(toGav(oldVersion), oldVersion.getVersion(), newVersion.getVersion()); } + public static ArtifactChange updated(Artifact oldVersion, Artifact newVersion, String channelName) { + Objects.requireNonNull(oldVersion); + Objects.requireNonNull(newVersion); + return new ArtifactChange(toGav(oldVersion), oldVersion.getVersion(), newVersion.getVersion(),channelName); + } private ArtifactChange(String gav, String oldVersion, String newVersion) { super(gav, oldVersion, newVersion); } + private ArtifactChange(String gav, String oldVersion, String newVersion, String channelName) { + super(gav, oldVersion, newVersion); + this.channelName= channelName; + } + @SuppressWarnings("OptionalGetWithoutIsPresent") public String getArtifactName() { // the name has to be present, as it's either old or new artifact's gav @@ -54,6 +66,10 @@ public Optional getOldVersion() { return getOldValue(); } + public Optional getChannelName() { + return Optional.ofNullable(channelName); + } + public Optional getNewVersion() { return getNewValue(); } diff --git a/prospero-common/src/main/java/org/wildfly/prospero/updates/UpdateFinder.java b/prospero-common/src/main/java/org/wildfly/prospero/updates/UpdateFinder.java index f868bd9e7..81875bdb7 100644 --- a/prospero-common/src/main/java/org/wildfly/prospero/updates/UpdateFinder.java +++ b/prospero-common/src/main/java/org/wildfly/prospero/updates/UpdateFinder.java @@ -21,6 +21,7 @@ import org.eclipse.aether.artifact.DefaultArtifact; import org.wildfly.channel.ChannelSession; import org.wildfly.channel.UnresolvedMavenArtifactException; +import org.wildfly.channel.VersionResult; import org.wildfly.prospero.api.ArtifactChange; import org.wildfly.prospero.api.exceptions.ArtifactResolutionException; @@ -82,10 +83,13 @@ public UpdateSet findUpdates(List artifacts) throws ArtifactResolution private Optional findUpdates(Artifact artifact) throws ArtifactResolutionException { final String latestVersion; + Optional channelName; try { - latestVersion = channelSession.findLatestMavenArtifactVersion(artifact.getGroupId(), - artifact.getArtifactId(), artifact.getExtension(), artifact.getClassifier(), null) - .getVersion(); + VersionResult versionResult = channelSession.findLatestMavenArtifactVersion(artifact.getGroupId(), + artifact.getArtifactId(), artifact.getExtension(), artifact.getClassifier(), null); + latestVersion = versionResult.getVersion(); + channelName = versionResult.getChannelName(); + } catch (UnresolvedMavenArtifactException e) { return Optional.of(ArtifactChange.removed(artifact)); } @@ -94,7 +98,7 @@ private Optional findUpdates(Artifact artifact) throws ArtifactR if (latestVersion == null || latest.getVersion().equals(artifact.getVersion())) { return Optional.empty(); } else { - return Optional.of(ArtifactChange.updated(artifact, latest)); + return Optional.of(ArtifactChange.updated(artifact, latest, channelName.orElse(""))); } }