Skip to content

Commit 0fc2bb8

Browse files
authored
added some unit tests for the merging case (#1175)
1 parent c2bf76c commit 0fc2bb8

File tree

3 files changed

+57
-11
lines changed

3 files changed

+57
-11
lines changed

spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/StrippedSourceContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@
2424
* Container for some of the source's fields, it holds its labels (nullable), name and
2525
* data.
2626
*/
27-
public final record StrippedSourceContainer(Map<String, String> labels, String name, Map<String, String> data) {
27+
public record StrippedSourceContainer(Map<String, String> labels, String name, Map<String, String> data) {
2828
}

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,18 @@
1616

1717
package org.springframework.cloud.kubernetes.commons.config;
1818

19+
import java.util.LinkedHashSet;
20+
import java.util.List;
1921
import java.util.Map;
2022
import java.util.Set;
23+
import java.util.stream.Collectors;
24+
import java.util.stream.Stream;
2125

2226
import org.junit.jupiter.api.Assertions;
2327
import org.junit.jupiter.api.Test;
2428

29+
import org.springframework.mock.env.MockEnvironment;
30+
2531
/**
2632
* @author wind57
2733
*/
@@ -159,4 +165,35 @@ void testWithPrefixSortedName() {
159165
Assertions.assertEquals(result.sourceData().get("prefix.c"), "d");
160166
}
161167

168+
/**
169+
* <pre>
170+
*
171+
* - we have configmap-one with an application.yaml with two properties propA = A, prop = B
172+
* - we have configmap-one-kubernetes with an application.yaml with two properties propA = AA, probC = C
173+
*
174+
* As a result we should get three properties as output.
175+
*
176+
* </pre>
177+
*/
178+
@Test
179+
void testMerge() {
180+
181+
StrippedSourceContainer configMapOne = new StrippedSourceContainer(Map.of(), "configmap-one",
182+
Map.of("application.yaml", "propA: A\npropB: B"));
183+
184+
StrippedSourceContainer configMapOneK8s = new StrippedSourceContainer(Map.of(), "configmap-one-kubernetes",
185+
Map.of("application.yaml", "propA: AA\npropC: C"));
186+
187+
LinkedHashSet<String> sourceNames = Stream.of("configmap-one", "configmap-one-kubernetes")
188+
.collect(Collectors.toCollection(LinkedHashSet::new));
189+
190+
MultipleSourcesContainer result = ConfigUtils.processNamedData(List.of(configMapOne, configMapOneK8s),
191+
new MockEnvironment(), sourceNames, "default", false);
192+
193+
Assertions.assertEquals(result.data().size(), 3);
194+
Assertions.assertEquals(result.data().get("propA"), "AA");
195+
Assertions.assertEquals(result.data().get("propB"), "B");
196+
Assertions.assertEquals(result.data().get("propC"), "C");
197+
}
198+
162199
}

spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,27 @@ public class PropertySourceUtilsTest {
4040

4141
@Test
4242
void yamlParserGenerator_noProfile() {
43-
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
44-
final Properties properties = function.apply("spring:\n application:\n name: myTestApp\n");
43+
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
44+
Properties properties = function.apply("spring:\n application:\n name: myTestApp\n");
4545
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp");
4646
assertThat(properties.getProperty("spring.profiles")).isNull();
4747
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
4848
}
4949

50+
@Test
51+
void yamlParserGenerator_simpleProperties() {
52+
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
53+
Properties properties = function.apply("propA: A\npropB: B");
54+
assertThat(properties.getProperty("propA")).isEqualTo("A");
55+
assertThat(properties.getProperty("propB")).isEqualTo("B");
56+
assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull();
57+
}
58+
5059
@Test
5160
void yamlParserGenerator_springProfiles_matchProfile() {
5261
willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class));
53-
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
54-
final Properties properties = function.apply(
62+
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
63+
Properties properties = function.apply(
5564
"spring:\n application:\n name: myTestApp\n---\nspring:\n profiles: dummy\n application:\n name: myDummyApp");
5665
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myDummyApp");
5766
assertThat(properties.getProperty("spring.profiles")).isEqualTo("dummy");
@@ -61,8 +70,8 @@ void yamlParserGenerator_springProfiles_matchProfile() {
6170
@Test
6271
void yamlParserGenerator_springProfiles_mismatchProfile() {
6372
willReturn(Boolean.FALSE).given(environment).acceptsProfiles(any(Profiles.class));
64-
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
65-
final Properties properties = function.apply(
73+
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
74+
Properties properties = function.apply(
6675
"spring:\n application:\n name: myTestApp\n---\nspring:\n profiles: dummy\n application:\n name: myDummyApp");
6776
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp");
6877
assertThat(properties.getProperty("spring.profiles")).isNull();
@@ -72,8 +81,8 @@ void yamlParserGenerator_springProfiles_mismatchProfile() {
7281
@Test
7382
void yamlParserGenerator_springConfigActivateOnProfile_matchProfile() {
7483
willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class));
75-
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
76-
final Properties properties = function.apply(
84+
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
85+
Properties properties = function.apply(
7786
"spring:\n application:\n name: myTestApp\n---\nspring:\n config:\n activate:\n on-profile: dummy\n application:\n name: myDummyApp");
7887
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myDummyApp");
7988
assertThat(properties.getProperty("spring.profiles")).isNull();
@@ -83,8 +92,8 @@ void yamlParserGenerator_springConfigActivateOnProfile_matchProfile() {
8392
@Test
8493
void yamlParserGenerator_springConfigActivateOnProfile_mismatchProfile() {
8594
willReturn(Boolean.FALSE).given(environment).acceptsProfiles(any(Profiles.class));
86-
final Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
87-
final Properties properties = function.apply(
95+
Function<String, Properties> function = PropertySourceUtils.yamlParserGenerator(environment);
96+
Properties properties = function.apply(
8897
"spring:\n application:\n name: myTestApp\n---\nspring:\n config:\n activate:\n on-profile: dummy\n application:\n name: myDummyApp");
8998
assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp");
9099
assertThat(properties.getProperty("spring.profiles")).isNull();

0 commit comments

Comments
 (0)