diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/StrippedSourceContainer.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/StrippedSourceContainer.java index 7044b904..aab7f48e 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/StrippedSourceContainer.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/StrippedSourceContainer.java @@ -24,5 +24,5 @@ import java.util.Map; * Container for some of the source's fields, it holds its labels (nullable), name and * data. */ -public final record StrippedSourceContainer(Map labels, String name, Map data) { +public record StrippedSourceContainer(Map labels, String name, Map data) { } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java index b8c7db4f..274a99f2 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsTests.java @@ -16,12 +16,18 @@ package org.springframework.cloud.kubernetes.commons.config; +import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.springframework.mock.env.MockEnvironment; + /** * @author wind57 */ @@ -159,4 +165,35 @@ class ConfigUtilsTests { Assertions.assertEquals(result.sourceData().get("prefix.c"), "d"); } + /** + *
+	 *
+	 *     - we have configmap-one with an application.yaml with two properties propA = A, prop = B
+	 *     - we have configmap-one-kubernetes with an application.yaml with two properties propA = AA, probC = C
+	 *
+	 *     As a result we should get three properties as output.
+	 *
+	 * 
+ */ + @Test + void testMerge() { + + StrippedSourceContainer configMapOne = new StrippedSourceContainer(Map.of(), "configmap-one", + Map.of("application.yaml", "propA: A\npropB: B")); + + StrippedSourceContainer configMapOneK8s = new StrippedSourceContainer(Map.of(), "configmap-one-kubernetes", + Map.of("application.yaml", "propA: AA\npropC: C")); + + LinkedHashSet sourceNames = Stream.of("configmap-one", "configmap-one-kubernetes") + .collect(Collectors.toCollection(LinkedHashSet::new)); + + MultipleSourcesContainer result = ConfigUtils.processNamedData(List.of(configMapOne, configMapOneK8s), + new MockEnvironment(), sourceNames, "default", false); + + Assertions.assertEquals(result.data().size(), 3); + Assertions.assertEquals(result.data().get("propA"), "AA"); + Assertions.assertEquals(result.data().get("propB"), "B"); + Assertions.assertEquals(result.data().get("propC"), "C"); + } + } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java index 919543e3..c5923ea2 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/PropertySourceUtilsTest.java @@ -40,18 +40,27 @@ public class PropertySourceUtilsTest { @Test void yamlParserGenerator_noProfile() { - final Function function = PropertySourceUtils.yamlParserGenerator(environment); - final Properties properties = function.apply("spring:\n application:\n name: myTestApp\n"); + Function function = PropertySourceUtils.yamlParserGenerator(environment); + Properties properties = function.apply("spring:\n application:\n name: myTestApp\n"); assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp"); assertThat(properties.getProperty("spring.profiles")).isNull(); assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull(); } + @Test + void yamlParserGenerator_simpleProperties() { + Function function = PropertySourceUtils.yamlParserGenerator(environment); + Properties properties = function.apply("propA: A\npropB: B"); + assertThat(properties.getProperty("propA")).isEqualTo("A"); + assertThat(properties.getProperty("propB")).isEqualTo("B"); + assertThat(properties.getProperty("spring.config.activate.on-profile")).isNull(); + } + @Test void yamlParserGenerator_springProfiles_matchProfile() { willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class)); - final Function function = PropertySourceUtils.yamlParserGenerator(environment); - final Properties properties = function.apply( + Function function = PropertySourceUtils.yamlParserGenerator(environment); + Properties properties = function.apply( "spring:\n application:\n name: myTestApp\n---\nspring:\n profiles: dummy\n application:\n name: myDummyApp"); assertThat(properties.getProperty("spring.application.name")).isEqualTo("myDummyApp"); assertThat(properties.getProperty("spring.profiles")).isEqualTo("dummy"); @@ -61,8 +70,8 @@ public class PropertySourceUtilsTest { @Test void yamlParserGenerator_springProfiles_mismatchProfile() { willReturn(Boolean.FALSE).given(environment).acceptsProfiles(any(Profiles.class)); - final Function function = PropertySourceUtils.yamlParserGenerator(environment); - final Properties properties = function.apply( + Function function = PropertySourceUtils.yamlParserGenerator(environment); + Properties properties = function.apply( "spring:\n application:\n name: myTestApp\n---\nspring:\n profiles: dummy\n application:\n name: myDummyApp"); assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp"); assertThat(properties.getProperty("spring.profiles")).isNull(); @@ -72,8 +81,8 @@ public class PropertySourceUtilsTest { @Test void yamlParserGenerator_springConfigActivateOnProfile_matchProfile() { willReturn(Boolean.TRUE).given(environment).acceptsProfiles(any(Profiles.class)); - final Function function = PropertySourceUtils.yamlParserGenerator(environment); - final Properties properties = function.apply( + Function function = PropertySourceUtils.yamlParserGenerator(environment); + Properties properties = function.apply( "spring:\n application:\n name: myTestApp\n---\nspring:\n config:\n activate:\n on-profile: dummy\n application:\n name: myDummyApp"); assertThat(properties.getProperty("spring.application.name")).isEqualTo("myDummyApp"); assertThat(properties.getProperty("spring.profiles")).isNull(); @@ -83,8 +92,8 @@ public class PropertySourceUtilsTest { @Test void yamlParserGenerator_springConfigActivateOnProfile_mismatchProfile() { willReturn(Boolean.FALSE).given(environment).acceptsProfiles(any(Profiles.class)); - final Function function = PropertySourceUtils.yamlParserGenerator(environment); - final Properties properties = function.apply( + Function function = PropertySourceUtils.yamlParserGenerator(environment); + Properties properties = function.apply( "spring:\n application:\n name: myTestApp\n---\nspring:\n config:\n activate:\n on-profile: dummy\n application:\n name: myDummyApp"); assertThat(properties.getProperty("spring.application.name")).isEqualTo("myTestApp"); assertThat(properties.getProperty("spring.profiles")).isNull();