From 65d5e96da9f46b42958f424d9c842f2eacf05920 Mon Sep 17 00:00:00 2001 From: Haytham Mohamed Date: Tue, 1 Dec 2020 15:51:47 -0600 Subject: [PATCH] style checked for config module --- .../config/KubernetesConfigTestBase.java | 19 +++--- .../ConfigReloadAutoConfigurationTest.java | 65 ++++++++++--------- 2 files changed, 43 insertions(+), 41 deletions(-) diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/KubernetesConfigTestBase.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/KubernetesConfigTestBase.java index c2ecce96..f37b6d00 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/KubernetesConfigTestBase.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/KubernetesConfigTestBase.java @@ -20,6 +20,7 @@ import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.server.mock.KubernetesServer; import org.junit.After; import org.junit.ClassRule; + import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; @@ -34,22 +35,15 @@ import org.springframework.context.annotation.Configuration; **/ public class KubernetesConfigTestBase { - private static ConfigurableApplicationContext context; - @ClassRule public static KubernetesServer server = new KubernetesServer(); + private static ConfigurableApplicationContext context; + protected static ConfigurableApplicationContext getContext() { return context; } - @After - public void close() { - if (this.context != null) { - this.context.close(); - } - } - protected static void setup(String... env) { context = new SpringApplicationBuilder(PropertyPlaceholderAutoConfiguration.class, KubernetesClientTestConfiguration.class, BootstrapConfiguration.class, @@ -58,6 +52,13 @@ public class KubernetesConfigTestBase { .properties(env).run(); } + @After + public void close() { + if (this.context != null) { + this.context.close(); + } + } + @Configuration(proxyBeanMethods = false) private static class KubernetesClientTestConfiguration { diff --git a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfigurationTest.java b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfigurationTest.java index d44e63cf..53d26bd6 100644 --- a/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfigurationTest.java +++ b/spring-cloud-kubernetes-config/src/test/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfigurationTest.java @@ -16,16 +16,17 @@ package org.springframework.cloud.kubernetes.config.reload; +import java.util.HashMap; + import io.fabric8.kubernetes.api.model.ConfigMapBuilder; import io.fabric8.kubernetes.client.Config; import io.fabric8.kubernetes.client.KubernetesClient; import org.junit.BeforeClass; import org.junit.Test; + import org.springframework.cloud.kubernetes.config.KubernetesConfigTestBase; -import java.util.HashMap; - -import static org.assertj.core.api.Assertions.*; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Haytham Mohamed @@ -38,6 +39,35 @@ public class ConfigReloadAutoConfigurationTest extends KubernetesConfigTestBase private static final String APPLICATION_NAME = "application"; + @BeforeClass + public static void setUpBeforeClass() { + + setup(); + KubernetesClient mockClient = getContext().getBean(KubernetesClient.class); + + // Configure the kubernetes master url to point to the mock server + System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, + mockClient.getConfiguration().getMasterUrl()); + System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); + System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); + System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, + "false"); + System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); + System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); + + HashMap data = new HashMap<>(); + data.put("bean.greeting", "Hello ConfigMap, %s!"); + server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) + .always(); + server.expect() + .withPath("/api/v1/namespaces/spring/configmaps/" + APPLICATION_NAME) + .andReturn(200, new ConfigMapBuilder().withNewMetadata() + .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) + .always(); + } + @Test public void kubernetesConfigReloadDisabled() throws Exception { setup("spring.cloud.kubernetes.reload.enabled=false"); @@ -104,33 +134,4 @@ public class ConfigReloadAutoConfigurationTest extends KubernetesConfigTestBase assertThat(this.getContext().containsBean("propertyChangeWatcher")).isFalse(); } - @BeforeClass - public static void setUpBeforeClass() { - - setup(); - KubernetesClient mockClient = getContext().getBean(KubernetesClient.class); - - // Configure the kubernetes master url to point to the mock server - System.setProperty(Config.KUBERNETES_MASTER_SYSTEM_PROPERTY, - mockClient.getConfiguration().getMasterUrl()); - System.setProperty(Config.KUBERNETES_TRUST_CERT_SYSTEM_PROPERTY, "true"); - System.setProperty(Config.KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY, "false"); - System.setProperty(Config.KUBERNETES_AUTH_TRYSERVICEACCOUNT_SYSTEM_PROPERTY, - "false"); - System.setProperty(Config.KUBERNETES_NAMESPACE_SYSTEM_PROPERTY, "test"); - System.setProperty(Config.KUBERNETES_HTTP2_DISABLE, "true"); - - HashMap data = new HashMap<>(); - data.put("bean.greeting", "Hello ConfigMap, %s!"); - server.expect().withPath("/api/v1/namespaces/test/configmaps/" + APPLICATION_NAME) - .andReturn(200, new ConfigMapBuilder().withNewMetadata() - .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) - .always(); - server.expect() - .withPath("/api/v1/namespaces/spring/configmaps/" + APPLICATION_NAME) - .andReturn(200, new ConfigMapBuilder().withNewMetadata() - .withName(APPLICATION_NAME).endMetadata().addToData(data).build()) - .always(); - } - }