Merge pull request #14305 from michael-pratt:master

* pr/14305:
  Polish
  Polish "Add Health details using maps"
  Add Health details using maps
pull/14312/head
Stephane Nicoll 6 years ago
commit 1df3250d84

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Christian Dupuis
* @author Phillip Webb * @author Phillip Webb
* @author Michael Pratt
* @since 1.1.0 * @since 1.1.0
*/ */
@JsonInclude(Include.NON_EMPTY) @JsonInclude(Include.NON_EMPTY)
@ -229,6 +230,19 @@ public final class Health {
return this; 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. * Set status to {@link Status#UNKNOWN} status.
* @return this {@link Builder} instance * @return this {@link Builder} instance

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,17 +17,22 @@
package org.springframework.boot.actuate.health; package org.springframework.boot.actuate.health;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
/** /**
* Tests for {@link Health}. * Tests for {@link Health}.
* *
* @author Phillip Webb * @author Phillip Webb
* @author Michael Pratt
* @author Stephane Nicoll
*/ */
public class HealthTests { public class HealthTests {
@ -53,7 +58,7 @@ public class HealthTests {
Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b"))
.build(); .build();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("a")).isEqualTo("b"); assertThat(health.getDetails()).containsOnly(entry("a", "b"));
} }
@Test @Test
@ -76,24 +81,53 @@ public class HealthTests {
RuntimeException ex = new RuntimeException("bang"); RuntimeException ex = new RuntimeException("bang");
Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b"))
.withException(ex).build(); .withException(ex).build();
assertThat(health.getDetails().get("a")).isEqualTo("b"); assertThat(health.getDetails()).containsOnly(entry("a", "b"),
assertThat(health.getDetails().get("error")) entry("error", "java.lang.RuntimeException: bang"));
.isEqualTo("java.lang.RuntimeException: bang");
} }
@Test @Test
public void withDetails() { public void withDetails() {
Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b")) Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b"))
.withDetail("c", "d").build(); .withDetail("c", "d").build();
assertThat(health.getDetails().get("a")).isEqualTo("b"); assertThat(health.getDetails()).containsOnly(entry("a", "b"), entry("c", "d"));
assertThat(health.getDetails().get("c")).isEqualTo("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 @Test
public void unknownWithDetails() { public void unknownWithDetails() {
Health health = new Health.Builder().unknown().withDetail("a", "b").build(); Health health = new Health.Builder().unknown().withDetail("a", "b").build();
assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN); assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN);
assertThat(health.getDetails().get("a")).isEqualTo("b"); assertThat(health.getDetails()).containsOnly(entry("a", "b"));
} }
@Test @Test
@ -107,7 +141,7 @@ public class HealthTests {
public void upWithDetails() { public void upWithDetails() {
Health health = new Health.Builder().up().withDetail("a", "b").build(); Health health = new Health.Builder().up().withDetail("a", "b").build();
assertThat(health.getStatus()).isEqualTo(Status.UP); assertThat(health.getStatus()).isEqualTo(Status.UP);
assertThat(health.getDetails().get("a")).isEqualTo("b"); assertThat(health.getDetails()).containsOnly(entry("a", "b"));
} }
@Test @Test
@ -122,8 +156,8 @@ public class HealthTests {
RuntimeException ex = new RuntimeException("bang"); RuntimeException ex = new RuntimeException("bang");
Health health = Health.down(ex).build(); Health health = Health.down(ex).build();
assertThat(health.getStatus()).isEqualTo(Status.DOWN); assertThat(health.getStatus()).isEqualTo(Status.DOWN);
assertThat(health.getDetails().get("error")) assertThat(health.getDetails())
.isEqualTo("java.lang.RuntimeException: bang"); .containsOnly(entry("error", "java.lang.RuntimeException: bang"));
} }
@Test @Test

Loading…
Cancel
Save