Skip to content

Commit

Permalink
refactor (openshift-maven-plugin): Migrate all tests from JUnit4 to J…
Browse files Browse the repository at this point in the history
…Unit5 (#1579)

Signed-off-by: Anurag Rajawat <anuragsinghrajawat22@gmail.com>
  • Loading branch information
anurag-rajawat authored and manusa committed Aug 23, 2022
1 parent f06a231 commit 10a76db
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 73 deletions.
10 changes: 8 additions & 2 deletions openshift-maven-plugin/plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,14 @@
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*/
package org.eclipse.jkube.maven.plugin.mojo;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.w3c.dom.Document;

import javax.xml.parsers.DocumentBuilder;
Expand All @@ -26,50 +26,39 @@
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Stream;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@RunWith(Parameterized.class)
public class OpenShiftGeneratedPluginDescriptorTest {
class OpenShiftGeneratedPluginDescriptorTest {
private File pluginDescriptor;

@Parameterized.Parameter
public String mojo;

@Parameterized.Parameter(1)
public String expectedRequiresDependencyResolution;

@Parameterized.Parameter(2)
public String expectedPhase;

@Before
public void setUp() {
@BeforeEach
void setUp() {
URL pluginDescriptorUrl = getClass().getResource("/META-INF/maven/plugin.xml");
assertThat(pluginDescriptorUrl).isNotNull();
pluginDescriptor = new File(pluginDescriptorUrl.getFile());
}

@Parameterized.Parameters(name = "{index} {0}, should have {1} requiresDependencyResolution and {2} phase")
public static Collection<Object[]> data() {
return Arrays.asList(
new Object[] { "build", "compile", "pre-integration-test"},
new Object[] { "resource", "compile", "process-resources"},
new Object[] { "apply", "compile+runtime", "install"},
new Object[] { "deploy", "compile+runtime", "validate"},
new Object[] { "watch", "compile+runtime", "package"},
new Object[] { "undeploy", "compile", "install"},
new Object[] { "debug", "compile+runtime", "package"},
new Object[] { "log", "compile+runtime", "validate"},
new Object[] { "push", "compile", "install"},
new Object[] { "helm", "", "pre-integration-test"},
new Object[] { "helm-push", "compile", "install"}
public static Stream<Arguments> data() {
return Stream.of(
Arguments.of("build", "compile", "pre-integration-test"),
Arguments.of("resource", "compile", "process-resources"),
Arguments.of("apply", "compile+runtime", "install"),
Arguments.of("deploy", "compile+runtime", "validate"),
Arguments.of("watch", "compile+runtime", "package"),
Arguments.of("undeploy", "compile", "install"),
Arguments.of("debug", "compile+runtime", "package"),
Arguments.of("log", "compile+runtime", "validate"),
Arguments.of("push", "compile", "install"),
Arguments.of("helm", "", "pre-integration-test"),
Arguments.of("helm-push", "compile", "install")
);
}

@Test
public void verifyPhaseAndRequiresDependencyResolution() throws Exception {
@ParameterizedTest(name = "{0}, should have {1} requiresDependencyResolution and {2} phase")
@MethodSource("data")
void verifyPhaseAndRequiresDependencyResolution(String mojo, String expectedRequiresDependencyResolution, String expectedPhase) throws Exception {
assertThat(getField(pluginDescriptor, "/plugin/mojos/mojo[goal='" + mojo + "']/requiresDependencyResolution"))
.isEqualTo(expectedRequiresDependencyResolution);
assertThat(getField(pluginDescriptor, "/plugin/mojos/mojo[goal='" + mojo + "']/phase"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,31 @@
import org.eclipse.jkube.kit.config.image.ImageConfiguration;
import org.eclipse.jkube.kit.config.image.build.BuildConfiguration;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.Properties;

import static org.junit.Assert.assertEquals;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OpenShiftResourceMojoTest {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

class OpenShiftResourceMojoTest {
private ClusterAccess mockedClusterAccess;
private ImageConfigResolver mockedImageConfigResolver;

private OpenshiftResourceMojo openShiftResourceMojo;

@Before
public void setUp() throws IOException {
@BeforeEach
void setUp(@TempDir Path temporaryFolder) throws IOException {
mockedClusterAccess = mock(ClusterAccess.class, RETURNS_DEEP_STUBS);
mockedImageConfigResolver = mock(ImageConfigResolver.class, RETURNS_DEEP_STUBS);
Properties properties = new Properties();
Expand All @@ -74,15 +72,15 @@ public void setUp() throws IOException {
this.openShiftResourceMojo.imageConfigResolver = mockedImageConfigResolver;
this.openShiftResourceMojo.javaProject = javaProject;
this.openShiftResourceMojo.interpolateTemplateParameters = true;
this.openShiftResourceMojo.resourceDir = temporaryFolder.newFolder("src", "main", "jkube");
this.openShiftResourceMojo.resourceDir = Files.createDirectories(temporaryFolder.resolve("src").resolve("main").resolve("jkube")).toFile();

when(mockedMavenProject.getProperties()).thenReturn(properties);
when(mockedJKubeServiceHub.getConfiguration().getProject()).thenReturn(javaProject);
when(mockedJKubeServiceHub.getConfiguration().getBasedir()).thenReturn(temporaryFolder.getRoot());
when(mockedJKubeServiceHub.getConfiguration().getBasedir()).thenReturn(temporaryFolder.toFile());
}

@Test
public void executeInternal_resolvesGroupInImageNameToClusterAccessNamespace_whenNamespaceDetected() throws MojoExecutionException, MojoFailureException {
void executeInternal_resolvesGroupInImageNameToClusterAccessNamespace_whenNamespaceDetected() throws MojoExecutionException, MojoFailureException {
// Given
ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("%g/%a")
Expand All @@ -92,20 +90,20 @@ public void executeInternal_resolvesGroupInImageNameToClusterAccessNamespace_whe
.build();
when(mockedClusterAccess.getNamespace()).thenReturn("test-custom-namespace");
when(mockedImageConfigResolver.resolve(eq(imageConfiguration), any())).thenReturn(Collections.singletonList(imageConfiguration));
this.openShiftResourceMojo.images = Collections.singletonList(imageConfiguration);
openShiftResourceMojo.images = Collections.singletonList(imageConfiguration);
openShiftResourceMojo.skip = true;

// When
openShiftResourceMojo.initJKubeServiceHubBuilder(openShiftResourceMojo.javaProject);
openShiftResourceMojo.executeInternal();

// Then
assertEquals(1, openShiftResourceMojo.resolvedImages.size());
assertEquals("test-custom-namespace/test-project", openShiftResourceMojo.resolvedImages.get(0).getName());
assertThat(openShiftResourceMojo.resolvedImages).singleElement()
.hasFieldOrPropertyWithValue("name", "test-custom-namespace/test-project");
}

@Test
public void executeInternal_resolvesGroupInImageNameToNamespaceSetViaConfiguration_whenNoNamespaceDetected() throws Exception {
void executeInternal_resolvesGroupInImageNameToNamespaceSetViaConfiguration_whenNoNamespaceDetected() throws Exception {
// Given
ImageConfiguration imageConfiguration = ImageConfiguration.builder()
.name("%g/%a")
Expand All @@ -123,7 +121,7 @@ public void executeInternal_resolvesGroupInImageNameToNamespaceSetViaConfigurati
openShiftResourceMojo.executeInternal();

// Then
assertEquals(1, openShiftResourceMojo.resolvedImages.size());
assertEquals("namespace-configured-via-plugin/test-project", openShiftResourceMojo.resolvedImages.get(0).getName());
assertThat(openShiftResourceMojo.resolvedImages).singleElement()
.hasFieldOrPropertyWithValue("name", "namespace-configured-via-plugin/test-project");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@
import io.fabric8.openshift.client.OpenShiftClient;
import org.eclipse.jkube.kit.config.resource.RuntimeMode;
import org.eclipse.jkube.kit.config.service.JKubeServiceHub;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
Expand All @@ -30,22 +29,19 @@
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class OpenshiftUndeployMojoTest {

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();
class OpenshiftUndeployMojoTest {
private JKubeServiceHub mockServiceHub;
private File kubernetesManifestFile;
private File openShiftManifestFile;
private File openShiftISManifestFile;
private OpenshiftUndeployMojo undeployMojo;

@Before
public void setUp() throws IOException {
@BeforeEach
void setUp(@TempDir File temporaryFolder) throws IOException {
mockServiceHub = mock(JKubeServiceHub.class, RETURNS_DEEP_STUBS);
kubernetesManifestFile = temporaryFolder.newFile();
openShiftManifestFile = temporaryFolder.newFile();
openShiftISManifestFile = temporaryFolder.newFile();
kubernetesManifestFile = File.createTempFile("junit", "ext", temporaryFolder);
openShiftManifestFile = File.createTempFile("junit", "ext", temporaryFolder);
openShiftISManifestFile = File.createTempFile("junit", "ext", temporaryFolder);
// @formatter:off
undeployMojo = new OpenshiftUndeployMojo() {{
kubernetesManifest = kubernetesManifestFile;
Expand All @@ -57,7 +53,7 @@ public void setUp() throws IOException {
}

@Test
public void getManifestsToUndeploy() {
void getManifestsToUndeploy() {
// Given
final OpenShiftClient client = mock(OpenShiftClient.class);
when(mockServiceHub.getClient()).thenReturn(client);
Expand All @@ -70,12 +66,12 @@ public void getManifestsToUndeploy() {
}

@Test
public void getRuntimeMode() {
void getRuntimeMode() {
assertThat(undeployMojo.getRuntimeMode()).isEqualTo(RuntimeMode.OPENSHIFT);
}

@Test
public void getLogPrefix() {
void getLogPrefix() {
assertThat(undeployMojo.getLogPrefix()).isEqualTo("oc: ");
}
}
Expand Down

0 comments on commit 10a76db

Please sign in to comment.