From ca8be3f6bd40ca3353ba3fc7d2e634eb853e2941 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 5 Sep 2018 13:34:02 +0200 Subject: [PATCH] Polish "Add Health details using maps" Closes gh-14305 --- .../boot/actuate/health/Health.java | 5 +- .../boot/actuate/health/HealthTests.java | 50 ++++--------------- 2 files changed, 14 insertions(+), 41 deletions(-) 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 9f29e9fa0e..f38645d495 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 @@ -231,10 +231,11 @@ public final class Health { } /** - * 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. + * Record details from the given {@code details} map. Keys from the given map + * replace any existing keys if there are duplicates. * @param details map of details * @return this {@link Builder} instance + * @since 2.1.0 */ public Builder withDetails(Map details) { Assert.notNull(details, "Details must not be null"); 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 d7b5c9ecf3..60f3651e92 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 @@ -25,6 +25,7 @@ import org.junit.Test; import org.junit.rules.ExpectedException; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; /** * Tests for {@link Health}. @@ -97,59 +98,30 @@ public class HealthTests { 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"); + Health health = Health.up().withDetails(details).build(); + assertThat(health.getDetails()).containsOnly(entry("a", "b"), entry("c", "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"); + Health health = Health.up().withDetail("a", "b").withDetails(details).build(); + assertThat(health.getDetails()).containsOnly(entry("a", "e"), entry("c", "d")); } @Test - public void withMultipleDetailsMaps() { + public void withDetailsMultipleMaps() { 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"); + details1.put("a", "e"); + details1.put("1", "2"); + Health health = Health.up().withDetails(details1).withDetails(details2).build(); + assertThat(health.getDetails()).containsOnly(entry("a", "e"), entry("c", "d"), + entry("1", "2")); } @Test