Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -47,6 +47,7 @@
*
* @author Christian Dupuis
* @author Phillip Webb
* @author Michael Pratt
* @since 1.1.0
*/
@JsonInclude(Include.NON_EMPTY)
Expand Down Expand Up @@ -229,6 +230,18 @@ public Builder withDetail(String key, Object value) {
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<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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -28,6 +30,7 @@
* Tests for {@link Health}.
*
* @author Phillip Webb
* @author Michael Pratt
*/
public class HealthTests {

Expand Down Expand Up @@ -89,6 +92,66 @@ public void withDetails() {
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.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<String, Object> 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<String, Object> details1 = new LinkedHashMap<>();
details1.put("a", "b");
details1.put("c", "d");

Map<String, Object> 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<String, Object> 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();
Expand Down