|
19 | 19 | import java.util.Collections;
|
20 | 20 | import java.util.LinkedHashMap;
|
21 | 21 | import java.util.Map;
|
| 22 | +import java.util.function.Predicate; |
22 | 23 |
|
23 | 24 | import org.junit.jupiter.api.Test;
|
24 | 25 |
|
@@ -224,6 +225,92 @@ void getHealthWhenGroupContainsCompositeContributorReturnsHealth() {
|
224 | 225 | assertThat(health.getComponents()).containsKey("test");
|
225 | 226 | }
|
226 | 227 |
|
| 228 | + @Test |
| 229 | + void getHealthWhenGroupContainsComponentOfCompositeContributorReturnsHealth() { |
| 230 | + CompositeHealth health = getCompositeHealth((name) -> name.equals("test/spring-1")); |
| 231 | + assertThat(health.getComponents()).containsKey("test"); |
| 232 | + CompositeHealth test = (CompositeHealth) health.getComponents().get("test"); |
| 233 | + assertThat(test.getComponents()).containsKey("spring-1"); |
| 234 | + assertThat(test.getComponents()).doesNotContainKey("spring-2"); |
| 235 | + assertThat(test.getComponents()).doesNotContainKey("test"); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + void getHealthWhenGroupExcludesComponentOfCompositeContributorReturnsHealth() { |
| 240 | + CompositeHealth health = getCompositeHealth( |
| 241 | + (name) -> name.startsWith("test/") && !name.equals("test/spring-2")); |
| 242 | + assertThat(health.getComponents()).containsKey("test"); |
| 243 | + CompositeHealth test = (CompositeHealth) health.getComponents().get("test"); |
| 244 | + assertThat(test.getComponents()).containsKey("spring-1"); |
| 245 | + assertThat(test.getComponents()).doesNotContainKey("spring-2"); |
| 246 | + } |
| 247 | + |
| 248 | + @Test |
| 249 | + void getHealthForPathWhenGroupContainsComponentOfCompositeContributorReturnsHealth() { |
| 250 | + Map<String, C> contributors = new LinkedHashMap<>(); |
| 251 | + contributors.put("spring-1", createNestedHealthContributor("spring-1")); |
| 252 | + contributors.put("spring-2", createNestedHealthContributor("spring-2")); |
| 253 | + C compositeContributor = createCompositeContributor(contributors); |
| 254 | + this.registry.registerContributor("test", compositeContributor); |
| 255 | + TestHealthEndpointGroup testGroup = new TestHealthEndpointGroup( |
| 256 | + (name) -> name.startsWith("test") && !name.equals("test/spring-1/b")); |
| 257 | + HealthEndpointGroups groups = HealthEndpointGroups.of(this.primaryGroup, |
| 258 | + Collections.singletonMap("testGroup", testGroup)); |
| 259 | + HealthResult<T> result = create(this.registry, groups).getHealth(ApiVersion.V3, null, SecurityContext.NONE, |
| 260 | + false, "testGroup", "test"); |
| 261 | + CompositeHealth health = (CompositeHealth) getHealth(result); |
| 262 | + assertThat(health.getComponents()).containsKey("spring-1"); |
| 263 | + assertThat(health.getComponents()).containsKey("spring-2"); |
| 264 | + CompositeHealth spring1 = (CompositeHealth) health.getComponents().get("spring-1"); |
| 265 | + CompositeHealth spring2 = (CompositeHealth) health.getComponents().get("spring-2"); |
| 266 | + assertThat(spring1.getComponents()).containsKey("a"); |
| 267 | + assertThat(spring1.getComponents()).containsKey("c"); |
| 268 | + assertThat(spring1.getComponents()).doesNotContainKey("b"); |
| 269 | + assertThat(spring2.getComponents()).containsKey("a"); |
| 270 | + assertThat(spring2.getComponents()).containsKey("c"); |
| 271 | + assertThat(spring2.getComponents()).containsKey("b"); |
| 272 | + } |
| 273 | + |
| 274 | + @Test |
| 275 | + void getHealthForComponentPathWhenNotPartOfGroup() { |
| 276 | + Map<String, C> contributors = new LinkedHashMap<>(); |
| 277 | + contributors.put("spring-1", createNestedHealthContributor("spring-1")); |
| 278 | + contributors.put("spring-2", createNestedHealthContributor("spring-2")); |
| 279 | + C compositeContributor = createCompositeContributor(contributors); |
| 280 | + this.registry.registerContributor("test", compositeContributor); |
| 281 | + TestHealthEndpointGroup testGroup = new TestHealthEndpointGroup( |
| 282 | + (name) -> name.startsWith("test") && !name.equals("test/spring-1/b")); |
| 283 | + HealthEndpointGroups groups = HealthEndpointGroups.of(this.primaryGroup, |
| 284 | + Collections.singletonMap("testGroup", testGroup)); |
| 285 | + HealthResult<T> result = create(this.registry, groups).getHealth(ApiVersion.V3, null, SecurityContext.NONE, |
| 286 | + false, "testGroup", "test", "spring-1", "b"); |
| 287 | + assertThat(result).isNull(); |
| 288 | + } |
| 289 | + |
| 290 | + private CompositeHealth getCompositeHealth(Predicate<String> memberPredicate) { |
| 291 | + C contributor1 = createContributor(this.up); |
| 292 | + C contributor2 = createContributor(this.down); |
| 293 | + Map<String, C> contributors = new LinkedHashMap<>(); |
| 294 | + contributors.put("spring-1", contributor1); |
| 295 | + contributors.put("spring-2", contributor2); |
| 296 | + C compositeContributor = createCompositeContributor(contributors); |
| 297 | + this.registry.registerContributor("test", compositeContributor); |
| 298 | + TestHealthEndpointGroup testGroup = new TestHealthEndpointGroup(memberPredicate); |
| 299 | + HealthEndpointGroups groups = HealthEndpointGroups.of(this.primaryGroup, |
| 300 | + Collections.singletonMap("testGroup", testGroup)); |
| 301 | + HealthResult<T> result = create(this.registry, groups).getHealth(ApiVersion.V3, null, SecurityContext.NONE, |
| 302 | + false, "testGroup"); |
| 303 | + return (CompositeHealth) getHealth(result); |
| 304 | + } |
| 305 | + |
| 306 | + private C createNestedHealthContributor(String name) { |
| 307 | + Map<String, C> map = new LinkedHashMap<>(); |
| 308 | + map.put("a", createContributor(Health.up().withDetail("hello", name + "-a").build())); |
| 309 | + map.put("b", createContributor(Health.up().withDetail("hello", name + "-b").build())); |
| 310 | + map.put("c", createContributor(Health.up().withDetail("hello", name + "-c").build())); |
| 311 | + return createCompositeContributor(map); |
| 312 | + } |
| 313 | + |
227 | 314 | @Test
|
228 | 315 | void getHealthWhenGroupHasAdditionalPath() {
|
229 | 316 | this.registry.registerContributor("test", createContributor(this.up));
|
|
0 commit comments