diff --git a/notification/src/main/java/com/amazon/opendistroforelasticsearch/alerting/destination/response/DestinationResponse.java b/notification/src/main/java/com/amazon/opendistroforelasticsearch/alerting/destination/response/DestinationResponse.java index 3eab4b6f..71712f87 100644 --- a/notification/src/main/java/com/amazon/opendistroforelasticsearch/alerting/destination/response/DestinationResponse.java +++ b/notification/src/main/java/com/amazon/opendistroforelasticsearch/alerting/destination/response/DestinationResponse.java @@ -15,8 +15,6 @@ package com.amazon.opendistroforelasticsearch.alerting.destination.response; -import org.elasticsearch.common.Strings; - /** * This class is a place holder for destination response metadata */ @@ -26,7 +24,7 @@ public class DestinationResponse extends BaseResponse { private DestinationResponse(final String responseString, final int statusCode) { super(statusCode); - if (Strings.isNullOrEmpty(responseString)) { + if (responseString == null) { throw new IllegalArgumentException("Response is missing"); } this.responseContent = responseString; diff --git a/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/ChimeDestinationTest.java b/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/ChimeDestinationTest.java index 16f28fa4..f4a2b5c0 100644 --- a/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/ChimeDestinationTest.java +++ b/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/ChimeDestinationTest.java @@ -24,6 +24,7 @@ import com.amazon.opendistroforelasticsearch.alerting.destination.response.DestinationResponse; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicStatusLine; import org.easymock.EasyMock; @@ -35,11 +36,14 @@ public class ChimeDestinationTest { @Test - public void testChimeMessage() throws Exception { + public void testChimeMessage_NullEntityResponse() throws Exception { CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); - DestinationResponse expectedChimeResponse = new DestinationResponse.Builder().withResponseContent("{}") - .withStatusCode(RestStatus.OK.getStatus()).build(); + // The DestinationHttpClient replaces a null entity with "{}". + DestinationResponse expectedChimeResponse = new DestinationResponse.Builder() + .withResponseContent("{}") + .withStatusCode(RestStatus.OK.getStatus()) + .build(); CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); @@ -70,6 +74,82 @@ public void testChimeMessage() throws Exception { assertEquals(expectedChimeResponse.getStatusCode(), actualChimeResponse.getStatusCode()); } + @Test + public void testChimeMessage_EmptyEntityResponse() throws Exception { + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); + + DestinationResponse expectedChimeResponse = new DestinationResponse.Builder() + .withResponseContent("") + .withStatusCode(RestStatus.OK.getStatus()) + .build(); + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); + + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); + + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity("")).anyTimes(); + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); + EasyMock.replay(mockHttpClient); + EasyMock.replay(httpResponse); + EasyMock.replay(mockStatusLine); + + DestinationHttpClient httpClient = new DestinationHttpClient(); + httpClient.setHttpClient(mockHttpClient); + ChimeDestinationFactory chimeDestinationFactory = new ChimeDestinationFactory(); + chimeDestinationFactory.setClient(httpClient); + + DestinationFactoryProvider.setFactory(DestinationType.CHIME, chimeDestinationFactory); + + String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " + + "link test: http://sample.com email test: marymajor@example.com All member callout: " + + "@All All Present member callout: @Present\"}"; + BaseMessage bm = new ChimeMessage.Builder("abc").withMessage(message). + withUrl("https://abc/com").build(); + DestinationResponse actualChimeResponse = (DestinationResponse) Notification.publish(bm); + + assertEquals(expectedChimeResponse.getResponseContent(), actualChimeResponse.getResponseContent()); + assertEquals(expectedChimeResponse.getStatusCode(), actualChimeResponse.getStatusCode()); + } + + @Test + public void testChimeMessage_NonemptyEntityResponse() throws Exception { + String responseContent = "It worked!"; + + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); + + DestinationResponse expectedChimeResponse = new DestinationResponse.Builder().withResponseContent(responseContent) + .withStatusCode(RestStatus.OK.getStatus()).build(); + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); + + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); + + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity(responseContent)).anyTimes(); + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); + EasyMock.replay(mockHttpClient); + EasyMock.replay(httpResponse); + EasyMock.replay(mockStatusLine); + + DestinationHttpClient httpClient = new DestinationHttpClient(); + httpClient.setHttpClient(mockHttpClient); + ChimeDestinationFactory chimeDestinationFactory = new ChimeDestinationFactory(); + chimeDestinationFactory.setClient(httpClient); + + DestinationFactoryProvider.setFactory(DestinationType.CHIME, chimeDestinationFactory); + + String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " + + "link test: http://sample.com email test: marymajor@example.com All member callout: " + + "@All All Present member callout: @Present\"}"; + BaseMessage bm = new ChimeMessage.Builder("abc").withMessage(message). + withUrl("https://abc/com").build(); + DestinationResponse actualChimeResponse = (DestinationResponse) Notification.publish(bm); + + assertEquals(expectedChimeResponse.getResponseContent(), actualChimeResponse.getResponseContent()); + assertEquals(expectedChimeResponse.getStatusCode(), actualChimeResponse.getStatusCode()); + } + @Test(expected = IllegalArgumentException.class) public void testUrlMissingMessage() { try { diff --git a/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/CustomWebhookMessageTest.java b/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/CustomWebhookMessageTest.java index 20a9af54..806c7c31 100644 --- a/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/CustomWebhookMessageTest.java +++ b/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/CustomWebhookMessageTest.java @@ -24,6 +24,7 @@ import com.amazon.opendistroforelasticsearch.alerting.destination.response.DestinationResponse; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicStatusLine; import org.easymock.EasyMock; @@ -38,12 +39,14 @@ public class CustomWebhookMessageTest { @Test - public void testCustomWebhookMessage() throws Exception { - + public void testCustomWebhookMessage_NullEntityResponse() throws Exception { CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); - DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder().withResponseContent("{}") - .withStatusCode(RestStatus.OK.getStatus()).build(); + // The DestinationHttpClient replaces a null entity with "{}". + DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder() + .withResponseContent("{}") + .withStatusCode(RestStatus.OK.getStatus()) + .build(); CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); @@ -79,6 +82,94 @@ public void testCustomWebhookMessage() throws Exception { assertEquals(expectedCustomWebhookResponse.getStatusCode(), actualCustomResponse.getStatusCode()); } + @Test + public void testCustomWebhookMessage_EmptyEntityResponse() throws Exception { + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); + + DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder() + .withResponseContent("") + .withStatusCode(RestStatus.OK.getStatus()) + .build(); + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); + + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); + + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity("")).anyTimes(); + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); + EasyMock.replay(mockHttpClient); + EasyMock.replay(httpResponse); + EasyMock.replay(mockStatusLine); + + DestinationHttpClient httpClient = new DestinationHttpClient(); + httpClient.setHttpClient(mockHttpClient); + CustomWebhookDestinationFactory customDestinationFactory = new CustomWebhookDestinationFactory(); + customDestinationFactory.setClient(httpClient); + + DestinationFactoryProvider.setFactory(DestinationType.CUSTOMWEBHOOK, customDestinationFactory); + + Map queryParams = new HashMap(); + queryParams.put("token", "R2x1UlN4ZHF8MXxxVFJpelJNVDgzdGNwMnVRenJwRFBHUkR0NlhROWhXOVVTZXpiTWx2azVr"); + + String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " + + "link test: http://sample.com email test: marymajor@example.com " + + "All member callout: @All All Present member callout: @Present\"}"; + BaseMessage bm = new CustomWebhookMessage.Builder("abc").withHost("hooks.chime.aws"). + withPath("incomingwebhooks/383c0e2b-d028-44f4-8d38-696754bc4574"). + withMessage(message). + withQueryParams(queryParams).build(); + DestinationResponse actualCustomResponse = (DestinationResponse) Notification.publish(bm); + + assertEquals(expectedCustomWebhookResponse.getResponseContent(), actualCustomResponse.getResponseContent()); + assertEquals(expectedCustomWebhookResponse.getStatusCode(), actualCustomResponse.getStatusCode()); + } + + @Test + public void testCustomWebhookMessage_NonemptyEntityResponse() throws Exception { + String responseContent = "It worked!"; + + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); + + DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder() + .withResponseContent(responseContent) + .withStatusCode(RestStatus.OK.getStatus()) + .build(); + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); + + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); + + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity(responseContent)).anyTimes(); + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); + EasyMock.replay(mockHttpClient); + EasyMock.replay(httpResponse); + EasyMock.replay(mockStatusLine); + + DestinationHttpClient httpClient = new DestinationHttpClient(); + httpClient.setHttpClient(mockHttpClient); + CustomWebhookDestinationFactory customDestinationFactory = new CustomWebhookDestinationFactory(); + customDestinationFactory.setClient(httpClient); + + DestinationFactoryProvider.setFactory(DestinationType.CUSTOMWEBHOOK, customDestinationFactory); + + Map queryParams = new HashMap(); + queryParams.put("token", "R2x1UlN4ZHF8MXxxVFJpelJNVDgzdGNwMnVRenJwRFBHUkR0NlhROWhXOVVTZXpiTWx2azVr"); + + String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " + + "link test: http://sample.com email test: marymajor@example.com " + + "All member callout: @All All Present member callout: @Present\"}"; + BaseMessage bm = new CustomWebhookMessage.Builder("abc").withHost("hooks.chime.aws"). + withPath("incomingwebhooks/383c0e2b-d028-44f4-8d38-696754bc4574"). + withMessage(message). + withQueryParams(queryParams).build(); + DestinationResponse actualCustomResponse = (DestinationResponse) Notification.publish(bm); + + assertEquals(expectedCustomWebhookResponse.getResponseContent(), actualCustomResponse.getResponseContent()); + assertEquals(expectedCustomWebhookResponse.getStatusCode(), actualCustomResponse.getStatusCode()); + } + @Test(expected = IllegalArgumentException.class) public void testUrlMissingMessage() { try { diff --git a/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/SlackDestinationTest.java b/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/SlackDestinationTest.java index cbcbdcf5..166d5427 100644 --- a/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/SlackDestinationTest.java +++ b/notification/src/test/java/com/amazon/opendistroforelasticsearch/alerting/destination/SlackDestinationTest.java @@ -24,6 +24,7 @@ import com.amazon.opendistroforelasticsearch.alerting.destination.response.DestinationResponse; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicStatusLine; import org.easymock.EasyMock; @@ -36,12 +37,14 @@ public class SlackDestinationTest { @Test - public void testSlackMessage() throws Exception { - + public void testSlackMessage_NullEntityResponse() throws Exception { CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); - DestinationResponse expectedSlackResponse = new DestinationResponse.Builder().withResponseContent("{}") - .withStatusCode(RestStatus.OK.getStatus()).build(); + // The DestinationHttpClient replaces a null entity with "{}". + DestinationResponse expectedSlackResponse = new DestinationResponse.Builder() + .withResponseContent("{}") + .withStatusCode(RestStatus.OK.getStatus()) + .build(); CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); @@ -73,6 +76,86 @@ public void testSlackMessage() throws Exception { assertEquals(expectedSlackResponse.getStatusCode(), actualSlackResponse.getStatusCode()); } + @Test + public void testSlackMessage_EmptyEntityResponse() throws Exception { + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); + + DestinationResponse expectedSlackResponse = new DestinationResponse.Builder() + .withResponseContent("") + .withStatusCode(RestStatus.OK.getStatus()) + .build(); + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); + + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); + + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity("")).anyTimes(); + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); + EasyMock.replay(mockHttpClient); + EasyMock.replay(httpResponse); + EasyMock.replay(mockStatusLine); + + DestinationHttpClient httpClient = new DestinationHttpClient(); + httpClient.setHttpClient(mockHttpClient); + SlackDestinationFactory slackDestinationFactory = new SlackDestinationFactory(); + slackDestinationFactory.setClient(httpClient); + + DestinationFactoryProvider.setFactory(DestinationType.SLACK, slackDestinationFactory); + + String message = "{\"text\":\"Vamshi Message gughjhjlkh Body emoji test: :) :+1: " + + "link test: http://sample.com email test: marymajor@example.com All member callout: " + + "@All All Present member callout: @Present\"}"; + BaseMessage bm = new SlackMessage.Builder("abc").withMessage(message). + withUrl("https://hooks.slack.com/services/xxxx/xxxxxx/xxxxxxxxx").build(); + + DestinationResponse actualSlackResponse = (DestinationResponse) Notification.publish(bm); + + assertEquals(expectedSlackResponse.getResponseContent(), actualSlackResponse.getResponseContent()); + assertEquals(expectedSlackResponse.getStatusCode(), actualSlackResponse.getStatusCode()); + } + + @Test + public void testSlackMessage_NonemptyEntityResponse() throws Exception { + String responseContent = "It worked!"; + + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); + + DestinationResponse expectedSlackResponse = new DestinationResponse.Builder() + .withResponseContent(responseContent) + .withStatusCode(RestStatus.OK.getStatus()) + .build(); + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); + + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); + + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity(responseContent)).anyTimes(); + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); + EasyMock.replay(mockHttpClient); + EasyMock.replay(httpResponse); + EasyMock.replay(mockStatusLine); + + DestinationHttpClient httpClient = new DestinationHttpClient(); + httpClient.setHttpClient(mockHttpClient); + SlackDestinationFactory slackDestinationFactory = new SlackDestinationFactory(); + slackDestinationFactory.setClient(httpClient); + + DestinationFactoryProvider.setFactory(DestinationType.SLACK, slackDestinationFactory); + + String message = "{\"text\":\"Vamshi Message gughjhjlkh Body emoji test: :) :+1: " + + "link test: http://sample.com email test: marymajor@example.com All member callout: " + + "@All All Present member callout: @Present\"}"; + BaseMessage bm = new SlackMessage.Builder("abc").withMessage(message). + withUrl("https://hooks.slack.com/services/xxxx/xxxxxx/xxxxxxxxx").build(); + + DestinationResponse actualSlackResponse = (DestinationResponse) Notification.publish(bm); + + assertEquals(expectedSlackResponse.getResponseContent(), actualSlackResponse.getResponseContent()); + assertEquals(expectedSlackResponse.getStatusCode(), actualSlackResponse.getStatusCode()); + } + @Test(expected = IllegalArgumentException.class) public void testUrlMissingMessage() { try { @@ -94,4 +177,4 @@ public void testContentMissingMessage() { throw ex; } } -} \ No newline at end of file +}