2626import org .junit .jupiter .api .Test ;
2727
2828import org .springframework .cloud .bootstrap .config .BootstrapPropertySource ;
29+ import org .springframework .cloud .kubernetes .commons .config .ConfigMapPropertySource ;
2930import org .springframework .cloud .kubernetes .commons .config .MountConfigMapPropertySource ;
3031import org .springframework .cloud .kubernetes .commons .config .MountSecretPropertySource ;
32+ import org .springframework .cloud .kubernetes .commons .config .SecretsPropertySource ;
3133import org .springframework .cloud .kubernetes .commons .config .SourceData ;
3234import org .springframework .core .env .CompositePropertySource ;
3335import org .springframework .core .env .EnumerablePropertySource ;
@@ -125,6 +127,7 @@ void testChangedListSameSizesEqual() {
125127 }
126128
127129 @ Test
130+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
128131 void testFindPropertySources () {
129132 MockEnvironment environment = new MockEnvironment ();
130133 MutablePropertySources propertySources = environment .getPropertySources ();
@@ -141,31 +144,100 @@ public Object getProperty(String name) {
141144 return null ;
142145 }
143146 }));
144- propertySources .addFirst (new MountConfigMapPropertySource ("mounted" , Map .of ("a " , "b " )));
147+ propertySources .addFirst (new MountConfigMapPropertySource ("mounted" , Map .of ("aa " , "bb " )));
145148
146149 List <? extends PropertySource > result = ConfigReloadUtil .findPropertySources (PlainPropertySource .class ,
147150 environment );
148151
149- Assertions .assertThat (result .size ()).isEqualTo (3 );
150- Assertions .assertThat (result .get (0 ).getProperty ("a" )).isEqualTo ("b" );
151- Assertions .assertThat (result .get (1 ).getProperty ("" )).isEqualTo ("plain" );
152- Assertions .assertThat (result .get (2 ).getProperty ("" )).isEqualTo ("from-inner-two-composite" );
153-
152+ Assertions .assertThat (result .size ()).isEqualTo (2 );
153+ Assertions .assertThat (result .get (0 ).getProperty ("a" )).isEqualTo ("plain" );
154+ Assertions .assertThat (result .get (1 ).getProperty ("" )).isEqualTo ("from-inner-two-composite" );
154155 }
155156
157+ /**
158+ * <pre>
159+ * - in environment we have one MountSecretPropertySource
160+ * - we search for a type that extends SecretPropertySource (SecretsTypePropertySource)
161+ *
162+ * - we pick up MountSecretPropertySource
163+ * </pre>
164+ */
156165 @ Test
166+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
157167 void testSecretsPropertySource () {
158168 MockEnvironment environment = new MockEnvironment ();
159169 MutablePropertySources propertySources = environment .getPropertySources ();
160170 propertySources .addFirst (new MountSecretPropertySource (new SourceData ("secret" , Map .of ("a" , "b" ))));
161171
172+ List <? extends PropertySource > result = ConfigReloadUtil .findPropertySources (SecretsTypePropertySource .class ,
173+ environment );
174+ assertThat (result .size ()).isEqualTo (1 );
175+ assertThat (result .get (0 ).getProperty ("a" )).isEqualTo ("b" );
176+ }
177+
178+ /**
179+ * <pre>
180+ * - in environment we have one MountSecretPropertySource
181+ * - we search for a type that does not extend SecretPropertySource (PlainPropertySource)
182+ *
183+ * - we don't pick up MountSecretPropertySource
184+ * </pre>
185+ */
186+ @ Test
187+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
188+ void testSecretsPropertySourceNotTaken () {
189+ MockEnvironment environment = new MockEnvironment ();
190+ MutablePropertySources propertySources = environment .getPropertySources ();
191+ propertySources .addFirst (new MountSecretPropertySource (new SourceData ("secret" , Map .of ("a" , "b" ))));
192+
162193 List <? extends PropertySource > result = ConfigReloadUtil .findPropertySources (PlainPropertySource .class ,
163194 environment );
195+ assertThat (result .size ()).isEqualTo (0 );
196+ }
197+
198+ /**
199+ * <pre>
200+ * - in environment we have one MountConfigMapPropertySource
201+ * - we search for a type that extends SecretPropertySource (ConfigMapTypePropertySource)
202+ *
203+ * - we pick up MountConfigMapPropertySource
204+ * </pre>
205+ */
206+ @ Test
207+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
208+ void testConfigMapPropertySource () {
209+ MockEnvironment environment = new MockEnvironment ();
210+ MutablePropertySources propertySources = environment .getPropertySources ();
211+ propertySources .addFirst (new MountConfigMapPropertySource ("secret" , Map .of ("a" , "b" )));
212+
213+ List <? extends PropertySource > result = ConfigReloadUtil .findPropertySources (ConfigMapTypePropertySource .class ,
214+ environment );
164215 assertThat (result .size ()).isEqualTo (1 );
165216 assertThat (result .get (0 ).getProperty ("a" )).isEqualTo ("b" );
166217 }
167218
219+ /**
220+ * <pre>
221+ * - in environment we have one MountConfigMapPropertySource
222+ * - we search for a type that does not extend ConfigMapPropertySource (PlainPropertySource)
223+ *
224+ * - we don't pick up MountConfigMapPropertySource
225+ * </pre>
226+ */
168227 @ Test
228+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
229+ void testConfigMapPropertySourceNotTaken () {
230+ MockEnvironment environment = new MockEnvironment ();
231+ MutablePropertySources propertySources = environment .getPropertySources ();
232+ propertySources .addFirst (new MountSecretPropertySource (new SourceData ("secret" , Map .of ("a" , "b" ))));
233+
234+ List <? extends PropertySource > result = ConfigReloadUtil .findPropertySources (PlainPropertySource .class ,
235+ environment );
236+ assertThat (result .size ()).isEqualTo (0 );
237+ }
238+
239+ @ Test
240+ @ SuppressWarnings ({ "rawtypes" , "deprecation" })
169241 void testBootstrapSecretsPropertySource () {
170242 MockEnvironment environment = new MockEnvironment ();
171243 MutablePropertySources propertySources = environment .getPropertySources ();
@@ -217,6 +289,29 @@ public Object getProperty(String name) {
217289
218290 }
219291
292+ /**
293+ * simulates Fabric8SecretsPropertySource or KubernetesClientSecretsPropertySource.
294+ */
295+ private static final class SecretsTypePropertySource <T > extends SecretsPropertySource {
296+
297+ SecretsTypePropertySource (SourceData sourceData ) {
298+ super (sourceData );
299+ }
300+
301+ }
302+
303+ /**
304+ * simulates Fabric8ConfigMapPropertySource or
305+ * KubernetesClientConfigMapPropertySource.
306+ */
307+ private static final class ConfigMapTypePropertySource <T > extends ConfigMapPropertySource {
308+
309+ ConfigMapTypePropertySource (String name , Map <String , Object > source ) {
310+ super (name , source );
311+ }
312+
313+ }
314+
220315 private static final class OneBootstrap <T > extends BootstrapPropertySource <T > {
221316
222317 private final EnumerablePropertySource <T > delegate ;
0 commit comments