Skip to content

Commit

Permalink
[issue:411] When displaying the update candidates, display the name o…
Browse files Browse the repository at this point in the history
…f the channel the update was found in
  • Loading branch information
moulalis committed Aug 30, 2023
1 parent ea4ba1a commit 3fe892b
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,11 @@ public void updatesFound(List<ArtifactChange> artifactUpdates) {
final Optional<String> newVersion = artifactUpdate.getNewVersion();
final Optional<String> 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)) {
Expand All @@ -180,9 +182,11 @@ public void changesFound(List<ArtifactChange> artifactUpdates) {
final Optional<String> newVersion = artifactUpdate.getNewVersion();
final Optional<String> 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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ArtifactChange> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand All @@ -54,6 +66,10 @@ public Optional<String> getOldVersion() {
return getOldValue();
}

public Optional<String> getChannelName() {
return Optional.ofNullable(channelName);
}

public Optional<String> getNewVersion() {
return getNewValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -82,10 +83,13 @@ public UpdateSet findUpdates(List<Artifact> artifacts) throws ArtifactResolution
private Optional<ArtifactChange> findUpdates(Artifact artifact) throws ArtifactResolutionException {

final String latestVersion;
Optional<String> 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));
}
Expand All @@ -94,7 +98,7 @@ private Optional<ArtifactChange> 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("")));
}
}

Expand Down

0 comments on commit 3fe892b

Please sign in to comment.