/
InfoContributorAutoConfigurationTests.java
219 lines (184 loc) · 7.73 KB
/
InfoContributorAutoConfigurationTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
* Copyright 2012-2021 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.boot.actuate.autoconfigure.info;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.info.BuildInfoContributor;
import org.springframework.boot.actuate.info.EnvironmentInfoContributor;
import org.springframework.boot.actuate.info.GitInfoContributor;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.boot.actuate.info.JavaInfoContributor;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.info.BuildProperties;
import org.springframework.boot.info.GitProperties;
import org.springframework.boot.info.JavaInfo;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link InfoContributorAutoConfiguration}.
*
* @author Stephane Nicoll
* @author Jonatan Ivanov
*/
class InfoContributorAutoConfigurationTests {
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(InfoContributorAutoConfiguration.class));
@Test
void envContributor() {
this.contextRunner.withPropertyValues("management.info.env.enabled=true")
.run((context) -> assertThat(context).hasSingleBean(EnvironmentInfoContributor.class));
}
@Test
void defaultInfoContributorsEnabled() {
this.contextRunner.run((context) -> assertThat(context).doesNotHaveBean(InfoContributor.class));
}
@Test
void defaultInfoContributorsEnabledWithPrerequisitesInPlace() {
this.contextRunner.withUserConfiguration(GitPropertiesConfiguration.class, BuildPropertiesConfiguration.class)
.run((context) -> assertThat(context.getBeansOfType(InfoContributor.class)).hasSize(2)
.satisfies((contributors) -> assertThat(contributors.values())
.hasOnlyElementsOfTypes(BuildInfoContributor.class, GitInfoContributor.class)));
}
@Test
void defaultInfoContributorsDisabledWithPrerequisitesInPlace() {
this.contextRunner.withUserConfiguration(GitPropertiesConfiguration.class, BuildPropertiesConfiguration.class)
.withPropertyValues("management.info.defaults.enabled=false")
.run((context) -> assertThat(context).doesNotHaveBean(InfoContributor.class));
}
@Test
void defaultInfoContributorsDisabledWithCustomOne() {
this.contextRunner.withUserConfiguration(CustomInfoContributorConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(InfoContributor.class);
assertThat(context.getBean(InfoContributor.class)).isSameAs(context.getBean("customInfoContributor"));
});
}
@SuppressWarnings("unchecked")
@Test
void gitPropertiesDefaultMode() {
this.contextRunner.withUserConfiguration(GitPropertiesConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(GitInfoContributor.class);
Map<String, Object> content = invokeContributor(context.getBean(GitInfoContributor.class));
Object git = content.get("git");
assertThat(git).isInstanceOf(Map.class);
Map<String, Object> gitInfo = (Map<String, Object>) git;
assertThat(gitInfo).containsOnlyKeys("branch", "commit");
});
}
@SuppressWarnings("unchecked")
@Test
void gitPropertiesFullMode() {
this.contextRunner.withPropertyValues("management.info.git.mode=full")
.withUserConfiguration(GitPropertiesConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(GitInfoContributor.class);
Map<String, Object> content = invokeContributor(context.getBean(GitInfoContributor.class));
Object git = content.get("git");
assertThat(git).isInstanceOf(Map.class);
Map<String, Object> gitInfo = (Map<String, Object>) git;
assertThat(gitInfo).containsOnlyKeys("branch", "commit", "foo");
assertThat(gitInfo.get("foo")).isEqualTo("bar");
});
}
@Test
void customGitInfoContributor() {
this.contextRunner.withUserConfiguration(CustomGitInfoContributorConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(GitInfoContributor.class);
assertThat(context.getBean(GitInfoContributor.class)).isSameAs(context.getBean("customGitInfoContributor"));
});
}
@SuppressWarnings("unchecked")
@Test
void buildProperties() {
this.contextRunner.withUserConfiguration(BuildPropertiesConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(BuildInfoContributor.class);
Map<String, Object> content = invokeContributor(context.getBean(BuildInfoContributor.class));
Object build = content.get("build");
assertThat(build).isInstanceOf(Map.class);
Map<String, Object> buildInfo = (Map<String, Object>) build;
assertThat(buildInfo).containsOnlyKeys("group", "artifact", "foo");
assertThat(buildInfo.get("foo")).isEqualTo("bar");
});
}
@Test
void customBuildInfoContributor() {
this.contextRunner.withUserConfiguration(CustomBuildInfoContributorConfiguration.class).run((context) -> {
assertThat(context).hasSingleBean(BuildInfoContributor.class);
assertThat(context.getBean(BuildInfoContributor.class))
.isSameAs(context.getBean("customBuildInfoContributor"));
});
}
@Test
void javaInfoContributor() {
this.contextRunner.withPropertyValues("management.info.java.enabled=true").run((context) -> {
assertThat(context).hasSingleBean(JavaInfoContributor.class);
Map<String, Object> content = invokeContributor(context.getBean(JavaInfoContributor.class));
assertThat(content).containsKey("java");
assertThat(content.get("java")).isInstanceOf(JavaInfo.class);
});
}
private Map<String, Object> invokeContributor(InfoContributor contributor) {
Info.Builder builder = new Info.Builder();
contributor.contribute(builder);
return builder.build().getDetails();
}
@Configuration(proxyBeanMethods = false)
static class GitPropertiesConfiguration {
@Bean
GitProperties gitProperties() {
Properties properties = new Properties();
properties.put("branch", "master");
properties.put("commit.id", "abcdefg");
properties.put("foo", "bar");
return new GitProperties(properties);
}
}
@Configuration(proxyBeanMethods = false)
static class BuildPropertiesConfiguration {
@Bean
BuildProperties buildProperties() {
Properties properties = new Properties();
properties.put("group", "com.example");
properties.put("artifact", "demo");
properties.put("foo", "bar");
return new BuildProperties(properties);
}
}
@Configuration(proxyBeanMethods = false)
static class CustomInfoContributorConfiguration {
@Bean
InfoContributor customInfoContributor() {
return (builder) -> {
};
}
}
@Configuration(proxyBeanMethods = false)
static class CustomGitInfoContributorConfiguration {
@Bean
GitInfoContributor customGitInfoContributor() {
return new GitInfoContributor(new GitProperties(new Properties()));
}
}
@Configuration(proxyBeanMethods = false)
static class CustomBuildInfoContributorConfiguration {
@Bean
BuildInfoContributor customBuildInfoContributor() {
return new BuildInfoContributor(new BuildProperties(new Properties()));
}
}
}