From ba03272d29b4456f82a5148aababe5697b10ffeb Mon Sep 17 00:00:00 2001 From: Aditya Jindal <13850971+aditjind@users.noreply.github.com> Date: Tue, 5 Apr 2022 13:43:56 -0700 Subject: [PATCH] Removal of NotificationEvent Request, Response and SearchResults (#153) * Removal of NotificationEvent Request, Response , Search Results Addition of NotificationEventDoc and Removal of NotificationEventInfo Addition of NotificationEventDocTests Signed-off-by: Jindal * Removing NotificationEventDoc and NotificationEventDocTests Signed-off-by: Jindal Co-authored-by: Jindal --- .../NotificationsPluginInterface.kt | 21 - .../action/GetNotificationEventRequest.kt | 171 -------- .../action/GetNotificationEventResponse.kt | 70 ---- .../action/NotificationsActions.kt | 11 - .../model/NotificationEventInfo.kt | 122 ------ .../model/NotificationEventSearchResult.kt | 71 ---- .../NotificationsPluginInterfaceTests.kt | 45 -- .../GetNotificationEventRequestTests.kt | 258 ------------ .../GetNotificationEventResponseTests.kt | 345 --------------- .../model/NotificationEventInfoTests.kt | 273 ------------ .../NotificationEventSearchResultTests.kt | 396 ------------------ 11 files changed, 1783 deletions(-) delete mode 100644 src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequest.kt delete mode 100644 src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponse.kt delete mode 100644 src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfo.kt delete mode 100644 src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResult.kt delete mode 100644 src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequestTests.kt delete mode 100644 src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponseTests.kt delete mode 100644 src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfoTests.kt delete mode 100644 src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResultTests.kt diff --git a/src/main/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterface.kt b/src/main/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterface.kt index 2da0562d..c3baa938 100644 --- a/src/main/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterface.kt +++ b/src/main/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterface.kt @@ -18,8 +18,6 @@ import org.opensearch.commons.notifications.action.GetChannelListRequest import org.opensearch.commons.notifications.action.GetChannelListResponse import org.opensearch.commons.notifications.action.GetNotificationConfigRequest import org.opensearch.commons.notifications.action.GetNotificationConfigResponse -import org.opensearch.commons.notifications.action.GetNotificationEventRequest -import org.opensearch.commons.notifications.action.GetNotificationEventResponse import org.opensearch.commons.notifications.action.GetPluginFeaturesRequest import org.opensearch.commons.notifications.action.GetPluginFeaturesResponse import org.opensearch.commons.notifications.action.LegacyPublishNotificationRequest @@ -28,7 +26,6 @@ import org.opensearch.commons.notifications.action.NotificationsActions.CREATE_N import org.opensearch.commons.notifications.action.NotificationsActions.DELETE_NOTIFICATION_CONFIG_ACTION_TYPE import org.opensearch.commons.notifications.action.NotificationsActions.GET_CHANNEL_LIST_ACTION_TYPE import org.opensearch.commons.notifications.action.NotificationsActions.GET_NOTIFICATION_CONFIG_ACTION_TYPE -import org.opensearch.commons.notifications.action.NotificationsActions.GET_NOTIFICATION_EVENT_ACTION_TYPE import org.opensearch.commons.notifications.action.NotificationsActions.GET_PLUGIN_FEATURES_ACTION_TYPE import org.opensearch.commons.notifications.action.NotificationsActions.LEGACY_PUBLISH_NOTIFICATION_ACTION_TYPE import org.opensearch.commons.notifications.action.NotificationsActions.SEND_NOTIFICATION_ACTION_TYPE @@ -119,24 +116,6 @@ object NotificationsPluginInterface { ) } - /** - * Get notification events. - * @param client Node client for making transport action - * @param request The request object - * @param listener The listener for getting response - */ - fun getNotificationEvent( - client: NodeClient, - request: GetNotificationEventRequest, - listener: ActionListener - ) { - client.execute( - GET_NOTIFICATION_EVENT_ACTION_TYPE, - request, - wrapActionListener(listener) { response -> recreateObject(response) { GetNotificationEventResponse(it) } } - ) - } - /** * Get notification plugin features. * @param client Node client for making transport action diff --git a/src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequest.kt b/src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequest.kt deleted file mode 100644 index 66bfba6a..00000000 --- a/src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequest.kt +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.action - -import org.opensearch.action.ActionRequest -import org.opensearch.action.ActionRequestValidationException -import org.opensearch.action.ValidateActions -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.common.io.stream.StreamOutput -import org.opensearch.common.io.stream.Writeable -import org.opensearch.common.xcontent.ToXContent -import org.opensearch.common.xcontent.ToXContentObject -import org.opensearch.common.xcontent.XContentBuilder -import org.opensearch.common.xcontent.XContentParser -import org.opensearch.common.xcontent.XContentParserUtils -import org.opensearch.commons.notifications.NotificationConstants.DEFAULT_MAX_ITEMS -import org.opensearch.commons.notifications.NotificationConstants.EVENT_ID_LIST_TAG -import org.opensearch.commons.notifications.NotificationConstants.FILTER_PARAM_LIST_TAG -import org.opensearch.commons.notifications.NotificationConstants.FROM_INDEX_TAG -import org.opensearch.commons.notifications.NotificationConstants.MAX_ITEMS_TAG -import org.opensearch.commons.notifications.NotificationConstants.SORT_FIELD_TAG -import org.opensearch.commons.notifications.NotificationConstants.SORT_ORDER_TAG -import org.opensearch.commons.utils.STRING_READER -import org.opensearch.commons.utils.STRING_WRITER -import org.opensearch.commons.utils.enumReader -import org.opensearch.commons.utils.fieldIfNotNull -import org.opensearch.commons.utils.logger -import org.opensearch.commons.utils.stringList -import org.opensearch.search.sort.SortOrder -import java.io.IOException - -/** - * Action Request for getting notification event. - */ -class GetNotificationEventRequest : ActionRequest, ToXContentObject { - val eventIds: Set - val fromIndex: Int - val maxItems: Int - val sortField: String? - val sortOrder: SortOrder? - val filterParams: Map - - companion object { - private val log by logger(GetNotificationEventRequest::class.java) - - /** - * reader to create instance of class from writable. - */ - val reader = Writeable.Reader { GetNotificationEventRequest(it) } - - /** - * Creator used in REST communication. - * @param parser XContentParser to deserialize data from. - */ - @JvmStatic - @Throws(IOException::class) - fun parse(parser: XContentParser): GetNotificationEventRequest { - var eventIds: Set = setOf() - var fromIndex = 0 - var maxItems = DEFAULT_MAX_ITEMS - var sortField: String? = null - var sortOrder: SortOrder? = null - var filterParams: Map = mapOf() - - 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_LIST_TAG -> eventIds = parser.stringList().toSet() - FROM_INDEX_TAG -> fromIndex = parser.intValue() - MAX_ITEMS_TAG -> maxItems = parser.intValue() - SORT_FIELD_TAG -> sortField = parser.textOrNull() - SORT_ORDER_TAG -> sortOrder = SortOrder.fromString(parser.text()) - FILTER_PARAM_LIST_TAG -> filterParams = parser.mapStrings() - else -> { - parser.skipChildren() - log.info("Unexpected field: $fieldName, while parsing GetNotificationEventRequest") - } - } - } - return GetNotificationEventRequest(eventIds, fromIndex, maxItems, sortField, sortOrder, filterParams) - } - } - - /** - * {@inheritDoc} - */ - override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { - return builder!!.startObject() - .field(EVENT_ID_LIST_TAG, eventIds) - .field(FROM_INDEX_TAG, fromIndex) - .field(MAX_ITEMS_TAG, maxItems) - .fieldIfNotNull(SORT_FIELD_TAG, sortField) - .fieldIfNotNull(SORT_ORDER_TAG, sortOrder) - .field(FILTER_PARAM_LIST_TAG, filterParams) - .endObject() - } - - /** - * constructor for creating the class - * @param eventIds the ids of the notification events (other parameters are not relevant if ids are present) - * @param fromIndex the starting index for paginated response - * @param maxItems the maximum number of items to return for paginated response - * @param sortField the sort field if response has many items - * @param sortOrder the sort order if response has many items - * @param filterParams the filter parameters - */ - constructor( - eventIds: Set = setOf(), - fromIndex: Int = 0, - maxItems: Int = DEFAULT_MAX_ITEMS, - sortField: String? = null, - sortOrder: SortOrder? = null, - filterParams: Map = mapOf() - ) { - this.eventIds = eventIds - this.fromIndex = fromIndex - this.maxItems = maxItems - this.sortField = sortField - this.sortOrder = sortOrder - this.filterParams = filterParams - } - - /** - * {@inheritDoc} - */ - @Throws(IOException::class) - constructor(input: StreamInput) : super(input) { - eventIds = input.readStringList().toSet() - fromIndex = input.readInt() - maxItems = input.readInt() - sortField = input.readOptionalString() - sortOrder = input.readOptionalWriteable(enumReader(SortOrder::class.java)) - filterParams = input.readMap(STRING_READER, STRING_READER) - } - - /** - * {@inheritDoc} - */ - @Throws(IOException::class) - override fun writeTo(output: StreamOutput) { - super.writeTo(output) - output.writeStringCollection(eventIds) - output.writeInt(fromIndex) - output.writeInt(maxItems) - output.writeOptionalString(sortField) - output.writeOptionalWriteable(sortOrder) - output.writeMap(filterParams, STRING_WRITER, STRING_WRITER) - } - - /** - * {@inheritDoc} - */ - override fun validate(): ActionRequestValidationException? { - var validationException: ActionRequestValidationException? = null - if (fromIndex < 0) { - validationException = ValidateActions.addValidationError("fromIndex is -ve", validationException) - } - if (maxItems <= 0) { - validationException = ValidateActions.addValidationError("maxItems is not +ve", validationException) - } - return validationException - } -} diff --git a/src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponse.kt b/src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponse.kt deleted file mode 100644 index c512aa48..00000000 --- a/src/main/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponse.kt +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.action - -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.common.io.stream.StreamOutput -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.commons.notifications.model.NotificationEventSearchResult -import java.io.IOException - -/** - * Action Response for getting notification event. - */ -class GetNotificationEventResponse : BaseResponse { - val searchResult: NotificationEventSearchResult - - companion object { - - /** - * reader to create instance of class from writable. - */ - val reader = Writeable.Reader { GetNotificationEventResponse(it) } - - /** - * Creator used in REST communication. - * @param parser XContentParser to deserialize data from. - */ - @JvmStatic - @Throws(IOException::class) - fun parse(parser: XContentParser): GetNotificationEventResponse { - return GetNotificationEventResponse(NotificationEventSearchResult(parser)) - } - } - - /** - * constructor for creating the class - * @param searchResult the notification event list - */ - constructor(searchResult: NotificationEventSearchResult) { - this.searchResult = searchResult - } - - /** - * {@inheritDoc} - */ - @Throws(IOException::class) - constructor(input: StreamInput) : super(input) { - searchResult = NotificationEventSearchResult(input) - } - - /** - * {@inheritDoc} - */ - @Throws(IOException::class) - override fun writeTo(output: StreamOutput) { - searchResult.writeTo(output) - } - - /** - * {@inheritDoc} - */ - override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { - return searchResult.toXContent(builder, params) - } -} diff --git a/src/main/kotlin/org/opensearch/commons/notifications/action/NotificationsActions.kt b/src/main/kotlin/org/opensearch/commons/notifications/action/NotificationsActions.kt index 204d9326..e4a8cfe5 100644 --- a/src/main/kotlin/org/opensearch/commons/notifications/action/NotificationsActions.kt +++ b/src/main/kotlin/org/opensearch/commons/notifications/action/NotificationsActions.kt @@ -30,11 +30,6 @@ object NotificationsActions { */ const val GET_NOTIFICATION_CONFIG_NAME = "cluster:admin/opensearch/notifications/configs/get" - /** - * Get notification events transport action name. - */ - const val GET_NOTIFICATION_EVENT_NAME = "cluster:admin/opensearch/notifications/events/get" - /** * Get notification plugin features transport action name. */ @@ -80,12 +75,6 @@ object NotificationsActions { val GET_NOTIFICATION_CONFIG_ACTION_TYPE = ActionType(GET_NOTIFICATION_CONFIG_NAME, ::GetNotificationConfigResponse) - /** - * Get notification events transport action type. - */ - val GET_NOTIFICATION_EVENT_ACTION_TYPE = - ActionType(GET_NOTIFICATION_EVENT_NAME, ::GetNotificationEventResponse) - /** * Get notification plugin features transport action type. */ diff --git a/src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfo.kt b/src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfo.kt deleted file mode 100644 index 51921fc8..00000000 --- a/src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfo.kt +++ /dev/null @@ -1,122 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.model - -import org.opensearch.common.Strings -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.common.io.stream.StreamOutput -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.CREATED_TIME_TAG -import org.opensearch.commons.notifications.NotificationConstants.EVENT_ID_TAG -import org.opensearch.commons.notifications.NotificationConstants.EVENT_TAG -import org.opensearch.commons.notifications.NotificationConstants.UPDATED_TIME_TAG -import org.opensearch.commons.utils.logger -import java.io.IOException -import java.time.Instant - -/** - * Data class representing Notification event with information. - */ -data class NotificationEventInfo( - val eventId: String, - val lastUpdatedTime: Instant, - val createdTime: Instant, - val notificationEvent: NotificationEvent -) : BaseModel { - - init { - require(!Strings.isNullOrEmpty(eventId)) { "event id is null or empty" } - } - - companion object { - private val log by logger(NotificationEventInfo::class.java) - - /** - * reader to create instance of class from writable. - */ - val reader = Writeable.Reader { NotificationEventInfo(it) } - - /** - * Creator used in REST communication. - * @param parser XContentParser to deserialize data from. - */ - @JvmStatic - @Throws(IOException::class) - fun parse(parser: XContentParser): NotificationEventInfo { - var eventId: String? = null - var lastUpdatedTime: Instant? = null - var createdTime: Instant? = null - var notificationEvent: NotificationEvent? = 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 -> eventId = parser.text() - UPDATED_TIME_TAG -> lastUpdatedTime = Instant.ofEpochMilli(parser.longValue()) - CREATED_TIME_TAG -> createdTime = Instant.ofEpochMilli(parser.longValue()) - EVENT_TAG -> notificationEvent = NotificationEvent.parse(parser) - else -> { - parser.skipChildren() - log.info("Unexpected field: $fieldName, while parsing event info") - } - } - } - eventId ?: throw IllegalArgumentException("$EVENT_ID_TAG field absent") - lastUpdatedTime ?: throw IllegalArgumentException("$UPDATED_TIME_TAG field absent") - createdTime ?: throw IllegalArgumentException("$CREATED_TIME_TAG field absent") - notificationEvent ?: throw IllegalArgumentException("$EVENT_TAG field absent") - return NotificationEventInfo( - eventId, - lastUpdatedTime, - createdTime, - notificationEvent - ) - } - } - - /** - * {@inheritDoc} - */ - override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder { - builder!! - return builder.startObject() - .field(EVENT_ID_TAG, eventId) - .field(UPDATED_TIME_TAG, lastUpdatedTime.toEpochMilli()) - .field(CREATED_TIME_TAG, createdTime.toEpochMilli()) - .field(EVENT_TAG, notificationEvent) - .endObject() - } - - /** - * Constructor used in transport action communication. - * @param input StreamInput stream to deserialize data from. - */ - constructor(input: StreamInput) : this( - eventId = input.readString(), - lastUpdatedTime = input.readInstant(), - createdTime = input.readInstant(), - notificationEvent = NotificationEvent.reader.read(input) - ) - - /** - * {@inheritDoc} - */ - override fun writeTo(output: StreamOutput) { - output.writeString(eventId) - output.writeInstant(lastUpdatedTime) - output.writeInstant(createdTime) - notificationEvent.writeTo(output) - } -} diff --git a/src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResult.kt b/src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResult.kt deleted file mode 100644 index e9b68406..00000000 --- a/src/main/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResult.kt +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.opensearch.commons.notifications.model - -import org.apache.lucene.search.TotalHits -import org.opensearch.action.search.SearchResponse -import org.opensearch.common.io.stream.StreamInput -import org.opensearch.common.xcontent.XContentParser -import org.opensearch.commons.notifications.NotificationConstants.EVENT_LIST_TAG - -/** - * Notification Event search results - */ -class NotificationEventSearchResult : SearchResults { - - /** - * single item result constructor - */ - constructor(objectItem: NotificationEventInfo) : super(EVENT_LIST_TAG, objectItem) - - /** - * multiple items result constructor - */ - constructor(objectList: List) : this( - 0, - objectList.size.toLong(), - TotalHits.Relation.EQUAL_TO, - objectList - ) - - /** - * all param constructor - */ - constructor( - startIndex: Long, - totalHits: Long, - totalHitRelation: TotalHits.Relation, - objectList: List - ) : super(startIndex, totalHits, totalHitRelation, EVENT_LIST_TAG, objectList) - - /** - * Constructor used in transport action communication. - * @param input StreamInput stream to deserialize data from. - */ - constructor(input: StreamInput) : super(input, NotificationEventInfo.reader) - - /** - * Construct object from XContentParser - */ - constructor(parser: XContentParser) : super(parser, EVENT_LIST_TAG) - - /** - * Construct object from SearchResponse - */ - constructor(from: Long, response: SearchResponse, searchHitParser: SearchHitParser) : super( - from, - response, - searchHitParser, - EVENT_LIST_TAG - ) - - /** - * {@inheritDoc} - */ - override fun parseItem(parser: XContentParser): NotificationEventInfo { - return NotificationEventInfo.parse(parser) - } -} diff --git a/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt index aaa831d1..556c35b6 100644 --- a/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt +++ b/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt @@ -29,8 +29,6 @@ import org.opensearch.commons.notifications.action.GetChannelListRequest import org.opensearch.commons.notifications.action.GetChannelListResponse import org.opensearch.commons.notifications.action.GetNotificationConfigRequest import org.opensearch.commons.notifications.action.GetNotificationConfigResponse -import org.opensearch.commons.notifications.action.GetNotificationEventRequest -import org.opensearch.commons.notifications.action.GetNotificationEventResponse import org.opensearch.commons.notifications.action.GetPluginFeaturesRequest import org.opensearch.commons.notifications.action.GetPluginFeaturesResponse import org.opensearch.commons.notifications.action.LegacyPublishNotificationRequest @@ -42,15 +40,10 @@ 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.NotificationEventInfo -import org.opensearch.commons.notifications.model.NotificationEventSearchResult import org.opensearch.commons.notifications.model.SeverityType import org.opensearch.commons.notifications.model.Slack import org.opensearch.rest.RestStatus @@ -127,22 +120,6 @@ internal class NotificationsPluginInterfaceTests { verify(listener, times(1)).onResponse(eq(response)) } - @Test - fun getNotificationEvent() { - val request = mock(GetNotificationEventRequest::class.java) - val response = mockGetNotificationEventResponse() - val listener: ActionListener = - mock(ActionListener::class.java) as ActionListener - - doAnswer { - (it.getArgument(2) as ActionListener) - .onResponse(response) - }.whenever(client).execute(any(ActionType::class.java), any(), any()) - - NotificationsPluginInterface.getNotificationEvent(client, request, listener) - verify(listener, times(1)).onResponse(eq(response)) - } - @Test fun getPluginFeatures() { val request = mock(GetPluginFeaturesRequest::class.java) @@ -250,26 +227,4 @@ internal class NotificationsPluginInterfaceTests { ) return GetNotificationConfigResponse(NotificationConfigSearchResult(configInfo)) } - - private fun mockGetNotificationEventResponse(): GetNotificationEventResponse { - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - Instant.now(), - Instant.now(), - sampleEvent - ) - return GetNotificationEventResponse(NotificationEventSearchResult(eventInfo)) - } } diff --git a/src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequestTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequestTests.kt deleted file mode 100644 index 53bb825a..00000000 --- a/src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventRequestTests.kt +++ /dev/null @@ -1,258 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.action - -import com.fasterxml.jackson.core.JsonParseException -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.junit.jupiter.api.assertThrows -import org.opensearch.commons.utils.createObjectFromJsonString -import org.opensearch.commons.utils.getJsonString -import org.opensearch.commons.utils.recreateObject -import org.opensearch.search.sort.SortOrder - -internal class GetNotificationEventRequestTests { - - private fun assertGetRequestEquals( - expected: GetNotificationEventRequest, - actual: GetNotificationEventRequest - ) { - assertEquals(expected.eventIds, actual.eventIds) - assertEquals(expected.fromIndex, actual.fromIndex) - assertEquals(expected.maxItems, actual.maxItems) - assertEquals(expected.sortField, actual.sortField) - assertEquals(expected.sortOrder, actual.sortOrder) - assertEquals(expected.filterParams, actual.filterParams) - } - - @Test - fun `Get request serialize and deserialize transport object should be equal`() { - val configRequest = GetNotificationEventRequest( - setOf("sample_event_id"), - 0, - 10, - "sortField", - SortOrder.DESC, - mapOf(Pair("filterKey", "filterValue")) - ) - val recreatedObject = recreateObject(configRequest) { GetNotificationEventRequest(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request serialize and deserialize using json object should be equal`() { - val configRequest = GetNotificationEventRequest( - setOf("sample_event_id"), - 0, - 10, - "sortField", - SortOrder.ASC, - mapOf(Pair("filterKey", "filterValue")) - ) - val jsonString = getJsonString(configRequest) - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with all field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest( - setOf("sample_event_id"), - 10, - 100, - "sortField", - SortOrder.DESC, - mapOf( - Pair("filterKey1", "filterValue1"), - Pair("filterKey2", "true"), - Pair("filterKey3", "filter,Value,3"), - Pair("filterKey4", "4") - ) - ) - val jsonString = """ - { - "event_id_list":["${configRequest.eventIds.first()}"], - "from_index":"10", - "max_items":"100", - "sort_field":"sortField", - "sort_order":"desc", - "filter_param_list": { - "filterKey1":"filterValue1", - "filterKey2":"true", - "filterKey3":"filter,Value,3", - "filterKey4":"4" - } - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only event_id field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(eventIds = setOf("sample_event_id")) - val jsonString = """ - { - "event_id_list":["${configRequest.eventIds.first()}"] - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only from_index field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(fromIndex = 20) - val jsonString = """ - { - "from_index":"20" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only max_items field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(maxItems = 100) - val jsonString = """ - { - "max_items":"100" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only sort_field field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(sortField = "sample_sort_field") - val jsonString = """ - { - "sort_field":"sample_sort_field" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only sort_order=asc field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(sortOrder = SortOrder.ASC) - val jsonString = """ - { - "sort_order":"asc" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only sort_order=ASC field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(sortOrder = SortOrder.ASC) - val jsonString = """ - { - "sort_order":"ASC" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only sort_order=desc field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(sortOrder = SortOrder.DESC) - val jsonString = """ - { - "sort_order":"desc" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with only sort_order=DESC field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest(sortOrder = SortOrder.DESC) - val jsonString = """ - { - "sort_order":"DESC" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request with invalid sort_order should throw exception`() { - val jsonString = """ - { - "sort_order":"descending" - } - """.trimIndent() - assertThrows { - createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - } - } - - @Test - fun `Get request with only filter_param_list field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest( - filterParams = mapOf( - Pair("filterKey1", "filterValue1"), - Pair("filterKey2", "true"), - Pair("filterKey3", "filter,Value,3"), - Pair("filterKey4", "4") - ) - ) - val jsonString = """ - { - "filter_param_list": { - "filterKey1":"filterValue1", - "filterKey2":"true", - "filterKey3":"filter,Value,3", - "filterKey4":"4" - } - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request no field should deserialize json object using parser`() { - val configRequest = GetNotificationEventRequest() - val jsonString = """ - { - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } - - @Test - fun `Get request should throw exception when invalid json object is passed`() { - val jsonString = "sample message" - assertThrows { - createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - } - } - - @Test - fun `Get request should safely ignore extra field in json object`() { - val configRequest = GetNotificationEventRequest(eventIds = setOf("sample_event_id")) - val jsonString = """ - { - "event_id_list":["${configRequest.eventIds.first()}"], - "extra_field_1":["extra", "value"], - "extra_field_2":{"extra":"value"}, - "extra_field_3":"extra value 3" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventRequest.parse(it) } - assertGetRequestEquals(configRequest, recreatedObject) - } -} diff --git a/src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponseTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponseTests.kt deleted file mode 100644 index 0de1c78b..00000000 --- a/src/test/kotlin/org/opensearch/commons/notifications/action/GetNotificationEventResponseTests.kt +++ /dev/null @@ -1,345 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.action - -import org.apache.lucene.search.TotalHits -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -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.NotificationEventInfo -import org.opensearch.commons.notifications.model.NotificationEventSearchResult -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 -import java.time.Instant - -internal class GetNotificationEventResponseTests { - - private fun assertSearchResultEquals( - expected: NotificationEventSearchResult, - actual: NotificationEventSearchResult - ) { - assertEquals(expected.startIndex, actual.startIndex) - assertEquals(expected.totalHits, actual.totalHits) - assertEquals(expected.totalHitRelation, actual.totalHitRelation) - assertEquals(expected.objectListFieldName, actual.objectListFieldName) - assertEquals(expected.objectList, actual.objectList) - } - - @Test - fun `Search result serialize and deserialize with event object should be equal`() { - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - Instant.now(), - Instant.now(), - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val searchResponse = GetNotificationEventResponse(searchResult) - val recreatedObject = recreateObject(searchResponse) { GetNotificationEventResponse(it) } - assertSearchResultEquals(searchResult, recreatedObject.searchResult) - } - - @Test - fun `Search result serialize and deserialize with multiple event status object should be equal`() { - val eventSource1 = EventSource( - "title 1", - "reference_id_1", - severity = SeverityType.INFO - ) - val eventSource2 = EventSource( - "title 2", - "reference_id_2", - severity = SeverityType.HIGH - ) - val status1 = EventStatus( - "config_id1", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val status2 = EventStatus( - "config_id2", - "name", - ConfigType.CHIME, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val eventInfo1 = NotificationEventInfo( - "event_id1", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource1, listOf(status1)) - ) - val eventInfo2 = NotificationEventInfo( - "event_id2", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource2, listOf(status2)) - ) - val eventInfo3 = NotificationEventInfo( - "event_id3", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource1, listOf(status1, status2)) - ) - val eventInfo4 = NotificationEventInfo( - "event_id4", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource2, listOf(status1, status2)) - ) - val searchResult = NotificationEventSearchResult( - 100, - 1000, - TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO, - listOf(eventInfo1, eventInfo2, eventInfo3, eventInfo4) - ) - val searchResponse = GetNotificationEventResponse(searchResult) - val recreatedObject = recreateObject(searchResponse) { GetNotificationEventResponse(it) } - assertSearchResultEquals(searchResult, recreatedObject.searchResult) - } - - @Test - fun `Search result serialize and deserialize using json event object should be equal`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val searchResponse = GetNotificationEventResponse(searchResult) - val jsonString = getJsonString(searchResponse) - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventResponse.parse(it) } - assertSearchResultEquals(searchResult, recreatedObject.searchResult) - } - - @Test - fun `Search result serialize and deserialize using json with multiple event object should be equal`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val eventSource1 = EventSource( - "title 1", - "reference_id_1", - severity = SeverityType.INFO - ) - val eventSource2 = EventSource( - "title 2", - "reference_id_2", - severity = SeverityType.HIGH - ) - val status1 = EventStatus( - "config_id1", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val status2 = EventStatus( - "config_id2", - "name", - ConfigType.CHIME, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val eventInfo1 = NotificationEventInfo( - "event_id1", - lastUpdatedTimeMs, - createdTimeMs, - NotificationEvent(eventSource1, listOf(status1)) - ) - val eventInfo2 = NotificationEventInfo( - "event_id2", - lastUpdatedTimeMs, - createdTimeMs, - NotificationEvent(eventSource2, listOf(status2)) - ) - val searchResult = NotificationEventSearchResult( - 100, - 1000, - TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO, - listOf(eventInfo1, eventInfo2) - ) - val searchResponse = GetNotificationEventResponse(searchResult) - val jsonString = getJsonString(searchResponse) - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventResponse.parse(it) } - assertSearchResultEquals(searchResult, recreatedObject.searchResult) - } - - @Test - fun `Search result should safely ignore extra field in json object`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val jsonString = """ - { - "start_index":"0", - "total_hits":"1", - "total_hit_relation":"eq", - "event_list":[ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "severity":"info", - "tags":[] - }, - "status_list":[ - { - "config_id":"config_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - ], - "extra_field_1":["extra", "value"], - "extra_field_2":{"extra":"value"}, - "extra_field_3":"extra value 3" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventResponse.parse(it) } - assertSearchResultEquals(searchResult, recreatedObject.searchResult) - } - - @Test - fun `Search result should safely fallback to default if startIndex, totalHits or totalHitRelation field absent in json object`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val jsonString = """ - { - "event_list":[ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "severity":"info", - "tags":[] - }, - "status_list":[ - { - "config_id":"config_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - ] - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { GetNotificationEventResponse.parse(it) } - assertSearchResultEquals(searchResult, recreatedObject.searchResult) - } - - @Test - fun `Search result should throw exception if event is absent in json`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val jsonString = """ - { - "start_index":"0", - "total_hits":"1", - "total_hit_relation":"eq", - "event_list":[ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}" - } - ] - } - """.trimIndent() - Assertions.assertThrows(IllegalArgumentException::class.java) { - createObjectFromJsonString(jsonString) { GetNotificationEventResponse.parse(it) } - } - } -} diff --git a/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfoTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfoTests.kt deleted file mode 100644 index 85d2dee9..00000000 --- a/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventInfoTests.kt +++ /dev/null @@ -1,273 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.model - -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.opensearch.commons.utils.createObjectFromJsonString -import org.opensearch.commons.utils.getJsonString -import org.opensearch.commons.utils.recreateObject -import java.time.Instant - -internal class NotificationEventInfoTests { - - @Test - fun `Event info serialize and deserialize with event object should be equal`() { - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - Instant.now(), - Instant.now(), - sampleEvent - ) - val recreatedObject = recreateObject(eventInfo) { NotificationEventInfo(it) } - assertEquals(eventInfo, recreatedObject) - } - - @Test - fun `Event info serialize and deserialize using json event object should be equal`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val jsonString = getJsonString(eventInfo) - val recreatedObject = createObjectFromJsonString(jsonString) { NotificationEventInfo.parse(it) } - assertEquals(eventInfo, recreatedObject) - } - - @Test - fun `Event info should safely ignore extra field in json object`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val jsonString = """ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "feature":"alerting", - "severity":"info", - "tags":[] - }, - "status_list":[ - { - "config_id":"config_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - }, - "extra_field_1":["extra", "value"], - "extra_field_2":{"extra":"value"}, - "extra_field_3":"extra value 3" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { NotificationEventInfo.parse(it) } - assertEquals(eventInfo, recreatedObject) - } - - @Test - fun `Event info should throw exception if event_id is empty`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "event_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - Assertions.assertThrows(IllegalArgumentException::class.java) { - NotificationEventInfo( - "", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - } - } - - @Test - fun `Event info should throw exception if event_id is absent in json`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val jsonString = """ - { - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "feature":"alerting", - "severity":"info", - "tags":["tag1", "tag2"] - }, - "status_list":[ - { - "event_id":"event_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - """.trimIndent() - Assertions.assertThrows(IllegalArgumentException::class.java) { - createObjectFromJsonString(jsonString) { NotificationEventInfo.parse(it) } - } - } - - @Test - fun `Event info should throw exception if lastUpdatedTimeMs is absent in json`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val jsonString = """ - { - "event_id":"event_id", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "feature":"alerting", - "severity":"info", - "tags":["tag1", "tag2"] - }, - "status_list":[ - { - "event_id":"event_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - """.trimIndent() - Assertions.assertThrows(IllegalArgumentException::class.java) { - createObjectFromJsonString(jsonString) { NotificationEventInfo.parse(it) } - } - } - - @Test - fun `Event info should throw exception if createdTimeMs is absent in json`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val jsonString = """ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "feature":"alerting", - "severity":"info", - "tags":["tag1", "tag2"] - }, - "status_list":[ - { - "event_id":"event_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - """.trimIndent() - Assertions.assertThrows(IllegalArgumentException::class.java) { - createObjectFromJsonString(jsonString) { NotificationEventInfo.parse(it) } - } - } - - @Test - fun `Event info should throw exception if event is absent in json`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val jsonString = """ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}" - } - """.trimIndent() - Assertions.assertThrows(IllegalArgumentException::class.java) { - createObjectFromJsonString(jsonString) { NotificationEventInfo.parse(it) } - } - } -} diff --git a/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResultTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResultTests.kt deleted file mode 100644 index b65a0783..00000000 --- a/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationEventSearchResultTests.kt +++ /dev/null @@ -1,396 +0,0 @@ -/* - * Copyright OpenSearch Contributors - * SPDX-License-Identifier: Apache-2.0 - */ -package org.opensearch.commons.notifications.model - -import org.apache.lucene.search.TotalHits -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Assertions.assertEquals -import org.junit.jupiter.api.Test -import org.opensearch.commons.utils.createObjectFromJsonString -import org.opensearch.commons.utils.getJsonString -import org.opensearch.commons.utils.recreateObject -import java.time.Instant - -internal class NotificationEventSearchResultTests { - - private fun assertSearchResultEquals( - expected: NotificationEventSearchResult, - actual: NotificationEventSearchResult - ) { - assertEquals(expected.startIndex, actual.startIndex) - assertEquals(expected.totalHits, actual.totalHits) - assertEquals(expected.totalHitRelation, actual.totalHitRelation) - assertEquals(expected.objectListFieldName, actual.objectListFieldName) - assertEquals(expected.objectList, actual.objectList) - } - - @Test - fun `Search result serialize and deserialize with event object should be equal`() { - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - Instant.now(), - Instant.now(), - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val recreatedObject = recreateObject(searchResult) { NotificationEventSearchResult(it) } - assertSearchResultEquals(searchResult, recreatedObject) - } - - @Test - fun `Search result serialize and deserialize with multiple event default values should be equal`() { - val eventSource1 = EventSource( - "title 1", - "reference_id_1", - severity = SeverityType.INFO - ) - val eventSource2 = EventSource( - "title 2", - "reference_id_2", - severity = SeverityType.HIGH - ) - val status1 = EventStatus( - "config_id1", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val status2 = EventStatus( - "config_id2", - "name", - ConfigType.CHIME, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val eventInfo1 = NotificationEventInfo( - "event_id1", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource1, listOf(status1)) - ) - val eventInfo2 = NotificationEventInfo( - "event_id2", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource2, listOf(status2)) - ) - val eventInfo3 = NotificationEventInfo( - "event_id3", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource1, listOf(status1, status2)) - ) - val eventInfo4 = NotificationEventInfo( - "event_id4", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource2, listOf(status1, status2)) - ) - val searchResult = NotificationEventSearchResult( - listOf(eventInfo1, eventInfo2, eventInfo3, eventInfo4) - ) - val expectedResult = NotificationEventSearchResult( - 0, - 4, - TotalHits.Relation.EQUAL_TO, - listOf(eventInfo1, eventInfo2, eventInfo3, eventInfo4) - ) - val recreatedObject = recreateObject(searchResult) { NotificationEventSearchResult(it) } - assertSearchResultEquals(expectedResult, recreatedObject) - } - - @Test - fun `Search result serialize and deserialize with multiple event status object should be equal`() { - val eventSource1 = EventSource( - "title 1", - "reference_id_1", - severity = SeverityType.INFO - ) - val eventSource2 = EventSource( - "title 2", - "reference_id_2", - severity = SeverityType.HIGH - ) - val status1 = EventStatus( - "config_id1", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val status2 = EventStatus( - "config_id2", - "name", - ConfigType.CHIME, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val eventInfo1 = NotificationEventInfo( - "event_id1", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource1, listOf(status1)) - ) - val eventInfo2 = NotificationEventInfo( - "event_id2", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource2, listOf(status2)) - ) - val eventInfo3 = NotificationEventInfo( - "event_id3", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource1, listOf(status1, status2)) - ) - val eventInfo4 = NotificationEventInfo( - "event_id4", - Instant.now(), - Instant.now(), - NotificationEvent(eventSource2, listOf(status1, status2)) - ) - val searchResult = NotificationEventSearchResult( - 100, - 1000, - TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO, - listOf(eventInfo1, eventInfo2, eventInfo3, eventInfo4) - ) - val recreatedObject = recreateObject(searchResult) { NotificationEventSearchResult(it) } - assertSearchResultEquals(searchResult, recreatedObject) - } - - @Test - fun `Search result serialize and deserialize using json event object should be equal`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val jsonString = getJsonString(searchResult) - val recreatedObject = createObjectFromJsonString(jsonString) { NotificationEventSearchResult(it) } - assertSearchResultEquals(searchResult, recreatedObject) - } - - @Test - fun `Search result serialize and deserialize using json with multiple event object should be equal`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val eventSource1 = EventSource( - "title 1", - "reference_id_1", - severity = SeverityType.INFO - ) - val eventSource2 = EventSource( - "title 2", - "reference_id_2", - severity = SeverityType.HIGH - ) - val status1 = EventStatus( - "config_id1", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val status2 = EventStatus( - "config_id2", - "name", - ConfigType.CHIME, - deliveryStatus = DeliveryStatus("404", "invalid recipient") - ) - val eventInfo1 = NotificationEventInfo( - "event_id1", - lastUpdatedTimeMs, - createdTimeMs, - NotificationEvent(eventSource1, listOf(status1)) - ) - val eventInfo2 = NotificationEventInfo( - "event_id2", - lastUpdatedTimeMs, - createdTimeMs, - NotificationEvent(eventSource2, listOf(status2)) - ) - val searchResult = NotificationEventSearchResult( - 100, - 1000, - TotalHits.Relation.GREATER_THAN_OR_EQUAL_TO, - listOf(eventInfo1, eventInfo2) - ) - val jsonString = getJsonString(searchResult) - val recreatedObject = createObjectFromJsonString(jsonString) { NotificationEventSearchResult(it) } - assertSearchResultEquals(searchResult, recreatedObject) - } - - @Test - fun `Search result should safely ignore extra field in json object`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val jsonString = """ - { - "start_index":"0", - "total_hits":"1", - "total_hit_relation":"eq", - "event_list":[ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "feature":"alerting", - "severity":"info", - "tags":[] - }, - "status_list":[ - { - "config_id":"config_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - ], - "extra_field_1":["extra", "value"], - "extra_field_2":{"extra":"value"}, - "extra_field_3":"extra value 3" - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { NotificationEventSearchResult(it) } - assertSearchResultEquals(searchResult, recreatedObject) - } - - @Test - fun `Search result should safely fallback to default if startIndex, totalHits or totalHitRelation field absent in json object`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val sampleEventSource = EventSource( - "title", - "reference_id", - severity = SeverityType.INFO - ) - val sampleStatus = EventStatus( - "config_id", - "name", - ConfigType.SLACK, - deliveryStatus = DeliveryStatus("200", "success") - ) - val sampleEvent = NotificationEvent(sampleEventSource, listOf(sampleStatus)) - val eventInfo = NotificationEventInfo( - "event_id", - lastUpdatedTimeMs, - createdTimeMs, - sampleEvent - ) - val searchResult = NotificationEventSearchResult(eventInfo) - val jsonString = """ - { - "event_list":[ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}", - "event":{ - "event_source":{ - "title":"title", - "reference_id":"reference_id", - "feature":"alerting", - "severity":"info", - "tags":[] - }, - "status_list":[ - { - "config_id":"config_id", - "config_type":"slack", - "config_name":"name", - "delivery_status": - { - "status_code":"200", - "status_text":"success" - } - } - ] - } - } - ] - } - """.trimIndent() - val recreatedObject = createObjectFromJsonString(jsonString) { NotificationEventSearchResult(it) } - assertSearchResultEquals(searchResult, recreatedObject) - } - - @Test - fun `Search result should throw exception if event is absent in json`() { - val lastUpdatedTimeMs = Instant.ofEpochMilli(Instant.now().toEpochMilli()) - val createdTimeMs = lastUpdatedTimeMs.minusSeconds(1000) - val jsonString = """ - { - "start_index":"0", - "total_hits":"1", - "total_hit_relation":"eq", - "event_list":[ - { - "event_id":"event_id", - "last_updated_time_ms":"${lastUpdatedTimeMs.toEpochMilli()}", - "created_time_ms":"${createdTimeMs.toEpochMilli()}" - } - ] - } - """.trimIndent() - Assertions.assertThrows(IllegalArgumentException::class.java) { - createObjectFromJsonString(jsonString) { NotificationEventSearchResult(it) } - } - } -}