Refactor some tests (1) (#1533)
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2013-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
class App {
|
||||
|
||||
}
|
||||
@@ -16,57 +16,31 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer.RegisteredEventSource;
|
||||
|
||||
/**
|
||||
* test that asserts the type of published event for blocking discovery.
|
||||
*
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootTest(
|
||||
properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.config.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://example" },
|
||||
classes = BlockingDiscoveryHealthPublishedEventTest.HealthEventListenerConfiguration.class)
|
||||
@SpringBootTest(properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.config.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://example",
|
||||
"spring.cloud.discovery.reactive.enabled=false" },
|
||||
classes = { HealthEventListenerConfiguration.class, App.class })
|
||||
class BlockingDiscoveryHealthPublishedEventTest {
|
||||
|
||||
private static boolean caught;
|
||||
@AfterEach
|
||||
void afterEach() {
|
||||
HealthEventListenerConfiguration.caught = false;
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
Assertions.assertTrue(caught);
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class HealthEventListenerConfiguration {
|
||||
|
||||
@Bean
|
||||
HealthEventListener healthEventListener() {
|
||||
return new HealthEventListener();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class HealthEventListener implements ApplicationListener<InstanceRegisteredEvent<?>> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(InstanceRegisteredEvent<?> event) {
|
||||
caught = true;
|
||||
Assertions.assertTrue(event.getSource() instanceof RegisteredEventSource);
|
||||
RegisteredEventSource registeredEventSource = (RegisteredEventSource) event.getSource();
|
||||
Assertions.assertTrue(registeredEventSource.inside());
|
||||
Assertions.assertNull(registeredEventSource.pod());
|
||||
Assertions.assertEquals(registeredEventSource.cloudPlatform(), "kubernetes");
|
||||
}
|
||||
|
||||
Assertions.assertTrue(HealthEventListenerConfiguration.caught);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -30,9 +30,8 @@ import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.cloud.config.environment.Environment;
|
||||
import org.springframework.cloud.config.environment.PropertySource;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -68,7 +67,7 @@ class ConfigServerBootstrapperTests {
|
||||
wireMockServer = new WireMockServer(options().dynamicPort());
|
||||
wireMockServer.start();
|
||||
WireMock.configureFor(wireMockServer.port());
|
||||
String APPS_NAME = """
|
||||
String appsName = """
|
||||
[{
|
||||
"instanceId": "uid2",
|
||||
"serviceId": "spring-cloud-kubernetes-configserver",
|
||||
@@ -84,7 +83,7 @@ class ConfigServerBootstrapperTests {
|
||||
""".formatted(wireMockServer.port(), wireMockServer.baseUrl());
|
||||
|
||||
stubFor(get("/apps/spring-cloud-kubernetes-configserver").willReturn(
|
||||
aResponse().withStatus(200).withBody(APPS_NAME).withHeader("content-type", "application/json")));
|
||||
aResponse().withStatus(200).withBody(appsName).withHeader("content-type", "application/json")));
|
||||
Environment environment = new Environment("test", "default");
|
||||
Map<String, Object> properties = new HashMap<>();
|
||||
properties.put("hello", "world");
|
||||
@@ -96,11 +95,16 @@ class ConfigServerBootstrapperTests {
|
||||
.withHeader("content-type", "application/json")));
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void afterEach() {
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testBootstrapper() {
|
||||
this.context = setup().run();
|
||||
context = setup().run();
|
||||
verify(1, getRequestedFor(urlEqualTo("/apps/spring-cloud-kubernetes-configserver")));
|
||||
assertThat(this.context.getEnvironment().getProperty("hello")).isEqualTo("world");
|
||||
assertThat(context.getEnvironment().getProperty("hello")).isEqualTo("world");
|
||||
}
|
||||
|
||||
SpringApplicationBuilder setup(String... env) {
|
||||
@@ -111,20 +115,20 @@ class ConfigServerBootstrapperTests {
|
||||
}
|
||||
|
||||
private String[] addDefaultEnv(String[] env) {
|
||||
Set<String> set = new LinkedHashSet<>();
|
||||
if (env != null && env.length > 0) {
|
||||
set.addAll(Arrays.asList(env));
|
||||
Set<String> set = new HashSet<>();
|
||||
if (env != null) {
|
||||
set.addAll(List.of(env));
|
||||
}
|
||||
set.add("server.port=0");
|
||||
set.add("spring.cloud.config.discovery.enabled=true");
|
||||
set.add("spring.config.import=optional:configserver:");
|
||||
set.add("spring.cloud.config.discovery.service-id=spring-cloud-kubernetes-configserver");
|
||||
set.add("spring.cloud.kubernetes.discovery.discoveryServerUrl=" + wireMockServer.baseUrl());
|
||||
set.add("spring.main.web-application-type=NONE");
|
||||
return set.toArray(new String[0]);
|
||||
}
|
||||
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
@TestConfiguration
|
||||
static class TestConfig {
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* Copyright 2013-2023 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
import static org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer.RegisteredEventSource;
|
||||
|
||||
/**
|
||||
* @author wind57
|
||||
*/
|
||||
@TestConfiguration
|
||||
class HealthEventListenerConfiguration {
|
||||
|
||||
static boolean caught = false;
|
||||
|
||||
@Bean
|
||||
HealthEventListener healthEventListener() {
|
||||
return new HealthEventListener();
|
||||
}
|
||||
|
||||
private static class HealthEventListener implements ApplicationListener<InstanceRegisteredEvent<?>> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(InstanceRegisteredEvent<?> event) {
|
||||
caught = true;
|
||||
Assertions.assertInstanceOf(RegisteredEventSource.class, event.getSource());
|
||||
RegisteredEventSource registeredEventSource = (RegisteredEventSource) event.getSource();
|
||||
Assertions.assertTrue(registeredEventSource.inside());
|
||||
Assertions.assertNull(registeredEventSource.pod());
|
||||
Assertions.assertEquals(registeredEventSource.cloudPlatform(), "kubernetes");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ package org.springframework.cloud.kubernetes.discovery;
|
||||
import java.net.URI;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.github.tomakehurst.wiremock.WireMockServer;
|
||||
import com.github.tomakehurst.wiremock.client.WireMock;
|
||||
@@ -26,6 +27,7 @@ import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryProperties;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
|
||||
@@ -105,8 +107,10 @@ class KubernetesReactiveDiscoveryClientTests {
|
||||
|
||||
@Test
|
||||
void getInstances() {
|
||||
KubernetesDiscoveryClientProperties properties = new KubernetesDiscoveryClientProperties();
|
||||
properties.setDiscoveryServerUrl(wireMockServer.baseUrl());
|
||||
|
||||
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60,
|
||||
false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false,
|
||||
wireMockServer.baseUrl());
|
||||
KubernetesReactiveDiscoveryClient discoveryClient = new KubernetesReactiveDiscoveryClient(WebClient.builder(),
|
||||
properties);
|
||||
StepVerifier.create(discoveryClient.getServices()).expectNext("test-svc-1", "test-svc-3").verifyComplete();
|
||||
@@ -114,8 +118,9 @@ class KubernetesReactiveDiscoveryClientTests {
|
||||
|
||||
@Test
|
||||
void getServices() {
|
||||
KubernetesDiscoveryClientProperties properties = new KubernetesDiscoveryClientProperties();
|
||||
properties.setDiscoveryServerUrl(wireMockServer.baseUrl());
|
||||
KubernetesDiscoveryProperties properties = new KubernetesDiscoveryProperties(true, true, Set.of(), true, 60,
|
||||
false, null, Set.of(), Map.of(), null, KubernetesDiscoveryProperties.Metadata.DEFAULT, 0, false, false,
|
||||
wireMockServer.baseUrl());
|
||||
KubernetesReactiveDiscoveryClient discoveryClient = new KubernetesReactiveDiscoveryClient(WebClient.builder(),
|
||||
properties);
|
||||
Map<String, String> metadata = new HashMap<>();
|
||||
|
||||
@@ -16,68 +16,40 @@
|
||||
|
||||
package org.springframework.cloud.kubernetes.discovery;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.TestConfiguration;
|
||||
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.cloud.kubernetes.commons.discovery.KubernetesDiscoveryClientHealthIndicatorInitializer;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* test that asserts the type of published event for reactive discovery.
|
||||
*
|
||||
* @author wind57
|
||||
*/
|
||||
@SpringBootTest(
|
||||
properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.config.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://example",
|
||||
// disable blocking implementation
|
||||
"spring.cloud.discovery.blocking.enabled=false" },
|
||||
classes = ReactiveDiscoveryHealthPublishedEventTest.HealthEventListenerConfiguration.class)
|
||||
@SpringBootTest(properties = { "spring.main.cloud-platform=kubernetes", "spring.cloud.config.enabled=false",
|
||||
"spring.cloud.kubernetes.discovery.discovery-server-url=http://example",
|
||||
// disable blocking implementation
|
||||
"spring.cloud.discovery.blocking.enabled=false" },
|
||||
classes = { HealthEventListenerConfiguration.class, App.class })
|
||||
class ReactiveDiscoveryHealthPublishedEventTest {
|
||||
|
||||
private static boolean caught;
|
||||
|
||||
// blocking client is not present, as such the blocking auto-configuration was not
|
||||
// picked up, therefor the health event comes from the reactive one.
|
||||
@Autowired
|
||||
private ObjectProvider<KubernetesDiscoveryClient> discoveryClients;
|
||||
|
||||
@AfterEach
|
||||
void afterEach() {
|
||||
HealthEventListenerConfiguration.caught = false;
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
Assertions.assertTrue(caught);
|
||||
Assertions.assertTrue(HealthEventListenerConfiguration.caught);
|
||||
Assertions.assertNull(discoveryClients.getIfAvailable());
|
||||
}
|
||||
|
||||
@TestConfiguration
|
||||
static class HealthEventListenerConfiguration {
|
||||
|
||||
@Bean
|
||||
ReactiveDiscoveryHealthPublishedEventTest.HealthEventListener healthEventListener() {
|
||||
return new ReactiveDiscoveryHealthPublishedEventTest.HealthEventListener();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class HealthEventListener implements ApplicationListener<InstanceRegisteredEvent<?>> {
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(InstanceRegisteredEvent<?> event) {
|
||||
caught = true;
|
||||
Assertions.assertTrue(event
|
||||
.getSource() instanceof KubernetesDiscoveryClientHealthIndicatorInitializer.RegisteredEventSource);
|
||||
KubernetesDiscoveryClientHealthIndicatorInitializer.RegisteredEventSource registeredEventSource = (KubernetesDiscoveryClientHealthIndicatorInitializer.RegisteredEventSource) event
|
||||
.getSource();
|
||||
Assertions.assertTrue(registeredEventSource.inside());
|
||||
Assertions.assertNull(registeredEventSource.pod());
|
||||
Assertions.assertEquals(registeredEventSource.cloudPlatform(), "kubernetes");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user