forked from eclipse-jkube/jkube
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JKubeProjectUtilTest.java
260 lines (219 loc) · 8.58 KB
/
JKubeProjectUtilTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/**
* Copyright (c) 2019 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at:
*
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
*/
package org.eclipse.jkube.kit.common.util;
import org.eclipse.jkube.kit.common.Dependency;
import org.eclipse.jkube.kit.common.JavaProject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.File;
import java.io.IOException;
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;
@Test
void hasDependencyWithGroupIdWithNulls() {
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(null, null);
// Then
assertThat(result).isFalse();
}
@Test
void hasDependencyWithGroupIdWithDependency() {
// Given
final JavaProject project = mock(JavaProject.class);
when(project.getDependencies()).thenReturn(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()
));
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.dep");
// Then
assertThat(result).isTrue();
}
@Test
void hasDependencyWithGroupIdWithNoDependency() {
// Given
final JavaProject project = mock(JavaProject.class);
when(project.getDependencies()).thenReturn(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()
));
// When
final boolean result = JKubeProjectUtil.hasDependencyWithGroupId(project, "io.nothere");
// Then
assertThat(result).isFalse();
}
@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()
));
// When
final boolean result = JKubeProjectUtil.hasTransitiveDependency(project, "org.example", "artifact");
// Then
assertThat(result).isTrue();
}
@Test
void hasTransitiveDependency_whenGroupArtifactNoMatchInProvidedDeps_shouldReturnTrue() {
// Given
final JavaProject project = mock(JavaProject.class);
// When
final boolean result = JKubeProjectUtil.hasTransitiveDependency(project, "org.example", "artifact");
// Then
assertThat(result).isFalse();
}
@Test
void getFinalOutputArtifact_withNothingProvided_returnsNull() {
// Given
JavaProject javaProject = JavaProject.builder().build();
// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
// Then
assertThat(finalOutputArtifact).isNull();
}
@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();
// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
// 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();
// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
// 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()
.artifact(artifactFile)
.buildFinalName("foo-test-final")
.packaging("jar")
.buildDirectory(temporaryFolder)
.build();
// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
// 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()
.artifact(artifactFile)
.buildFinalName("foo-test-final")
.packaging("jar")
.build();
// When
File finalOutputArtifact = JKubeProjectUtil.getFinalOutputArtifact(javaProject);
// Then
assertThat(artifactFileCreated).isTrue();
assertThat(finalOutputArtifact).hasName("foo-test-1.0.0.jar");
}
@Test
void getProperty_whenSystemPropertyPresent_returnsSystemProperty() {
// Given
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);
// Then
assertThat(result).isEqualTo("true");
} finally {
System.clearProperty("jkube.testProperty");
}
}
@Test
void getProperty_whenProjectPropertyPresent_returnsProjectProperty() {
// Given
Properties properties = new Properties();
properties.put("jkube.test.project.property", "true");
JavaProject javaProject = JavaProject.builder().properties(properties).build();
// When
String result = JKubeProjectUtil.getProperty("jkube.test.project.property", javaProject);
// Then
assertThat(result).isEqualTo("true");
}
@Test
void resolveArtifact_whenArtifactPresent_shouldReturnArtifact() {
// Given
File actualArtifact = new File(temporaryFolder, "test-artifact-0.0.1.jar");
JavaProject javaProject = JavaProject.builder()
.dependency(Dependency.builder()
.groupId("org.example")
.artifactId("test-artifact")
.version("0.0.1")
.type("jar")
.file(actualArtifact)
.build())
.build();
// When
File resolvedArtifact = JKubeProjectUtil.resolveArtifact(javaProject, "org.example", "test-artifact", "0.0.1", "jar");
// Then
assertThat(resolvedArtifact).isNotNull().isEqualTo(actualArtifact);
}
@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"));
// Then
assertThat(illegalStateException).hasMessage("Cannot find artifact test-artifact-0.0.1.jar within the resolved resources");
}
}