Skip to content

Commit

Permalink
Merge pull request #20 from TomasHofman/multiple-channels-in-file
Browse files Browse the repository at this point in the history
Allow multiple channels to be loaded from a single file
  • Loading branch information
TomasHofman authored Nov 11, 2024
2 parents fe527f4 + 5cf569b commit 55dbd07
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 4 deletions.
12 changes: 9 additions & 3 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<version>${version.jandex}</version>
</dependency>

<!-- Dependency tree generation -->
<dependency>
<groupId>org.apache.maven.shared</groupId>
Expand Down Expand Up @@ -164,9 +170,9 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
<version>${version.jandex}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
Expand Down Expand Up @@ -104,7 +105,8 @@ protected void initChannelSession() throws MojoExecutionException {
channelFilePath = Path.of(mavenSession.getExecutionRootDirectory()).resolve(channelFilePath);
}
getLog().info("Reading channel file " + channelFilePath);
channels.add(ChannelMapper.from(channelFilePath.toUri().toURL()));
String yaml = Files.readString(channelFilePath);
channels.addAll(ChannelMapper.fromString(yaml));
}
}
if (StringUtils.isNotBlank(channelGAV)) {
Expand Down Expand Up @@ -140,6 +142,8 @@ protected void initChannelSession() throws MojoExecutionException {
}
} catch (MalformedURLException e) {
throw new MojoExecutionException("Can't parse the channel or manifest file path", e);
} catch (IOException e) {
throw new MojoExecutionException("Can't read channel metadata file", e);
}

if (!remoteRepositories.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.wildfly.channelplugin;

import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.assertj.core.api.Assertions;
import org.eclipse.aether.RepositorySystem;
import org.eclipse.aether.artifact.DefaultArtifact;
import org.eclipse.aether.resolution.ArtifactRequest;
import org.eclipse.aether.resolution.ArtifactResult;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.mockito.Mockito;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.List;

public class AbstractChannelMojoTestCase {

@TempDir
File tempDir;

private final RepositorySystem repositorySystem = Mockito.mock(RepositorySystem.class);

private AbstractChannelMojo mojo;

@BeforeEach
public void before() throws Exception {
// Prepare test resources as local files
Path channelFile = prepareResource("multiple-channels.yaml");
Path manifestFile = prepareResource("manifest.yaml");

// Mock RepositorySystem, the resolveArtifact() method must return a valid manifest file because this is needed
// for ChannelSession init.
ArtifactResult artifactResult = new ArtifactResult(new ArtifactRequest());
artifactResult.setArtifact(new DefaultArtifact(null, null, null, null, null, Collections.emptyMap(),
manifestFile.toFile()));
Mockito.when(repositorySystem.resolveArtifact(Mockito.any(), Mockito.any()))
.thenReturn(artifactResult);

// Initialize a concrete instance of the AbstractChannelMojo class
mojo = new AbstractChannelMojo() {
@Override
public void execute() throws MojoExecutionException, MojoFailureException {

}
};
mojo.channelFile = channelFile.toString();
mojo.remoteRepositories = Collections.emptyList();
mojo.repositorySystem = repositorySystem;
}

@Test
public void testMultipleChannelsInFile() throws Exception {
mojo.initChannelSession();

Assertions.assertThat(mojo.channels.size()).isEqualTo(2);
Assertions.assertThat(mojo.channels).extracting("manifestCoordinate.artifactId").containsAll(
List.of("eap-8.0", "eap-xp-5.0"));
}

@SuppressWarnings("ResultOfMethodCallIgnored")
private Path prepareResource(String resourceName) throws IOException {
tempDir.mkdir();
Path targetFile = tempDir.toPath().resolve(resourceName);
InputStream resource = getClass().getResourceAsStream(resourceName);
Assertions.assertThat(resource).isNotNull().withFailMessage("Resource not found: " + resourceName);
Files.copy(resource, targetFile);
return targetFile;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
schemaVersion: 1.0.0
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
schemaVersion: "2.0.0"
name: "eap-xp-5.0-channel"
repositories:
- id: "Brew"
url: "https://download.eng.bos.redhat.com/brewroot/repos/jb-eap-8.0-maven-build/latest/maven/"
manifest:
maven:
groupId: "org.jboss.eap.channels"
artifactId: "eap-xp-5.0"
version: "1.0.0"
noStreamStrategy: "none"
---
schemaVersion: "2.0.0"
name: "eap-8.0-channel"
repositories:
- id: "Brew"
url: "https://download.eng.bos.redhat.com/brewroot/repos/jb-eap-8.0-maven-build/latest/maven/"
manifest:
maven:
groupId: "org.jboss.eap.channels"
artifactId: "eap-8.0"
version: "1.0.0"
noStreamStrategy: "none"
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>

<!-- Maven plugin test tooling -->
<dependency>
<groupId>com.soebes.itf.jupiter.extension</groupId>
Expand Down

0 comments on commit 55dbd07

Please sign in to comment.