diff --git a/core/src/main/java/org/wildfly/channel/ChannelImpl.java b/core/src/main/java/org/wildfly/channel/ChannelImpl.java index 380ea581..85eb7027 100644 --- a/core/src/main/java/org/wildfly/channel/ChannelImpl.java +++ b/core/src/main/java/org/wildfly/channel/ChannelImpl.java @@ -16,6 +16,7 @@ */ package org.wildfly.channel; +import static java.util.Collections.singleton; import static java.util.Objects.requireNonNull; import static org.wildfly.channel.version.VersionMatcher.COMPARATOR; @@ -24,6 +25,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Optional; @@ -41,7 +43,8 @@ class ChannelImpl implements AutoCloseable { private static final Logger LOG = Logger.getLogger(ChannelImpl.class); - private Channel channelDefinition; + private final Channel channelDefinition; + private Channel resolvedChannel; private List requiredChannels = Collections.emptyList(); @@ -53,6 +56,7 @@ class ChannelImpl implements AutoCloseable { private boolean dependency = false; public Optional blocklist = Optional.empty(); + private ChannelManifestCoordinate resolvedCoordinate; public ChannelManifest getManifest() { return channelManifest; @@ -78,8 +82,11 @@ void init(MavenVersionsResolver.Factory factory, List channels) { resolver = factory.create(channelDefinition.getRepositories()); + final Channel.Builder resolvedChannelBuilder = new Channel.Builder(channelDefinition); if (channelDefinition.getManifestCoordinate() != null) { - channelManifest = resolveManifest(channelDefinition.getManifestCoordinate()); + final ChannelManifestCoordinate coordinate = resolveManifestVersion(channelDefinition); + resolvedChannelBuilder.setManifestCoordinate(coordinate); + channelManifest = resolveManifest(coordinate); } else { channelManifest = new ChannelManifest(null, null, null, Collections.emptyList()); } @@ -94,12 +101,17 @@ void init(MavenVersionsResolver.Factory factory, List channels) { } if (channelDefinition.getBlocklistCoordinate() != null) { - BlocklistCoordinate blocklistCoordinate = channelDefinition.getBlocklistCoordinate(); - final List urls = resolver.resolveChannelMetadata(List.of(blocklistCoordinate)); - this.blocklist = urls.stream() - .map(Blocklist::from) - .findFirst(); + BlocklistCoordinate blocklistCoordinate = resolveBlocklistVersion(channelDefinition); + if (blocklistCoordinate != null) { + resolvedChannelBuilder.setBlocklistCoordinate(blocklistCoordinate); + final List urls = resolver.resolveChannelMetadata(List.of(blocklistCoordinate)); + this.blocklist = urls.stream() + .map(Blocklist::from) + .findFirst(); + } } + + this.resolvedChannel = resolvedChannelBuilder.build(); } private ChannelImpl findRequiredChannel(MavenVersionsResolver.Factory factory, List channels, ManifestRequirement manifestRequirement) { @@ -179,8 +191,12 @@ boolean isDependency() { return dependency; } - Channel getChannelDefinition() { - return channelDefinition; + Channel getResolvedChannelDefinition() { + return resolvedChannel; + } + + public Blocklist getBlocklist() { + return blocklist.orElse(null); } static class ResolveLatestVersionResult { @@ -193,8 +209,79 @@ static class ResolveLatestVersionResult { } } + private Set attemptedRepositories() { + return new HashSet<>(channelDefinition.getRepositories()); + } + + private ChannelManifestCoordinate resolveManifestVersion(Channel baseDefinition) { + final ChannelManifestCoordinate manifestCoordinate = baseDefinition.getManifestCoordinate(); + + // if we already have a version or it is a URL manifest, return it + if (manifestCoordinate.getUrl() != null || manifestCoordinate.getMaven().getVersion() != null) { + return manifestCoordinate; + } + + final Set allVersions = resolver.getAllVersions( + manifestCoordinate.getGroupId(), + manifestCoordinate.getArtifactId(), + manifestCoordinate.getExtension(), + manifestCoordinate.getClassifier() + ); + Optional latestVersion = VersionMatcher.getLatestVersion(allVersions); + String version = latestVersion.orElseThrow(() -> + new ArtifactTransferException(String.format("Unable to resolve the latest version of channel metadata %s:%s", manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId()), + singleton(new ArtifactCoordinate(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), manifestCoordinate.getExtension(), manifestCoordinate.getClassifier(), "")), + attemptedRepositories())); + + return new ChannelManifestCoordinate(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), version); + + } + + private BlocklistCoordinate resolveBlocklistVersion(Channel baseDefinition) { + final BlocklistCoordinate blocklistCoordinate = baseDefinition.getBlocklistCoordinate(); + + if (blocklistCoordinate == null) { + return null; + } + + // if we already have a version or it is a URL blocklist, return it + if (blocklistCoordinate.getUrl() != null || blocklistCoordinate.getMaven().getVersion() != null) { + return blocklistCoordinate; + } + + final Set allVersions = resolver.getAllVersions( + blocklistCoordinate.getGroupId(), + blocklistCoordinate.getArtifactId(), + blocklistCoordinate.getExtension(), + blocklistCoordinate.getClassifier() + ); + Optional latestVersion = VersionMatcher.getLatestVersion(allVersions); + + // different from manifest resolution. If we were not able to resolve the blocklist, we assume it doesn't exist (yet) and + // we proceed without blocklist + return latestVersion + .map(v->new BlocklistCoordinate(blocklistCoordinate.getGroupId(), blocklistCoordinate.getArtifactId(), v)) + .orElse(null); + } + private ChannelManifest resolveManifest(ChannelManifestCoordinate manifestCoordinate) throws UnresolvedMavenArtifactException { - return resolver.resolveChannelMetadata(List.of(manifestCoordinate)) + if (manifestCoordinate.getUrl() == null && manifestCoordinate.getMaven().getVersion() == null) { + final Set allVersions = resolver.getAllVersions( + manifestCoordinate.getGroupId(), + manifestCoordinate.getArtifactId(), + manifestCoordinate.getExtension(), + manifestCoordinate.getClassifier() + ); + Optional latestVersion = VersionMatcher.getLatestVersion(allVersions); + String version = latestVersion.orElseThrow(() -> + new ArtifactTransferException(String.format("Unable to resolve the latest version of channel metadata %s:%s", manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId()), + singleton(new ArtifactCoordinate(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), manifestCoordinate.getExtension(), manifestCoordinate.getClassifier(), "")), + attemptedRepositories())); + resolvedCoordinate = new ChannelManifestCoordinate(manifestCoordinate.getGroupId(), manifestCoordinate.getArtifactId(), version); + } else { + resolvedCoordinate = manifestCoordinate; + } + return resolver.resolveChannelMetadata(List.of(resolvedCoordinate)) .stream() .map(ChannelManifestMapper::from) .findFirst().orElseThrow(); @@ -248,7 +335,7 @@ Optional resolveLatestVersion(String groupId, String return Optional.of(new ResolveLatestVersionResult(latestMetadataVersion, this)); } catch (NoStreamFoundException e) { LOG.debugf(e, "Metadata resolution for %s:%s failed in channel %s", - groupId, artifactId, this.getChannelDefinition().getName()); + groupId, artifactId, this.getResolvedChannelDefinition().getName()); return Optional.empty(); } case MAVEN_RELEASE: @@ -260,7 +347,7 @@ Optional resolveLatestVersion(String groupId, String return Optional.of(new ResolveLatestVersionResult(releaseMetadataVersion, this)); } catch (NoStreamFoundException e) { LOG.debugf(e, "Metadata resolution for %s:%s failed in channel %s", - groupId, artifactId, this.getChannelDefinition().getName()); + groupId, artifactId, this.getResolvedChannelDefinition().getName()); return Optional.empty(); } default: diff --git a/core/src/main/java/org/wildfly/channel/ChannelSession.java b/core/src/main/java/org/wildfly/channel/ChannelSession.java index 1eea2a21..d8d95b7a 100644 --- a/core/src/main/java/org/wildfly/channel/ChannelSession.java +++ b/core/src/main/java/org/wildfly/channel/ChannelSession.java @@ -90,6 +90,18 @@ public ChannelSession(List channelDefinitions, MavenVersionsResolver.Fa validateNoDuplicatedManifests(); } + /** + * Get the definitions of channels used by this session. Returned version contains resolved versions + * of channel metadata (if applicable). + * + * @return List of {@code RuntimeChannel}s used to resolve artifacts by this session + */ + public List getRuntimeChannels() { + return this.channels.stream() + .map(c->new RuntimeChannel(c.getResolvedChannelDefinition(), c.getManifest(), c.getBlocklist())) + .collect(Collectors.toList()); + } + /** * Resolve the Maven artifact according to the session's channels. *

@@ -121,7 +133,7 @@ public MavenArtifact resolveMavenArtifact(String groupId, String artifactId, Str ChannelImpl.ResolveArtifactResult artifact = channel.resolveArtifact(groupId, artifactId, extension, classifier, latestVersion); recorder.recordStream(groupId, artifactId, latestVersion); - return new MavenArtifact(groupId, artifactId, extension, classifier, latestVersion, artifact.file, artifact.channel.getChannelDefinition().getName()); + return new MavenArtifact(groupId, artifactId, extension, classifier, latestVersion, artifact.file, artifact.channel.getResolvedChannelDefinition().getName()); } /** @@ -153,7 +165,7 @@ public List resolveMavenArtifacts(List coordi final MavenArtifact resolvedArtifact = new MavenArtifact(request.getGroupId(), request.getArtifactId(), request.getExtension(), request.getClassifier(), request.getVersion(), resolveArtifactResults.get(i).file, - resolveArtifactResults.get(i).channel.getChannelDefinition().getName()); + resolveArtifactResults.get(i).channel.getResolvedChannelDefinition().getName()); recorder.recordStream(resolvedArtifact.getGroupId(), resolvedArtifact.getArtifactId(), resolvedArtifact.getVersion()); res.add(resolvedArtifact); @@ -227,7 +239,7 @@ public List resolveDirectMavenArtifacts(List */ public VersionResult findLatestMavenArtifactVersion(String groupId, String artifactId, String extension, String classifier, String baseVersion) throws NoStreamFoundException { final ChannelImpl.ResolveLatestVersionResult channelWithLatestVersion = findChannelWithLatestVersion(groupId, artifactId, extension, classifier, baseVersion); - return new VersionResult(channelWithLatestVersion.version, channelWithLatestVersion.channel.getChannelDefinition().getName()); + return new VersionResult(channelWithLatestVersion.version, channelWithLatestVersion.channel.getResolvedChannelDefinition().getName()); } @Override @@ -274,7 +286,7 @@ private ChannelImpl.ResolveLatestVersionResult findChannelWithLatestVersion(Stri return foundVersions.get(foundLatestVersionInChannels.orElseThrow(() -> { final ArtifactCoordinate coord = new ArtifactCoordinate(groupId, artifactId, extension, classifier, ""); final Set repositories = channels.stream() - .map(ChannelImpl::getChannelDefinition) + .map(ChannelImpl::getResolvedChannelDefinition) .flatMap(d -> d.getRepositories().stream()) .collect(Collectors.toSet()); throw new NoStreamFoundException( diff --git a/core/src/main/java/org/wildfly/channel/RuntimeChannel.java b/core/src/main/java/org/wildfly/channel/RuntimeChannel.java new file mode 100644 index 00000000..1a63df80 --- /dev/null +++ b/core/src/main/java/org/wildfly/channel/RuntimeChannel.java @@ -0,0 +1,58 @@ +/* + * Copyright 2024 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.channel; + +import java.util.Objects; + +public class RuntimeChannel { + + private final Channel channel; + private final ChannelManifest channelManifest; + private final Blocklist blocklist; + + public RuntimeChannel(Channel channel, ChannelManifest channelManifest, Blocklist blocklist) { + this.channel = channel; + this.channelManifest = channelManifest; + this.blocklist = blocklist; + } + + public Channel getChannelDefinition() { + return channel; + } + + public ChannelManifest getChannelManifest() { + return channelManifest; + } + + public Blocklist getChannelBlocklist() { + return blocklist; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + RuntimeChannel that = (RuntimeChannel) o; + return Objects.equals(channel, that.channel) && Objects.equals(channelManifest, that.channelManifest) && Objects.equals(blocklist, that.blocklist); + } + + @Override + public int hashCode() { + return Objects.hash(channel, channelManifest, blocklist); + } +} diff --git a/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java b/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java index 713ef385..3f1a8d9a 100644 --- a/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java +++ b/core/src/test/java/org/wildfly/channel/ChannelSessionInitTestCase.java @@ -28,7 +28,9 @@ import java.nio.file.Files; import java.nio.file.Path; import java.util.List; +import java.util.Set; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -359,6 +361,63 @@ public void duplicatedManifestIDsAreDetected() throws Exception { assertThrows(RuntimeException.class, () -> new ChannelSession(channels, factory)); } + @Test + public void getVersionOfResolvedManifest() throws Exception { + final MavenVersionsResolver.Factory factory = mock(MavenVersionsResolver.Factory.class); + MavenVersionsResolver resolver = mock(MavenVersionsResolver.class); + when(factory.create(any())).thenReturn(resolver); + + final ChannelManifest requiredManifest = new ManifestBuilder() + .setId("manifest-one") + .build(); + mockManifest(resolver, requiredManifest, "test.channels:base-manifest:1.0.0"); + when(resolver.getAllVersions("test.channels", "base-manifest", ChannelManifest.EXTENSION, ChannelManifest.CLASSIFIER)) + .thenReturn(Set.of("1.0.0")); + + final List channels = List.of(new Channel.Builder() + .setName("channel one") + .addRepository("test", "test") + .setManifestCoordinate("test.channels", "base-manifest") + .build()); + try (ChannelSession channelSession = new ChannelSession(channels, factory)) { + assertThat(channelSession.getRuntimeChannels()) + .map(RuntimeChannel::getChannelDefinition) + .map(Channel::getManifestCoordinate) + .map(ChannelManifestCoordinate::getVersion) + .containsOnly("1.0.0"); + + } + } + + @Test + public void getVersionOfResolvedBlocklist() throws Exception { + final MavenVersionsResolver.Factory factory = mock(MavenVersionsResolver.Factory.class); + MavenVersionsResolver resolver = mock(MavenVersionsResolver.class); + when(factory.create(any())).thenReturn(resolver); + + final ChannelManifest requiredManifest = new ManifestBuilder() + .setId("manifest-one") + .build(); + mockManifest(resolver, requiredManifest, "test.channels:base-manifest:1.0.0"); + when(resolver.getAllVersions("test.channels", "blocklist", BlocklistCoordinate.EXTENSION, BlocklistCoordinate.CLASSIFIER)) + .thenReturn(Set.of("1.0.0")); + + final List channels = List.of(new Channel.Builder() + .setName("channel one") + .addRepository("test", "test") + .setManifestCoordinate("test.channels", "base-manifest", "1.0.0") + .setBlocklistCoordinate(new BlocklistCoordinate("test.channels", "blocklist")) + .build()); + try (ChannelSession channelSession = new ChannelSession(channels, factory)) { + assertThat(channelSession.getRuntimeChannels()) + .map(RuntimeChannel::getChannelDefinition) + .map(Channel::getBlocklistCoordinate) + .map(BlocklistCoordinate::getVersion) + .containsOnly("1.0.0"); + + } + } + private void mockManifest(MavenVersionsResolver resolver, ChannelManifest manifest, String gav) throws IOException { mockManifest(resolver, ChannelManifestMapper.toYaml(manifest), gav); } diff --git a/core/src/test/java/org/wildfly/channel/ChannelWithBlocklistTestCase.java b/core/src/test/java/org/wildfly/channel/ChannelWithBlocklistTestCase.java index dd7a8917..4a0a2c39 100644 --- a/core/src/test/java/org/wildfly/channel/ChannelWithBlocklistTestCase.java +++ b/core/src/test/java/org/wildfly/channel/ChannelWithBlocklistTestCase.java @@ -64,6 +64,7 @@ public void testFindLatestMavenArtifactVersion() throws Exception { " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -79,11 +80,10 @@ public void testFindLatestMavenArtifactVersion() throws Exception { " - groupId: org.wildfly\n" + " artifactId: wildfly-ee-galleon-pack\n" + " versionPattern: .*"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/test-blocklist.yaml"))); + mockBlocklistResolution(resolver, "channels/test-blocklist.yaml"); when(factory.create(any())).thenReturn(resolver); when(resolver.getAllVersions("org.wildfly", "wildfly-ee-galleon-pack", null, null)) @@ -109,6 +109,7 @@ public void testFindLatestMavenArtifactVersionBlocklistDoesntExist() throws Exce " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -124,7 +125,7 @@ public void testFindLatestMavenArtifactVersionBlocklistDoesntExist() throws Exce " - groupId: org.wildfly\n" + " artifactId: '*'\n" + " versionPattern: '25\\.\\d+\\.\\d+.Final'"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) @@ -156,6 +157,7 @@ public void testFindLatestMavenArtifactVersionWithWildcardBlocklist() throws Exc " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -171,11 +173,10 @@ public void testFindLatestMavenArtifactVersionWithWildcardBlocklist() throws Exc " - groupId: org.wildfly\n" + " artifactId: '*'\n" + " versionPattern: '25\\.\\d+\\.\\d+.Final'"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/test-blocklist-with-wildcards.yaml"))); + mockBlocklistResolution(resolver, "channels/test-blocklist-with-wildcards.yaml"); when(factory.create(any())).thenReturn(resolver); when(resolver.getAllVersions("org.wildfly", "wildfly-ee-galleon-pack", null, null)) @@ -201,6 +202,7 @@ public void testFindLatestMavenArtifactVersionBlocklistsAllVersionsException() t " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -216,11 +218,10 @@ public void testFindLatestMavenArtifactVersionBlocklistsAllVersionsException() t " - groupId: org.wildfly\n" + " artifactId: '*'\n" + " versionPattern: '25\\.\\d+\\.\\d+.Final'"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/test-blocklist.yaml"))); + mockBlocklistResolution(resolver, "channels/test-blocklist.yaml"); when(factory.create(any())).thenReturn(resolver); when(resolver.getAllVersions("org.wildfly", "wildfly-ee-galleon-pack", null, null)).thenReturn(new HashSet<>(singleton("25.0.1.Final"))); @@ -249,6 +250,7 @@ public void testResolveLatestMavenArtifact() throws Exception { " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -264,11 +266,10 @@ public void testResolveLatestMavenArtifact() throws Exception { " - groupId: org.wildfly\n" + " artifactId: '*'\n" + " versionPattern: '25\\.\\d+\\.\\d+.Final'"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/test-blocklist.yaml"))); + mockBlocklistResolution(resolver, "channels/test-blocklist.yaml"); File resolvedArtifactFile = mock(File.class); @@ -304,6 +305,7 @@ public void testResolveLatestMavenArtifactThrowUnresolvedMavenArtifactException( " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -319,11 +321,10 @@ public void testResolveLatestMavenArtifactThrowUnresolvedMavenArtifactException( " - groupId: org.wildfly\n" + " artifactId: '*'\n" + " versionPattern: '25\\.\\d+\\.\\d+.Final'"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/test-blocklist.yaml"))); + mockBlocklistResolution(resolver, "channels/test-blocklist.yaml"); when(factory.create(any())).thenReturn(resolver); when(resolver.getAllVersions("org.wildfly", "wildfly-ee-galleon-pack", null, null)).thenReturn(new HashSet<>(Set.of("25.0.1.Final","26.0.0.Final"))); @@ -340,6 +341,13 @@ public void testResolveLatestMavenArtifactThrowUnresolvedMavenArtifactException( verify(resolver, times(2)).close(); } + private void mockBlocklistResolution(MavenVersionsResolver resolver, String fileName) { + when(resolver.getAllVersions("org.wildfly", "wildfly-blocklist", BlocklistCoordinate.EXTENSION, BlocklistCoordinate.CLASSIFIER)) + .thenReturn(Set.of("1.0.0")); + when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist", "1.0.0")))) + .thenReturn(List.of(this.getClass().getClassLoader().getResource(fileName))); + } + @Test public void testResolveMavenArtifactsFromOneChannel() throws Exception { List channels = ChannelMapper.fromString( @@ -352,6 +360,7 @@ public void testResolveMavenArtifactsFromOneChannel() throws Exception { " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -370,11 +379,10 @@ public void testResolveMavenArtifactsFromOneChannel() throws Exception { " - groupId: org.wildfly\n" + " artifactId: wildfly-cli\n" + " version: \"26.0.0.Final\""); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/test-blocklist.yaml"))); + mockBlocklistResolution(resolver, "channels/test-blocklist.yaml"); File resolvedArtifactFile1 = mock(File.class); File resolvedArtifactFile2 = mock(File.class); @@ -476,6 +484,7 @@ public void testChannelWithInvalidBlacklist() throws Exception { " maven:\n" + " groupId: test\n" + " artifactId: 'test.manifest'\n" + + " version: '1.0.0'\n" + "repositories:\n" + " - id: test\n" + " url: http://test.te"); @@ -491,11 +500,10 @@ public void testChannelWithInvalidBlacklist() throws Exception { " - groupId: org.wildfly\n" + " artifactId: '*'\n" + " versionPattern: '25\\.\\d+\\.\\d+.Final'"); - when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest")))) + when(resolver.resolveChannelMetadata(List.of(new ChannelManifestCoordinate("test", "test.manifest", "1.0.0")))) .thenReturn(List.of(tempDir.resolve("manifest.yaml").toUri().toURL())); - when(resolver.resolveChannelMetadata(List.of(new BlocklistCoordinate("org.wildfly", "wildfly-blocklist")))) - .thenReturn(List.of(this.getClass().getClassLoader().getResource("channels/invalid-blocklist.yaml"))); + mockBlocklistResolution(resolver, "channels/invalid-blocklist.yaml"); when(factory.create(any())).thenReturn(resolver);