diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java index da277125ff14..9f29e9fa0ebe 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -47,6 +47,7 @@ * * @author Christian Dupuis * @author Phillip Webb + * @author Michael Pratt * @since 1.1.0 */ @JsonInclude(Include.NON_EMPTY) @@ -229,6 +230,18 @@ public Builder withDetail(String key, Object value) { return this; } + /** + * Add details from the given {@code details} map into existing details. Keys from + * the given map will replace any existing keys if there are duplicates. + * @param details map of details + * @return this {@link Builder} instance + */ + public Builder withDetails(Map details) { + Assert.notNull(details, "Details must not be null"); + this.details.putAll(details); + return this; + } + /** * Set status to {@link Status#UNKNOWN} status. * @return this {@link Builder} instance diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java index 70fc98e56a5d..d7b5c9ecf368 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2017 the original author or authors. + * Copyright 2012-2018 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. @@ -17,6 +17,8 @@ package org.springframework.boot.actuate.health; import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.Map; import org.junit.Rule; import org.junit.Test; @@ -28,6 +30,7 @@ * Tests for {@link Health}. * * @author Phillip Webb + * @author Michael Pratt */ public class HealthTests { @@ -89,6 +92,66 @@ public void withDetails() { assertThat(health.getDetails().get("c")).isEqualTo("d"); } + @Test + public void withDetailsMap() { + Map details = new LinkedHashMap<>(); + details.put("a", "b"); + details.put("c", "d"); + + Health.Builder builder = Health.up(); + builder.withDetails(details); + + Health health = builder.build(); + assertThat(health.getDetails().get("a")).isEqualTo("b"); + assertThat(health.getDetails().get("c")).isEqualTo("d"); + } + + @Test + public void withDetailsMapDuplicateKeys() { + Map details = new LinkedHashMap<>(); + details.put("a", "b"); + details.put("c", "d"); + details.put("a", "e"); + + Health.Builder builder = Health.up(); + builder.withDetails(details); + + Health health = builder.build(); + assertThat(health.getDetails().get("a")).isEqualTo("e"); + assertThat(health.getDetails().get("c")).isEqualTo("d"); + } + + @Test + public void withMultipleDetailsMaps() { + Map details1 = new LinkedHashMap<>(); + details1.put("a", "b"); + details1.put("c", "d"); + + Map details2 = new LinkedHashMap<>(); + details2.put("1", "2"); + + Health.Builder builder = Health.up(); + builder.withDetails(details1); + builder.withDetails(details2); + + Health health = builder.build(); + assertThat(health.getDetails().get("a")).isEqualTo("b"); + assertThat(health.getDetails().get("c")).isEqualTo("d"); + assertThat(health.getDetails().get("1")).isEqualTo("2"); + } + + @Test + public void mixWithDetailsUsage() { + Map details = new LinkedHashMap<>(); + details.put("a", "b"); + + Health.Builder builder = Health.up().withDetails(details).withDetail("c", "d"); + + Health health = builder.build(); + assertThat(health.getDetails().get("a")).isEqualTo("b"); + assertThat(health.getDetails().get("c")).isEqualTo("d"); + } + @Test public void unknownWithDetails() { Health health = new Health.Builder().unknown().withDetail("a", "b").build();