data) {
String leaderKey = getLeaderKey();
String leaderId = data.get(leaderKey);
- if (leaderId == null) {
+ if (!StringUtils.hasText(leaderId)) {
return null;
}
diff --git a/spring-cloud-kubernetes-commons/src/main/resources/META-INF/spring.factories b/spring-cloud-kubernetes-commons/src/main/resources/META-INF/spring.factories
index ddd9639ef8..c9658215cf 100644
--- a/spring-cloud-kubernetes-commons/src/main/resources/META-INF/spring.factories
+++ b/spring-cloud-kubernetes-commons/src/main/resources/META-INF/spring.factories
@@ -3,3 +3,7 @@ org.springframework.cloud.kubernetes.commons.KubernetesCommonsAutoConfiguration,
org.springframework.cloud.kubernetes.commons.config.reload.ConfigReloadAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.kubernetes.commons.config.KubernetesBootstrapConfiguration
+
+# ConfigData Loaders
+org.springframework.boot.context.config.ConfigDataLoader=\
+org.springframework.cloud.kubernetes.commons.config.KubernetesConfigDataLoader
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java
index 9da75c0357..b0d100fdec 100644
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapConfigPropertiesTests.java
@@ -18,15 +18,20 @@
import java.util.Arrays;
import java.util.Collections;
+import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
+import java.util.Set;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
+import org.springframework.mock.env.MockEnvironment;
+
/**
* @author wind57
*/
-public class ConfigMapConfigPropertiesTests {
+class ConfigMapConfigPropertiesTests {
/**
*
@@ -41,16 +46,16 @@ public class ConfigMapConfigPropertiesTests {
* a config as above will result in a NormalizedSource where prefix is empty
*/
@Test
- public void testUseNameAsPrefixUnsetEmptySources() {
+ void testUseNameAsPrefixUnsetEmptySources() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setSources(Collections.emptyList());
properties.setName("config-map-a");
properties.setNamespace("spring-k8s");
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), "",
+ Assertions.assertSame(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), ConfigUtils.Prefix.DEFAULT,
"empty sources must generate a List with a single NormalizedSource, where prefix is empty");
}
@@ -69,17 +74,17 @@ public void testUseNameAsPrefixUnsetEmptySources() {
* "useNameAsPrefix: true", because sources are empty
*/
@Test
- public void testUseNameAsPrefixSetEmptySources() {
+ void testUseNameAsPrefixSetEmptySources() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setSources(Collections.emptyList());
properties.setUseNameAsPrefix(true);
properties.setName("config-map-a");
properties.setNamespace("spring-k8s");
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), "",
+ Assertions.assertSame(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), ConfigUtils.Prefix.DEFAULT,
"empty sources must generate a List with a single NormalizedSource, where prefix is empty,"
+ "no matter of 'spring.cloud.kubernetes.config.useNameAsPrefix' value");
}
@@ -100,7 +105,7 @@ public void testUseNameAsPrefixSetEmptySources() {
* the config map name
*/
@Test
- public void testUseNameAsPrefixUnsetNonEmptySources() {
+ void testUseNameAsPrefixUnsetNonEmptySources() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setUseNameAsPrefix(true);
properties.setNamespace("spring-k8s");
@@ -109,10 +114,11 @@ public void testUseNameAsPrefixUnsetNonEmptySources() {
one.setName("config-map-one");
properties.setSources(Collections.singletonList(one));
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 1, "a single NormalizedSource is expected");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), "config-map-one");
+ Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix().prefixProvider().get(),
+ "config-map-one");
}
/**
@@ -137,7 +143,7 @@ public void testUseNameAsPrefixUnsetNonEmptySources() {
* 'spring.cloud.kubernetes.config.useNameAsPrefix' will be taken.
*/
@Test
- public void testUseNameAsPrefixSetNonEmptySources() {
+ void testUseNameAsPrefixSetNonEmptySources() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setUseNameAsPrefix(true);
properties.setNamespace("spring-k8s");
@@ -155,12 +161,14 @@ public void testUseNameAsPrefixSetNonEmptySources() {
properties.setSources(Arrays.asList(one, two, three));
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 3, "3 NormalizedSources are expected");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), "");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(1)).prefix(), "config-map-two");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(2)).prefix(), "config-map-three");
+ Assertions.assertSame(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), ConfigUtils.Prefix.DEFAULT);
+ Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(1)).prefix().prefixProvider().get(),
+ "config-map-two");
+ Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(2)).prefix().prefixProvider().get(),
+ "config-map-three");
}
/**
@@ -185,7 +193,7 @@ public void testUseNameAsPrefixSetNonEmptySources() {
*
*/
@Test
- public void testMultipleCases() {
+ void testMultipleCases() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setUseNameAsPrefix(false);
properties.setNamespace("spring-k8s");
@@ -209,13 +217,16 @@ public void testMultipleCases() {
properties.setSources(Arrays.asList(one, two, three, four));
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 4, "4 NormalizedSources are expected");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix(), "one");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(1)).prefix(), "two");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(2)).prefix(), "three");
- Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(3)).prefix(), "");
+ Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(0)).prefix().prefixProvider().get(),
+ "one");
+ Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(1)).prefix().prefixProvider().get(),
+ "two");
+ Assertions.assertEquals(((NamedConfigMapNormalizedSource) sources.get(2)).prefix().prefixProvider().get(),
+ "three");
+ Assertions.assertSame(((NamedConfigMapNormalizedSource) sources.get(3)).prefix(), ConfigUtils.Prefix.DEFAULT);
}
/**
@@ -233,13 +244,13 @@ public void testMultipleCases() {
* added is not a breaking change for the already existing functionality)
*/
@Test
- public void testUseIncludeProfileSpecificSourcesNoChanges() {
+ void testUseIncludeProfileSpecificSourcesNoChanges() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setSources(Collections.emptyList());
properties.setName("config-map-a");
properties.setNamespace("spring-k8s");
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource");
Assertions.assertTrue(((NamedConfigMapNormalizedSource) sources.get(0)).profileSpecificSources());
@@ -263,14 +274,14 @@ public void testUseIncludeProfileSpecificSourcesNoChanges() {
* and must be propagated to the normalized source.
*/
@Test
- public void testUseIncludeProfileSpecificSourcesDefaultChanged() {
+ void testUseIncludeProfileSpecificSourcesDefaultChanged() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setSources(Collections.emptyList());
properties.setName("config-map-a");
properties.setNamespace("spring-k8s");
properties.setIncludeProfileSpecificSources(false);
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource");
Assertions.assertFalse(((NamedConfigMapNormalizedSource) sources.get(0)).profileSpecificSources());
@@ -300,7 +311,7 @@ public void testUseIncludeProfileSpecificSourcesDefaultChanged() {
*
*/
@Test
- public void testUseIncludeProfileSpecificSourcesDefaultChangedSourceOverride() {
+ void testUseIncludeProfileSpecificSourcesDefaultChangedSourceOverride() {
ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
properties.setSources(Collections.emptyList());
properties.setName("config-map-a");
@@ -320,7 +331,7 @@ public void testUseIncludeProfileSpecificSourcesDefaultChangedSourceOverride() {
properties.setSources(Arrays.asList(one, two, three));
- List sources = properties.determineSources();
+ List sources = properties.determineSources(new MockEnvironment());
Assertions.assertEquals(sources.size(), 3);
Assertions.assertTrue(((NamedConfigMapNormalizedSource) sources.get(0)).profileSpecificSources());
@@ -328,4 +339,89 @@ public void testUseIncludeProfileSpecificSourcesDefaultChangedSourceOverride() {
Assertions.assertFalse(((NamedConfigMapNormalizedSource) sources.get(2)).profileSpecificSources());
}
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * config:
+ * useNameAsPrefix: false
+ * namespace: spring-k8s
+ * includeProfileSpecificSources: false
+ * sources:
+ * - labels:
+ * - name: first-label
+ * value: configmap-one
+ * useNameAsPrefix: false
+ * explicitPrefix: one
+ * - labels:
+ * - name: second-label
+ * value: configmap-two
+ * includeProfileSpecificSources: true
+ * useNameAsPrefix: true
+ * explicitPrefix: two
+ * - labels:
+ * - name: third-label
+ * value: configmap-three
+ * explicitPrefix: three
+ * - labels:
+ * - name: fourth-label
+ * value: configmap-four
+ *
+ *
+ */
+ @Test
+ void testLabelsMultipleCases() {
+ ConfigMapConfigProperties properties = new ConfigMapConfigProperties();
+ properties.setUseNameAsPrefix(false);
+ properties.setNamespace("spring-k8s");
+ properties.setIncludeProfileSpecificSources(false);
+
+ ConfigMapConfigProperties.Source one = new ConfigMapConfigProperties.Source();
+ one.setLabels(Map.of("first-label", "configmap-one"));
+ one.setUseNameAsPrefix(false);
+ one.setExplicitPrefix("one");
+
+ ConfigMapConfigProperties.Source two = new ConfigMapConfigProperties.Source();
+ two.setLabels(Map.of("second-label", "configmap-two"));
+ two.setUseNameAsPrefix(true);
+ two.setExplicitPrefix("two");
+ two.setIncludeProfileSpecificSources(true);
+
+ ConfigMapConfigProperties.Source three = new ConfigMapConfigProperties.Source();
+ three.setLabels(Map.of("third-label", "configmap-three"));
+ three.setExplicitPrefix("three");
+
+ ConfigMapConfigProperties.Source four = new ConfigMapConfigProperties.Source();
+ four.setLabels(Map.of("fourth-label", "configmap-four"));
+
+ properties.setSources(Arrays.asList(one, two, three, four));
+
+ List sources = properties.determineSources(new MockEnvironment());
+ // we get 8 property sources, since "named" ones with "application" are
+ // duplicated.
+ // that's OK, since later in the code we get a LinkedHashSet out of them all,
+ // so they become 5 only.
+ Assertions.assertEquals(sources.size(), 8, "4 NormalizedSources are expected");
+
+ LabeledConfigMapNormalizedSource labeled1 = (LabeledConfigMapNormalizedSource) sources.get(1);
+ Assertions.assertEquals(labeled1.prefix().prefixProvider().get(), "one");
+ Assertions.assertFalse(labeled1.profileSpecificSources());
+
+ LabeledConfigMapNormalizedSource labeled3 = (LabeledConfigMapNormalizedSource) sources.get(3);
+ Assertions.assertEquals(labeled3.prefix().prefixProvider().get(), "two");
+ Assertions.assertTrue(labeled3.profileSpecificSources());
+
+ LabeledConfigMapNormalizedSource labeled5 = (LabeledConfigMapNormalizedSource) sources.get(5);
+ Assertions.assertEquals(labeled5.prefix().prefixProvider().get(), "three");
+ Assertions.assertFalse(labeled5.profileSpecificSources());
+
+ LabeledConfigMapNormalizedSource labeled7 = (LabeledConfigMapNormalizedSource) sources.get(7);
+ Assertions.assertSame(labeled7.prefix(), ConfigUtils.Prefix.DEFAULT);
+ Assertions.assertFalse(labeled7.profileSpecificSources());
+
+ Set set = new LinkedHashSet<>(sources);
+ Assertions.assertEquals(5, set.size());
+ }
+
}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceTests.java
deleted file mode 100644
index 979feb80f7..0000000000
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigMapPropertySourceTests.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Copyright 2013-2022 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.commons.config;
-
-import java.util.Map;
-import java.util.Set;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-/**
- * @author wind57
- */
-class ConfigMapPropertySourceTests {
-
- @Test
- void testWithPrefix() {
- ConfigMapPrefixContext context = new ConfigMapPrefixContext(Map.of("a", "b", "c", "d"), "prefix", "namespace",
- Set.of("name1", "name2"));
-
- SourceData result = ConfigMapPropertySource.withPrefix(context);
-
- Assertions.assertEquals(result.sourceName().length(), 31);
- Assertions.assertTrue(result.sourceName().contains("name2"));
- Assertions.assertTrue(result.sourceName().contains("name1"));
- Assertions.assertTrue(result.sourceName().contains("configmap"));
- Assertions.assertTrue(result.sourceName().contains("namespace"));
-
- Assertions.assertEquals(result.sourceData().get("prefix.a"), "b");
- Assertions.assertEquals(result.sourceData().get("prefix.c"), "d");
- }
-
-}
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 5bed6bfe92..b8c7db4f5a 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,42 +16,68 @@
package org.springframework.cloud.kubernetes.commons.config;
+import java.util.Map;
+import java.util.Set;
+
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
/**
* @author wind57
*/
-public class ConfigUtilsTests {
+class ConfigUtilsTests {
+
+ @Test
+ void testExplicitPrefixSet() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix("explicitPrefix", null, false, "irrelevant");
+ Assertions.assertSame(result, ConfigUtils.Prefix.KNOWN);
+ Assertions.assertEquals(result.prefixProvider().get(), "explicitPrefix");
+ }
@Test
- public void testExplicitPrefixSet() {
- String result = ConfigUtils.findPrefix("explicitPrefix", null, false, "irrelevant");
- Assertions.assertEquals(result, "explicitPrefix");
+ void testUseNameAsPrefixTrue() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix("", Boolean.TRUE, false, "name-to-use");
+ Assertions.assertSame(result, ConfigUtils.Prefix.KNOWN);
+ Assertions.assertEquals(result.prefixProvider().get(), "name-to-use");
}
@Test
- public void testUseNameAsPrefixTrue() {
- String result = ConfigUtils.findPrefix("", Boolean.TRUE, false, "name-to-use");
- Assertions.assertEquals(result, "name-to-use");
+ void testUseNameAsPrefixFalse() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix("", Boolean.FALSE, false, "name-not-to-use");
+ Assertions.assertSame(result, ConfigUtils.Prefix.DEFAULT);
}
@Test
- public void testUseNameAsPrefixFalse() {
- String result = ConfigUtils.findPrefix("", Boolean.FALSE, false, "name-not-to-use");
- Assertions.assertEquals(result, "");
+ void testDefaultUseNameAsPrefixTrue() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix("", null, true, "name-to-use");
+ Assertions.assertSame(result, ConfigUtils.Prefix.KNOWN);
+ Assertions.assertEquals(result.prefixProvider().get(), "name-to-use");
}
@Test
- public void testDefaultUseNameAsPrefixTrue() {
- String result = ConfigUtils.findPrefix("", null, true, "name-to-use");
- Assertions.assertEquals(result, "name-to-use");
+ void testNoMatch() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix("", null, false, "name-not-to-use");
+ Assertions.assertSame(result, ConfigUtils.Prefix.DEFAULT);
}
@Test
- public void testNoMatch() {
- String result = ConfigUtils.findPrefix("", null, false, "name-not-to-use");
- Assertions.assertEquals(result, "");
+ void testUnsetEmpty() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix("", null, false, "name-not-to-use");
+ Assertions.assertSame(result, ConfigUtils.Prefix.DEFAULT);
+
+ String expected = Assertions.assertDoesNotThrow(() -> result.prefixProvider().get());
+ Assertions.assertEquals("", expected);
+ }
+
+ @Test
+ void testDelayed() {
+ ConfigUtils.Prefix result = ConfigUtils.findPrefix(null, true, false, null);
+ Assertions.assertSame(result, ConfigUtils.Prefix.DELAYED);
+
+ IllegalArgumentException ex = Assertions.assertThrows(IllegalArgumentException.class,
+ () -> result.prefixProvider().get());
+
+ Assertions.assertEquals("prefix is delayed, needs to be taken elsewhere", ex.getMessage());
}
/**
@@ -66,7 +92,7 @@ public void testNoMatch() {
* above will generate "true" for a normalized source
*/
@Test
- public void testUseIncludeProfileSpecificSourcesOnlyDefaultSet() {
+ void testUseIncludeProfileSpecificSourcesOnlyDefaultSet() {
Assertions.assertTrue(ConfigUtils.includeProfileSpecificSources(true, null));
}
@@ -82,7 +108,7 @@ public void testUseIncludeProfileSpecificSourcesOnlyDefaultSet() {
* above will generate "false" for a normalized source
*/
@Test
- public void testUseIncludeProfileSpecificSourcesOnlyDefaultNotSet() {
+ void testUseIncludeProfileSpecificSourcesOnlyDefaultNotSet() {
Assertions.assertFalse(ConfigUtils.includeProfileSpecificSources(false, null));
}
@@ -101,8 +127,36 @@ public void testUseIncludeProfileSpecificSourcesOnlyDefaultNotSet() {
* above will generate "false" for a normalized source
*/
@Test
- public void testUseIncludeProfileSpecificSourcesSourcesOverridesDefault() {
+ void testUseIncludeProfileSpecificSourcesSourcesOverridesDefault() {
Assertions.assertFalse(ConfigUtils.includeProfileSpecificSources(true, false));
}
+ @Test
+ void testWithPrefix() {
+ PrefixContext context = new PrefixContext(Map.of("a", "b", "c", "d"), "prefix", "namespace",
+ Set.of("name1", "name2"));
+
+ SourceData result = ConfigUtils.withPrefix("configmap", context);
+
+ Assertions.assertEquals(result.sourceName(), "configmap.name1.name2.namespace");
+
+ Assertions.assertEquals(result.sourceData().get("prefix.a"), "b");
+ Assertions.assertEquals(result.sourceData().get("prefix.c"), "d");
+ }
+
+ /*
+ * source names should be reproducible all the time, this test asserts this.
+ */
+ @Test
+ void testWithPrefixSortedName() {
+ PrefixContext context = new PrefixContext(Map.of("a", "b", "c", "d"), "prefix", "namespace",
+ Set.of("namec", "namea", "nameb"));
+
+ SourceData result = ConfigUtils.withPrefix("configmap", context);
+ Assertions.assertEquals(result.sourceName(), "configmap.namea.nameb.namec.namespace");
+
+ Assertions.assertEquals(result.sourceData().get("prefix.a"), "b");
+ Assertions.assertEquals(result.sourceData().get("prefix.c"), "d");
+ }
+
}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledConfigMapNormalizedSourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledConfigMapNormalizedSourceTests.java
new file mode 100644
index 0000000000..068716e090
--- /dev/null
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledConfigMapNormalizedSourceTests.java
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2013-2022 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.commons.config;
+
+import java.util.Map;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * @author wind57
+ */
+class LabeledConfigMapNormalizedSourceTests {
+
+ private static final ConfigUtils.Prefix PREFIX = ConfigUtils.findPrefix("prefix", false, false, "prefix");
+
+ @Test
+ void testEqualsAndHashCode() {
+ LabeledConfigMapNormalizedSource left = new LabeledConfigMapNormalizedSource("name", Map.of("key", "value"),
+ false, false);
+ LabeledConfigMapNormalizedSource right = new LabeledConfigMapNormalizedSource("name", Map.of("key", "value"),
+ true, false);
+
+ Assertions.assertEquals(left.hashCode(), right.hashCode());
+ Assertions.assertEquals(left, right);
+ }
+
+ @Test
+ void testType() {
+ LabeledConfigMapNormalizedSource source = new LabeledConfigMapNormalizedSource("name", Map.of("key", "value"),
+ false, false);
+ Assertions.assertSame(source.type(), NormalizedSourceType.LABELED_CONFIG_MAP);
+ }
+
+ @Test
+ void testTarget() {
+ LabeledConfigMapNormalizedSource source = new LabeledConfigMapNormalizedSource("name", Map.of("key", "value"),
+ false, false);
+ Assertions.assertEquals(source.target(), "configmap");
+ }
+
+ @Test
+ void testConstructorFields() {
+ LabeledConfigMapNormalizedSource source = new LabeledConfigMapNormalizedSource("namespace",
+ Map.of("key", "value"), false, PREFIX, true);
+ Assertions.assertEquals(source.labels(), Map.of("key", "value"));
+ Assertions.assertEquals(source.namespace().get(), "namespace");
+ Assertions.assertFalse(source.failFast());
+ Assertions.assertSame(PREFIX, source.prefix());
+ Assertions.assertTrue(source.profileSpecificSources());
+ }
+
+ @Test
+ void testConstructorWithoutPrefixFields() {
+ LabeledConfigMapNormalizedSource source = new LabeledConfigMapNormalizedSource("namespace",
+ Map.of("key", "value"), true, true);
+ Assertions.assertEquals(source.labels(), Map.of("key", "value"));
+ Assertions.assertEquals(source.namespace().get(), "namespace");
+ Assertions.assertTrue(source.failFast());
+ Assertions.assertSame(ConfigUtils.Prefix.DEFAULT, source.prefix());
+ Assertions.assertTrue(source.profileSpecificSources());
+ }
+
+}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledSecretNormalizedSourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledSecretNormalizedSourceTests.java
index ac0d15659c..9f0774f35a 100644
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledSecretNormalizedSourceTests.java
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/LabeledSecretNormalizedSourceTests.java
@@ -31,8 +31,27 @@ class LabeledSecretNormalizedSourceTests {
@Test
void testEqualsAndHashCode() {
- LabeledSecretNormalizedSource left = new LabeledSecretNormalizedSource("namespace", labels, false);
- LabeledSecretNormalizedSource right = new LabeledSecretNormalizedSource("namespace", labels, true);
+ LabeledSecretNormalizedSource left = new LabeledSecretNormalizedSource("namespace", labels, false, false);
+ LabeledSecretNormalizedSource right = new LabeledSecretNormalizedSource("namespace", labels, true, false);
+
+ Assertions.assertEquals(left.hashCode(), right.hashCode());
+ Assertions.assertEquals(left, right);
+ }
+
+ /*
+ * left and right instance have a different prefix, but the hashCode is the same; as
+ * it is not taken into consideration when computing hashCode
+ */
+ @Test
+ void testEqualsAndHashCodePrefixDoesNotMatter() {
+
+ ConfigUtils.Prefix knownLeft = ConfigUtils.findPrefix("left", false, false, "some");
+ ConfigUtils.Prefix knownRight = ConfigUtils.findPrefix("right", false, false, "some");
+
+ LabeledSecretNormalizedSource left = new LabeledSecretNormalizedSource("namespace", labels, true, knownLeft,
+ false);
+ LabeledSecretNormalizedSource right = new LabeledSecretNormalizedSource("namespace", labels, true, knownRight,
+ false);
Assertions.assertEquals(left.hashCode(), right.hashCode());
Assertions.assertEquals(left, right);
@@ -40,28 +59,40 @@ void testEqualsAndHashCode() {
@Test
void testType() {
- LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false);
+ LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false, false);
Assertions.assertSame(source.type(), NormalizedSourceType.LABELED_SECRET);
}
@Test
void testImmutableGetLabels() {
- LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false);
+ LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false, false);
Assertions.assertThrows(RuntimeException.class, () -> source.labels().put("c", "d"));
}
@Test
void testTarget() {
- LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false);
- Assertions.assertEquals(source.target(), "Secret");
+ LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false, false);
+ Assertions.assertEquals(source.target(), "secret");
}
@Test
void testConstructorFields() {
- LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false);
+ ConfigUtils.Prefix prefix = ConfigUtils.findPrefix("prefix", false, false, "some");
+ LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, false, prefix,
+ true);
Assertions.assertTrue(source.name().isEmpty());
Assertions.assertEquals(source.namespace().get(), "namespace");
Assertions.assertFalse(source.failFast());
+ Assertions.assertTrue(source.profileSpecificSources());
+ }
+
+ @Test
+ void testConstructorWithoutPrefixFields() {
+ LabeledSecretNormalizedSource source = new LabeledSecretNormalizedSource("namespace", labels, true, true);
+ Assertions.assertEquals(source.namespace().get(), "namespace");
+ Assertions.assertTrue(source.failFast());
+ Assertions.assertSame(ConfigUtils.Prefix.DEFAULT, source.prefix());
+ Assertions.assertTrue(source.profileSpecificSources());
}
}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSourceTests.java
index 35d5d8cb29..0cb9bb05cd 100644
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSourceTests.java
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedConfigMapNormalizedSourceTests.java
@@ -24,12 +24,18 @@
*/
class NamedConfigMapNormalizedSourceTests {
+ private static final ConfigUtils.Prefix PREFIX = ConfigUtils.findPrefix("prefix", false, false, "prefix");
+
@Test
void testEqualsAndHashCode() {
- NamedConfigMapNormalizedSource left = new NamedConfigMapNormalizedSource("name", "namespace", false, "prefix",
+
+ ConfigUtils.Prefix knownLeft = ConfigUtils.findPrefix("prefix", false, false, "some");
+ ConfigUtils.Prefix knownRight = ConfigUtils.findPrefix("prefix", false, false, "non-equal-prefix");
+
+ NamedConfigMapNormalizedSource left = new NamedConfigMapNormalizedSource("name", "namespace", false, knownLeft,
true);
- NamedConfigMapNormalizedSource right = new NamedConfigMapNormalizedSource("name", "namespace", true,
- "non-equal-prefix", false);
+ NamedConfigMapNormalizedSource right = new NamedConfigMapNormalizedSource("name", "namespace", true, knownRight,
+ false);
Assertions.assertEquals(left.hashCode(), right.hashCode());
Assertions.assertEquals(left, right);
@@ -37,21 +43,22 @@ void testEqualsAndHashCode() {
@Test
void testType() {
- NamedConfigMapNormalizedSource one = new NamedConfigMapNormalizedSource("name", "namespace", false, "prefix",
+
+ NamedConfigMapNormalizedSource one = new NamedConfigMapNormalizedSource("name", "namespace", false, PREFIX,
true);
Assertions.assertSame(one.type(), NormalizedSourceType.NAMED_CONFIG_MAP);
}
@Test
void testTarget() {
- NamedConfigMapNormalizedSource one = new NamedConfigMapNormalizedSource("name", "namespace", false, "prefix",
+ NamedConfigMapNormalizedSource one = new NamedConfigMapNormalizedSource("name", "namespace", false, PREFIX,
true);
- Assertions.assertEquals(one.target(), "Config Map");
+ Assertions.assertEquals(one.target(), "configmap");
}
@Test
void testConstructorFields() {
- NamedConfigMapNormalizedSource one = new NamedConfigMapNormalizedSource("name", "namespace", false, "prefix",
+ NamedConfigMapNormalizedSource one = new NamedConfigMapNormalizedSource("name", "namespace", false, PREFIX,
true);
Assertions.assertEquals(one.name().get(), "name");
Assertions.assertEquals(one.namespace().get(), "namespace");
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSourceTests.java
index 3a3968676e..9739d387be 100644
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSourceTests.java
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/NamedSecretNormalizedSourceTests.java
@@ -24,10 +24,12 @@
*/
class NamedSecretNormalizedSourceTests {
+ private static final ConfigUtils.Prefix PREFIX = ConfigUtils.findPrefix("prefix", false, false, "prefix");
+
@Test
void testEqualsAndHashCode() {
- NamedSecretNormalizedSource left = new NamedSecretNormalizedSource("name", "namespace", false);
- NamedSecretNormalizedSource right = new NamedSecretNormalizedSource("name", "namespace", true);
+ NamedSecretNormalizedSource left = new NamedSecretNormalizedSource("name", "namespace", false, false);
+ NamedSecretNormalizedSource right = new NamedSecretNormalizedSource("name", "namespace", true, false);
Assertions.assertEquals(left.hashCode(), right.hashCode());
Assertions.assertEquals(left, right);
@@ -35,22 +37,34 @@ void testEqualsAndHashCode() {
@Test
void testType() {
- NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", false);
+ NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", false, false);
Assertions.assertSame(source.type(), NormalizedSourceType.NAMED_SECRET);
}
@Test
void testTarget() {
- NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", false);
- Assertions.assertEquals(source.target(), "Secret");
+ NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", false, false);
+ Assertions.assertEquals(source.target(), "secret");
}
@Test
void testConstructorFields() {
- NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", false);
+ NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", false, PREFIX, true);
Assertions.assertEquals(source.name().get(), "name");
Assertions.assertEquals(source.namespace().get(), "namespace");
Assertions.assertFalse(source.failFast());
+ Assertions.assertSame(PREFIX, source.prefix());
+ Assertions.assertTrue(source.profileSpecificSources());
+ }
+
+ @Test
+ void testConstructorWithoutPrefixFields() {
+ NamedSecretNormalizedSource source = new NamedSecretNormalizedSource("name", "namespace", true, true);
+ Assertions.assertEquals(source.name().get(), "name");
+ Assertions.assertEquals(source.namespace().get(), "namespace");
+ Assertions.assertTrue(source.failFast());
+ Assertions.assertSame(ConfigUtils.Prefix.DEFAULT, source.prefix());
+ Assertions.assertTrue(source.profileSpecificSources());
}
}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java
index 626d6a0e64..ef689907e7 100644
--- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SecretsConfigPropertiesTests.java
@@ -21,6 +21,7 @@
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Assertions;
@@ -109,7 +110,284 @@ void multipleSources() {
NormalizedSource fiveResult = iterator.next();
Assertions.assertEquals(((LabeledSecretNormalizedSource) fiveResult).labels(),
Collections.singletonMap("three", "3"));
+ }
+
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * secrets:
+ * name: secret-a
+ * namespace: spring-k8s
+ *
+ *
+ * a config as above will result in a NormalizedSource where prefix is empty
+ */
+ @Test
+ void testUseNameAsPrefixUnsetEmptySources() {
+ SecretsConfigProperties properties = new SecretsConfigProperties();
+ properties.setSources(Collections.emptyList());
+ properties.setName("secret-a");
+ properties.setNamespace("spring-k8s");
+
+ List sources = properties.determineSources(new MockEnvironment());
+ Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource");
+
+ Assertions.assertSame(((NamedSecretNormalizedSource) sources.get(0)).prefix(), ConfigUtils.Prefix.DEFAULT);
+ }
+
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * secrets:
+ * useNameAsPrefix: true
+ * name: secret-a
+ * namespace: spring-k8s
+ *
+ *
+ * a config as above will result in a NormalizedSource where prefix is empty, even if
+ * "useNameAsPrefix: true", because sources are empty
+ */
+ @Test
+ void testUseNameAsPrefixSetEmptySources() {
+ SecretsConfigProperties properties = new SecretsConfigProperties();
+ properties.setSources(Collections.emptyList());
+ properties.setUseNameAsPrefix(true);
+ properties.setName("secret-a");
+ properties.setNamespace("spring-k8s");
+
+ List sources = properties.determineSources(new MockEnvironment());
+ Assertions.assertEquals(sources.size(), 1, "empty sources must generate a List with a single NormalizedSource");
+
+ Assertions.assertSame(((NamedSecretNormalizedSource) sources.get(0)).prefix(), ConfigUtils.Prefix.DEFAULT,
+ "empty sources must generate a List with a single NormalizedSource, where prefix is unset,"
+ + "no matter of 'spring.cloud.kubernetes.secret.useNameAsPrefix' value");
+ }
+
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * secrets:
+ * useNameAsPrefix: true
+ * namespace: spring-k8s
+ * sources:
+ * - name: secret-one
+ *
+ *
+ * a config as above will result in a NormalizedSource where prefix will be equal to
+ * the secret name
+ */
+ @Test
+ void testUseNameAsPrefixUnsetNonEmptySources() {
+ SecretsConfigProperties properties = new SecretsConfigProperties();
+ properties.setUseNameAsPrefix(true);
+ properties.setNamespace("spring-k8s");
+
+ SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
+ one.setName("secret-one");
+ properties.setSources(Collections.singletonList(one));
+
+ List sources = properties.determineSources(new MockEnvironment());
+ Assertions.assertEquals(sources.size(), 1, "a single NormalizedSource is expected");
+
+ Assertions.assertEquals(((NamedSecretNormalizedSource) sources.get(0)).prefix().prefixProvider().get(),
+ "secret-one");
+ }
+
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * secrets:
+ * useNameAsPrefix: true
+ * namespace: spring-k8s
+ * sources:
+ * - name: secret-one
+ * useNameAsPrefix: false
+ * - name: secret-two
+ * useNameAsPrefix: true
+ * - name: secret-three
+ *
+ *
+ * this test proves that 'spring.cloud.kubernetes.secrets.sources[].useNameAsPrefix'
+ * will override 'spring.cloud.kubernetes.secrets.useNameAsPrefix'. For the last entry
+ * in sources, since there is no explicit 'useNameAsPrefix', the one from
+ * 'spring.cloud.kubernetes.secrets.useNameAsPrefix' will be taken.
+ */
+ @Test
+ void testUseNameAsPrefixSetNonEmptySources() {
+ SecretsConfigProperties properties = new SecretsConfigProperties();
+ properties.setUseNameAsPrefix(true);
+ properties.setNamespace("spring-k8s");
+
+ SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
+ one.setName("secret-one");
+ one.setUseNameAsPrefix(false);
+
+ SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
+ two.setName("secret-two");
+ two.setUseNameAsPrefix(true);
+
+ SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
+ three.setName("secret-three");
+
+ properties.setSources(Arrays.asList(one, two, three));
+
+ List sources = properties.determineSources(new MockEnvironment());
+ Assertions.assertEquals(sources.size(), 3, "3 NormalizedSources are expected");
+
+ Assertions.assertSame(((NamedSecretNormalizedSource) sources.get(0)).prefix(), ConfigUtils.Prefix.DEFAULT);
+ Assertions.assertEquals(((NamedSecretNormalizedSource) sources.get(1)).prefix().prefixProvider().get(),
+ "secret-two");
+ Assertions.assertEquals(((NamedSecretNormalizedSource) sources.get(2)).prefix().prefixProvider().get(),
+ "secret-three");
+ }
+
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * secrets:
+ * useNameAsPrefix: false
+ * namespace: spring-k8s
+ * sources:
+ * - name: secret-one
+ * useNameAsPrefix: false
+ * explicitPrefix: one
+ * - name: secret-two
+ * useNameAsPrefix: true
+ * explicitPrefix: two
+ * - name: secret-three
+ * explicitPrefix: three
+ * - name: secret-four
+ *
+ *
+ */
+ @Test
+ void testMultipleCases() {
+ SecretsConfigProperties properties = new SecretsConfigProperties();
+ properties.setUseNameAsPrefix(false);
+ properties.setNamespace("spring-k8s");
+
+ SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
+ one.setName("secret-one");
+ one.setUseNameAsPrefix(false);
+ one.setExplicitPrefix("one");
+
+ SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
+ two.setName("secret-two");
+ two.setUseNameAsPrefix(true);
+ two.setExplicitPrefix("two");
+
+ SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
+ three.setName("secret-three");
+ three.setExplicitPrefix("three");
+
+ SecretsConfigProperties.Source four = new SecretsConfigProperties.Source();
+ four.setName("secret-four");
+
+ properties.setSources(Arrays.asList(one, two, three, four));
+
+ List sources = properties.determineSources(new MockEnvironment());
+ Assertions.assertEquals(sources.size(), 4, "4 NormalizedSources are expected");
+
+ Assertions.assertEquals(((NamedSecretNormalizedSource) sources.get(0)).prefix().prefixProvider().get(), "one");
+ Assertions.assertEquals(((NamedSecretNormalizedSource) sources.get(1)).prefix().prefixProvider().get(), "two");
+ Assertions.assertEquals(((NamedSecretNormalizedSource) sources.get(2)).prefix().prefixProvider().get(),
+ "three");
+ Assertions.assertSame(((NamedSecretNormalizedSource) sources.get(3)).prefix(), ConfigUtils.Prefix.DEFAULT);
+ }
+
+ /**
+ *
+ * spring:
+ * cloud:
+ * kubernetes:
+ * secrets:
+ * useNameAsPrefix: false
+ * namespace: spring-k8s
+ * includeProfileSpecificSources: false
+ * sources:
+ * - labels:
+ * - name: first-label
+ * value: secret-one
+ * useNameAsPrefix: false
+ * explicitPrefix: one
+ * - labels:
+ * - name: second-label
+ * value: secret-two
+ * includeProfileSpecificSources: true
+ * useNameAsPrefix: true
+ * explicitPrefix: two
+ * - labels:
+ * - name: third-label
+ * value: secret-three
+ * explicitPrefix: three
+ * - labels:
+ * - name: fourth-label
+ * value: secret-four
+ *
+ *
+ */
+ @Test
+ void testLabelsMultipleCases() {
+ SecretsConfigProperties properties = new SecretsConfigProperties();
+ properties.setUseNameAsPrefix(false);
+ properties.setNamespace("spring-k8s");
+ properties.setIncludeProfileSpecificSources(false);
+
+ SecretsConfigProperties.Source one = new SecretsConfigProperties.Source();
+ one.setLabels(Map.of("first-label", "secret-one"));
+ one.setUseNameAsPrefix(false);
+ one.setExplicitPrefix("one");
+
+ SecretsConfigProperties.Source two = new SecretsConfigProperties.Source();
+ two.setLabels(Map.of("second-label", "secret-two"));
+ two.setUseNameAsPrefix(true);
+ two.setExplicitPrefix("two");
+ two.setIncludeProfileSpecificSources(true);
+
+ SecretsConfigProperties.Source three = new SecretsConfigProperties.Source();
+ three.setLabels(Map.of("third-label", "secret-three"));
+ three.setExplicitPrefix("three");
+
+ SecretsConfigProperties.Source four = new SecretsConfigProperties.Source();
+ four.setLabels(Map.of("fourth-label", "secret-four"));
+
+ properties.setSources(Arrays.asList(one, two, three, four));
+
+ List sources = properties.determineSources(new MockEnvironment());
+ // we get 8 property sources, since "named" ones with "application" are
+ // duplicated.
+ // that's OK, since later in the code we get a LinkedHashSet out of them all,
+ // so they become 5 only.
+ Assertions.assertEquals(sources.size(), 8, "4 NormalizedSources are expected");
+
+ LabeledSecretNormalizedSource labeled1 = (LabeledSecretNormalizedSource) sources.get(1);
+ Assertions.assertEquals(labeled1.prefix().prefixProvider().get(), "one");
+ Assertions.assertFalse(labeled1.profileSpecificSources());
+
+ LabeledSecretNormalizedSource labeled3 = (LabeledSecretNormalizedSource) sources.get(3);
+ Assertions.assertEquals(labeled3.prefix().prefixProvider().get(), "two");
+ Assertions.assertTrue(labeled3.profileSpecificSources());
+
+ LabeledSecretNormalizedSource labeled5 = (LabeledSecretNormalizedSource) sources.get(5);
+ Assertions.assertEquals(labeled5.prefix().prefixProvider().get(), "three");
+ Assertions.assertFalse(labeled5.profileSpecificSources());
+
+ LabeledSecretNormalizedSource labeled7 = (LabeledSecretNormalizedSource) sources.get(7);
+ Assertions.assertSame(labeled7.prefix(), ConfigUtils.Prefix.DEFAULT);
+ Assertions.assertFalse(labeled7.profileSpecificSources());
+ Set set = new LinkedHashSet<>(sources);
+ Assertions.assertEquals(5, set.size());
}
}
diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessorTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessorTests.java
new file mode 100644
index 0000000000..98647e09af
--- /dev/null
+++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataEntriesProcessorTests.java
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2013-2022 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.commons.config;
+
+import java.util.Map;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import org.springframework.mock.env.MockEnvironment;
+
+/**
+ * @author wind57
+ */
+class SourceDataEntriesProcessorTests {
+
+ @Test
+ void testSingleYml() {
+
+ Map result = SourceDataEntriesProcessor.processAllEntries(Map.of("one.yml", "key: \n value"),
+ new MockEnvironment());
+
+ Assertions.assertEquals(1, result.size());
+ Assertions.assertEquals("value", result.get("key"));
+ }
+
+ @Test
+ void testSingleYaml() {
+
+ Map result = SourceDataEntriesProcessor.processAllEntries(Map.of("one.yaml", "key: \n value"),
+ new MockEnvironment());
+
+ Assertions.assertEquals(1, result.size());
+ Assertions.assertEquals("value", result.get("key"));
+ }
+
+ @Test
+ void testSingleProperties() {
+
+ Map result = SourceDataEntriesProcessor.processAllEntries(Map.of("one.properties", "key=value"),
+ new MockEnvironment());
+
+ Assertions.assertEquals(1, result.size());
+ Assertions.assertEquals("value", result.get("key"));
+ }
+
+ /**
+ *
+ * two properties present, none are file treated
+ *
+ */
+ @Test
+ void twoEntriesNoneFileTreated() {
+
+ Map.Entry one = Map.entry("one", "1");
+ Map.Entry two = Map.entry("two", "2");
+ Map map = Map.ofEntries(one, two);
+
+ Map result = SourceDataEntriesProcessor.processAllEntries(map, new MockEnvironment());
+
+ Assertions.assertEquals(2, result.size());
+ Assertions.assertEquals("1", result.get("one"));
+ Assertions.assertEquals("2", result.get("two"));
+ }
+
+ /**
+ *