Merge pull request #14305 from michael-pratt:master

* pr/14305:
  Polish
  Polish "Add Health details using maps"
  Add Health details using maps
This commit is contained in:
Stephane Nicoll
2018-09-05 13:41:10 +02:00
2 changed files with 60 additions and 12 deletions

View File

@@ -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,19 @@ public final class Health {
return this;
}
/**
* 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<String, ?> 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

View File

@@ -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,17 +17,22 @@
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;
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}.
*
* @author Phillip Webb
* @author Michael Pratt
* @author Stephane Nicoll
*/
public class HealthTests {
@@ -53,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
@@ -76,24 +81,53 @@ 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
public void withDetailsMap() {
Map<String, Object> details = new LinkedHashMap<>();
details.put("a", "b");
details.put("c", "d");
Health health = Health.up().withDetails(details).build();
assertThat(health.getDetails()).containsOnly(entry("a", "b"), entry("c", "d"));
}
@Test
public void withDetailsMapDuplicateKeys() {
Map<String, Object> details = new LinkedHashMap<>();
details.put("c", "d");
details.put("a", "e");
Health health = Health.up().withDetail("a", "b").withDetails(details).build();
assertThat(health.getDetails()).containsOnly(entry("a", "e"), entry("c", "d"));
}
@Test
public void withDetailsMultipleMaps() {
Map<String, Object> details1 = new LinkedHashMap<>();
details1.put("a", "b");
details1.put("c", "d");
Map<String, Object> details2 = new LinkedHashMap<>();
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
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
@@ -107,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
@@ -122,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