From 5c86f9eca41af9e6b2dc6d2d2bb9a7e3a1368fcd Mon Sep 17 00:00:00 2001 From: Michael Pratt Date: Tue, 4 Sep 2018 16:40:43 +0000 Subject: [PATCH 1/3] Add Health details using maps See gh-14305 --- .../boot/actuate/health/Health.java | 15 ++++- .../boot/actuate/health/HealthTests.java | 65 ++++++++++++++++++- 2 files changed, 78 insertions(+), 2 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 da277125ff1..9f29e9fa0eb 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 @@ import org.springframework.util.Assert; * * @author Christian Dupuis * @author Phillip Webb + * @author Michael Pratt * @since 1.1.0 */ @JsonInclude(Include.NON_EMPTY) @@ -229,6 +230,18 @@ public final class Health { 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 70fc98e56a5..d7b5c9ecf36 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 @@ import static org.assertj.core.api.Assertions.assertThat; * Tests for {@link Health}. * * @author Phillip Webb + * @author Michael Pratt */ public class HealthTests { @@ -89,6 +92,66 @@ public class HealthTests { 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(); From ca8be3f6bd40ca3353ba3fc7d2e634eb853e2941 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 5 Sep 2018 13:34:02 +0200 Subject: [PATCH 2/3] 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 9f29e9fa0eb..f38645d4953 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 d7b5c9ecf36..60f3651e92d 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 From 7ff41e7c8c7fd160d8fa97400ebbce707093e064 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 5 Sep 2018 13:39:51 +0200 Subject: [PATCH 3/3] Polish --- .../boot/actuate/health/HealthTests.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) 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 60f3651e92d..057703cb3de 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 @@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.entry; * * @author Phillip Webb * @author Michael Pratt + * @author Stephane Nicoll */ public class HealthTests { @@ -57,7 +58,7 @@ public class HealthTests { Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) .build(); assertThat(health.getStatus()).isEqualTo(Status.UP); - assertThat(health.getDetails().get("a")).isEqualTo("b"); + assertThat(health.getDetails()).containsOnly(entry("a", "b")); } @Test @@ -80,17 +81,15 @@ public class HealthTests { RuntimeException ex = new RuntimeException("bang"); Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) .withException(ex).build(); - assertThat(health.getDetails().get("a")).isEqualTo("b"); - assertThat(health.getDetails().get("error")) - .isEqualTo("java.lang.RuntimeException: bang"); + assertThat(health.getDetails()).containsOnly(entry("a", "b"), + entry("error", "java.lang.RuntimeException: bang")); } @Test public void withDetails() { Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) .withDetail("c", "d").build(); - assertThat(health.getDetails().get("a")).isEqualTo("b"); - assertThat(health.getDetails().get("c")).isEqualTo("d"); + assertThat(health.getDetails()).containsOnly(entry("a", "b"), entry("c", "d")); } @Test @@ -128,7 +127,7 @@ public class HealthTests { public void unknownWithDetails() { Health health = new Health.Builder().unknown().withDetail("a", "b").build(); assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN); - assertThat(health.getDetails().get("a")).isEqualTo("b"); + assertThat(health.getDetails()).containsOnly(entry("a", "b")); } @Test @@ -142,7 +141,7 @@ public class HealthTests { public void upWithDetails() { Health health = new Health.Builder().up().withDetail("a", "b").build(); assertThat(health.getStatus()).isEqualTo(Status.UP); - assertThat(health.getDetails().get("a")).isEqualTo("b"); + assertThat(health.getDetails()).containsOnly(entry("a", "b")); } @Test @@ -157,8 +156,8 @@ public class HealthTests { RuntimeException ex = new RuntimeException("bang"); Health health = Health.down(ex).build(); assertThat(health.getStatus()).isEqualTo(Status.DOWN); - assertThat(health.getDetails().get("error")) - .isEqualTo("java.lang.RuntimeException: bang"); + assertThat(health.getDetails()) + .containsOnly(entry("error", "java.lang.RuntimeException: bang")); } @Test