Skip to content

Commit 1df3250

Browse files
committed
Merge pull request #14305 from michael-pratt:master
* pr/14305: Polish Polish "Add Health details using maps" Add Health details using maps
2 parents 21bc3d3 + 7ff41e7 commit 1df3250

File tree

2 files changed

+60
-12
lines changed
  • spring-boot-project/spring-boot-actuator/src

2 files changed

+60
-12
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/health/Health.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,6 +47,7 @@
4747
*
4848
* @author Christian Dupuis
4949
* @author Phillip Webb
50+
* @author Michael Pratt
5051
* @since 1.1.0
5152
*/
5253
@JsonInclude(Include.NON_EMPTY)
@@ -229,6 +230,19 @@ public Builder withDetail(String key, Object value) {
229230
return this;
230231
}
231232

233+
/**
234+
* Record details from the given {@code details} map. Keys from the given map
235+
* replace any existing keys if there are duplicates.
236+
* @param details map of details
237+
* @return this {@link Builder} instance
238+
* @since 2.1.0
239+
*/
240+
public Builder withDetails(Map<String, ?> details) {
241+
Assert.notNull(details, "Details must not be null");
242+
this.details.putAll(details);
243+
return this;
244+
}
245+
232246
/**
233247
* Set status to {@link Status#UNKNOWN} status.
234248
* @return this {@link Builder} instance

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/health/HealthTests.java

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,17 +17,22 @@
1717
package org.springframework.boot.actuate.health;
1818

1919
import java.util.Collections;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
2022

2123
import org.junit.Rule;
2224
import org.junit.Test;
2325
import org.junit.rules.ExpectedException;
2426

2527
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.entry;
2629

2730
/**
2831
* Tests for {@link Health}.
2932
*
3033
* @author Phillip Webb
34+
* @author Michael Pratt
35+
* @author Stephane Nicoll
3136
*/
3237
public class HealthTests {
3338

@@ -53,7 +58,7 @@ public void createWithDetails() {
5358
Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b"))
5459
.build();
5560
assertThat(health.getStatus()).isEqualTo(Status.UP);
56-
assertThat(health.getDetails().get("a")).isEqualTo("b");
61+
assertThat(health.getDetails()).containsOnly(entry("a", "b"));
5762
}
5863

5964
@Test
@@ -76,24 +81,53 @@ public void withException() {
7681
RuntimeException ex = new RuntimeException("bang");
7782
Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b"))
7883
.withException(ex).build();
79-
assertThat(health.getDetails().get("a")).isEqualTo("b");
80-
assertThat(health.getDetails().get("error"))
81-
.isEqualTo("java.lang.RuntimeException: bang");
84+
assertThat(health.getDetails()).containsOnly(entry("a", "b"),
85+
entry("error", "java.lang.RuntimeException: bang"));
8286
}
8387

8488
@Test
8589
public void withDetails() {
8690
Health health = new Health.Builder(Status.UP, Collections.singletonMap("a", "b"))
8791
.withDetail("c", "d").build();
88-
assertThat(health.getDetails().get("a")).isEqualTo("b");
89-
assertThat(health.getDetails().get("c")).isEqualTo("d");
92+
assertThat(health.getDetails()).containsOnly(entry("a", "b"), entry("c", "d"));
93+
}
94+
95+
@Test
96+
public void withDetailsMap() {
97+
Map<String, Object> details = new LinkedHashMap<>();
98+
details.put("a", "b");
99+
details.put("c", "d");
100+
Health health = Health.up().withDetails(details).build();
101+
assertThat(health.getDetails()).containsOnly(entry("a", "b"), entry("c", "d"));
102+
}
103+
104+
@Test
105+
public void withDetailsMapDuplicateKeys() {
106+
Map<String, Object> details = new LinkedHashMap<>();
107+
details.put("c", "d");
108+
details.put("a", "e");
109+
Health health = Health.up().withDetail("a", "b").withDetails(details).build();
110+
assertThat(health.getDetails()).containsOnly(entry("a", "e"), entry("c", "d"));
111+
}
112+
113+
@Test
114+
public void withDetailsMultipleMaps() {
115+
Map<String, Object> details1 = new LinkedHashMap<>();
116+
details1.put("a", "b");
117+
details1.put("c", "d");
118+
Map<String, Object> details2 = new LinkedHashMap<>();
119+
details1.put("a", "e");
120+
details1.put("1", "2");
121+
Health health = Health.up().withDetails(details1).withDetails(details2).build();
122+
assertThat(health.getDetails()).containsOnly(entry("a", "e"), entry("c", "d"),
123+
entry("1", "2"));
90124
}
91125

92126
@Test
93127
public void unknownWithDetails() {
94128
Health health = new Health.Builder().unknown().withDetail("a", "b").build();
95129
assertThat(health.getStatus()).isEqualTo(Status.UNKNOWN);
96-
assertThat(health.getDetails().get("a")).isEqualTo("b");
130+
assertThat(health.getDetails()).containsOnly(entry("a", "b"));
97131
}
98132

99133
@Test
@@ -107,7 +141,7 @@ public void unknown() {
107141
public void upWithDetails() {
108142
Health health = new Health.Builder().up().withDetail("a", "b").build();
109143
assertThat(health.getStatus()).isEqualTo(Status.UP);
110-
assertThat(health.getDetails().get("a")).isEqualTo("b");
144+
assertThat(health.getDetails()).containsOnly(entry("a", "b"));
111145
}
112146

113147
@Test
@@ -122,8 +156,8 @@ public void downWithException() {
122156
RuntimeException ex = new RuntimeException("bang");
123157
Health health = Health.down(ex).build();
124158
assertThat(health.getStatus()).isEqualTo(Status.DOWN);
125-
assertThat(health.getDetails().get("error"))
126-
.isEqualTo("java.lang.RuntimeException: bang");
159+
assertThat(health.getDetails())
160+
.containsOnly(entry("error", "java.lang.RuntimeException: bang"));
127161
}
128162

129163
@Test

0 commit comments

Comments
 (0)