diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java index 05a826c9af..de9bcf8e24 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtils.java @@ -20,6 +20,7 @@ import java.util.Arrays; import java.util.Base64; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; @@ -157,21 +158,6 @@ public static void onException(boolean failFast, Exception e) { LOG.warn(e.getMessage() + ". Ignoring.", e); } - /* - * This method will return a SourceData that has a name in the form : - * "configmap.my-configmap.my-configmap-2.namespace" and the "data" from the context - * is appended with prefix. So if incoming is "a=b", the result will be : "prefix.a=b" - */ - @Deprecated(forRemoval = true) - public static SourceData withPrefix(String target, PrefixContext context) { - Map withPrefix = CollectionUtils.newHashMap(context.data().size()); - context.data().forEach((key, value) -> withPrefix.put(context.prefix() + "." + key, value)); - - String propertySourceTokens = String.join(PROPERTY_SOURCE_NAME_SEPARATOR, - context.propertySourceNames().stream().sorted().collect(Collectors.toCollection(LinkedHashSet::new))); - return new SourceData(sourceName(target, propertySourceTokens, context.namespace()), withPrefix); - } - public static String sourceName(String target, String applicationName, String namespace) { return target + PROPERTY_SOURCE_NAME_SEPARATOR + applicationName + PROPERTY_SOURCE_NAME_SEPARATOR + namespace; } @@ -201,8 +187,7 @@ public static MultipleSourcesContainer processNamedData(List hashByName = strippedSources.stream() .collect(Collectors.toMap(StrippedSourceContainer::name, Function.identity())); - LinkedHashSet foundSourceNames = new LinkedHashSet<>(); - Map data = new HashMap<>(); + LinkedHashMap> data = new LinkedHashMap<>(); // This is an ordered stream, and it means that non-profile-based sources will be // processed before profile-based sources. @@ -212,7 +197,6 @@ public static MultipleSourcesContainer processNamedData(List rawData = stripped.data(); if (decode) { @@ -237,7 +221,7 @@ public static MultipleSourcesContainer processNamedData(List activeProf .anyMatch(activeProfile -> ENDS_WITH_PROFILE_AND_EXTENSION.test(keyName, activeProfile))); } - static String sourceDataName(String target, LinkedHashSet sourceNames, String namespace) { + static String sourceDataName(String target, Set sourceNames, String namespace) { String sortedNames = sourceNames.stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); return sourceName(target, sortedNames, namespace); } @@ -318,13 +302,11 @@ public static MultipleSourcesContainer processLabeledData(List sourceNames = new LinkedHashSet<>(); - Map data = new HashMap<>(); + LinkedHashMap> data = new LinkedHashMap<>(); all.forEach(source -> { String foundSourceName = source.name(); LOG.debug("Loaded source with name : '" + foundSourceName + " in namespace: '" + namespace + "'"); - sourceNames.add(foundSourceName); Map rawData = source.data(); if (decode) { @@ -335,7 +317,7 @@ public static MultipleSourcesContainer processLabeledData(List decodeData(Map data) { diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java index 5a18a096dc..1ce77840d6 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/LabeledSourceData.java @@ -17,7 +17,7 @@ package org.springframework.cloud.kubernetes.commons.config; import java.util.Arrays; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; import java.util.stream.Collectors; @@ -57,26 +57,24 @@ public final SourceData compute(Map labels, Prefix prefix, Strin profiles = Arrays.stream(activeProfiles).collect(Collectors.toSet()); } data = dataSupplier(labels, profiles); + LinkedHashMap> sourceData = data.data(); + sourceDataName = sourceDataName(target, sourceData.keySet(), namespace); - LinkedHashSet sourceNames = data.names(); - Map sourceDataForSourceName = data.data(); - sourceDataName = sourceDataName(target, sourceNames, namespace); - - if (sourceNames.isEmpty()) { + if (sourceData.isEmpty()) { return emptySourceData(labels, target, namespace); } if (prefix.getName().equals(Prefix.DEFAULT.getName())) { - return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceNames, sourceDataForSourceName)); + return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceData)); } if (prefix.getName().equals(Prefix.KNOWN.getName())) { return new SourceData(sourceDataName, - prefixFlattenedSourceData(sourceNames, sourceDataForSourceName, prefix.prefixProvider().get())); + prefixFlattenedSourceData(sourceData, prefix.prefixProvider().get())); } if (prefix.getName().equals(Prefix.DELAYED.getName())) { - return new SourceData(sourceDataName, nameFlattenedSourceData(sourceNames, sourceDataForSourceName)); + return new SourceData(sourceDataName, nameFlattenedSourceData(sourceData)); } throw new IllegalArgumentException("Unsupported prefix: " + prefix); @@ -84,7 +82,8 @@ public final SourceData compute(Map labels, Prefix prefix, Strin catch (Exception e) { LOG.warn("Failure in reading labeled sources"); onException(failFast, e); - return new SourceData(sourceDataName(target, data.names(), namespace), Map.of(ERROR_PROPERTY, "true")); + return new SourceData(sourceDataName(target, data.data().keySet(), namespace), + Map.of(ERROR_PROPERTY, "true")); } } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java index 892171f758..86a7cc63f7 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/MultipleSourcesContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2022 the original author or authors. + * Copyright 2013-2025 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. @@ -16,7 +16,7 @@ package org.springframework.cloud.kubernetes.commons.config; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -25,10 +25,9 @@ * Container that stores multiple sources, to be exact their names and their flattenned * data. We force a LinkedHashSet on purpose, to preserve the order of sources. */ -public record MultipleSourcesContainer(LinkedHashSet names, Map data) { +public record MultipleSourcesContainer(LinkedHashMap> data) { - private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashSet<>(0), - Map.of()); + private static final MultipleSourcesContainer EMPTY = new MultipleSourcesContainer(new LinkedHashMap<>(0)); public static MultipleSourcesContainer empty() { return EMPTY; diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java index f87d078336..94b16cd065 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/NamedSourceData.java @@ -16,8 +16,10 @@ package org.springframework.cloud.kubernetes.commons.config; +import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.Set; import java.util.stream.Collectors; import org.apache.commons.logging.Log; @@ -60,31 +62,29 @@ public final SourceData compute(String sourceName, Prefix prefix, String target, } data = dataSupplier(sourceNamesToSearchFor); - Map sourceDataForSourceName = data.data(); - LinkedHashSet sourceNamesFound = data.names(); - String sortedNames = data.names() - .stream() + LinkedHashMap> sourceData = data.data(); + + Set sourceNamesFound = sourceData.keySet(); + String sortedNames = sourceNamesFound.stream() .sorted() .collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); sourceDataName = generateSourceName(target, sortedNames, namespace, activeProfiles); - if (data.names().isEmpty()) { + if (sourceData.isEmpty()) { return emptySourceData(target, sourceName, namespace); } if (prefix.getName().equals(Prefix.DEFAULT.getName())) { - return new SourceData(sourceDataName, - defaultFlattenedSourceData(sourceNamesFound, sourceDataForSourceName)); + return new SourceData(sourceDataName, defaultFlattenedSourceData(sourceData)); } if (prefix.getName().equals(Prefix.KNOWN.getName())) { - return new SourceData(sourceDataName, prefixFlattenedSourceData(sourceNamesFound, - sourceDataForSourceName, prefix.prefixProvider().get())); + return new SourceData(sourceDataName, + prefixFlattenedSourceData(sourceData, prefix.prefixProvider().get())); } if (prefix.getName().equals(Prefix.DELAYED.getName())) { - return new SourceData(sourceDataName, - nameFlattenedSourceData(sourceNamesFound, sourceDataForSourceName)); + return new SourceData(sourceDataName, nameFlattenedSourceData(sourceData)); } throw new IllegalArgumentException("Unsupported prefix: " + prefix); @@ -93,7 +93,11 @@ public final SourceData compute(String sourceName, Prefix prefix, String target, catch (Exception e) { LOG.warn("Failure in reading named sources"); onException(failFast, e); - String names = data.names().stream().sorted().collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); + String names = data.data() + .keySet() + .stream() + .sorted() + .collect(Collectors.joining(PROPERTY_SOURCE_NAME_SEPARATOR)); return new SourceData(generateSourceName(target, names, namespace, activeProfiles), Map.of(ERROR_PROPERTY, "true")); } diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java deleted file mode 100644 index 720998e34a..0000000000 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/PrefixContext.java +++ /dev/null @@ -1,32 +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; - -/** - * A holder for data needed to compute prefix based properties, in case of a secret or - * config map. - * - * @author wind57 - * @deprecated will be deleted in a future release. - */ -@Deprecated(forRemoval = true) -public record PrefixContext(Map data, String prefix, String namespace, - Set propertySourceNames) { -} diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java index 2f22310750..c63386a993 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattener.java @@ -17,7 +17,7 @@ package org.springframework.cloud.kubernetes.commons.config; import java.util.HashMap; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.Map; /** @@ -32,31 +32,19 @@ private SourceDataFlattener() { /** * Flattens the data from rawData without any additional processing. */ - static Map defaultFlattenedSourceData(LinkedHashSet names, Map rawData) { + static Map defaultFlattenedSourceData(LinkedHashMap> sourceData) { Map flattenedData = new HashMap<>(); - - names.forEach(name -> { - @SuppressWarnings("unchecked") - Map singleDataEntry = (Map) rawData.getOrDefault(name, Map.of()); - flattenedData.putAll(singleDataEntry); - }); - + sourceData.values().forEach(flattenedData::putAll); return flattenedData; } /** * Flattens the data from rawData by adding a prefix for each key. */ - static Map prefixFlattenedSourceData(LinkedHashSet names, Map rawData, + static Map prefixFlattenedSourceData(LinkedHashMap> sourceData, String prefix) { Map flattenedData = new HashMap<>(); - - names.forEach(name -> { - @SuppressWarnings("unchecked") - Map singleDataEntry = (Map) rawData.getOrDefault(name, Map.of()); - singleDataEntry.forEach((key, value) -> flattenedData.put(prefix + "." + key, value)); - }); - + sourceData.values().forEach(data -> data.forEach((key, value) -> flattenedData.put(prefix + "." + key, value))); return flattenedData; } @@ -64,15 +52,9 @@ static Map prefixFlattenedSourceData(LinkedHashSet names * Flattens the data from rawData by adding a prefix for each key, which is equal to * the source name. */ - static Map nameFlattenedSourceData(LinkedHashSet names, Map rawData) { + static Map nameFlattenedSourceData(LinkedHashMap> sourceData) { Map flattenedData = new HashMap<>(); - - names.forEach(name -> { - @SuppressWarnings("unchecked") - Map singleDataEntry = (Map) rawData.getOrDefault(name, Map.of()); - singleDataEntry.forEach((key, value) -> flattenedData.put(name + "." + key, value)); - }); - + sourceData.forEach((name, data) -> data.forEach((key, value) -> flattenedData.put(name + "." + key, value))); return flattenedData; } diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java index ec18f1ca03..23ef551cfc 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/ConfigUtilsProcessSourceTests.java @@ -80,10 +80,9 @@ void testProcessNamedDataOne() { MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames, namespace, decode, includeDefaultProfileData); Assertions.assertThat(result).isNotNull(); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("configmap-a"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("configmap-a"); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("configmap-a"); + Map data = result.data().get("configmap-a"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1")); } @@ -162,7 +161,7 @@ void testProcessNamedDataTwo(CapturedOutput output) { MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames, namespace, decode, includeDefaultProfileData); Assertions.assertThat(result).isNotNull(); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account"); /** *
@@ -176,8 +175,7 @@ void testProcessNamedDataTwo(CapturedOutput output) {
 		 *			since 'k8s' is not an active profile.
 		 * 
*/ - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("account"); + Map data = result.data().get("account"); Assertions.assertThat(data) .containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "three", "3", "five", "5")); Assertions.assertThat(output.getOut()).contains("entry : account-k8s.properties will be skipped"); @@ -258,7 +256,7 @@ void testProcessNamedDataThree(CapturedOutput output) { MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames, namespace, decode, includeDefaultProfileData); Assertions.assertThat(result).isNotNull(); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account-default"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account-default"); /** *
@@ -274,8 +272,7 @@ void testProcessNamedDataThree(CapturedOutput output) {
 		 *		6. we do not have 'four=4' since we do not read 'account-k8s.properties'
 		 * 
*/ - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("account-default"); + Map data = result.data().get("account-default"); Assertions.assertThat(data) .containsExactlyInAnyOrderEntriesOf(Map.of("one", "1", "two", "2", "three", "3", "five", "5")); Assertions.assertThat(output.getOut()).contains("entry : account-k8s.properties will be skipped"); @@ -373,14 +370,13 @@ void testProcessNamedDataFive(CapturedOutput output) { MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames, namespace, decode, includeDefaultProfileData); Assertions.assertThat(result).isNotNull(); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account"); /* *
 - we only read from 'account-k8s.properties' 
