|
24 | 24 | import com.amazon.opendistroforelasticsearch.alerting.destination.response.DestinationResponse;
|
25 | 25 | import org.apache.http.client.methods.CloseableHttpResponse;
|
26 | 26 | import org.apache.http.client.methods.HttpPost;
|
| 27 | +import org.apache.http.entity.StringEntity; |
27 | 28 | import org.apache.http.impl.client.CloseableHttpClient;
|
28 | 29 | import org.apache.http.message.BasicStatusLine;
|
29 | 30 | import org.easymock.EasyMock;
|
|
38 | 39 | public class CustomWebhookMessageTest {
|
39 | 40 |
|
40 | 41 | @Test
|
41 |
| - public void testCustomWebhookMessage() throws Exception { |
42 |
| - |
| 42 | + public void testCustomWebhookMessage_NullEntityResponse() throws Exception { |
43 | 43 | CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class);
|
44 | 44 |
|
45 |
| - DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder().withResponseContent("{}") |
46 |
| - .withStatusCode(RestStatus.OK.getStatus()).build(); |
| 45 | + // The DestinationHttpClient replaces a null entity with "{}". |
| 46 | + DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder() |
| 47 | + .withResponseContent("{}") |
| 48 | + .withStatusCode(RestStatus.OK.getStatus()) |
| 49 | + .build(); |
47 | 50 | CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class);
|
48 | 51 | EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse);
|
49 | 52 |
|
@@ -79,6 +82,94 @@ public void testCustomWebhookMessage() throws Exception {
|
79 | 82 | assertEquals(expectedCustomWebhookResponse.getStatusCode(), actualCustomResponse.getStatusCode());
|
80 | 83 | }
|
81 | 84 |
|
| 85 | + @Test |
| 86 | + public void testCustomWebhookMessage_EmptyEntityResponse() throws Exception { |
| 87 | + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); |
| 88 | + |
| 89 | + DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder() |
| 90 | + .withResponseContent("") |
| 91 | + .withStatusCode(RestStatus.OK.getStatus()) |
| 92 | + .build(); |
| 93 | + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); |
| 94 | + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); |
| 95 | + |
| 96 | + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); |
| 97 | + |
| 98 | + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); |
| 99 | + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity("")).anyTimes(); |
| 100 | + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); |
| 101 | + EasyMock.replay(mockHttpClient); |
| 102 | + EasyMock.replay(httpResponse); |
| 103 | + EasyMock.replay(mockStatusLine); |
| 104 | + |
| 105 | + DestinationHttpClient httpClient = new DestinationHttpClient(); |
| 106 | + httpClient.setHttpClient(mockHttpClient); |
| 107 | + CustomWebhookDestinationFactory customDestinationFactory = new CustomWebhookDestinationFactory(); |
| 108 | + customDestinationFactory.setClient(httpClient); |
| 109 | + |
| 110 | + DestinationFactoryProvider.setFactory(DestinationType.CUSTOMWEBHOOK, customDestinationFactory); |
| 111 | + |
| 112 | + Map<String, String> queryParams = new HashMap<String, String>(); |
| 113 | + queryParams.put("token", "R2x1UlN4ZHF8MXxxVFJpelJNVDgzdGNwMnVRenJwRFBHUkR0NlhROWhXOVVTZXpiTWx2azVr"); |
| 114 | + |
| 115 | + String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " + |
| 116 | + "link test: http://sample.com email test: [email protected] " + |
| 117 | + "All member callout: @All All Present member callout: @Present\"}"; |
| 118 | + BaseMessage bm = new CustomWebhookMessage.Builder("abc").withHost("hooks.chime.aws"). |
| 119 | + withPath("incomingwebhooks/383c0e2b-d028-44f4-8d38-696754bc4574"). |
| 120 | + withMessage(message). |
| 121 | + withQueryParams(queryParams).build(); |
| 122 | + DestinationResponse actualCustomResponse = (DestinationResponse) Notification.publish(bm); |
| 123 | + |
| 124 | + assertEquals(expectedCustomWebhookResponse.getResponseContent(), actualCustomResponse.getResponseContent()); |
| 125 | + assertEquals(expectedCustomWebhookResponse.getStatusCode(), actualCustomResponse.getStatusCode()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testCustomWebhookMessage_NonemptyEntityResponse() throws Exception { |
| 130 | + String responseContent = "It worked!"; |
| 131 | + |
| 132 | + CloseableHttpClient mockHttpClient = EasyMock.createMock(CloseableHttpClient.class); |
| 133 | + |
| 134 | + DestinationResponse expectedCustomWebhookResponse = new DestinationResponse.Builder() |
| 135 | + .withResponseContent(responseContent) |
| 136 | + .withStatusCode(RestStatus.OK.getStatus()) |
| 137 | + .build(); |
| 138 | + CloseableHttpResponse httpResponse = EasyMock.createMock(CloseableHttpResponse.class); |
| 139 | + EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost.class))).andReturn(httpResponse); |
| 140 | + |
| 141 | + BasicStatusLine mockStatusLine = EasyMock.createMock(BasicStatusLine.class); |
| 142 | + |
| 143 | + EasyMock.expect(httpResponse.getStatusLine()).andReturn(mockStatusLine); |
| 144 | + EasyMock.expect(httpResponse.getEntity()).andReturn(new StringEntity(responseContent)).anyTimes(); |
| 145 | + EasyMock.expect(mockStatusLine.getStatusCode()).andReturn(RestStatus.OK.getStatus()); |
| 146 | + EasyMock.replay(mockHttpClient); |
| 147 | + EasyMock.replay(httpResponse); |
| 148 | + EasyMock.replay(mockStatusLine); |
| 149 | + |
| 150 | + DestinationHttpClient httpClient = new DestinationHttpClient(); |
| 151 | + httpClient.setHttpClient(mockHttpClient); |
| 152 | + CustomWebhookDestinationFactory customDestinationFactory = new CustomWebhookDestinationFactory(); |
| 153 | + customDestinationFactory.setClient(httpClient); |
| 154 | + |
| 155 | + DestinationFactoryProvider.setFactory(DestinationType.CUSTOMWEBHOOK, customDestinationFactory); |
| 156 | + |
| 157 | + Map<String, String> queryParams = new HashMap<String, String>(); |
| 158 | + queryParams.put("token", "R2x1UlN4ZHF8MXxxVFJpelJNVDgzdGNwMnVRenJwRFBHUkR0NlhROWhXOVVTZXpiTWx2azVr"); |
| 159 | + |
| 160 | + String message = "{\"Content\":\"Message gughjhjlkh Body emoji test: :) :+1: " + |
| 161 | + "link test: http://sample.com email test: [email protected] " + |
| 162 | + "All member callout: @All All Present member callout: @Present\"}"; |
| 163 | + BaseMessage bm = new CustomWebhookMessage.Builder("abc").withHost("hooks.chime.aws"). |
| 164 | + withPath("incomingwebhooks/383c0e2b-d028-44f4-8d38-696754bc4574"). |
| 165 | + withMessage(message). |
| 166 | + withQueryParams(queryParams).build(); |
| 167 | + DestinationResponse actualCustomResponse = (DestinationResponse) Notification.publish(bm); |
| 168 | + |
| 169 | + assertEquals(expectedCustomWebhookResponse.getResponseContent(), actualCustomResponse.getResponseContent()); |
| 170 | + assertEquals(expectedCustomWebhookResponse.getStatusCode(), actualCustomResponse.getStatusCode()); |
| 171 | + } |
| 172 | + |
82 | 173 | @Test(expected = IllegalArgumentException.class)
|
83 | 174 | public void testUrlMissingMessage() {
|
84 | 175 | try {
|
|
0 commit comments