Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Allow empty notification responses #272

Merged
Merged
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
Expand Up @@ -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
*/
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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: [email protected] 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: [email protected] 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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<String, String> queryParams = new HashMap<String, String>();
queryParams.put("token", "R2x1UlN4ZHF8MXxxVFJpelJNVDgzdGNwMnVRenJwRFBHUkR0NlhROWhXOVVTZXpiTWx2azVr");

String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " +
"link test: http://sample.com email test: [email protected] " +
"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<String, String> queryParams = new HashMap<String, String>();
queryParams.put("token", "R2x1UlN4ZHF8MXxxVFJpelJNVDgzdGNwMnVRenJwRFBHUkR0NlhROWhXOVVTZXpiTWx2azVr");

String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " +
"link test: http://sample.com email test: [email protected] " +
"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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down Expand Up @@ -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: [email protected] 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: [email protected] 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 {
Expand All @@ -94,4 +177,4 @@ public void testContentMissingMessage() {
throw ex;
}
}
}
}