From 479d9892a9d37bc397f0be7be4bcbdf1ae3f71a3 Mon Sep 17 00:00:00 2001 From: Chase Engelbrecht Date: Wed, 13 Mar 2024 10:10:38 -0700 Subject: [PATCH] Run ktlintFormat Signed-off-by: Chase Engelbrecht --- .../action/AcknowledgeAlertResponse.kt | 1 - .../alerting/action/AlertingActions.kt | 4 +++ .../BucketSelectorExtAggregator.kt | 5 +++- .../BucketSelectorExtFilter.kt | 1 + .../commons/alerting/alerts/AlertError.kt | 1 - .../alerting/model/AggregationResultBucket.kt | 6 ++-- .../commons/alerting/model/Alert.kt | 14 +++++---- .../alerting/model/BucketLevelTrigger.kt | 3 +- .../alerting/model/ClusterMetricsInput.kt | 30 ++++++++++++------- .../commons/alerting/model/DataSources.kt | 2 +- .../alerting/model/DocLevelMonitorInput.kt | 9 ++++-- .../commons/alerting/model/DocLevelQuery.kt | 6 ++-- .../alerting/model/DocumentLevelTrigger.kt | 6 ++-- .../commons/alerting/model/Finding.kt | 3 +- .../commons/alerting/model/FindingDocument.kt | 3 +- .../commons/alerting/model/Monitor.kt | 21 +++++++++---- .../alerting/model/QueryLevelTrigger.kt | 10 +++++-- .../commons/alerting/model/Schedule.kt | 11 ++++--- .../commons/alerting/model/SearchInput.kt | 3 +- .../commons/alerting/model/action/Action.kt | 5 +++- .../notifications/model/ChannelMessage.kt | 1 - .../opensearch/commons/alerting/AlertTests.kt | 2 +- .../commons/alerting/TestHelpers.kt | 16 +++++++--- .../action/AcknowledgeAlertResponseTests.kt | 1 - .../action/DeleteMonitorRequestTests.kt | 1 - .../alerting/action/GetAlertsRequestTests.kt | 2 -- .../action/GetFindingsRequestTests.kt | 1 - .../action/GetFindingsResponseTests.kt | 1 - .../action/IndexMonitorRequestTests.kt | 26 ++++++++++++---- .../commons/alerting/model/FindingTests.kt | 2 +- .../commons/alerting/model/ScheduleTest.kt | 12 +++++--- .../NotificationsPluginInterfaceTests.kt | 6 +++- .../action/SendNotificationResponseTests.kt | 2 -- .../model/NotificationConfigTests.kt | 1 - .../notifications/model/SmtpAccountTests.kt | 3 +- 35 files changed, 147 insertions(+), 74 deletions(-) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponse.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponse.kt index de0e26da..3fbb12ad 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponse.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponse.kt @@ -45,7 +45,6 @@ class AcknowledgeAlertResponse : BaseResponse { @Throws(IOException::class) override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder { - builder.startObject().startArray("success") acknowledged.forEach { builder.value(it.id) } builder.endArray().startArray("failed") diff --git a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt index 23a5ce77..739217f2 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/action/AlertingActions.kt @@ -16,15 +16,19 @@ object AlertingActions { @JvmField val INDEX_MONITOR_ACTION_TYPE = ActionType(INDEX_MONITOR_ACTION_NAME, ::IndexMonitorResponse) + @JvmField val GET_ALERTS_ACTION_TYPE = ActionType(GET_ALERTS_ACTION_NAME, ::GetAlertsResponse) + @JvmField val DELETE_MONITOR_ACTION_TYPE = ActionType(DELETE_MONITOR_ACTION_NAME, ::DeleteMonitorResponse) + @JvmField val GET_FINDINGS_ACTION_TYPE = ActionType(GET_FINDINGS_ACTION_NAME, ::GetFindingsResponse) + @JvmField val ACKNOWLEDGE_ALERTS_ACTION_TYPE = ActionType(ACKNOWLEDGE_ALERTS_ACTION_NAME, ::AcknowledgeAlertResponse) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtAggregator.kt b/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtAggregator.kt index 373bd5e9..c0e48b41 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtAggregator.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtAggregator.kt @@ -129,7 +129,10 @@ class BucketSelectorExtAggregator : SiblingPipelineAggregator { } return BucketSelectorIndices( - name(), parentBucketPath, selectedBucketsIndex, originalAgg.metadata + name(), + parentBucketPath, + selectedBucketsIndex, + originalAgg.metadata ) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtFilter.kt b/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtFilter.kt index 0a776d0f..48b0f388 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtFilter.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/aggregation/bucketselectorext/BucketSelectorExtFilter.kt @@ -14,6 +14,7 @@ import java.io.IOException class BucketSelectorExtFilter : BaseModel { // used for composite aggregations val filtersMap: HashMap? + // used for filtering string term aggregation val filters: IncludeExclude? diff --git a/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt b/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt index 9854f9e5..7df3d15e 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/alerts/AlertError.kt @@ -33,7 +33,6 @@ data class AlertError(val timestamp: Instant, val message: String) : Writeable, @JvmStatic @Throws(IOException::class) fun parse(xcp: XContentParser): AlertError { - lateinit var timestamp: Instant lateinit var message: String diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/AggregationResultBucket.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/AggregationResultBucket.kt index 2c75332d..ad9de3c7 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/AggregationResultBucket.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/AggregationResultBucket.kt @@ -59,8 +59,10 @@ data class AggregationResultBucket( throw ParsingException( xcp.tokenLocation, String.format( - Locale.ROOT, "Failed to parse object: expecting token with name [%s] but found [%s]", - CONFIG_NAME, xcp.currentName() + Locale.ROOT, + "Failed to parse object: expecting token with name [%s] but found [%s]", + CONFIG_NAME, + xcp.currentName() ) ) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt index a3f096b9..f1f1221e 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Alert.kt @@ -42,8 +42,10 @@ data class Alert( ) : Writeable, ToXContent { init { - if (errorMessage != null) require(state == State.DELETED || state == State.ERROR) { - "Attempt to create an alert with an error in state: $state" + if (errorMessage != null) { + require(state == State.DELETED || state == State.ERROR) { + "Attempt to create an alert with an error in state: $state" + } } } @@ -139,7 +141,9 @@ data class Alert( monitorVersion = sin.readLong(), monitorUser = if (sin.readBoolean()) { User(sin) - } else null, + } else { + null + }, triggerId = sin.readString(), triggerName = sin.readString(), findingIds = sin.readStringList(), @@ -216,10 +220,10 @@ data class Alert( const val NO_ID = "" const val NO_VERSION = Versions.NOT_FOUND - @JvmStatic @JvmOverloads + @JvmStatic + @JvmOverloads @Throws(IOException::class) fun parse(xcp: XContentParser, id: String = NO_ID, version: Long = NO_VERSION): Alert { - lateinit var monitorId: String var schemaVersion = NO_SCHEMA_VERSION lateinit var monitorName: String diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/BucketLevelTrigger.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/BucketLevelTrigger.kt index 56975beb..e35d79d2 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/BucketLevelTrigger.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/BucketLevelTrigger.kt @@ -83,7 +83,8 @@ data class BucketLevelTrigger( const val PARENT_BUCKET_PATH = "parentBucketPath" val XCONTENT_REGISTRY = NamedXContentRegistry.Entry( - Trigger::class.java, ParseField(BUCKET_LEVEL_TRIGGER_FIELD), + Trigger::class.java, + ParseField(BUCKET_LEVEL_TRIGGER_FIELD), CheckedFunction { parseInner(it) } ) diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt index fc805917..74677cbc 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/ClusterMetricsInput.kt @@ -43,10 +43,11 @@ data class ClusterMetricsInput( "Invalid URI constructed from the path and path_params inputs, or the url input." } - if (url.isNotEmpty() && validateFieldsNotEmpty()) + if (url.isNotEmpty() && validateFieldsNotEmpty()) { require(constructedUri == constructUrlFromInputs()) { "The provided URL and URI fields form different URLs." } + } require(constructedUri.host.lowercase() == SUPPORTED_HOST) { "Only host '$SUPPORTED_HOST' is supported." @@ -104,7 +105,8 @@ data class ClusterMetricsInput( /** * This parse function uses [XContentParser] to parse JSON input and store corresponding fields to create a [ClusterMetricsInput] object */ - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parseInner(xcp: XContentParser): ClusterMetricsInput { var path = "" var pathParams = "" @@ -161,17 +163,20 @@ data class ClusterMetricsInput( if (pathParams.isNotEmpty()) { pathParams = pathParams.trim('/') ILLEGAL_PATH_PARAMETER_CHARACTERS.forEach { character -> - if (pathParams.contains(character)) + if (pathParams.contains(character)) { throw IllegalArgumentException( "The provided path parameters contain invalid characters or spaces. Please omit: " + "${ILLEGAL_PATH_PARAMETER_CHARACTERS.joinToString(" ")}" ) + } } } - if (apiType.requiresPathParams && pathParams.isEmpty()) + if (apiType.requiresPathParams && pathParams.isEmpty()) { throw IllegalArgumentException("The API requires path parameters.") - if (!apiType.supportsPathParams && pathParams.isNotEmpty()) + } + if (!apiType.supportsPathParams && pathParams.isNotEmpty()) { throw IllegalArgumentException("The API does not use path parameters.") + } return pathParams } @@ -187,11 +192,13 @@ data class ClusterMetricsInput( ClusterMetricType.values() .filter { option -> option != ClusterMetricType.BLANK } .forEach { option -> - if (uriPath.startsWith(option.prependPath) || uriPath.startsWith(option.defaultPath)) + if (uriPath.startsWith(option.prependPath) || uriPath.startsWith(option.defaultPath)) { apiType = option + } } - if (apiType.isBlank()) + if (apiType.isBlank()) { throw IllegalArgumentException("The API could not be determined from the provided URI.") + } return apiType } @@ -213,12 +220,15 @@ data class ClusterMetricsInput( * If [path] and [pathParams] are empty, populates them with values from [url]. */ private fun parseEmptyFields() { - if (pathParams.isEmpty()) + if (pathParams.isEmpty()) { pathParams = this.parsePathParams() - if (path.isEmpty()) + } + if (path.isEmpty()) { path = if (pathParams.isEmpty()) clusterMetricType.defaultPath else clusterMetricType.prependPath - if (url.isEmpty()) + } + if (url.isEmpty()) { url = constructedUri.toString() + } } /** diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt index 2c48f6f6..43e6643a 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DataSources.kt @@ -88,7 +88,7 @@ data class DataSources( ALERTS_HISTORY_INDEX_FIELD to alertsHistoryIndex, ALERTS_HISTORY_INDEX_PATTERN_FIELD to alertsHistoryIndexPattern, QUERY_INDEX_MAPPINGS_BY_TYPE to queryIndexMappingsByType, - FINDINGS_ENABLED_FIELD to findingsEnabled, + FINDINGS_ENABLED_FIELD to findingsEnabled ) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInput.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInput.kt index 9a914f3e..2abb6d6b 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInput.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelMonitorInput.kt @@ -64,10 +64,12 @@ data class DocLevelMonitorInput( val XCONTENT_REGISTRY = NamedXContentRegistry.Entry( Input::class.java, - ParseField(DOC_LEVEL_INPUT_FIELD), CheckedFunction { parse(it) } + ParseField(DOC_LEVEL_INPUT_FIELD), + CheckedFunction { parse(it) } ) - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parse(xcp: XContentParser): DocLevelMonitorInput { var description: String = NO_DESCRIPTION val indices: MutableList = mutableListOf() @@ -106,7 +108,8 @@ data class DocLevelMonitorInput( return DocLevelMonitorInput(description = description, indices = indices, queries = docLevelQueries) } - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun readFrom(sin: StreamInput): DocLevelMonitorInput { return DocLevelMonitorInput(sin) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt index 220c928d..3b49b83c 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt @@ -69,7 +69,8 @@ data class DocLevelQuery( const val NO_ID = "" val INVALID_CHARACTERS: List = listOf(" ", "[", "]", "{", "}", "(", ")") - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parse(xcp: XContentParser): DocLevelQuery { var id: String = UUID.randomUUID().toString() lateinit var query: String @@ -111,7 +112,8 @@ data class DocLevelQuery( ) } - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun readFrom(sin: StreamInput): DocLevelQuery { return DocLevelQuery(sin) } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/DocumentLevelTrigger.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/DocumentLevelTrigger.kt index a9cd0d69..4405af16 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/DocumentLevelTrigger.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/DocumentLevelTrigger.kt @@ -80,11 +80,13 @@ data class DocumentLevelTrigger( const val QUERY_IDS_FIELD = "query_ids" val XCONTENT_REGISTRY = NamedXContentRegistry.Entry( - Trigger::class.java, ParseField(DOCUMENT_LEVEL_TRIGGER_FIELD), + Trigger::class.java, + ParseField(DOCUMENT_LEVEL_TRIGGER_FIELD), CheckedFunction { parseInner(it) } ) - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parseInner(xcp: XContentParser): DocumentLevelTrigger { var id = UUIDs.base64UUID() // assign a default triggerId if one is not specified lateinit var name: String diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Finding.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Finding.kt index 899189b8..52c82c2d 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Finding.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Finding.kt @@ -81,7 +81,8 @@ class Finding( const val TIMESTAMP_FIELD = "timestamp" const val NO_ID = "" - @JvmStatic @JvmOverloads + @JvmStatic + @JvmOverloads @Throws(IOException::class) fun parse(xcp: XContentParser): Finding { var id: String = NO_ID diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/FindingDocument.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/FindingDocument.kt index c30eb6be..93397089 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/FindingDocument.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/FindingDocument.kt @@ -52,7 +52,8 @@ class FindingDocument( const val NO_ID = "" const val NO_INDEX = "" - @JvmStatic @JvmOverloads + @JvmStatic + @JvmOverloads @Throws(IOException::class) fun parse(xcp: XContentParser, id: String = NO_ID, index: String = NO_INDEX): FindingDocument { var found = false diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt index a12e5ef7..2d14164f 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Monitor.kt @@ -94,7 +94,9 @@ data class Monitor( monitorType = sin.readEnum(MonitorType::class.java), user = if (sin.readBoolean()) { User(sin) - } else null, + } else { + null + }, schemaVersion = sin.readInt(), inputs = sin.readList((Input)::readFrom), triggers = sin.readList((Trigger)::readFrom), @@ -181,8 +183,11 @@ data class Monitor( // Outputting type with each Input so that the generic Input.readFrom() can read it out.writeVInt(inputs.size) inputs.forEach { - if (it is SearchInput) out.writeEnum(Input.Type.SEARCH_INPUT) - else out.writeEnum(Input.Type.DOCUMENT_LEVEL_INPUT) + if (it is SearchInput) { + out.writeEnum(Input.Type.SEARCH_INPUT) + } else { + out.writeEnum(Input.Type.DOCUMENT_LEVEL_INPUT) + } it.writeTo(out) } // Outputting type with each Trigger so that the generic Trigger.readFrom() can read it @@ -273,8 +278,9 @@ data class Monitor( ) while (xcp.nextToken() != XContentParser.Token.END_ARRAY) { val input = Input.parse(xcp) - if (input is ClusterMetricsInput) + if (input is ClusterMetricsInput) { supportedClusterMetricsSettings?.validateApiType(input) + } inputs.add(input) } } @@ -291,8 +297,11 @@ data class Monitor( ENABLED_TIME_FIELD -> enabledTime = xcp.instant() LAST_UPDATE_TIME_FIELD -> lastUpdateTime = xcp.instant() UI_METADATA_FIELD -> uiMetadata = xcp.map() - DATA_SOURCES_FIELD -> dataSources = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) DataSources() - else DataSources.parse(xcp) + DATA_SOURCES_FIELD -> dataSources = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { + DataSources() + } else { + DataSources.parse(xcp) + } OWNER_FIELD -> owner = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) owner else xcp.text() else -> { xcp.skipChildren() diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/QueryLevelTrigger.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/QueryLevelTrigger.kt index 98011ff5..13dc8266 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/QueryLevelTrigger.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/QueryLevelTrigger.kt @@ -57,7 +57,9 @@ data class QueryLevelTrigger( /** Returns a representation of the trigger suitable for passing into painless and mustache scripts. */ fun asTemplateArg(): Map { return mapOf( - ID_FIELD to id, NAME_FIELD to name, SEVERITY_FIELD to severity, + ID_FIELD to id, + NAME_FIELD to name, + SEVERITY_FIELD to severity, ACTIONS_FIELD to actions.map { it.asTemplateArg() } ) } @@ -77,7 +79,8 @@ data class QueryLevelTrigger( const val SCRIPT_FIELD = "script" val XCONTENT_REGISTRY = NamedXContentRegistry.Entry( - Trigger::class.java, ParseField(QUERY_LEVEL_TRIGGER_FIELD), + Trigger::class.java, + ParseField(QUERY_LEVEL_TRIGGER_FIELD), CheckedFunction { parseInner(it) } ) @@ -113,7 +116,8 @@ data class QueryLevelTrigger( * It isn't typically conventional but this parse method will account for both START_OBJECT * and FIELD_NAME as the starting token to cover both cases. */ - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parseInner(xcp: XContentParser): QueryLevelTrigger { var id = UUIDs.base64UUID() // assign a default triggerId if one is not specified lateinit var name: String diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/Schedule.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/Schedule.kt index 68c174ca..fe6ac431 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/Schedule.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/Schedule.kt @@ -33,7 +33,8 @@ sealed class Schedule : BaseModel { val cronParser = CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX)) - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parse(xcp: XContentParser): Schedule { var expression: String? = null var timezone: ZoneId? = null @@ -91,7 +92,8 @@ sealed class Schedule : BaseModel { return requireNotNull(schedule) { "Schedule is null." } } - @JvmStatic @Throws(IllegalArgumentException::class) + @JvmStatic + @Throws(IllegalArgumentException::class) private fun getTimeZone(timeZone: String): ZoneId { try { return ZoneId.of(timeZone) @@ -106,10 +108,11 @@ sealed class Schedule : BaseModel { @Throws(IOException::class) fun readFrom(sin: StreamInput): Schedule { val type = sin.readEnum(Schedule.TYPE::class.java) - if (type == Schedule.TYPE.CRON) + if (type == Schedule.TYPE.CRON) { return CronSchedule(sin) - else + } else { return IntervalSchedule(sin) + } } } diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/SearchInput.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/SearchInput.kt index d12e935b..b7b0864d 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/SearchInput.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/SearchInput.kt @@ -46,7 +46,8 @@ data class SearchInput(val indices: List, val query: SearchSourceBuilder val XCONTENT_REGISTRY = NamedXContentRegistry.Entry(Input::class.java, ParseField("search"), CheckedFunction { parseInner(it) }) - @JvmStatic @Throws(IOException::class) + @JvmStatic + @Throws(IOException::class) fun parseInner(xcp: XContentParser): SearchInput { val indices = mutableListOf() lateinit var searchSourceBuilder: SearchSourceBuilder diff --git a/src/main/kotlin/org/opensearch/commons/alerting/model/action/Action.kt b/src/main/kotlin/org/opensearch/commons/alerting/model/action/Action.kt index e8b9f87e..1907fd05 100644 --- a/src/main/kotlin/org/opensearch/commons/alerting/model/action/Action.kt +++ b/src/main/kotlin/org/opensearch/commons/alerting/model/action/Action.kt @@ -130,8 +130,11 @@ data class Action( NAME_FIELD -> name = xcp.textOrNull() DESTINATION_ID_FIELD -> destinationId = xcp.textOrNull() SUBJECT_TEMPLATE_FIELD -> { - subjectTemplate = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) null else + subjectTemplate = if (xcp.currentToken() == XContentParser.Token.VALUE_NULL) { + null + } else { Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG) + } } MESSAGE_TEMPLATE_FIELD -> messageTemplate = Script.parse(xcp, Script.DEFAULT_TEMPLATE_LANG) THROTTLE_FIELD -> { diff --git a/src/main/kotlin/org/opensearch/commons/notifications/model/ChannelMessage.kt b/src/main/kotlin/org/opensearch/commons/notifications/model/ChannelMessage.kt index 18a65d39..b6310885 100644 --- a/src/main/kotlin/org/opensearch/commons/notifications/model/ChannelMessage.kt +++ b/src/main/kotlin/org/opensearch/commons/notifications/model/ChannelMessage.kt @@ -48,7 +48,6 @@ data class ChannelMessage( @JvmStatic @Throws(IOException::class) fun parse(parser: XContentParser): ChannelMessage { - var textDescription: String? = null var htmlDescription: String? = null var attachment: Attachment? = null diff --git a/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt index 23e47825..6f17c3be 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/AlertTests.kt @@ -46,7 +46,7 @@ class AlertTests { assertEquals( templateArgs[Alert.PARENTS_BUCKET_PATH], alert.aggregationResultBucket?.parentBucketPath, - "Template args parentBucketPath does not match", + "Template args parentBucketPath does not match" ) } diff --git a/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt b/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt index 6ab056c5..9bebe458 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/TestHelpers.kt @@ -205,9 +205,11 @@ fun randomDocumentLevelTrigger( name = name, severity = severity, condition = condition, - actions = if (actions.isEmpty() && destinationId.isNotBlank()) + actions = if (actions.isEmpty() && destinationId.isNotBlank()) { (0..RandomNumbers.randomIntBetween(Random(), 0, 10)).map { randomAction(destinationId = destinationId) } - else actions + } else { + actions + } ) } @@ -413,7 +415,10 @@ fun randomAlert(monitor: Monitor = randomQueryLevelMonitor()): Alert { val trigger = randomQueryLevelTrigger() val actionExecutionResults = mutableListOf(randomActionExecutionResult(), randomActionExecutionResult()) return Alert( - monitor, trigger, Instant.now().truncatedTo(ChronoUnit.MILLIS), null, + monitor, + trigger, + Instant.now().truncatedTo(ChronoUnit.MILLIS), + null, actionExecutionResults = actionExecutionResults ) } @@ -428,7 +433,10 @@ fun randomAlertWithAggregationResultBucket(monitor: Monitor = randomBucketLevelM val trigger = randomBucketLevelTrigger() val actionExecutionResults = mutableListOf(randomActionExecutionResult(), randomActionExecutionResult()) return Alert( - monitor, trigger, Instant.now().truncatedTo(ChronoUnit.MILLIS), null, + monitor, + trigger, + Instant.now().truncatedTo(ChronoUnit.MILLIS), + null, actionExecutionResults = actionExecutionResults, aggregationResultBucket = AggregationResultBucket( "parent_bucket_path_1", diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponseTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponseTests.kt index 16d5ef9a..4b084358 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponseTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/AcknowledgeAlertResponseTests.kt @@ -15,7 +15,6 @@ class AcknowledgeAlertResponseTests { @Test fun `test acknowledge alert response`() { - val acknowledged = mutableListOf( Alert( "1234", 0L, 1, "monitor-1234", "test-monitor", 0L, randomUser(), diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorRequestTests.kt index 43fa43e3..1b73feac 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorRequestTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/DeleteMonitorRequestTests.kt @@ -10,7 +10,6 @@ class DeleteMonitorRequestTests { @Test fun `test delete monitor request`() { - val req = DeleteMonitorRequest("1234", WriteRequest.RefreshPolicy.IMMEDIATE) Assert.assertNotNull(req) Assert.assertEquals("1234", req.monitorId) diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/GetAlertsRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/GetAlertsRequestTests.kt index 32ade4ba..2eae8c1a 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/GetAlertsRequestTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/GetAlertsRequestTests.kt @@ -13,7 +13,6 @@ internal class GetAlertsRequestTests { @Test fun `test get alerts request`() { - val table = Table("asc", "sortString", null, 1, 0, "") val req = GetAlertsRequest(table, "1", "active", null, null, listOf("1", "2"), listOf("alert1", "alert2")) @@ -36,7 +35,6 @@ internal class GetAlertsRequestTests { @Test fun `test get alerts request with filter`() { - val table = Table("asc", "sortString", null, 1, 0, "") val req = GetAlertsRequest(table, "1", "active", null, null) assertNotNull(req) diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt index f83cb8de..37675f67 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsRequestTests.kt @@ -13,7 +13,6 @@ internal class GetFindingsRequestTests { @Test fun `test get findings request`() { - val table = Table("asc", "sortString", null, 1, 0, "") val req = GetFindingsRequest("2121", table, "1", "finding_index_name", listOf("1", "2")) diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsResponseTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsResponseTests.kt index 1f7e733e..f8c2399a 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsResponseTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/GetFindingsResponseTests.kt @@ -16,7 +16,6 @@ internal class GetFindingsResponseTests { @Test fun `test get findings response`() { - // Alerting GetFindingsResponse mock #1 val finding1 = Finding( "1", diff --git a/src/test/kotlin/org/opensearch/commons/alerting/action/IndexMonitorRequestTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/action/IndexMonitorRequestTests.kt index 34a1c334..e4c1684a 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/action/IndexMonitorRequestTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/action/IndexMonitorRequestTests.kt @@ -20,9 +20,12 @@ class IndexMonitorRequestTests { @Test fun `test index monitor post request`() { - val req = IndexMonitorRequest( - "1234", 1L, 2L, WriteRequest.RefreshPolicy.IMMEDIATE, RestRequest.Method.POST, + "1234", + 1L, + 2L, + WriteRequest.RefreshPolicy.IMMEDIATE, + RestRequest.Method.POST, randomQueryLevelMonitor().copy(inputs = listOf(SearchInput(emptyList(), SearchSourceBuilder()))) ) Assertions.assertNotNull(req) @@ -41,7 +44,11 @@ class IndexMonitorRequestTests { @Test fun `test index bucket monitor post request`() { val req = IndexMonitorRequest( - "1234", 1L, 2L, WriteRequest.RefreshPolicy.IMMEDIATE, RestRequest.Method.POST, + "1234", + 1L, + 2L, + WriteRequest.RefreshPolicy.IMMEDIATE, + RestRequest.Method.POST, randomBucketLevelMonitor() ) Assertions.assertNotNull(req) @@ -61,7 +68,11 @@ class IndexMonitorRequestTests { @Test fun `Index bucket monitor serialize and deserialize transport object should be equal`() { val bucketLevelMonitorRequest = IndexMonitorRequest( - "1234", 1L, 2L, WriteRequest.RefreshPolicy.IMMEDIATE, RestRequest.Method.POST, + "1234", + 1L, + 2L, + WriteRequest.RefreshPolicy.IMMEDIATE, + RestRequest.Method.POST, randomBucketLevelMonitor() ) @@ -80,9 +91,12 @@ class IndexMonitorRequestTests { @Test fun `test index monitor put request`() { - val req = IndexMonitorRequest( - "1234", 1L, 2L, WriteRequest.RefreshPolicy.IMMEDIATE, RestRequest.Method.PUT, + "1234", + 1L, + 2L, + WriteRequest.RefreshPolicy.IMMEDIATE, + RestRequest.Method.PUT, randomQueryLevelMonitor().copy(inputs = listOf(SearchInput(emptyList(), SearchSourceBuilder()))) ) Assertions.assertNotNull(req) diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/FindingTests.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/FindingTests.kt index 50615cb2..10579a41 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/FindingTests.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/FindingTests.kt @@ -24,7 +24,7 @@ internal class FindingTests { assertEquals( templateArgs[Finding.MONITOR_NAME_FIELD], finding.monitorName, - "Template args 'monitorName' field does not match:", + "Template args 'monitorName' field does not match:" ) assertEquals( templateArgs[Finding.QUERIES_FIELD], diff --git a/src/test/kotlin/org/opensearch/commons/alerting/model/ScheduleTest.kt b/src/test/kotlin/org/opensearch/commons/alerting/model/ScheduleTest.kt index 6eee00c8..a55030f1 100644 --- a/src/test/kotlin/org/opensearch/commons/alerting/model/ScheduleTest.kt +++ b/src/test/kotlin/org/opensearch/commons/alerting/model/ScheduleTest.kt @@ -51,7 +51,8 @@ class ScheduleTest : XContentTestBase { val nextTimeToExecute = cronSchedule.getExpectedNextExecutionTime(enabledTimeInstance, null) assertNotNull(nextTimeToExecute, "There should be next execute time") assertEquals( - testInstance.plusSeconds(2L), nextTimeToExecute, + testInstance.plusSeconds(2L), + nextTimeToExecute, "nextTimeToExecute should be 2 seconds after test instance" ) } @@ -69,7 +70,8 @@ class ScheduleTest : XContentTestBase { val nextTimeToExecute = cronSchedule.getExpectedNextExecutionTime(enabledTimeInstance, previousExecutionTimeInstance) assertNotNull(nextTimeToExecute, "There should be next execute time") assertEquals( - previousExecutionTimeInstance.plusSeconds(2L), nextTimeToExecute, + previousExecutionTimeInstance.plusSeconds(2L), + nextTimeToExecute, "nextTimeToExecute should be 2 seconds after test instance" ) } @@ -87,7 +89,8 @@ class ScheduleTest : XContentTestBase { val nextTimeToExecute = intervalSchedule.getExpectedNextExecutionTime(enabledTimeInstance, null) assertNotNull(nextTimeToExecute, "There should be next execute time") assertEquals( - enabledTimeInstance.plusSeconds(120L), nextTimeToExecute, + enabledTimeInstance.plusSeconds(120L), + nextTimeToExecute, "nextTimeToExecute should be 120 seconds seconds after enabled time" ) } @@ -107,7 +110,8 @@ class ScheduleTest : XContentTestBase { val nextTimeToExecute = intervalSchedule.getExpectedNextExecutionTime(enabledTimeInstance, previousExecutionTimeInstance) assertNotNull(nextTimeToExecute, "There should be next execute time") assertEquals( - previousExecutionTimeInstance.plusSeconds(60L), nextTimeToExecute, + previousExecutionTimeInstance.plusSeconds(60L), + nextTimeToExecute, "nextTimeToExecute should be 60 seconds after previous execution time" ) } diff --git a/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt index 61cbca7a..e359ec73 100644 --- a/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt +++ b/src/test/kotlin/org/opensearch/commons/notifications/NotificationsPluginInterfaceTests.kt @@ -202,7 +202,11 @@ internal class NotificationsPluginInterfaceTests { }.whenever(client).execute(any(ActionType::class.java), any(), any()) NotificationsPluginInterface.sendNotification( - client, notificationInfo, channelMessage, listOf("channelId1", "channelId2"), listener + client, + notificationInfo, + channelMessage, + listOf("channelId1", "channelId2"), + listener ) verify(listener, times(1)).onResponse(eq(response)) } diff --git a/src/test/kotlin/org/opensearch/commons/notifications/action/SendNotificationResponseTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/action/SendNotificationResponseTests.kt index 4d828998..1de3c8fa 100644 --- a/src/test/kotlin/org/opensearch/commons/notifications/action/SendNotificationResponseTests.kt +++ b/src/test/kotlin/org/opensearch/commons/notifications/action/SendNotificationResponseTests.kt @@ -22,7 +22,6 @@ internal class SendNotificationResponseTests { @Test fun `Create response serialize and deserialize transport object should be equal`() { - val sampleEvent = getSampleEvent() val recreatedObject = recreateObject(sampleEvent) { SendNotificationResponse(it) } @@ -31,7 +30,6 @@ internal class SendNotificationResponseTests { @Test fun `Create response serialize and deserialize using json object should be equal`() { - val sampleEvent = getSampleEvent() val jsonString = getJsonString(sampleEvent) diff --git a/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationConfigTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationConfigTests.kt index f8aa9e9d..69905781 100644 --- a/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationConfigTests.kt +++ b/src/test/kotlin/org/opensearch/commons/notifications/model/NotificationConfigTests.kt @@ -175,7 +175,6 @@ internal class NotificationConfigTests { } @Test - fun `Config should safely ignore unknown config type in json object`() { val sampleSlack = Slack("https://domain.com/sample_slack_url#1234567890") val sampleConfig = NotificationConfig( diff --git a/src/test/kotlin/org/opensearch/commons/notifications/model/SmtpAccountTests.kt b/src/test/kotlin/org/opensearch/commons/notifications/model/SmtpAccountTests.kt index ffdf26c7..6617d1f6 100644 --- a/src/test/kotlin/org/opensearch/commons/notifications/model/SmtpAccountTests.kt +++ b/src/test/kotlin/org/opensearch/commons/notifications/model/SmtpAccountTests.kt @@ -71,7 +71,8 @@ internal class SmtpAccountTests { fun `SmtpAccount should safely ignore extra field in json object`() { val sampleSmtpAccount = SmtpAccount( "domain.com", - 1234, MethodType.START_TLS, + 1234, + MethodType.START_TLS, "from@domain.com" ) val jsonString = """