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; ...@@ -22,6 +22,9 @@ import java.util.Map;
import org.springframework.expression.Expression; import org.springframework.expression.Expression;
import org.springframework.expression.ParserContext; import org.springframework.expression.ParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser; 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 org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationEvent; import de.codecentric.boot.admin.event.ClientApplicationEvent;
...@@ -78,13 +81,17 @@ public class HipchatNotifier extends AbstractStatusChangeNotifier { ...@@ -78,13 +81,17 @@ public class HipchatNotifier extends AbstractStatusChangeNotifier {
authToken); authToken);
} }
protected Map<String, Object> createHipChatNotification(ClientApplicationEvent event) { protected HttpEntity<Map<String, Object>> createHipChatNotification(
Map<String, Object> result = new HashMap<>(); ClientApplicationEvent event) {
result.put("color", getColor(event)); Map<String, Object> body = new HashMap<>();
result.put("message", getMessage(event)); body.put("color", getColor(event));
result.put("notify", getNotify()); body.put("message", getMessage(event));
result.put("message_format", "html"); body.put("notify", getNotify());
return result; body.put("message_format", "html");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new HttpEntity<>(body, headers);
} }
protected boolean getNotify() { protected boolean getNotify() {
...@@ -97,7 +104,8 @@ public class HipchatNotifier extends AbstractStatusChangeNotifier { ...@@ -97,7 +104,8 @@ public class HipchatNotifier extends AbstractStatusChangeNotifier {
protected String getColor(ClientApplicationEvent event) { protected String getColor(ClientApplicationEvent event) {
if (event instanceof ClientApplicationStatusChangedEvent) { if (event instanceof ClientApplicationStatusChangedEvent) {
return "UP".equals(((ClientApplicationStatusChangedEvent) event).getTo().getStatus()) ? "green" : "red"; return "UP".equals(((ClientApplicationStatusChangedEvent) event).getTo().getStatus())
? "green" : "red";
} else { } else {
return "gray"; return "gray";
} }
......
package de.codecentric.boot.admin.notify; 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.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when;
import java.net.URI; import java.net.URI;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; 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 org.springframework.web.client.RestTemplate;
import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent; import de.codecentric.boot.admin.event.ClientApplicationStatusChangedEvent;
...@@ -40,17 +44,27 @@ public class HipchatNotifierTest { ...@@ -40,17 +44,27 @@ public class HipchatNotifierTest {
StatusInfo infoDown = StatusInfo.ofDown(); StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp(); 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( notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
infoDown, infoUp)); infoDown, infoUp));
Map<String, Object> expected = new HashMap<String, Object>(); assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type",
expected.put("color", "green"); asList("application/json"));
expected.put("message", "<strong>App</strong>/-id- is <strong>UP</strong>");
expected.put("notify", Boolean.TRUE); Map<String, Object> body = httpRequest.getValue().getBody();
expected.put("message_format", "html"); 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 @Test
...@@ -58,16 +72,25 @@ public class HipchatNotifierTest { ...@@ -58,16 +72,25 @@ public class HipchatNotifierTest {
StatusInfo infoDown = StatusInfo.ofDown(); StatusInfo infoDown = StatusInfo.ofDown();
StatusInfo infoUp = StatusInfo.ofUp(); 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( notifier.notify(new ClientApplicationStatusChangedEvent(
Application.create("App").withId("-id-").withHealthUrl("http://health").build(), Application.create("App").withId("-id-").withHealthUrl("http://health").build(),
infoUp, infoDown)); infoUp, infoDown));
Map<String, Object> expected = new HashMap<String, Object>(); assertThat(httpRequest.getValue().getHeaders()).containsEntry("Content-Type",
expected.put("color", "red"); asList("application/json"));
expected.put("message", "<strong>App</strong>/-id- is <strong>DOWN</strong>");
expected.put("notify", Boolean.TRUE);
expected.put("message_format", "html");
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