Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jkube-kit-common): Removed org.mockito.Mockito references from JKubeProjectUtilTest #2709

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,37 @@

import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
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;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Collections;
import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class JKubeProjectUtilTest {

@TempDir
File temporaryFolder;

private JavaProject project;

private File artifactFile;

private File finalOutputArtifact;

@BeforeEach
void setUp() throws IOException {
project = JavaProject.builder().build();
artifactFile = Files.createFile(temporaryFolder.toPath().resolve("foo-test-1.0.0.jar")).toFile();
}

@Test
void hasDependencyWithGroupIdWithNulls() {
// When
Expand All @@ -45,8 +57,7 @@ void hasDependencyWithGroupIdWithNulls() {
@Test
void hasDependencyWithGroupIdWithDependency() {
// Given
final JavaProject project = mock(JavaProject.class);
when(project.getDependencies()).thenReturn(Arrays.asList(
project.setDependencies(Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
Expand All @@ -60,12 +71,12 @@ void hasDependencyWithGroupIdWithDependency() {
@Test
void hasDependencyWithGroupIdWithNoDependency() {
// Given
final JavaProject project = mock(JavaProject.class);
when(project.getDependencies()).thenReturn(Arrays.asList(
project.toBuilder()
.dependencies(Arrays.asList(
Dependency.builder().groupId("io.dep").build(),
Dependency.builder().groupId("io.dep").artifactId("artifact").version("1.3.37").build(),
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()
));
Dependency.builder().groupId("io.other").artifactId("artifact").version("1.3.37").build()))
.build();
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.nothere");
// Then
Expand All @@ -75,10 +86,10 @@ void hasDependencyWithGroupIdWithNoDependency() {
@Test
void hasTransitiveDependency_whenGroupArtifactMatchesInProvidedDeps_shouldReturnTrue() {
// Given
final JavaProject project = mock(JavaProject.class);
when(project.getDependenciesWithTransitive()).thenReturn(Collections.singletonList(
Dependency.builder().groupId("org.example").artifactId("artifact").version("1.3.37").build()
));
project = project.toBuilder()
.dependenciesWithTransitive(Collections.singletonList(
Dependency.builder().groupId("org.example").artifactId("artifact").version("1.3.37").build()))
.build();

// When
final boolean result = JKubeProjectUtil.hasTransitiveDependency(project, "org.example", "artifact");
Expand All @@ -90,8 +101,6 @@ void hasTransitiveDependency_whenGroupArtifactMatchesInProvidedDeps_shouldReturn
@Test
void hasTransitiveDependency_whenGroupArtifactNoMatchInProvidedDeps_shouldReturnTrue() {
// Given
final JavaProject project = mock(JavaProject.class);

// When
final boolean result = JKubeProjectUtil.hasTransitiveDependency(project, "org.example", "artifact");

Expand All @@ -102,10 +111,8 @@ void hasTransitiveDependency_whenGroupArtifactNoMatchInProvidedDeps_shouldReturn
@Test
void getFinalOutputArtifact_withNothingProvided_returnsNull() {
// Given
JavaProject javaProject = JavaProject.builder().build();

// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(project);

// Then
assertThat(finalOutputArtifact).isNull();
Expand All @@ -114,81 +121,68 @@ void getFinalOutputArtifact_withNothingProvided_returnsNull() {
@Test
void getFinalOutputArtifact_withProjectArtifactVersionPackagingBuildDir_returnsInferredArtifact() throws IOException {
// Given
File artifactFile = new File(temporaryFolder, "foo-test-1.0.0.jar");
boolean artifactFileCreated = artifactFile.createNewFile();
JavaProject javaProject = JavaProject.builder()
.artifactId("foo-test")
.version("1.0.0")
.packaging("jar")
.buildDirectory(temporaryFolder)
.build();
project = project.toBuilder()
.artifactId("foo-test")
.version("1.0.0")
.packaging("jar")
.buildDirectory(temporaryFolder)
.build();

// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(project);

// Then
assertThat(artifactFileCreated).isTrue();
assertThat(finalOutputArtifact).hasName("foo-test-1.0.0.jar");
}

@Test
void getFinalOutputArtifact_withBuildFinalNamePackagingBuildDir_returnsInferredArtifact() throws IOException {
// Given
File artifactFile = new File(temporaryFolder, "foo-test-final.jar");
boolean artifactFileCreated = artifactFile.createNewFile();
JavaProject javaProject = JavaProject.builder()
.buildFinalName("foo-test-final")
.packaging("jar")
.buildDirectory(temporaryFolder)
.build();
Files.createFile(temporaryFolder.toPath().resolve("foo-test-final.jar"));
project = project.toBuilder()
.buildFinalName("foo-test-final")
.packaging("jar")
.buildDirectory(temporaryFolder)
.build();

// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(project);

// Then
assertThat(artifactFileCreated).isTrue();
assertThat(finalOutputArtifact).hasName("foo-test-final.jar");
}

@Test
void getFinalOutputArtifact_withArtifactAndBuildFinalNameAndPackaging_returnsInferredArtifact() throws IOException {
// Given
File artifactFile = new File(temporaryFolder, "foo-test-1.0.0.jar");
File buildFinalArtifactFile = new File(temporaryFolder, "foo-test-final.jar");
boolean buildFinalArtifactFileCreated = buildFinalArtifactFile.createNewFile();
boolean artifactFileCreated = artifactFile.createNewFile();
JavaProject javaProject = JavaProject.builder()
Files.createFile(temporaryFolder.toPath().resolve("foo-test-final.jar"));
project = project.toBuilder()
.artifact(artifactFile)
.buildFinalName("foo-test-final")
.packaging("jar")
.buildDirectory(temporaryFolder)
.build();

// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(project);

// Then
assertThat(artifactFileCreated).isTrue();
assertThat(buildFinalArtifactFileCreated).isTrue();
assertThat(finalOutputArtifact).hasName("foo-test-final.jar");
}

@Test
void getFinalOutputArtifact_withArtifactAndBuildFinalNameAndPackaging_returnsArtifactFromJavaProject() throws IOException {
// Given
File artifactFile = new File(temporaryFolder, "foo-test-1.0.0.jar");
boolean artifactFileCreated = artifactFile.createNewFile();
JavaProject javaProject = JavaProject.builder()
project = project.toBuilder()
.artifact(artifactFile)
.buildFinalName("foo-test-final")
.packaging("jar")
.build();

// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(project);

// Then
assertThat(artifactFileCreated).isTrue();
assertThat(finalOutputArtifact).hasName("foo-test-1.0.0.jar");
}

Expand All @@ -198,10 +192,9 @@ void getProperty_whenSystemPropertyPresent_returnsSystemProperty() {
try {
System.setProperty("jkube.testProperty", "true");
// TODO : Replace this when https://github.com/eclipse/jkube/issues/958 gets fixed
JavaProject javaProject = JavaProject.builder().build();

// When
String result = JKubeProjectUtil.getProperty("jkube.testProperty", javaProject);
String result = JKubeProjectUtil.getProperty("jkube.testProperty", project);

// Then
assertThat(result).isEqualTo("true");
Expand All @@ -215,10 +208,10 @@ void getProperty_whenProjectPropertyPresent_returnsProjectProperty() {
// Given
Properties properties = new Properties();
properties.put("jkube.test.project.property", "true");
JavaProject javaProject = JavaProject.builder().properties(properties).build();
project = project.toBuilder().properties(properties).build();

// When
String result = JKubeProjectUtil.getProperty("jkube.test.project.property", javaProject);
String result = JKubeProjectUtil.getProperty("jkube.test.project.property", project);

// Then
assertThat(result).isEqualTo("true");
Expand All @@ -227,32 +220,29 @@ void getProperty_whenProjectPropertyPresent_returnsProjectProperty() {
@Test
void resolveArtifact_whenArtifactPresent_shouldReturnArtifact() {
// Given
File actualArtifact = new File(temporaryFolder, "test-artifact-0.0.1.jar");
JavaProject javaProject = JavaProject.builder()
project = project.toBuilder()
.dependency(Dependency.builder()
.groupId("org.example")
.artifactId("test-artifact")
.version("0.0.1")
.type("jar")
.file(actualArtifact)
.file(artifactFile)
.build())
.build();

// When
File resolvedArtifact = JKubeProjectUtil.resolveArtifact(javaProject, "org.example", "test-artifact", "0.0.1", "jar");
File resolvedArtifact = JKubeProjectUtil.resolveArtifact(project, "org.example", "test-artifact", "0.0.1", "jar");

// Then
assertThat(resolvedArtifact).isNotNull().isEqualTo(actualArtifact);
assertThat(resolvedArtifact).isNotNull().isEqualTo(artifactFile);
}

@Test
void resolveArtifact_whenNoArtifactPresent_shouldThrowException() {
// Given
JavaProject javaProject = JavaProject.builder().build();

// When
IllegalStateException illegalStateException = assertThrows(IllegalStateException.class,
() -> JKubeProjectUtil.resolveArtifact(javaProject, "org.example", "test-artifact", "0.0.1", "jar"));
() -> JKubeProjectUtil.resolveArtifact(project, "org.example", "test-artifact", "0.0.1", "jar"));

// Then
assertThat(illegalStateException).hasMessage("Cannot find artifact test-artifact-0.0.1.jar within the resolved resources");
Expand Down