2121import java .util .Collections ;
2222import java .util .HashMap ;
2323import java .util .Iterator ;
24+ import java .util .List ;
2425import java .util .Map ;
26+ import java .util .Set ;
2527import java .util .function .BiConsumer ;
2628
29+ import com .netflix .archaius .api .ArchaiusType ;
2730import com .netflix .archaius .api .Config ;
2831import com .netflix .archaius .api .ConfigListener ;
2932import com .netflix .archaius .exceptions .ParseException ;
3538import static org .junit .jupiter .api .Assertions .assertEquals ;
3639import static org .junit .jupiter .api .Assertions .assertFalse ;
3740import static org .junit .jupiter .api .Assertions .assertThrows ;
41+ import static org .junit .jupiter .api .Assertions .assertTrue ;
3842import static org .mockito .Mockito .mock ;
3943import static org .mockito .Mockito .verify ;
4044
@@ -54,6 +58,23 @@ public class AbstractConfigTest {
5458 entries .put ("stringList" , "a,b,c" );
5559 entries .put ("uriList" , "http://example.com,http://example.org" );
5660 entries .put ("underlyingList" , Arrays .asList ("a" , "b" , "c" ));
61+ entries .put ("springYmlList[0]" , "1" );
62+ entries .put ("springYmlList[1]" , "2" );
63+ entries .put ("springYmlList[2]" , "3" );
64+ entries .put ("springYmlIntList[0]" , 1 );
65+ entries .put ("springYmlIntList[1]" , 2 );
66+ entries .put ("springYmlIntList[2]" , 3 );
67+ // Repeated entry to distinguish set and list
68+ entries .put ("springYmlList[3]" , "3" );
69+ entries .put ("springYmlMap.key1" , "1" );
70+ entries .put ("springYmlMap.key2" , "2" );
71+ entries .put ("springYmlMap.key3" , "3" );
72+ entries .put ("springYmlWithSomeInvalidList[0]" , "abc,def" );
73+ entries .put ("springYmlWithSomeInvalidList[1]" , "abc" );
74+ entries .put ("springYmlWithSomeInvalidList[2]" , "a=b" );
75+ entries .put ("springYmlWithSomeInvalidMap.key1" , "a=b" );
76+ entries .put ("springYmlWithSomeInvalidMap.key2" , "c" );
77+ entries .put ("springYmlWithSomeInvalidMap.key3" , "d,e" );
5778 }
5879
5980 @ Override
@@ -213,4 +234,59 @@ public void testListeners() {
213234 verify (listener ).onError (mockError , mockChildConfig );
214235 }
215236 }
237+
238+ @ Test
239+ public void testSpringYml () {
240+ // Working cases for set, list, and map
241+ Set <Integer > set =
242+ config .get (ArchaiusType .forSetOf (Integer .class ), "springYmlList" , Collections .singleton (1 ));
243+ assertEquals (set .size (), 3 );
244+ assertTrue (set .contains (1 ));
245+ assertTrue (set .contains (2 ));
246+ assertTrue (set .contains (3 ));
247+
248+ List <Integer > list =
249+ config .get (ArchaiusType .forListOf (Integer .class ), "springYmlList" , Arrays .asList (1 ));
250+ assertEquals (Arrays .asList (1 , 2 , 3 , 3 ), list );
251+
252+ List <Integer > intList =
253+ config .get (ArchaiusType .forListOf (Integer .class ), "springYmlIntList" , Arrays .asList (1 ));
254+ assertEquals (Arrays .asList (1 , 2 , 3 ), intList );
255+
256+ Map <String , Integer > map =
257+ config .get (ArchaiusType .forMapOf (String .class , Integer .class ),
258+ "springYmlMap" , Collections .emptyMap ());
259+ assertEquals (map .size (), 3 );
260+ assertEquals (1 , map .get ("key1" ));
261+ assertEquals (2 , map .get ("key2" ));
262+ assertEquals (3 , map .get ("key3" ));
263+
264+ // Not a proper list, so we have the default value returned
265+ List <Integer > invalidList =
266+ config .get (ArchaiusType .forListOf (Integer .class ), "springYmlMap" , Arrays .asList (1 ));
267+ assertEquals (invalidList , Arrays .asList (1 ));
268+
269+ // Not a proper set, so we have the default value returned
270+ Set <Integer > invalidSet =
271+ config .get (ArchaiusType .forSetOf (Integer .class ), "springYmlMap" , Collections .singleton (1 ));
272+ assertEquals (invalidSet , Collections .singleton (1 ));
273+
274+ // Not a proper map, so we have the default value returned
275+ Map <String , String > invalidMap =
276+ config .get (
277+ ArchaiusType .forMapOf (String .class , String .class ),
278+ "springYmlList" ,
279+ Collections .singletonMap ("default" , "default" ));
280+ assertEquals (1 , invalidMap .size ());
281+ assertEquals ("default" , invalidMap .get ("default" ));
282+ }
283+
284+ @ Test
285+ public void testSpringYamlAsNormalValue () {
286+ // Confirm that values that are intended to be read as a Spring YML Map can still be read normally
287+ // and also do not return values when read at the top level as anything other than a map.
288+ assertEquals ("1" , config .get (String .class , "springYmlMap.key1" ));
289+ assertEquals (2 , config .get (Integer .class , "springYmlMap.key2" ));
290+ assertEquals (false , config .containsKey ("springYmlMap" ));
291+ }
216292}
0 commit comments