Skip to content

Commit 3f81769

Browse files
committed
fix-1935: more tests
Signed-off-by: wind57 <[email protected]>
1 parent 053f459 commit 3f81769

File tree

8 files changed

+338
-30
lines changed

8 files changed

+338
-30
lines changed

spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/array_with_profiles/ArrayWithProfiles.java

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,15 @@
1616

1717
package org.springframework.cloud.kubernetes.fabric8.config.array_with_profiles;
1818

19+
import java.util.HashMap;
20+
1921
import io.fabric8.kubernetes.api.model.ConfigMapBuilder;
2022
import io.fabric8.kubernetes.client.Config;
2123
import io.fabric8.kubernetes.client.KubernetesClient;
22-
import org.junit.jupiter.api.Test;
23-
import org.springframework.beans.factory.annotation.Autowired;
24+
2425
import org.springframework.boot.test.context.SpringBootTest;
2526
import org.springframework.cloud.kubernetes.commons.config.Constants;
2627
import org.springframework.cloud.kubernetes.fabric8.config.TestApplication;
27-
import org.springframework.test.context.ActiveProfiles;
28-
import org.springframework.test.web.reactive.server.WebTestClient;
29-
30-
import java.util.HashMap;
3128

3229
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
3330
import static org.springframework.cloud.kubernetes.fabric8.config.ConfigMapTestUtil.readResourceFile;
@@ -36,14 +33,10 @@
3633
* @author wind57
3734
*/
3835
@SpringBootTest(webEnvironment = RANDOM_PORT, classes = TestApplication.class,
39-
properties = { "spring.application.name=array-with-profiles",
40-
"spring.cloud.kubernetes.reload.enabled=false", "spring.main.cloud-platform=KUBERNETES" })
41-
@ActiveProfiles("dev")
36+
properties = { "spring.application.name=array-with-profiles", "spring.cloud.kubernetes.reload.enabled=false",
37+
"spring.main.cloud-platform=KUBERNETES" })
4238
abstract class ArrayWithProfiles {
4339

44-
@Autowired
45-
private WebTestClient webClient;
46-
4740
private static final String APPLICATION_NAME = "array-with-profiles";
4841

4942
static void setUpBeforeClass(KubernetesClient mockClient) {
@@ -67,15 +60,4 @@ static void setUpBeforeClass(KubernetesClient mockClient) {
6760
.create();
6861
}
6962

70-
@Test
71-
void testItemsEndpoint() {
72-
this.webClient.get()
73-
.uri("/api/items")
74-
.exchange()
75-
.expectStatus()
76-
.isOk()
77-
.expectBody()
78-
.consumeWith(System.out::println);
79-
}
80-
8163
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.array_with_profiles;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import io.fabric8.kubernetes.client.KubernetesClient;
23+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.test.context.ActiveProfiles;
29+
import org.springframework.test.context.TestPropertySource;
30+
import org.springframework.test.web.reactive.server.WebTestClient;
31+
32+
/**
33+
* @author wind57
34+
*/
35+
@TestPropertySource(properties = { "spring.cloud.bootstrap.enabled=true" })
36+
@EnableKubernetesMockClient(crud = true, https = false)
37+
@ActiveProfiles("dev")
38+
class BootstrapMultipleYamlDocumentsDevActiveTests extends ArrayWithProfiles {
39+
40+
@Autowired
41+
private WebTestClient webClient;
42+
43+
private static KubernetesClient mockClient;
44+
45+
@BeforeAll
46+
public static void setUpBeforeClass() {
47+
setUpBeforeClass(mockClient);
48+
}
49+
50+
/**
51+
* <pre>
52+
* - dev is an active profile
53+
* - which means beans.items = [Item 6, Item 7, Item 8]
54+
* </pre>
55+
*/
56+
@Test
57+
void testItemsEndpoint() {
58+
this.webClient.get()
59+
.uri("/api/items")
60+
.exchange()
61+
.expectStatus()
62+
.isOk()
63+
.expectBody(List.class)
64+
.isEqualTo(List.of("Item 6", "Item 7", "Item 8"));
65+
}
66+
67+
/**
68+
* <pre>
69+
* - dev is an active profile
70+
* - which means beans.map = {"name", "Alice", "role", "admin"}
71+
* </pre>
72+
*/
73+
@Test
74+
void testMapEndpoint() {
75+
this.webClient.get()
76+
.uri("/api/map")
77+
.exchange()
78+
.expectStatus()
79+
.isOk()
80+
.expectBody(Map.class)
81+
.isEqualTo(Map.of("name", "Alice", "role", "admin"));
82+
}
83+
84+
}
Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,67 @@
1616

1717
package org.springframework.cloud.kubernetes.fabric8.config.array_with_profiles;
1818

19+
import java.util.List;
20+
import java.util.Map;
21+
1922
import io.fabric8.kubernetes.client.KubernetesClient;
2023
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
2124
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
2228
import org.springframework.test.context.TestPropertySource;
29+
import org.springframework.test.web.reactive.server.WebTestClient;
2330

2431
/**
2532
* @author wind57
2633
*/
2734
@TestPropertySource(properties = { "spring.cloud.bootstrap.enabled=true" })
2835
@EnableKubernetesMockClient(crud = true, https = false)
29-
class BootstrapArrayWithProfilesTests extends ArrayWithProfiles {
36+
class BootstrapMultipleYamlDocumentsNoActiveProfileTests extends ArrayWithProfiles {
37+
38+
@Autowired
39+
private WebTestClient webClient;
3040

3141
private static KubernetesClient mockClient;
3242

3343
@BeforeAll
3444
public static void setUpBeforeClass() {
3545
setUpBeforeClass(mockClient);
3646
}
47+
48+
/**
49+
* <pre>
50+
* - dev is not an active profile
51+
* - which means beans.items = [Item 10]
52+
* </pre>
53+
*/
54+
@Test
55+
void testItemsEndpoint() {
56+
this.webClient.get()
57+
.uri("/api/items")
58+
.exchange()
59+
.expectStatus()
60+
.isOk()
61+
.expectBody(List.class)
62+
.isEqualTo(List.of("Item 10"));
63+
}
64+
65+
/**
66+
* <pre>
67+
* - dev is not an active profile
68+
* - which means beans.map = {"name", "ER", "role", "user"}
69+
* </pre>
70+
*/
71+
@Test
72+
void testMapEndpoint() {
73+
this.webClient.get()
74+
.uri("/api/map")
75+
.exchange()
76+
.expectStatus()
77+
.isOk()
78+
.expectBody(Map.class)
79+
.isEqualTo(Map.of("name", "ER", "role", "user"));
80+
}
81+
3782
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.array_with_profiles;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import io.fabric8.kubernetes.client.KubernetesClient;
23+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.test.context.ActiveProfiles;
29+
import org.springframework.test.context.TestPropertySource;
30+
import org.springframework.test.web.reactive.server.WebTestClient;
31+
32+
/**
33+
* @author wind57
34+
*/
35+
@TestPropertySource(properties = { "spring.config.import=kubernetes:" })
36+
@EnableKubernetesMockClient(crud = true, https = false)
37+
@ActiveProfiles("dev")
38+
class ConfigDataMultipleYamlDocumentsDevActiveTests extends ArrayWithProfiles {
39+
40+
@Autowired
41+
private WebTestClient webClient;
42+
43+
private static KubernetesClient mockClient;
44+
45+
@BeforeAll
46+
public static void setUpBeforeClass() {
47+
setUpBeforeClass(mockClient);
48+
}
49+
50+
/**
51+
* <pre>
52+
* - dev is not an active profile
53+
* - which means beans.items = [Item 6, Item 7, Item 8]
54+
* </pre>
55+
*/
56+
@Test
57+
void testItemsEndpoint() {
58+
this.webClient.get()
59+
.uri("/api/items")
60+
.exchange()
61+
.expectStatus()
62+
.isOk()
63+
.expectBody(List.class)
64+
.isEqualTo(List.of("Item 6", "Item 7", "Item 8"));
65+
}
66+
67+
/**
68+
* <pre>
69+
* - dev is not an active profile
70+
* - which means beans.map = {"name", "Alice", "role", "admin"}
71+
* </pre>
72+
*/
73+
@Test
74+
void testMapEndpoint() {
75+
this.webClient.get()
76+
.uri("/api/map")
77+
.exchange()
78+
.expectStatus()
79+
.isOk()
80+
.expectBody(Map.class)
81+
.isEqualTo(Map.of("name", "Alice", "role", "admin"));
82+
}
83+
84+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2013-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.fabric8.config.array_with_profiles;
18+
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
import io.fabric8.kubernetes.client.KubernetesClient;
23+
import io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient;
24+
import org.junit.jupiter.api.BeforeAll;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.test.context.TestPropertySource;
29+
import org.springframework.test.web.reactive.server.WebTestClient;
30+
31+
/**
32+
* @author wind57
33+
*/
34+
@TestPropertySource(properties = { "spring.config.import=kubernetes:" })
35+
@EnableKubernetesMockClient(crud = true, https = false)
36+
class ConfigDataMultipleYamlDocumentsNoActiveProfileTests extends ArrayWithProfiles {
37+
38+
@Autowired
39+
private WebTestClient webClient;
40+
41+
private static KubernetesClient mockClient;
42+
43+
@BeforeAll
44+
public static void setUpBeforeClass() {
45+
setUpBeforeClass(mockClient);
46+
}
47+
48+
/**
49+
* <pre>
50+
* - dev is not an active profile
51+
* - which means beans.items = [Item 10]
52+
* </pre>
53+
*/
54+
@Test
55+
void testItemsEndpoint() {
56+
this.webClient.get()
57+
.uri("/api/items")
58+
.exchange()
59+
.expectStatus()
60+
.isOk()
61+
.expectBody(List.class)
62+
.isEqualTo(List.of("Item 10"));
63+
}
64+
65+
/**
66+
* <pre>
67+
* - dev is not an active profile
68+
* - which means beans.map = {"name", "ER", "role", "user"}
69+
* </pre>
70+
*/
71+
@Test
72+
void testMapEndpoint() {
73+
this.webClient.get()
74+
.uri("/api/map")
75+
.exchange()
76+
.expectStatus()
77+
.isOk()
78+
.expectBody(Map.class)
79+
.isEqualTo(Map.of("name", "ER", "role", "user"));
80+
}
81+
82+
}

0 commit comments

Comments
 (0)