Commit 5c90f473 by earthmonkey0 Committed by Johannes Edmeier

Add content-type header to hipchat request

fixes #382
parent 21aac376
......@@ -22,6 +22,9 @@ import java.util.Map;
import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationEvent;
......@@ -78,13 +81,17 @@ public class HipchatNotifier extends AbstractStatusChangeNotifier {
authToken);
}
protected Map<String, Object> createHipChatNotification(ClientApplicationEvent event) {
Map<String, Object> result = new HashMap<>();
result.put("color", getColor(event));
result.put("message", getMessage(event));
result.put("notify", getNotify());
result.put("message_format", "html");
return result;
protected HttpEntity<Map<String, Object>> createHipChatNotification(
ClientApplicationEvent event) {
Map<String, Object> body = new HashMap<>();
body.put("color", getColor(event));
body.put("message", getMessage(event));
body.put("notify", getNotify());
body.put("message_format", "html");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(body, headers);
}
protected boolean getNotify() {
......@@ -97,7 +104,8 @@ public class HipchatNotifier extends AbstractStatusChangeNotifier {
protected String getColor(ClientApplicationEvent event) {
if (event instanceof ClientApplicationStatusChangedEvent) {
return "UP".equals(((ClientApplicationStatusChangedEvent) event).getTo().getStatus()) ? "green" : "red";
return "UP".equals(((ClientApplicationStatusChangedEvent) event).getTo().getStatus())
? "green" : "red";
} else {
return "gray";
}
......
package de.codecentric.boot.admin.notify;
import static org.mockito.Matchers.any;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.springframework.http.HttpEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
......@@ -40,17 +44,27 @@ public class HipchatNotifierTest {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
@SuppressWarnings("unchecked")
ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor
.forClass((Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class)))
.thenReturn(ResponseEntity.ok((Void) null));
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
infoDown, infoUp));
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("color", "green");
expected.put("message", "<strong>App</strong>/-id- is <strong>UP</strong>");
expected.put("notify", Boolean.TRUE);
expected.put("message_format", "html");
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type",
asList("application/json"));
Map<String, Object> body = httpRequest.getValue().getBody();
assertThat(body).containsEntry("color", "green");
assertThat(body).containsEntry("message",
"<strong>App</strong>/-id- is <strong>UP</strong>");
assertThat(body).containsEntry("notify", Boolean.TRUE);
assertThat(body).containsEntry("message_format", "html");
verify(restTemplate).postForEntity(any(String.class), eq(expected), eq(Void.class));
}
@Test
......@@ -58,16 +72,25 @@ public class HipchatNotifierTest {
StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp();
@SuppressWarnings("unchecked")
ArgumentCaptor<HttpEntity<Map<String, Object>>> httpRequest = ArgumentCaptor
.forClass((Class<HttpEntity<Map<String, Object>>>) (Class<?>) HttpEntity.class);
when(restTemplate.postForEntity(isA(String.class), httpRequest.capture(), eq(Void.class)))
.thenReturn(ResponseEntity.ok((Void) null));
notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
infoUp, infoDown));
Map<String, Object> expected = new HashMap<String, Object>();
expected.put("color", "red");
expected.put("message", "<strong>App</strong>/-id- is <strong>DOWN</strong>");
expected.put("notify", Boolean.TRUE);
expected.put("message_format", "html");
assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type",
asList("application/json"));
verify(restTemplate).postForEntity(any(String.class), eq(expected), eq(Void.class));
Map<String, Object> body = httpRequest.getValue().getBody();
assertThat(body).containsEntry("color", "red");
assertThat(body).containsEntry("message",
"<strong>App</strong>/-id- is <strong>DOWN</strong>");
assertThat(body).containsEntry("notify", Boolean.TRUE);
assertThat(body).containsEntry("message_format", "html");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment