Skip to content

Commit 94d1354

Browse files
test cases
Signed-off-by: Arjav <[email protected]>
1 parent b694b18 commit 94d1354

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.configserver;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository;
24+
import org.springframework.context.ConfigurableApplicationContext;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
29+
classes = { KubernetesConfigServerApplication.class },
30+
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",
31+
"spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite",
32+
"spring.cloud.config.server.composite[0].type=git",
33+
"spring.cloud.config.server.composite[0].uri=https://github.com/spring-cloud-samples/config-repo",
34+
"spring.cloud.config.server.composite[1].type=kubernetes",
35+
"spring.cloud.config.server.composite[1].config-map-namespace=default",
36+
"spring.cloud.config.server.composite[1].secrets-namespace=default" })
37+
class CompositeProfileWithGitAndKubernetesConfigSourcesTests {
38+
39+
@Autowired
40+
private ConfigurableApplicationContext context;
41+
42+
@Test
43+
void kubernetesEnvironmentRepositoryIsLoaded() {
44+
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(2);
45+
}
46+
47+
@Test
48+
void kubernetesPropertySourceSuppliersAreLoaded() {
49+
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty();
50+
}
51+
52+
@Test
53+
void gitEnvironmentRepositoryIsLoaded() {
54+
assertThat(context.getBeanNamesForType(JGitEnvironmentRepository.class)).hasSize(1);
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.configserver;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
28+
classes = { KubernetesConfigServerApplication.class },
29+
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",
30+
"spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite",
31+
"spring.cloud.config.server.composite[0].type=kubernetes",
32+
"spring.cloud.config.server.composite[0].config-map-namespace=default",
33+
"spring.cloud.config.server.composite[0].secrets-namespace=default",
34+
"spring.cloud.config.server.composite[1].type=kubernetes",
35+
"spring.cloud.config.server.composite[1].config-map-namespace=another-namespace",
36+
"spring.cloud.config.server.composite[1].secrets-namespace=another-namespace" })
37+
class CompositeProfileWithMultipleKubernetesConfigSourcesTests {
38+
39+
@Autowired
40+
private ConfigurableApplicationContext context;
41+
42+
@Test
43+
void kubernetesEnvironmentRepositoriesAreLoaded() {
44+
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(3);
45+
}
46+
47+
@Test
48+
void kubernetesPropertySourceSuppliersAreLoaded() {
49+
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty();
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.configserver;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.context.ConfigurableApplicationContext;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
28+
classes = { KubernetesConfigServerApplication.class },
29+
properties = { "spring.main.cloud-platform=KUBERNETES", "spring.profiles.include=kubernetes",
30+
"spring.cloud.kubernetes.client.namespace=default", "spring.profiles.active=composite",
31+
"spring.cloud.config.server.composite[0].type=kubernetes",
32+
"spring.cloud.config.server.composite[0].config-map-namespace=default",
33+
"spring.cloud.config.server.composite[0].secrets-namespace=default" })
34+
class CompositeProfileWithOnlyKubernetesConfigSourceTests {
35+
36+
@Autowired
37+
private ConfigurableApplicationContext context;
38+
39+
@Test
40+
void kubernetesEnvironmentRepositoryIsLoaded() {
41+
assertThat(context.getBeanNamesForType(KubernetesEnvironmentRepository.class)).hasSize(2);
42+
}
43+
44+
@Test
45+
void kubernetesPropertySourceSuppliersAreLoaded() {
46+
assertThat(context.getBeanNamesForType(KubernetesPropertySourceSupplier.class)).isNotEmpty();
47+
}
48+
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2013-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.kubernetes.configserver;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.ObjectProvider;
22+
import org.springframework.boot.test.context.SpringBootTest;
23+
import org.springframework.boot.test.mock.mockito.MockBean;
24+
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.when;
32+
33+
@SpringJUnitConfig
34+
@SpringBootTest
35+
class KubernetesEnvironmentRepositoryFactoryTests {
36+
37+
@MockBean
38+
private ObjectProvider<KubernetesEnvironmentRepository> kubernetesEnvironmentRepositoryProvider;
39+
40+
@Test
41+
void testBuild() {
42+
KubernetesEnvironmentRepository mockRepository = mock(KubernetesEnvironmentRepository.class);
43+
when(kubernetesEnvironmentRepositoryProvider.getIfAvailable()).thenReturn(mockRepository);
44+
45+
KubernetesEnvironmentRepositoryFactory factory = new KubernetesEnvironmentRepositoryFactory(
46+
kubernetesEnvironmentRepositoryProvider);
47+
KubernetesConfigServerProperties properties = new KubernetesConfigServerProperties();
48+
49+
EnvironmentRepository repository = factory.build(properties);
50+
51+
assertThat(repository).isNotNull();
52+
assertThat(repository).isInstanceOf(KubernetesEnvironmentRepository.class);
53+
assertThat(repository).isEqualTo(mockRepository);
54+
}
55+
56+
@Configuration
57+
static class TestConfig {
58+
59+
@Bean
60+
public KubernetesEnvironmentRepositoryFactory kubernetesEnvironmentRepositoryFactory(
61+
ObjectProvider<KubernetesEnvironmentRepository> kubernetesEnvironmentRepositoryProvider) {
62+
return new KubernetesEnvironmentRepositoryFactory(kubernetesEnvironmentRepositoryProvider);
63+
}
64+
65+
}
66+
67+
}

0 commit comments

Comments
 (0)