Skip to content

Commit

Permalink
Refactor ManifestVersionResolver to use new ChannelSession API
Browse files Browse the repository at this point in the history
  • Loading branch information
spyrkob committed Oct 15, 2024
1 parent c7c6a9b commit 41c904d
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<version.org.jboss.galleon>6.0.3.Final</version.org.jboss.galleon>
<version.org.mockito>5.13.0</version.org.mockito>
<version.junit>4.13.2</version.junit>
<version.org.wildfly.channel>1.1.0.Final</version.org.wildfly.channel>
<version.org.wildfly.channel>1.2.0.Final</version.org.wildfly.channel>
<version.assertj>3.26.3</version.assertj>

<version.org.apache.maven.plugins.pmd>3.25.0</version.org.apache.maven.plugins.pmd>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.wildfly.prospero.metadata;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

import org.jboss.galleon.util.HashUtils;
import org.wildfly.channel.Channel;
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelSession;
import org.wildfly.channel.Repository;
import org.wildfly.channel.RuntimeChannel;

public class ChannelManifestVersionResolver {

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.ChannelSession;
import org.wildfly.channel.Repository;
import org.wildfly.channel.RuntimeChannel;
import org.wildfly.channel.maven.VersionResolverFactory;
import org.wildfly.channel.spi.MavenVersionsResolver;
import org.wildfly.channel.version.VersionMatcher;
Expand Down Expand Up @@ -55,12 +57,43 @@ public class ManifestVersionResolver {

private final VersionResolverFactory resolverFactory;

public static ManifestVersionRecord getCurrentVersions(ChannelSession session) throws IOException {
Objects.requireNonNull(session);

final ManifestVersionRecord record = new ManifestVersionRecord();

for (RuntimeChannel runtimeChannel : session.getRuntimeChannels()) {
final Channel channelDefinition = runtimeChannel.getChannelDefinition();
final ChannelManifestCoordinate manifestCoordinate = channelDefinition.getManifestCoordinate();
final ChannelManifest manifest = runtimeChannel.getChannelManifest();
if (manifestCoordinate == null) {
final List<String> repos = channelDefinition.getRepositories().stream()
.map(Repository::getId)
.collect(Collectors.toList());
record.addManifest(new ManifestVersionRecord.NoManifest(repos, channelDefinition.getNoStreamStrategy().toString()));
} else if (manifestCoordinate.getUrl() != null) {
String hashCode = HashUtils.hash(read(manifestCoordinate.getUrl()));
record.addManifest(new ManifestVersionRecord.UrlManifest(manifestCoordinate.getUrl().toExternalForm(), hashCode, manifest.getDescription()));
} else if (manifestCoordinate.getMaven() != null) {
final String description = manifest.getDescription();
record.addManifest(new ManifestVersionRecord.MavenManifest(
manifestCoordinate.getGroupId(),
manifestCoordinate.getArtifactId(),
manifestCoordinate.getVersion(),
description));
}
}

return record;
}

/**
* creates the resolver using an offline repository session
*
* @param localMavenCache - path to the local cache used to resolve metadata during provisioning
* @param system
*/
@Deprecated
public ManifestVersionResolver(Path localMavenCache, RepositorySystem system) {
Objects.requireNonNull(localMavenCache);
Objects.requireNonNull(system);
Expand All @@ -70,6 +103,7 @@ public ManifestVersionResolver(Path localMavenCache, RepositorySystem system) {
}

// used in tests only
@Deprecated
ManifestVersionResolver(VersionResolverFactory resolverFactory) {
this.resolverFactory = resolverFactory;
}
Expand All @@ -81,6 +115,7 @@ public ManifestVersionResolver(Path localMavenCache, RepositorySystem system) {
* @return {@code ManifestVersionRecord} of latest available versions of manifests
* @throws IOException - if unable to resolve any of the manifests
*/
@Deprecated
public ManifestVersionRecord getCurrentVersions(List<Channel> channels) throws IOException {
Objects.requireNonNull(channels);

Expand Down Expand Up @@ -140,7 +175,7 @@ private String getManifestDescription(ChannelManifestCoordinate manifestCoordina
return description;
}

private String read(URL url) throws IOException {
private static String read(URL url) throws IOException {
try(InputStream inputStream = url.openStream();
OutputStream outputStream = new ByteArrayOutputStream()) {
inputStream.transferTo(outputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
import org.wildfly.channel.ChannelManifest;
import org.wildfly.channel.ChannelManifestCoordinate;
import org.wildfly.channel.ChannelManifestMapper;
import org.wildfly.channel.ChannelSession;
import org.wildfly.channel.Repository;
import org.wildfly.channel.RuntimeChannel;
import org.wildfly.channel.maven.VersionResolverFactory;
import org.wildfly.channel.spi.MavenVersionsResolver;

Expand All @@ -43,6 +45,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -180,4 +183,59 @@ public void emptyChannelListMapsToEmptyVersionRecord() throws Exception {

}

@Test
public void channelSessionToVersionRecord() throws Exception {
final File manifestFile = temp.newFile();
final URL url = manifestFile.toURI().toURL();
final Channel channel1 = new Channel.Builder()
.setManifestCoordinate("group_a", "artifact_a", "1.0.0")
.build();
final Channel channel2 = new Channel.Builder()
.setManifestUrl(url)
.build();
final Channel channel3 = new Channel.Builder()
.setResolveStrategy(Channel.NoStreamStrategy.LATEST)
.addRepository("test-repo", "http://test.te")
.build();

final ChannelManifest manifest1 = new ChannelManifest.Builder().setDescription("Manifest 1").build();
final ChannelManifest manifest2 = new ChannelManifest.Builder().setDescription("Manifest 2").build();

final List<RuntimeChannel> channels = List.of(
new RuntimeChannel(channel1, manifest1, null),
new RuntimeChannel(channel2, manifest2, null),
new RuntimeChannel(channel3, null, null)

);

final ChannelSession sessionMock = mock(ChannelSession.class);
when(sessionMock.getRuntimeChannels()).thenReturn(channels);
final ManifestVersionRecord currentVersions = ManifestVersionResolver.getCurrentVersions(sessionMock);


assertThat(currentVersions.getOpenManifests())
.flatMap(ManifestVersionRecord.NoManifest::getRepos)
.containsExactly("test-repo");
assertThat(currentVersions.getOpenManifests())
.map(ManifestVersionRecord.NoManifest::getStrategy)
.containsExactly(Channel.NoStreamStrategy.LATEST.toString());

assertThat(currentVersions.getUrlManifests())
.map(ManifestVersionRecord.UrlManifest::getUrl)
.containsExactly(url.toExternalForm());
assertThat(currentVersions.getUrlManifests())
.map(ManifestVersionRecord.UrlManifest::getHash)
.containsExactly(HashUtils.hashFile(Path.of(url.toURI())));
assertThat(currentVersions.getUrlManifests())
.map(ManifestVersionRecord.UrlManifest::getDescription)
.containsExactly("Manifest 2");

assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getVersion)
.containsExactly("1.0.0");
assertThat(currentVersions.getMavenManifests())
.map(ManifestVersionRecord.MavenManifest::getDescription)
.containsExactly("Manifest 1");
}

}

0 comments on commit 41c904d

Please sign in to comment.