Skip to content

Commit 347244a

Browse files
committed
bump mockito to support jdk21, IC-2024.2 runs tests with it regardless
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent ce7e878 commit 347244a

File tree

7 files changed

+197
-33
lines changed

7 files changed

+197
-33
lines changed

build.gradle.kts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import org.gradle.api.tasks.testing.logging.TestExceptionFormat
12
import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType
23
import org.jetbrains.intellij.platform.gradle.TestFrameworkType
34
import org.jetbrains.intellij.platform.gradle.models.ProductRelease
@@ -59,9 +60,8 @@ dependencies {
5960
// for unit tests
6061
testImplementation(libs.junit)
6162
testImplementation(libs.assertj.core)
62-
testImplementation(libs.mockito.inline)
63+
testImplementation(libs.mockito.core)
6364
testImplementation(libs.opentest4j) // known issue: https://plugins.jetbrains.com/docs/intellij/tools-intellij-platform-gradle-plugin-faq.html#missing-opentest4j-dependency-in-test-framework
64-
6565
}
6666

6767
tasks {
@@ -73,6 +73,9 @@ tasks {
7373
useJUnit()
7474
systemProperty("tools.dl.path", temporaryDir)
7575
jvmArgs("-Djava.awt.headless=true")
76+
testLogging {
77+
exceptionFormat = TestExceptionFormat.FULL
78+
}
7679
}
7780

7881
withType<Test> {

gradle/libs.versions.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ commons-lang3 = "3.12.0"
77
commons-exec = "1.3"
88
common-lang = "3.9.4"
99
assertj-core = "3.22.0"
10-
mockito-inline = "4.5.1"
10+
mockito-core = "5.14.2"
1111
opentest4j = "1.3.0"
1212

1313
# plugins
@@ -22,7 +22,7 @@ kubernetes-httpclient-okhttp = { group = "io.fabric8", name = "kubernetes-httpcl
2222
jackson-core = { group = "com.fasterxml.jackson.core", name = "jackson-core", version.ref = "jackson-core" }
2323
commons-lang3 = { group = "org.apache.commons", name = "commons-lang3", version.ref = "commons-lang3" }
2424
assertj-core = { group = "org.assertj", name = "assertj-core", version.ref = "assertj-core" }
25-
mockito-inline = { group = "org.mockito", name = "mockito-inline", version.ref = "mockito-inline" }
25+
mockito-core = { group = "org.mockito", name = "mockito-core", version.ref = "mockito-core" }
2626
commons-exec = { group = "org.apache.commons", name = "commons-exec", version.ref = "commons-exec" }
2727
common-lang = { group = "com.twelvemonkeys.common", name = "common-lang", version.ref = "common-lang" }
2828
opentest4j = { group = "org.opentest4j", name = "opentest4j", version.ref = "opentest4j" }

src/main/java/com/redhat/devtools/intellij/common/kubernetes/ClusterHelper.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ private ClusterHelper() {
2424

2525
public static boolean isOpenShift(KubernetesClient client) {
2626
try {
27-
return client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false);
27+
return client != null
28+
&& client.hasApiGroup(OpenShiftClient.BASE_API_GROUP, false);
2829
} catch (KubernetesClientException e) {
2930
return false;
3031
}

src/main/java/com/redhat/devtools/intellij/common/utils/ConfigHelper.java

+142-19
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,40 @@ public class ConfigHelper {
2323
* Returns {@code true} if the given {@link io.fabric8.kubernetes.client.Config}s are equal.
2424
* They are considered equal if they're equal in
2525
* <ul>
26-
* <li>current context (cluster, user, current namespace, extensions)</li>
27-
* <li>(authentication) token</li>
26+
* <li>current context (cluster, user, current namespace)</li>
27+
* <li>auth info</li>
2828
* </ul>
2929
*
3030
* @param thisConfig the first config to compare
3131
* @param thatConfig the second config to compare
3232
* @return true if both configs are equal in context, contexts and token
3333
*
34-
* @see #areEqualContext(NamedContext, NamedContext)
34+
* @see #areEqualCurrentContext(Config, Config)
35+
* @see #areEqualCluster(Config, Config)
36+
* @see #areEqualAuthInfo(Config, Config)
3537
*/
3638
public static boolean areEqual(Config thisConfig, Config thatConfig) {
3739
return areEqualCurrentContext(thisConfig, thatConfig)
38-
&& areEqualToken(thisConfig, thatConfig);
40+
&& areEqualCluster(thisConfig, thatConfig)
41+
&& areEqualAuthInfo(thisConfig, thatConfig);
3942
}
4043

4144
/**
4245
* Returns {@code true} if the given {@link io.fabric8.kubernetes.client.Config}s are equal in current context.
4346
* They are considered equal if they're equal in
4447
* <ul>
45-
* <li>current context (cluster, user, current namespace, extensions)</li>
46-
* <li>(existing) contexts</li>
47-
* <li>(authentication) token</li>
48+
* <li>name</li>
49+
* <li>cluster</li>
50+
* <li>user</li>
51+
* <li>current namespace</li>
4852
* </ul>
4953
*
5054
* @param thisConfig the first config to compare
5155
* @param thatConfig the second config to compare
52-
* @return true if both configs are equal in context, contexts and token
56+
* @return true if both configs are equal in context, existing contexts and token
5357
*
5458
* @see Config#getCurrentContext()
59+
* @see #areEqualContext(NamedContext, NamedContext)
5560
*/
5661
public static boolean areEqualCurrentContext(Config thisConfig, Config thatConfig) {
5762
if (thisConfig == null) {
@@ -64,9 +69,9 @@ public static boolean areEqualCurrentContext(Config thisConfig, Config thatConfi
6469
}
6570

6671
/**
67-
* Returns {@code true} if both given {@link NamedContext} are equal.
68-
* They are considered equal if they're equal in
72+
* Returns {@code true} if both given {@link NamedContext} are equal in
6973
* <ul>
74+
* <li>name</li>
7075
* <li>cluster</li>
7176
* <li>user</li>
7277
* <li>current namespace</li>
@@ -76,20 +81,19 @@ public static boolean areEqualCurrentContext(Config thisConfig, Config thatConfi
7681
* @param thatContext the second context to compare
7782
* @return true if both contexts are equal
7883
*
84+
* @see #areEqualContext(Context, Context)
7985
* @see NamedContext
8086
* @see Context
8187
*/
82-
private static boolean areEqualContext(NamedContext thisContext, NamedContext thatContext) {
88+
public static boolean areEqualContext(NamedContext thisContext, NamedContext thatContext) {
8389
if (thisContext == null) {
8490
return thatContext == null;
8591
} else if (thatContext == null) {
8692
return false;
8793
}
88-
if (!Objects.equals(thisContext.getName(), thatContext.getName())) {
89-
return false;
90-
}
9194

92-
return areEqualContext(thisContext.getContext(), thatContext.getContext());
95+
return Objects.equals(thisContext.getName(), thatContext.getName())
96+
&& areEqualContext(thisContext.getContext(), thatContext.getContext());
9397
}
9498

9599
/**
@@ -115,13 +119,132 @@ private static boolean areEqualContext(Context thisContext, Context thatContext)
115119
return false;
116120
}
117121

118-
if (!Objects.equals(thisContext.getCluster(), thatContext.getCluster())){
122+
return Objects.equals(thisContext.getCluster(), thatContext.getCluster())
123+
&& Objects.equals(thisContext.getUser(), thatContext.getUser())
124+
&& Objects.equals(thisContext.getNamespace(), thatContext.getNamespace());
125+
}
126+
127+
/**
128+
* Returns {@code true} if both given {@link Config} are equal in
129+
* <ul>
130+
* <li>master url</li>
131+
* <li>(blindly) trust certificates</li>
132+
* <li>proxies</li>
133+
* <li>auth info</li>
134+
* </ul>
135+
*
136+
* @param thisConfig the first config to compare
137+
* @param thatConfig the second config to compare
138+
* @return true if both configs are equal in master url, trust certs, proxies and auth info
139+
*
140+
* @see Config
141+
*/
142+
public static boolean areEqualCluster(Config thisConfig, Config thatConfig) {
143+
if (thisConfig == null) {
144+
return thatConfig == null;
145+
} else if (thatConfig == null) {
146+
return false;
147+
}
148+
149+
return Objects.equals(thisConfig.getMasterUrl(), thatConfig.getMasterUrl())
150+
&& areEqualTrustCerts(thisConfig, thatConfig)
151+
&& areEqualProxy(thisConfig, thatConfig)
152+
&& areEqualAuthInfo(thisConfig, thatConfig);
153+
}
154+
155+
/**
156+
* Returns {@code true} if both given {@link Config} are equal in
157+
* <ul>
158+
* <li>http proxy</li>
159+
* <li>https proxy</li>
160+
* <li>proxy username</li>
161+
* <li>proxy password</li>
162+
* </ul>
163+
*
164+
* @param thisConfig the first config to compare
165+
* @param thatConfig the second config to compare
166+
* @return true if both configs are equal in http- & https-proxy, proxy username & password
167+
*
168+
* @see Config
169+
*/
170+
private static boolean areEqualProxy(Config thisConfig, Config thatConfig) {
171+
if (thisConfig == null) {
172+
return thatConfig == null;
173+
} else if (thatConfig == null) {
174+
return false;
175+
}
176+
177+
return Objects.equals(thisConfig.getHttpProxy(), thatConfig.getHttpProxy())
178+
&& Objects.equals(thisConfig.getHttpsProxy(), thatConfig.getHttpsProxy())
179+
&& Objects.equals(thisConfig.getProxyUsername(), thatConfig.getProxyUsername())
180+
&& Objects.equals(thisConfig.getProxyPassword(), thatConfig.getProxyPassword());
181+
}
182+
183+
/**
184+
* Returns {@code true} if both given {@link Config} are equal in
185+
* <ul>
186+
* <li>(blindly) trusting certificates</li>
187+
* <li>disable hostname verification</li>
188+
* <li>ca cert data</li>
189+
* <li>ca cert file</li>
190+
* </ul>
191+
*
192+
* @param thisConfig the first config to compare
193+
* @param thatConfig the second config to compare
194+
* @return true if both configs are equal in trusting certs, disabling hostname verification, ca cert data & file
195+
*
196+
* @see Config
197+
*/
198+
private static boolean areEqualTrustCerts(Config thisConfig, Config thatConfig) {
199+
if (thisConfig == null) {
200+
return thatConfig == null;
201+
} else if (thatConfig == null) {
119202
return false;
120-
} else if (!Objects.equals(thisContext.getNamespace(), thatContext.getNamespace())){
203+
}
204+
205+
return thisConfig.isTrustCerts() == thatConfig.isTrustCerts()
206+
&& thisConfig.isDisableHostnameVerification() == thatConfig.isDisableHostnameVerification()
207+
&& Objects.equals(thisConfig.getCaCertData(), thatConfig.getCaCertData())
208+
&& Objects.equals(thisConfig.getCaCertFile(), thatConfig.getCaCertFile());
209+
}
210+
211+
/**
212+
* Returns {@code true} if both given {@link Config} are equal in auth info
213+
* <ul>
214+
* <li>client cert file</li>
215+
* <li>client cert data</li>
216+
* <li>client key file</li>
217+
* <li>client key data</li>
218+
* <li>client key algo</li>
219+
* <li>username</li>
220+
* <li>password</li>
221+
* <li>proxies</li>
222+
* <li>token</li>
223+
* </ul>
224+
*
225+
* @param thisConfig the first config to compare
226+
* @param thatConfig the second config to compare
227+
* @return true if both configs are equal in client cert file/data, key file/data/algo, username, password
228+
* proxies and token
229+
*
230+
* @see Config
231+
*/
232+
public static boolean areEqualAuthInfo(Config thisConfig, Config thatConfig) {
233+
if (thisConfig == null) {
234+
return thatConfig == null;
235+
} else if (thatConfig == null) {
121236
return false;
122-
} else {
123-
return Objects.equals(thisContext.getUser(), thatContext.getUser());
124237
}
238+
239+
return Objects.equals(thisConfig.getClientCertFile(), thatConfig.getClientCertFile())
240+
&& Objects.equals(thisConfig.getClientCertData(), thatConfig.getClientCertData())
241+
&& Objects.equals(thisConfig.getClientKeyFile(), thatConfig.getClientKeyFile())
242+
&& Objects.equals(thisConfig.getClientKeyData(), thatConfig.getClientKeyData())
243+
&& Objects.equals(thisConfig.getClientKeyAlgo(), thatConfig.getClientKeyAlgo())
244+
&& Objects.equals(thisConfig.getUsername(), thatConfig.getUsername())
245+
&& Objects.equals(thisConfig.getPassword(), thatConfig.getPassword())
246+
&& areEqualProxy(thisConfig, thatConfig)
247+
&& areEqualToken(thisConfig, thatConfig);
125248
}
126249

127250
/**

src/main/java/com/redhat/devtools/intellij/common/utils/ConfigWatcher.java

+12-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import com.intellij.openapi.diagnostic.Logger;
1414
import io.fabric8.kubernetes.client.Config;
15-
import io.fabric8.kubernetes.client.ConfigBuilder;
1615

1716
import java.io.IOException;
1817
import java.nio.file.FileSystems;
@@ -132,15 +131,23 @@ private void watch(Consumer<io.fabric8.kubernetes.client.Config> listener, Watch
132131
for (WatchKey key = service.take(); key != null; key = service.take()) {
133132
key.pollEvents().forEach((event) -> {
134133
Path changed = getAbsolutePath(directory, (Path) event.context());
135-
if (isConfigPath(changed)) {
136-
Config config = new ConfigBuilder().build();
137-
listener.accept(config);
138-
}
134+
notifyListener(listener, changed);
139135
});
140136
key.reset();
141137
}
142138
}
143139

140+
private void notifyListener(Consumer<Config> listener, Path changed) {
141+
if (isConfigPath(changed)) {
142+
try {
143+
var config = Config.autoConfigure(null);
144+
listener.accept(config);
145+
} catch (Exception e) {
146+
LOG.warn("Loading config at '" + changed + "' failed.", e);
147+
}
148+
}
149+
}
150+
144151
protected boolean isConfigPath(Path path) {
145152
return configs != null
146153
&& configs.contains(path);

src/main/java/com/redhat/devtools/intellij/common/utils/ToolsConfigHelper.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
import java.io.IOException;
1717
import java.net.URL;
1818

19+
/**
20+
* Util methods that deal with tool (oc, kubectl, odo, etc.) configs.
21+
*/
1922
public class ToolsConfigHelper {
2023
private static final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
2124

22-
public static ToolsConfig loadToolsConfig() throws IOException {
23-
return loadToolsConfig(ToolsConfigHelper.class.getResource("/tools.json"));
24-
}
25-
2625
static ToolsConfig loadToolsConfig(URL url) throws IOException {
2726
try {
2827
return mapper.readValue(url, ToolsConfig.class);

src/test/java/com/redhat/devtools/intellij/common/utils/ConfigHelperTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,28 @@ public void areEqualContexts_returns_false_given_config_has_additional_context()
170170
assertThat(equal).isFalse();
171171
}
172172

173+
@Test
174+
public void areEqualAuthInfo_returns_false_given_contexts_differ_in_username() {
175+
// given
176+
Config config1 = clientConfig("yoda", null);
177+
Config config2 = clientConfig("obiwan", null);
178+
// when
179+
boolean equal = ConfigHelper.areEqualAuthInfo(config1, config2);
180+
// then
181+
assertThat(equal).isFalse();
182+
}
183+
184+
@Test
185+
public void areEqualAuthInfo_returns_false_given_contexts_differ_in_password() {
186+
// given
187+
Config config1 = clientConfig("yoda", "the force");
188+
Config config2 = clientConfig("yoda", "the light saber");
189+
// when
190+
boolean equal = ConfigHelper.areEqualAuthInfo(config1, config2);
191+
// then
192+
assertThat(equal).isFalse();
193+
}
194+
173195
@Test
174196
public void areEqualToken_returns_true_given_contexts_have_same_token() {
175197
// given
@@ -242,6 +264,15 @@ private static Config clientConfig(String token) {
242264
return clientConfig(token, null, null);
243265
}
244266

267+
private static Config clientConfig(String username, String password) {
268+
Config config = clientConfig(null, null, null);
269+
doReturn(username)
270+
.when(config).getUsername();
271+
doReturn(password)
272+
.when(config).getPassword();
273+
return config;
274+
}
275+
245276
private static Config clientConfig(NamedContext currentContext) {
246277
return clientConfig(null, currentContext, null);
247278
}

0 commit comments

Comments
 (0)