From 6c5c1a6b44a4af303f0328f940e74ef34ea4330b Mon Sep 17 00:00:00 2001 From: Hailong Cui Date: Thu, 30 Mar 2023 15:42:48 +0800 Subject: [PATCH] convert empty httpEntity to {} to avoid DeliveryStatus initialization exception Signed-off-by: Hailong Cui --- .../core/client/DestinationHttpClient.kt | 4 +++- .../core/destinations/ChimeDestinationTests.kt | 2 +- .../destinations/CustomWebhookDestinationTests.kt | 15 ++++++++++++++- .../core/destinations/SlackDestinationTests.kt | 2 +- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/notifications/core/src/main/kotlin/org/opensearch/notifications/core/client/DestinationHttpClient.kt b/notifications/core/src/main/kotlin/org/opensearch/notifications/core/client/DestinationHttpClient.kt index de9751cd..9493d3d4 100644 --- a/notifications/core/src/main/kotlin/org/opensearch/notifications/core/client/DestinationHttpClient.kt +++ b/notifications/core/src/main/kotlin/org/opensearch/notifications/core/client/DestinationHttpClient.kt @@ -141,7 +141,9 @@ class DestinationHttpClient { @Throws(IOException::class) fun getResponseString(response: CloseableHttpResponse): String { val entity: HttpEntity = response.entity ?: return "{}" - return EntityUtils.toString(entity) + val responseString = EntityUtils.toString(entity) + // DeliveryStatus need statusText must not be empty, convert empty response to {} + return if (responseString.isNullOrEmpty()) "{}" else responseString } @Throws(IOException::class) diff --git a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/ChimeDestinationTests.kt b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/ChimeDestinationTests.kt index 607ee06e..209d7388 100644 --- a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/ChimeDestinationTests.kt +++ b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/ChimeDestinationTests.kt @@ -88,7 +88,7 @@ internal class ChimeDestinationTests { @Test fun `test chime message empty entity response`() { val mockHttpClient: CloseableHttpClient = EasyMock.createMock(CloseableHttpClient::class.java) - val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "") + val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "{}") val httpResponse = mockk() EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost::class.java))).andReturn(httpResponse) diff --git a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/CustomWebhookDestinationTests.kt b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/CustomWebhookDestinationTests.kt index fc344252..c4db2ae0 100644 --- a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/CustomWebhookDestinationTests.kt +++ b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/CustomWebhookDestinationTests.kt @@ -104,7 +104,7 @@ internal class CustomWebhookDestinationTests { @MethodSource("methodToHttpRequestType") fun `test custom webhook message empty entity response`(method: String, expectedHttpClass: Class) { val mockHttpClient: CloseableHttpClient = EasyMock.createMock(CloseableHttpClient::class.java) - val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "") + val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "{}") val httpResponse = mockk() EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost::class.java))).andReturn(httpResponse) @@ -218,4 +218,17 @@ internal class CustomWebhookDestinationTests { val actualRequestBody = httpClient.buildRequestBody(destination, message) assertEquals(messageText, actualRequestBody) } + + @Test + fun `test get response string`() { + val httpClient = DestinationHttpClient() + val response = mockk() + every { response.entity } returns null + var responseString = httpClient.getResponseString(response) + assertEquals(responseString, "{}") + + every { response.entity } returns StringEntity("") + responseString = httpClient.getResponseString(response) + assertEquals(responseString, "{}") + } } diff --git a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/SlackDestinationTests.kt b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/SlackDestinationTests.kt index 7bc642bf..e2d0216d 100644 --- a/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/SlackDestinationTests.kt +++ b/notifications/core/src/test/kotlin/org/opensearch/notifications/core/destinations/SlackDestinationTests.kt @@ -90,7 +90,7 @@ internal class SlackDestinationTests { @Test fun `test Slack message empty entity response`() { val mockHttpClient: CloseableHttpClient = EasyMock.createMock(CloseableHttpClient::class.java) - val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "") + val expectedWebhookResponse = DestinationMessageResponse(RestStatus.OK.status, "{}") val httpResponse = mockk() EasyMock.expect(mockHttpClient.execute(EasyMock.anyObject(HttpPost::class.java))).andReturn(httpResponse)