*/ - @SuppressWarnings("unchecked") - Map accountData = (Map) result.data().get("account"); + Map accountData = result.data().get("account"); Assertions.assertThat(accountData).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "four", "4")); Assertions.assertThat(output.getOut()).contains("entry : account.properties will be skipped"); Assertions.assertThat(output.getOut()).contains("entry : account-default.properties will be skipped"); @@ -462,7 +458,7 @@ void testProcessNamedDataSix(CapturedOutput output) { MultipleSourcesContainer result = ConfigUtils.processNamedData(strippedSources, environment, sourceNames, namespace, decode, includeDefaultProfileData); Assertions.assertThat(result).isNotNull(); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("account-k8s"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("account-k8s"); /** *
@@ -481,8 +477,7 @@ void testProcessNamedDataSix(CapturedOutput output) {
 		 *			(because 'prod' is not an active profile)
 		 * 
*/ - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("account-k8s"); + Map data = result.data().get("account-k8s"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1111", "five", "5")); Assertions.assertThat(output.getOut()).contains("entry : account-prod.properties will be skipped"); Assertions.assertThat(output.getOut()).contains("entry : account.properties will be skipped"); 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 6a40e4f0cc..a0a831c3fb 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 @@ -166,13 +166,11 @@ void testMerge() { new MockEnvironment(), sourceNames, "default", false); Assertions.assertThat(result.data().size()).isEqualTo(2); - @SuppressWarnings("unchecked") - Map one = (Map) result.data().get("configmap-one"); + Map one = result.data().get("configmap-one"); Assertions.assertThat(one.get("propA")).isEqualTo("A"); Assertions.assertThat(one.get("propB")).isEqualTo("B"); - @SuppressWarnings("unchecked") - Map oneKubernetes = (Map) result.data().get("configmap-one-kubernetes"); + Map oneKubernetes = result.data().get("configmap-one-kubernetes"); Assertions.assertThat(oneKubernetes.get("propA")).isEqualTo("AA"); Assertions.assertThat(oneKubernetes.get("propC")).isEqualTo("C"); @@ -205,7 +203,6 @@ void testKeysWithPrefixNonEmptyPrefix() { } @Test - @SuppressWarnings("unchecked") void testIssue1757() { StrippedSourceContainer containerA = new StrippedSourceContainer(Map.of("load", "true"), "client-1", @@ -217,14 +214,13 @@ void testIssue1757() { MultipleSourcesContainer container = ConfigUtils.processLabeledData(List.of(containerA, containerB), new MockEnvironment(), Map.of("load", "true"), "default", Set.of(), false); - System.out.println(container); - assertThat(container.names()).containsExactlyInAnyOrder("client-1", "client-2"); + assertThat(container.data().keySet()).containsExactlyInAnyOrder("client-1", "client-2"); - Map client1Data = (Map) container.data().get("client-1"); + Map client1Data = container.data().get("client-1"); assertThat(client1Data) .containsExactlyInAnyOrderEntriesOf(Map.of("client-id", "clientA", "client-secret", "a")); - Map client2Data = (Map) container.data().get("client-2"); + Map client2Data = container.data().get("client-2"); assertThat(client2Data) .containsExactlyInAnyOrderEntriesOf(Map.of("client-id", "clientB", "client-secret", "b")); diff --git a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java index f03d274373..3fa1a78f23 100644 --- a/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java +++ b/spring-cloud-kubernetes-commons/src/test/java/org/springframework/cloud/kubernetes/commons/config/SourceDataFlattenerTests.java @@ -16,8 +16,7 @@ package org.springframework.cloud.kubernetes.commons.config; -import java.util.HashMap; -import java.util.LinkedHashSet; +import java.util.LinkedHashMap; import java.util.Map; import org.assertj.core.api.Assertions; @@ -36,15 +35,11 @@ class SourceDataFlattenerTests { */ @Test void defaultFlattenedSourceDataNoOverlap() { - LinkedHashSet names = new LinkedHashSet<>(); - names.add("nameA"); - names.add("nameB"); + LinkedHashMap> data = new LinkedHashMap<>(); + data.put("nameA", Map.of("a", "b")); + data.put("nameB", Map.of("c", "d")); - Map rawData = new HashMap<>(); - rawData.put("nameA", Map.of("a", "b")); - rawData.put("nameB", Map.of("c", "d")); - - Map result = SourceDataFlattener.defaultFlattenedSourceData(names, rawData); + Map result = SourceDataFlattener.defaultFlattenedSourceData(data); Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("a", "b", "c", "d")); } @@ -58,17 +53,12 @@ void defaultFlattenedSourceDataNoOverlap() { */ @Test void defaultFlattenedSourceDataOverlap() { - LinkedHashSet names = new LinkedHashSet<>(); - names.add("nameA"); - names.add("nameB"); - names.add("nameC"); - - Map rawData = new HashMap<>(); - rawData.put("nameA", Map.of("a", "b")); - rawData.put("nameB", Map.of("c", "d")); - rawData.put("nameC", Map.of("a", "w")); + LinkedHashMap> data = new LinkedHashMap<>(); + data.put("nameA", Map.of("a", "b")); + data.put("nameB", Map.of("c", "d")); + data.put("nameC", Map.of("a", "w")); - Map result = SourceDataFlattener.defaultFlattenedSourceData(names, rawData); + Map result = SourceDataFlattener.defaultFlattenedSourceData(data); Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("a", "w", "c", "d")); } @@ -82,15 +72,11 @@ void defaultFlattenedSourceDataOverlap() { */ @Test void prefixFlattenedSourceDataNoOverlap() { - LinkedHashSet names = new LinkedHashSet<>(); - names.add("nameA"); - names.add("nameB"); - - Map rawData = new HashMap<>(); - rawData.put("nameA", Map.of("a", "b")); - rawData.put("nameB", Map.of("c", "d")); + LinkedHashMap> data = new LinkedHashMap<>(); + data.put("nameA", Map.of("a", "b")); + data.put("nameB", Map.of("c", "d")); - Map result = SourceDataFlattener.prefixFlattenedSourceData(names, rawData, "one"); + Map result = SourceDataFlattener.prefixFlattenedSourceData(data, "one"); Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("one.a", "b", "one.c", "d")); } @@ -105,17 +91,12 @@ void prefixFlattenedSourceDataNoOverlap() { */ @Test void prefixFlattenedSourceDataOverlap() { - LinkedHashSet names = new LinkedHashSet<>(); - names.add("nameA"); - names.add("nameB"); - names.add("nameC"); + LinkedHashMap> data = new LinkedHashMap<>(); + data.put("nameA", Map.of("a", "b")); + data.put("nameB", Map.of("c", "d")); + data.put("nameC", Map.of("a", "w")); - Map rawData = new HashMap<>(); - rawData.put("nameA", Map.of("a", "b")); - rawData.put("nameB", Map.of("c", "d")); - rawData.put("nameC", Map.of("a", "w")); - - Map result = SourceDataFlattener.prefixFlattenedSourceData(names, rawData, "one"); + Map result = SourceDataFlattener.prefixFlattenedSourceData(data, "one"); Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("one.a", "w", "one.c", "d")); } @@ -128,15 +109,11 @@ void prefixFlattenedSourceDataOverlap() { */ @Test void nameFlattenedSourceDataNoOverlap() { - LinkedHashSet names = new LinkedHashSet<>(); - names.add("nameA"); - names.add("nameB"); - - Map rawData = new HashMap<>(); - rawData.put("nameA", Map.of("a", "b")); - rawData.put("nameB", Map.of("c", "d")); + LinkedHashMap> data = new LinkedHashMap<>(); + data.put("nameA", Map.of("a", "b")); + data.put("nameB", Map.of("c", "d")); - Map result = SourceDataFlattener.nameFlattenedSourceData(names, rawData); + Map result = SourceDataFlattener.nameFlattenedSourceData(data); Assertions.assertThat(result).containsExactlyInAnyOrderEntriesOf(Map.of("nameA.a", "b", "nameB.c", "d")); } @@ -150,17 +127,12 @@ void nameFlattenedSourceDataNoOverlap() { */ @Test void nameFlattenedSourceDataOverlap() { - LinkedHashSet names = new LinkedHashSet<>(); - names.add("nameA"); - names.add("nameB"); - names.add("nameC"); - - Map rawData = new HashMap<>(); - rawData.put("nameA", Map.of("a", "b")); - rawData.put("nameB", Map.of("c", "d")); - rawData.put("nameC", Map.of("a", "w")); + LinkedHashMap> data = new LinkedHashMap<>(); + data.put("nameA", Map.of("a", "b")); + data.put("nameB", Map.of("c", "d")); + data.put("nameC", Map.of("a", "w")); - Map result = SourceDataFlattener.nameFlattenedSourceData(names, rawData); + Map result = SourceDataFlattener.nameFlattenedSourceData(data); Assertions.assertThat(result) .containsExactlyInAnyOrderEntriesOf(Map.of("nameA.a", "b", "nameB.c", "d", "nameC.a", "w")); diff --git a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java index 4be4ed2ea3..a5e88ff978 100644 --- a/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java +++ b/spring-cloud-kubernetes-fabric8-config/src/test/java/org/springframework/cloud/kubernetes/fabric8/config/Fabric8ConfigUtilsTests.java @@ -63,7 +63,7 @@ void testSecretDataByLabelsSecretNotFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("color", "red"), new MockEnvironment(), Set.of()); Assertions.assertThat(result.data()).isEmpty(); - Assertions.assertThat(result.names()).isEmpty(); + Assertions.assertThat(result.data().keySet()).isEmpty(); } // secret "my-secret" is deployed with label {color:pink}; we search for it by same @@ -80,10 +80,9 @@ void testSecretDataByLabelsSecretFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("color", "pink"), new MockEnvironment(), Set.of()); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-secret"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-secret"); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-secret"); + Map data = result.data().get("my-secret"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); } @@ -102,10 +101,9 @@ void testSecretDataByLabelsSecretFoundWithPropertyFile() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("color", "pink"), new MockEnvironment(), Set.of()); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-secret"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-secret"); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-secret"); + Map data = result.data().get("my-secret"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1")); } @@ -132,15 +130,13 @@ void testSecretDataByLabelsTwoSecretsFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("color", "pink"), new MockEnvironment(), Set.of()); - Assertions.assertThat(result.names()).contains("my-secret"); - Assertions.assertThat(result.names()).contains("my-secret-2"); + Assertions.assertThat(result.data().keySet()).contains("my-secret"); + Assertions.assertThat(result.data().keySet()).contains("my-secret-2"); - @SuppressWarnings("unchecked") - Map mySecretData = (Map) result.data().get("my-secret"); + Map mySecretData = result.data().get("my-secret"); Assertions.assertThat(mySecretData).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); - @SuppressWarnings("unchecked") - Map mySecret2Data = (Map) result.data().get("my-secret-2"); + Map mySecret2Data = result.data().get("my-secret-2"); Assertions.assertThat(mySecret2Data).containsExactlyInAnyOrderEntriesOf(Map.of("property-2", "value-2")); } @@ -202,20 +198,17 @@ void testSecretDataByLabelsThreeSecretsFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByLabels(client, "spring-k8s", Map.of("tag", "fit", "color", "blue"), new MockEnvironment(), Set.of("k8s")); - Assertions.assertThat(result.names()).contains("blue-circle-secret"); - Assertions.assertThat(result.names()).contains("blue-square-secret"); - Assertions.assertThat(result.names()).contains("blue-square-secret-k8s"); + Assertions.assertThat(result.data().keySet()).contains("blue-circle-secret"); + Assertions.assertThat(result.data().keySet()).contains("blue-square-secret"); + Assertions.assertThat(result.data().keySet()).contains("blue-square-secret-k8s"); - @SuppressWarnings("unchecked") - Map dataBlueSecret = (Map) result.data().get("blue-circle-secret"); + Map dataBlueSecret = result.data().get("blue-circle-secret"); Assertions.assertThat(dataBlueSecret).containsExactlyInAnyOrderEntriesOf(Map.of("one", "1")); - @SuppressWarnings("unchecked") - Map dataSquareSecret = (Map) result.data().get("blue-square-secret"); + Map dataSquareSecret = result.data().get("blue-square-secret"); Assertions.assertThat(dataSquareSecret).containsExactlyInAnyOrderEntriesOf(Map.of("two", "2")); - @SuppressWarnings("unchecked") - Map dataSquareSecretK8s = (Map) result.data().get("blue-square-secret-k8s"); + Map dataSquareSecretK8s = result.data().get("blue-square-secret-k8s"); Assertions.assertThat(dataSquareSecretK8s).containsExactlyInAnyOrderEntriesOf(Map.of("four", "4")); } @@ -231,7 +224,7 @@ void testSecretDataByNameSecretNotFound() { names.add("nope"); MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).isEmpty(); + Assertions.assertThat(result.data()).isEmpty(); Assertions.assertThat(result.data()).isEmpty(); } @@ -249,10 +242,9 @@ void testSecretDataByNameSecretFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names().size()).isEqualTo(1); + Assertions.assertThat(result.data().size()).isEqualTo(1); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-secret"); + Map data = result.data().get("my-secret"); Assertions.assertThat(data.get("property")).isEqualTo("value"); } @@ -279,17 +271,15 @@ void testSecretDataByNameTwoSecretsFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.secretsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).contains("my-secret"); - Assertions.assertThat(result.names()).contains("my-secret-2"); + Assertions.assertThat(result.data().keySet()).contains("my-secret"); + Assertions.assertThat(result.data().keySet()).contains("my-secret-2"); Assertions.assertThat(result.data().size()).isEqualTo(2); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-secret"); + Map data = result.data().get("my-secret"); Assertions.assertThat(data.get("property")).isEqualTo("value"); - @SuppressWarnings("unchecked") - Map data2 = (Map) result.data().get("my-secret-2"); + Map data2 = result.data().get("my-secret-2"); Assertions.assertThat(data2.get("property-2")).isEqualTo("value-2"); } @@ -307,10 +297,9 @@ void testConfigMapsDataByNameFoundNoData() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-config-map"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map"); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-config-map"); + Map data = result.data().get("my-config-map"); Assertions.assertThat(data).isEmpty(); } @@ -326,7 +315,7 @@ void testConfigMapsDataByNameNotFound() { names.add("my-config-map-not-found"); MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).isEmpty(); + Assertions.assertThat(result.data().keySet()).isEmpty(); Assertions.assertThat(result.data()).isEmpty(); } @@ -345,10 +334,9 @@ void testConfigMapDataByNameFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-config-map"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map"); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-config-map"); + Map data = result.data().get("my-config-map"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); } @@ -368,10 +356,9 @@ void testConfigMapDataByNameFoundWithPropertyFile() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).containsExactlyInAnyOrder("my-config-map"); + Assertions.assertThat(result.data().keySet()).containsExactlyInAnyOrder("my-config-map"); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-config-map"); + Map data = result.data().get("my-config-map"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("key1", "value1")); } @@ -399,17 +386,15 @@ void testConfigMapDataByNameTwoFound() { MultipleSourcesContainer result = Fabric8ConfigUtils.configMapsDataByName(client, "spring-k8s", names, new MockEnvironment()); - Assertions.assertThat(result.names()).contains("my-config-map"); - Assertions.assertThat(result.names()).contains("my-config-map-2"); + Assertions.assertThat(result.data().keySet()).contains("my-config-map"); + Assertions.assertThat(result.data().keySet()).contains("my-config-map-2"); Assertions.assertThat(result.data().size()).isEqualTo(2); - @SuppressWarnings("unchecked") - Map data = (Map) result.data().get("my-config-map"); + Map data = result.data().get("my-config-map"); Assertions.assertThat(data).containsExactlyInAnyOrderEntriesOf(Map.of("property", "value")); - @SuppressWarnings("unchecked") - Map data2 = (Map) result.data().get("my-config-map-2"); + Map data2 = result.data().get("my-config-map-2"); Assertions.assertThat(data2).containsExactlyInAnyOrderEntriesOf(Map.of("property-2", "value-2")); }