Skip to content

Commit

Permalink
Email recipients made nested for backward compatibility with Alerting (
Browse files Browse the repository at this point in the history
…#83)

[Tests]
Unit tests added/updated

Signed-off-by: @akbhatta
  • Loading branch information
akbhatta authored Oct 13, 2021
1 parent cbeba12 commit e51a67a
Show file tree
Hide file tree
Showing 11 changed files with 361 additions and 141 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,21 @@ import org.opensearch.commons.notifications.NotificationConstants.EMAIL_ACCOUNT_
import org.opensearch.commons.notifications.NotificationConstants.EMAIL_GROUP_ID_LIST_TAG
import org.opensearch.commons.notifications.NotificationConstants.RECIPIENT_LIST_TAG
import org.opensearch.commons.utils.logger
import org.opensearch.commons.utils.objectList
import org.opensearch.commons.utils.stringList
import org.opensearch.commons.utils.validateEmail
import java.io.IOException

/**
* Data class representing Email account and default recipients.
*/
data class Email(
val emailAccountID: String,
val recipients: List<String>,
val recipients: List<EmailRecipient>,
val emailGroupIds: List<String>
) : BaseConfigData {

init {
require(!Strings.isNullOrEmpty(emailAccountID)) { "emailAccountID is null or empty" }
recipients.forEach {
validateEmail(it)
}
}

companion object {
Expand All @@ -79,7 +76,7 @@ data class Email(
@Throws(IOException::class)
fun parse(parser: XContentParser): Email {
var emailAccountID: String? = null
var recipients: List<String> = listOf()
var recipients: List<EmailRecipient> = listOf()
var emailGroupIds: List<String> = listOf()

XContentParserUtils.ensureExpectedToken(
Expand All @@ -92,7 +89,7 @@ data class Email(
parser.nextToken()
when (fieldName) {
EMAIL_ACCOUNT_ID_TAG -> emailAccountID = parser.text()
RECIPIENT_LIST_TAG -> recipients = parser.stringList()
RECIPIENT_LIST_TAG -> recipients = parser.objectList { EmailRecipient.parse(it) }
EMAIL_GROUP_ID_LIST_TAG -> emailGroupIds = parser.stringList()
else -> {
parser.skipChildren()
Expand All @@ -111,7 +108,7 @@ data class Email(
*/
constructor(input: StreamInput) : this(
emailAccountID = input.readString(),
recipients = input.readStringList(),
recipients = input.readList(EmailRecipient.reader),
emailGroupIds = input.readStringList()
)

Expand All @@ -120,7 +117,7 @@ data class Email(
*/
override fun writeTo(output: StreamOutput) {
output.writeString(emailAccountID)
output.writeStringCollection(recipients)
output.writeList(recipients)
output.writeStringCollection(emailGroupIds)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,16 @@ import org.opensearch.common.xcontent.XContentParser
import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.notifications.NotificationConstants.RECIPIENT_LIST_TAG
import org.opensearch.commons.utils.logger
import org.opensearch.commons.utils.stringList
import org.opensearch.commons.utils.validateEmail
import org.opensearch.commons.utils.objectList
import java.io.IOException

/**
* Data class representing Email group.
*/
data class EmailGroup(
val recipients: List<String>
val recipients: List<EmailRecipient>
) : BaseConfigData {

init {
recipients.forEach {
validateEmail(it)
}
}

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

Expand All @@ -72,7 +65,7 @@ data class EmailGroup(
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser): EmailGroup {
var recipients: List<String>? = null
var recipients: List<EmailRecipient>? = null

XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_OBJECT,
Expand All @@ -83,7 +76,7 @@ data class EmailGroup(
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
RECIPIENT_LIST_TAG -> recipients = parser.stringList()
RECIPIENT_LIST_TAG -> recipients = parser.objectList { EmailRecipient.parse(it) }
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing EmailGroup")
Expand All @@ -100,14 +93,14 @@ data class EmailGroup(
* @param input StreamInput stream to deserialize data from.
*/
constructor(input: StreamInput) : this(
recipients = input.readStringList()
recipients = input.readList(EmailRecipient.reader)
)

/**
* {@inheritDoc}
*/
override fun writeTo(output: StreamOutput) {
output.writeStringCollection(recipients)
output.writeList(recipients)
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/

/*
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*
*/
package org.opensearch.commons.notifications.model

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.RECIPIENT_TAG
import org.opensearch.commons.utils.logger
import org.opensearch.commons.utils.validateEmail
import java.io.IOException

/**
* Data class representing Email recipient.
*/
data class EmailRecipient(
val recipient: String
) : BaseConfigData {

init {
validateEmail(recipient)
}

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

/**
* reader to create instance of class from writable.
*/
val reader = Writeable.Reader { EmailRecipient(it) }

/**
* Parser to parse xContent
*/
val xParser = XParser { parse(it) }

/**
* Creator used in REST communication.
* @param parser XContentParser to deserialize data from.
*/
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser): EmailRecipient {
var recipient: String? = null

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

/**
* Constructor used in transport action communication.
* @param input StreamInput stream to deserialize data from.
*/
constructor(input: StreamInput) : this(
recipient = input.readString()
)

/**
* {@inheritDoc}
*/
override fun writeTo(output: StreamOutput) {
output.writeString(recipient)
}

/**
* {@inheritDoc}
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
return builder.startObject()
.field(RECIPIENT_TAG, recipient)
.endObject()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.opensearch.commons.notifications.model.Chime
import org.opensearch.commons.notifications.model.ConfigType
import org.opensearch.commons.notifications.model.Email
import org.opensearch.commons.notifications.model.EmailGroup
import org.opensearch.commons.notifications.model.EmailRecipient
import org.opensearch.commons.notifications.model.MethodType
import org.opensearch.commons.notifications.model.NotificationConfig
import org.opensearch.commons.notifications.model.Slack
Expand Down Expand Up @@ -84,7 +85,7 @@ internal class CreateNotificationConfigRequestTests {
}

private fun createEmailGroupContentConfigObject(): NotificationConfig {
val sampleEmailGroup = EmailGroup(listOf("[email protected]"))
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]")))
return NotificationConfig(
"name",
"description",
Expand All @@ -98,7 +99,7 @@ internal class CreateNotificationConfigRequestTests {
private fun createEmailContentConfigObject(): NotificationConfig {
val sampleEmail = Email(
emailAccountID = "[email protected]",
recipients = listOf("[email protected]"),
recipients = listOf(EmailRecipient("[email protected]")),
emailGroupIds = listOf("[email protected]")
)
return NotificationConfig(
Expand Down Expand Up @@ -365,7 +366,7 @@ internal class CreateNotificationConfigRequestTests {

@Test
fun `Create config should deserialize json object using parser Email Group`() {
val sampleEmailGroup = EmailGroup(listOf("[email protected]"))
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]")))
val config = NotificationConfig(
"name",
"description",
Expand All @@ -384,7 +385,7 @@ internal class CreateNotificationConfigRequestTests {
"config_type":"email_group",
"feature_list":["index_management"],
"is_enabled":true,
"email_group":{"recipient_list":["[email protected]"]}
"email_group":{"recipient_list":[{"recipient":"[email protected]"}]}
}
}
""".trimIndent()
Expand All @@ -396,7 +397,7 @@ internal class CreateNotificationConfigRequestTests {
fun `Update config should deserialize json object using parser Email`() {
val sampleEmail = Email(
emailAccountID = "[email protected]",
recipients = listOf("[email protected]"),
recipients = listOf(EmailRecipient("[email protected]")),
emailGroupIds = listOf("[email protected]")
)
val config = NotificationConfig(
Expand All @@ -417,8 +418,11 @@ internal class CreateNotificationConfigRequestTests {
"config_type":"email",
"feature_list":["index_management"],
"is_enabled":true,
"email":{"email_account_id":"[email protected]","recipient_list":["[email protected]"],
"email_group_id_list":["[email protected]"] }
"email":{
"email_account_id":"[email protected]",
"recipient_list":[{"recipient":"[email protected]"}],
"email_group_id_list":["[email protected]"]
}
}
}
""".trimIndent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ internal class GetPluginFeaturesResponseTests {
actual: GetPluginFeaturesResponse
) {
assertEquals(expected.allowedConfigTypeList, actual.allowedConfigTypeList)
assertEquals(expected.allowedConfigFeatureList, actual.allowedConfigFeatureList)
assertEquals(expected.pluginFeatures, actual.pluginFeatures)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import org.opensearch.commons.notifications.model.Chime
import org.opensearch.commons.notifications.model.ConfigType
import org.opensearch.commons.notifications.model.Email
import org.opensearch.commons.notifications.model.EmailGroup
import org.opensearch.commons.notifications.model.EmailRecipient
import org.opensearch.commons.notifications.model.MethodType
import org.opensearch.commons.notifications.model.NotificationConfig
import org.opensearch.commons.notifications.model.Slack
Expand Down Expand Up @@ -84,7 +85,7 @@ internal class UpdateNotificationConfigRequestTests {
}

private fun createEmailGroupContentConfigObject(): NotificationConfig {
val sampleEmailGroup = EmailGroup(listOf("[email protected]"))
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]")))
return NotificationConfig(
"name",
"description",
Expand All @@ -98,7 +99,7 @@ internal class UpdateNotificationConfigRequestTests {
private fun createEmailContentConfigObject(): NotificationConfig {
val sampleEmail = Email(
emailAccountID = "[email protected]",
recipients = listOf("[email protected]"),
recipients = listOf(EmailRecipient("[email protected]")),
emailGroupIds = listOf("[email protected]")
)
return NotificationConfig(
Expand Down Expand Up @@ -334,7 +335,7 @@ internal class UpdateNotificationConfigRequestTests {

@Test
fun `Update config should deserialize json object using parser Email Group`() {
val sampleEmailGroup = EmailGroup(listOf("[email protected]"))
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("[email protected]")))
val config = NotificationConfig(
"name",
"description",
Expand All @@ -353,7 +354,7 @@ internal class UpdateNotificationConfigRequestTests {
"config_type":"email_group",
"feature_list":["index_management"],
"is_enabled":true,
"email_group":{"recipient_list":["[email protected]"]}
"email_group":{"recipient_list":[{"recipient":"[email protected]"}]}
}
}
""".trimIndent()
Expand All @@ -366,7 +367,7 @@ internal class UpdateNotificationConfigRequestTests {
fun `Update config should deserialize json object using parser Email`() {
val sampleEmail = Email(
emailAccountID = "[email protected]",
recipients = listOf("[email protected]"),
recipients = listOf(EmailRecipient("[email protected]")),
emailGroupIds = listOf("[email protected]")
)
val config = NotificationConfig(
Expand All @@ -387,8 +388,11 @@ internal class UpdateNotificationConfigRequestTests {
"config_type":"email",
"feature_list":["index_management"],
"is_enabled":true,
"email":{"email_account_id":"[email protected]","recipient_list":["[email protected]"],
"email_group_id_list":["[email protected]"] }
"email":{
"email_account_id":"[email protected]",
"recipient_list":[{"recipient":"[email protected]"}],
"email_group_id_list":["[email protected]"]
}
}
}
""".trimIndent()
Expand Down
Loading

0 comments on commit e51a67a

Please sign in to comment.