2727import io .kubernetes .client .openapi .models .V1Secret ;
2828import io .kubernetes .client .openapi .models .V1SecretBuilder ;
2929import io .kubernetes .client .openapi .models .V1SecretList ;
30- import io .kubernetes .client .openapi .models .V1SecretListBuilder ;
30+ import org .junit .jupiter .api .AfterAll ;
31+ import org .junit .jupiter .api .AfterEach ;
3132import org .junit .jupiter .api .BeforeAll ;
3233import org .junit .jupiter .api .Test ;
34+ import org .mockito .Mockito ;
3335
3436import org .springframework .cloud .config .environment .Environment ;
37+ import org .springframework .cloud .kubernetes .client .config .KubernetesClientConfigMapsCache ;
38+ import org .springframework .cloud .kubernetes .client .config .KubernetesClientSecretsCache ;
3539import org .springframework .cloud .kubernetes .commons .config .Constants ;
3640
3741import static org .assertj .core .api .Assertions .assertThat ;
38- import static org .mockito .ArgumentMatchers .eq ;
3942import static org .mockito .Mockito .mock ;
4043import static org .mockito .Mockito .when ;
4144
4750 */
4851class KubernetesPropertySourceSupplierTests {
4952
50- private static final CoreV1Api coreApi = mock (CoreV1Api .class );
53+ private static final CoreV1Api CORE_V1_API = mock (CoreV1Api .class );
5154
5255 private static final V1ConfigMapList CONFIGMAP_DEFAULT_LIST = new V1ConfigMapList ()
5356 .addItemsItem (buildConfigMap ("gateway" , "default" ));
@@ -58,41 +61,51 @@ class KubernetesPropertySourceSupplierTests {
5861 private static final V1ConfigMapList CONFIGMAP_TEAM_B_LIST = new V1ConfigMapList ()
5962 .addItemsItem (buildConfigMap ("orders" , "team-b" ));
6063
61- private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretListBuilder ()
62- .addToItems (buildSecret ("gateway" , "default" ))
63- .build ();
64+ private static final V1SecretList SECRET_DEFAULT_LIST = new V1SecretList ()
65+ .addItemsItem (buildSecret ("gateway" , "default" ));
6466
65- private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretListBuilder ()
66- .addToItems (buildSecret ("stores" , "team-a" ))
67- .build ();
67+ private static final V1SecretList SECRET_TEAM_A_LIST = new V1SecretList ()
68+ .addItemsItem (buildSecret ("stores" , "team-a" ));
6869
69- private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretListBuilder ()
70- .addToItems (buildSecret ("orders" , "team-b" ))
71- .build ();
70+ private static final V1SecretList SECRET_TEAM_B_LIST = new V1SecretList ()
71+ .addItemsItem (buildSecret ("orders" , "team-b" ));
72+
73+ private static final KubernetesConfigServerProperties PROPERTIES = properties ();
7274
7375 @ BeforeAll
74- public static void before () throws ApiException {
75- when (coreApi .listNamespacedConfigMap (eq ( "default" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
76- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
76+ static void beforeAll () throws ApiException {
77+ when (CORE_V1_API .listNamespacedConfigMap ("default" , null , null , null , null , null , null , null , null , null , null ,
78+ null ))
7779 .thenReturn (CONFIGMAP_DEFAULT_LIST );
78- when (coreApi .listNamespacedConfigMap (eq ( "team-a" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
79- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
80+ when (CORE_V1_API .listNamespacedConfigMap ("team-a" , null , null , null , null , null , null , null , null , null , null ,
81+ null ))
8082 .thenReturn (CONFIGMAP_TEAM_A_LIST );
81- when (coreApi .listNamespacedConfigMap (eq ( "team-b" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
82- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
83+ when (CORE_V1_API .listNamespacedConfigMap ("team-b" , null , null , null , null , null , null , null , null , null , null ,
84+ null ))
8385 .thenReturn (CONFIGMAP_TEAM_B_LIST );
8486
85- when (coreApi .listNamespacedSecret (eq ( "default" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
86- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
87+ when (CORE_V1_API .listNamespacedSecret ("default" , null , null , null , null , null , null , null , null , null , null ,
88+ null ))
8789 .thenReturn (SECRET_DEFAULT_LIST );
88- when (coreApi .listNamespacedSecret (eq ( "team-a" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
89- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
90+ when (CORE_V1_API .listNamespacedSecret ("team-a" , null , null , null , null , null , null , null , null , null , null ,
91+ null ))
9092 .thenReturn (SECRET_TEAM_A_LIST );
91- when (coreApi .listNamespacedSecret (eq ( "team-b" ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ,
92- eq ( null ), eq ( null ), eq ( null ), eq ( null ), eq ( null ) ))
93+ when (CORE_V1_API .listNamespacedSecret ("team-b" , null , null , null , null , null , null , null , null , null , null ,
94+ null ))
9395 .thenReturn (SECRET_TEAM_B_LIST );
9496 }
9597
98+ @ AfterAll
99+ static void afterAll () {
100+ Mockito .reset (CORE_V1_API );
101+ }
102+
103+ @ AfterEach
104+ void afterEach () {
105+ new KubernetesClientConfigMapsCache ().discardAll ();
106+ new KubernetesClientSecretsCache ().discardAll ();
107+ }
108+
96109 @ Test
97110 void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded () {
98111 KubernetesConfigServerProperties kubernetesConfigServerProperties = new KubernetesConfigServerProperties ();
@@ -101,8 +114,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllConfigMapsAreIncluded() {
101114 KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
102115 .configMapPropertySourceSupplier (kubernetesConfigServerProperties );
103116
104- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
105- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
117+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
118+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
106119
107120 Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
108121 assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (1 );
@@ -122,8 +135,8 @@ void whenExtraNamespacesAddedThenConfigMapsInCurrentNamespaceAreNotIncluded() {
122135 KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
123136 .configMapPropertySourceSupplier (kubernetesConfigServerProperties );
124137
125- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
126- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
138+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
139+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
127140
128141 Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
129142 assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (0 );
@@ -143,8 +156,8 @@ void whenCurrentAndExtraNamespacesAddedThenAllSecretsAreIncluded() {
143156 KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
144157 .secretsPropertySourceSupplier (kubernetesConfigServerProperties );
145158
146- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
147- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
159+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
160+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
148161
149162 Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
150163 assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (1 );
@@ -164,8 +177,8 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
164177 KubernetesPropertySourceSupplier kubernetesPropertySourceSupplier = new KubernetesConfigServerAutoConfiguration ()
165178 .secretsPropertySourceSupplier (kubernetesConfigServerProperties );
166179
167- KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (coreApi ,
168- Collections .singletonList (kubernetesPropertySourceSupplier ), "default" );
180+ KubernetesEnvironmentRepository environmentRepository = new KubernetesEnvironmentRepository (CORE_V1_API ,
181+ Collections .singletonList (kubernetesPropertySourceSupplier ), "default" , PROPERTIES );
169182
170183 Environment environmentGateway = environmentRepository .findOne ("gateway" , "" , "" );
171184 assertThat (environmentGateway .getPropertySources ().size ()).isEqualTo (0 );
@@ -179,19 +192,25 @@ void whenExtraNamespacesAddedThenSecretsInCurrentNamespaceAreNotIncluded() {
179192
180193 private static V1ConfigMap buildConfigMap (String name , String namespace ) {
181194 return new V1ConfigMapBuilder ()
182- .withMetadata (
183- new V1ObjectMetaBuilder ().withName (name ).withNamespace (namespace ).withResourceVersion ("1" ).build ())
184- .addToData (Constants .APPLICATION_YAML , "dummy:\n property:\n string: \" " + name + "\" \n " )
195+ .withMetadata (new V1ObjectMetaBuilder ().withName (name ).withNamespace (namespace ).build ())
196+ .addToData (Constants .APPLICATION_PROPERTIES , """
197+ dummy.property.string=%s
198+ """ .formatted (name ))
185199 .build ();
186200 }
187201
188202 private static V1Secret buildSecret (String name , String namespace ) {
189203 return new V1SecretBuilder ()
190- .withMetadata (
191- new V1ObjectMetaBuilder ().withName (name ).withResourceVersion ("0" ).withNamespace (namespace ).build ())
204+ .withMetadata (new V1ObjectMetaBuilder ().withName (name ).withNamespace (namespace ).build ())
192205 .addToData ("password" , "p455w0rd" .getBytes ())
193206 .addToData ("username" , "user" .getBytes ())
194207 .build ();
195208 }
196209
210+ private static KubernetesConfigServerProperties properties () {
211+ KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties ();
212+ properties .setOrder (1 );
213+ return properties ;
214+ }
215+
197216}
0 commit comments