Skip to content

Commit

Permalink
Addition of NotificationEvent to SendNotificationResponse and Removal…
Browse files Browse the repository at this point in the history
… of NotificationID (opensearch-project#156)

* Removal of NotificationEvent Request, Response , Search Results

Addition of NotificationEventDoc and Removal of NotificationEventInfo

Addition of NotificationEventDocTests

Signed-off-by: Jindal <aditjind@88665a235f8e.ant.amazon.com>

* Removing NotificationEventDoc and NotificationEventDocTests

Signed-off-by: Jindal <aditjind@88665a235f8e.ant.amazon.com>

Co-authored-by: Jindal <aditjind@88665a235f8e.ant.amazon.com>
  • Loading branch information
adityaj1107 and Jindal committed Apr 7, 2022
1 parent 2ffa50d commit a244ff9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,16 @@ import org.opensearch.common.io.stream.Writeable
import org.opensearch.common.xcontent.ToXContent
import org.opensearch.common.xcontent.XContentBuilder
import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.notifications.NotificationConstants.EVENT_ID_TAG
import org.opensearch.commons.utils.logger
import org.opensearch.commons.notifications.model.NotificationEvent
import java.io.IOException

/**
* Action Response for send notification.
*/
class SendNotificationResponse : BaseResponse {
val notificationId: String
val notificationEvent: NotificationEvent

companion object {
private val log by logger(SendNotificationResponse::class.java)

/**
* reader to create instance of class from writable.
Expand All @@ -36,60 +33,38 @@ class SendNotificationResponse : BaseResponse {
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser): SendNotificationResponse {
var notificationId: String? = null

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
parser.currentToken(),
parser
)
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
EVENT_ID_TAG -> notificationId = parser.text()
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing SendNotificationResponse")
}
}
}
notificationId ?: throw IllegalArgumentException("$EVENT_ID_TAG field absent")
return SendNotificationResponse(notificationId)
return SendNotificationResponse(NotificationEvent.parse(parser))
}
}

/**
* constructor for creating the class
* @param configId the id of the created notification configuration
* @param notificationEvent the id of the created notification configuration
*/
constructor(configId: String) {
this.notificationId = configId
constructor(notificationEvent: NotificationEvent) {
this.notificationEvent = notificationEvent
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
constructor(input: StreamInput) : super(input) {
notificationId = input.readString()
notificationEvent = NotificationEvent(input)
}

/**
* {@inheritDoc}
*/
@Throws(IOException::class)
override fun writeTo(output: StreamOutput) {
output.writeString(notificationId)
notificationEvent.writeTo(output)
}

/**
* {@inheritDoc}
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
return builder.startObject()
.field(EVENT_ID_TAG, notificationId)
.endObject()
return notificationEvent.toXContent(builder, params)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ import org.opensearch.commons.notifications.model.Channel
import org.opensearch.commons.notifications.model.ChannelList
import org.opensearch.commons.notifications.model.ChannelMessage
import org.opensearch.commons.notifications.model.ConfigType
import org.opensearch.commons.notifications.model.DeliveryStatus
import org.opensearch.commons.notifications.model.EventSource
import org.opensearch.commons.notifications.model.EventStatus
import org.opensearch.commons.notifications.model.NotificationConfig
import org.opensearch.commons.notifications.model.NotificationConfigInfo
import org.opensearch.commons.notifications.model.NotificationConfigSearchResult
import org.opensearch.commons.notifications.model.NotificationEvent
import org.opensearch.commons.notifications.model.SeverityType
import org.opensearch.commons.notifications.model.Slack
import org.opensearch.rest.RestStatus
Expand Down Expand Up @@ -180,7 +183,16 @@ internal class NotificationsPluginInterfaceTests {
null
)

val response = SendNotificationResponse("configId")
val sampleStatus = EventStatus(
"config_id",
"name",
ConfigType.SLACK,
deliveryStatus = DeliveryStatus("404", "invalid recipient")
)

val sampleEvent = NotificationEvent(notificationInfo, listOf(sampleStatus))

val response = SendNotificationResponse(sampleEvent)
val listener: ActionListener<SendNotificationResponse> =
mock(ActionListener::class.java) as ActionListener<SendNotificationResponse>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,15 @@
package org.opensearch.commons.notifications.action

import com.fasterxml.jackson.core.JsonParseException
import org.junit.Test
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import org.opensearch.commons.notifications.model.ConfigType
import org.opensearch.commons.notifications.model.DeliveryStatus
import org.opensearch.commons.notifications.model.EventSource
import org.opensearch.commons.notifications.model.EventStatus
import org.opensearch.commons.notifications.model.NotificationEvent
import org.opensearch.commons.notifications.model.SeverityType
import org.opensearch.commons.utils.createObjectFromJsonString
import org.opensearch.commons.utils.getJsonString
import org.opensearch.commons.utils.recreateObject
Expand All @@ -16,25 +22,29 @@ internal class SendNotificationResponseTests {

@Test
fun `Create response serialize and deserialize transport object should be equal`() {
val configResponse = SendNotificationResponse("sample_notification_id")
val recreatedObject = recreateObject(configResponse) { SendNotificationResponse(it) }
assertEquals(configResponse.notificationId, recreatedObject.notificationId)

val sampleEvent = getSampleEvent()

val recreatedObject = recreateObject(sampleEvent) { SendNotificationResponse(it) }
assertEquals(sampleEvent, recreatedObject)
}

@Test
fun `Create response serialize and deserialize using json object should be equal`() {
val configResponse = SendNotificationResponse("sample_notification_id")
val jsonString = getJsonString(configResponse)

val sampleEvent = getSampleEvent()

val jsonString = getJsonString(sampleEvent)
val recreatedObject = createObjectFromJsonString(jsonString) { SendNotificationResponse.parse(it) }
assertEquals(configResponse.notificationId, recreatedObject.notificationId)
assertEquals(sampleEvent, recreatedObject)
}

@Test
fun `Create response should deserialize json object using parser`() {
val notificationId = "sample_notification_id"
val jsonString = "{\"event_id\":\"$notificationId\"}"
val sampleEvent = getSampleEvent()
val jsonString = "{\"event_id\":\"$sampleEvent\"}"
val recreatedObject = createObjectFromJsonString(jsonString) { SendNotificationResponse.parse(it) }
assertEquals(notificationId, recreatedObject.notificationId)
assertEquals(sampleEvent, recreatedObject)
}

@Test
Expand All @@ -55,16 +65,32 @@ internal class SendNotificationResponseTests {

@Test
fun `Create response should safely ignore extra field in json object`() {
val notificationId = "sample_notification_id"
val sampleEvent = getSampleEvent()
val jsonString = """
{
"event_id":"$notificationId",
"event_id":"$sampleEvent",
"extra_field_1":["extra", "value"],
"extra_field_2":{"extra":"value"},
"extra_field_3":"extra value 3"
}
""".trimIndent()
val recreatedObject = createObjectFromJsonString(jsonString) { SendNotificationResponse.parse(it) }
assertEquals(notificationId, recreatedObject.notificationId)
assertEquals(sampleEvent, recreatedObject)
}

private fun getSampleEvent(): NotificationEvent {
val sampleEventSource = EventSource(
"title",
"reference_id",
severity = SeverityType.INFO
)
val sampleStatus = EventStatus(
"config_id",
"name",
ConfigType.SLACK,
deliveryStatus = DeliveryStatus("404", "invalid recipient")
)

return NotificationEvent(sampleEventSource, listOf(sampleStatus))
}
}

0 comments on commit a244ff9

Please sign in to comment.