Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added common utils for microsoft teams #374

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ enum class ConfigType(val tag: String) {
return tag
}
},
MICROSOFT_TEAMS("microsoft_teams") {
override fun toString(): String {
return tag
}
},
SMTP_ACCOUNT("smtp_account") {
override fun toString(): String {
return tag
Expand All @@ -54,6 +59,11 @@ enum class ConfigType(val tag: String) {
override fun toString(): String {
return tag
}
},
MICROSOFT_TEAMS("microsoft_teams") {
override fun toString(): String {
return tag
}
};

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ data class EventStatus(
ConfigType.SLACK -> requireNotNull(deliveryStatus)
ConfigType.EMAIL -> require(emailRecipientStatus.isNotEmpty())
ConfigType.SNS -> requireNotNull(deliveryStatus)
ConfigType.MICROSOFT_TEAMS -> requireNotNull(deliveryStatus)
ConfigType.NONE -> log.info("Some config field not recognized")
else -> {
log.info("non-allowed config type for Status")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
/*
* 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.XContentParserUtils
import org.opensearch.commons.notifications.NotificationConstants.URL_TAG
import org.opensearch.commons.utils.logger
import org.opensearch.commons.utils.validateUrl
import org.opensearch.core.xcontent.ToXContent
import org.opensearch.core.xcontent.XContentBuilder
import org.opensearch.core.xcontent.XContentParser
import java.io.IOException
<<<<<<< HEAD

/**
* Data class representing Microsoft Teams channel.
*/

data class MicrosoftTeams(
val url: String
=======
/**
* Data class representing Microsoft Teams channel.
*/
data class MicrosoftTeams(
val url: String
>>>>>>> b13bf3d337f15337a3c6999c5285482a79fd5a5d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you have code merge conflicts here.

) : BaseConfigData {

init {
require(!Strings.isNullOrEmpty(url)) { "URL is null or empty" }
validateUrl(url)
}

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

/**
<<<<<<< HEAD
* reader to create instance of class from writable.
=======
* Reader to create instance of class from writable.
>>>>>>> b13bf3d337f15337a3c6999c5285482a79fd5a5d
*/
val reader = Writeable.Reader { MicrosoftTeams(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): MicrosoftTeams {
var url: String? = null

XContentParserUtils.ensureExpectedToken(
<<<<<<< HEAD
XContentParser.Token.START_OBJECT,
parser.currentToken(),
parser
=======
XContentParser.Token.START_OBJECT,
parser.currentToken(),
parser
>>>>>>> b13bf3d337f15337a3c6999c5285482a79fd5a5d
)
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
URL_TAG -> url = parser.text()
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing Microsoft Teams destination")
}
}
}
url ?: throw IllegalArgumentException("$URL_TAG field absent")
return MicrosoftTeams(url)
}
}

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

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

/**
* {@inheritDoc}
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
builder!!
return builder.startObject()
<<<<<<< HEAD
.field(URL_TAG, url)
.endObject()
=======
.field(URL_TAG, url)
.endObject()
>>>>>>> b13bf3d337f15337a3c6999c5285482a79fd5a5d
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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.MicrosoftTeams
import org.opensearch.commons.notifications.model.SesAccount
import org.opensearch.commons.notifications.model.Slack
import org.opensearch.commons.notifications.model.SmtpAccount
Expand All @@ -34,10 +35,12 @@ internal object ConfigDataProperties {
Pair(ConfigType.WEBHOOK, ConfigProperty(Webhook.reader, Webhook.xParser)),
Pair(ConfigType.EMAIL, ConfigProperty(Email.reader, Email.xParser)),
Pair(ConfigType.SNS, ConfigProperty(Sns.reader, Sns.xParser)),
Pair(ConfigType.MICROSOFT_TEAMS, ConfigProperty(MicrosoftTeams.reader, MicrosoftTeams.xParser)),
Pair(ConfigType.SES_ACCOUNT, ConfigProperty(SesAccount.reader, SesAccount.xParser)),
Pair(ConfigType.EMAIL_GROUP, ConfigProperty(EmailGroup.reader, EmailGroup.xParser)),
Pair(ConfigType.SMTP_ACCOUNT, ConfigProperty(SmtpAccount.reader, SmtpAccount.xParser))
)
Pair(ConfigType.SMTP_ACCOUNT, ConfigProperty(SmtpAccount.reader, SmtpAccount.xParser)),
Pair(ConfigType.MICROSOFT_TEAMS, ConfigProperty(MicrosoftTeams.reader, MicrosoftTeams.xParser))
)

/**
* Get Reader for provided config type
Expand All @@ -62,6 +65,7 @@ internal object ConfigDataProperties {
ConfigType.CHIME -> configData is Chime
ConfigType.SNS -> configData is Sns
ConfigType.SES_ACCOUNT -> configData is SesAccount
ConfigType.MICROSOFT_TEAMS -> configData is MicrosoftTeams
ConfigType.NONE -> true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
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
import org.opensearch.commons.notifications.model.SmtpAccount
import org.opensearch.commons.notifications.model.Webhook
import org.opensearch.commons.notifications.model.*
import org.opensearch.commons.utils.createObjectFromJsonString
import org.opensearch.commons.utils.getJsonString
import org.opensearch.commons.utils.recreateObject
Expand Down Expand Up @@ -57,6 +48,16 @@ internal class CreateNotificationConfigRequestTests {
isEnabled = true
)
}
private fun createMicrosoftTeamsContentConfigObject(): NotificationConfig {
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.com/sample_microsoft_teams_url#1234567890")
return NotificationConfig(
"name",
"description",
ConfigType.MICROSOFT_TEAMS,
configData = sampleMicrosoftTeams,
isEnabled = true
)
}

private fun createEmailGroupContentConfigObject(): NotificationConfig {
val sampleEmailGroup = EmailGroup(listOf(EmailRecipient("dummy@company.com")))
Expand Down Expand Up @@ -114,6 +115,20 @@ internal class CreateNotificationConfigRequestTests {
assertNull(recreatedObject.validate())
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
}
@Test
fun `Create config serialize and deserialize transport object should be equal microsoft teams`() {
val configRequest = CreateNotificationConfigRequest(
createMicrosoftTeamsContentConfigObject()
)
val recreatedObject =
recreateObject(configRequest) {
CreateNotificationConfigRequest(
it
)
}
assertNull(recreatedObject.validate())
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
}

@Test
fun `Create config serialize and deserialize transport object should be equal slack`() {
Expand Down Expand Up @@ -189,6 +204,15 @@ internal class CreateNotificationConfigRequestTests {
assertNull(recreatedObject.validate())
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
}
@Test
fun `Create config serialize and deserialize using json object should be equal microsoft teams`() {
val configRequest = CreateNotificationConfigRequest(
createMicrosoftTeamsContentConfigObject()
)
val jsonString = getJsonString(configRequest)
val recreatedObject = createObjectFromJsonString(jsonString) { CreateNotificationConfigRequest.parse(it) }
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
}

@Test
fun `Create config serialize and deserialize using json object should be equal`() {
Expand Down Expand Up @@ -275,6 +299,32 @@ internal class CreateNotificationConfigRequestTests {
val recreatedObject = createObjectFromJsonString(jsonString) { CreateNotificationConfigRequest.parse(it) }
assertEquals(config, recreatedObject.notificationConfig)
}
@Test
fun `Create config should deserialize json object using parser microsoft teams`() {
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.com/sample_microsoft_teams_url#1234567890")
val config = NotificationConfig(
"name",
"description",
ConfigType.MICROSOFT_TEAMS,
configData = sampleMicrosoftTeams,
isEnabled = true
)

val jsonString = """
{
"config_id":"config_id1",
"config":{
"name":"name",
"description":"description",
"config_type":"microsoft_teams",
"is_enabled":true,
"microsoft_teams":{"url":"https://domain.com/sample_microsoft_teams_url#1234567890"}
}
}
""".trimIndent()
val recreatedObject = createObjectFromJsonString(jsonString) { CreateNotificationConfigRequest.parse(it) }
assertEquals(config, recreatedObject.notificationConfig)
}

@Test
fun `Create config should deserialize json object using parser webhook`() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNull
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
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
import org.opensearch.commons.notifications.model.SmtpAccount
import org.opensearch.commons.notifications.model.Webhook
import org.opensearch.commons.notifications.model.*
import org.opensearch.commons.utils.createObjectFromJsonString
import org.opensearch.commons.utils.getJsonString
import org.opensearch.commons.utils.recreateObject
Expand All @@ -35,7 +26,16 @@ internal class UpdateNotificationConfigRequestTests {
isEnabled = true
)
}

private fun createMicrosoftTeamsContentConfigObject(): NotificationConfig {
val sampleMicrosoftTeams = MicrosoftTeams("https://domain.com/sample_microsoft_teams_url#1234567890")
return NotificationConfig(
"name",
"description",
ConfigType.MICROSOFT_TEAMS,
configData = sampleMicrosoftTeams,
isEnabled = true
)
}
private fun createSlackContentConfigObject(): NotificationConfig {
val sampleSlack = Slack("https://domain.com/sample_slack_url#1234567890")
return NotificationConfig(
Expand Down Expand Up @@ -109,6 +109,15 @@ internal class UpdateNotificationConfigRequestTests {
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
assertEquals("config_id", recreatedObject.configId)
}
@Test
fun `Update config serialize and deserialize transport object should be equal Microsoft Teams`() {
val configRequest = UpdateNotificationConfigRequest("config_id", createMicrosoftTeamsContentConfigObject())
val recreatedObject =
recreateObject(configRequest) { UpdateNotificationConfigRequest(it) }
assertNull(recreatedObject.validate())
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
assertEquals("config_id", recreatedObject.configId)
}

@Test
fun `Update config serialize and deserialize transport object should be equal Slack`() {
Expand Down Expand Up @@ -168,6 +177,14 @@ internal class UpdateNotificationConfigRequestTests {
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
assertEquals("config_id", recreatedObject.configId)
}
@Test
fun `Update config serialize and deserialize using json object should be equal microsoft Teams`() {
val configRequest = UpdateNotificationConfigRequest("config_id", createMicrosoftTeamsContentConfigObject())
val jsonString = getJsonString(configRequest)
val recreatedObject = createObjectFromJsonString(jsonString) { UpdateNotificationConfigRequest.parse(it) }
assertEquals(configRequest.notificationConfig, recreatedObject.notificationConfig)
assertEquals("config_id", recreatedObject.configId)
}

@Test
fun `Update config serialize and deserialize using json object should be equal slack`() {
Expand Down Expand Up @@ -271,7 +288,6 @@ internal class UpdateNotificationConfigRequestTests {
assertEquals(config, recreatedObject.notificationConfig)
assertEquals("config_id1", recreatedObject.configId)
}

@Test
fun `Update config should deserialize json object using parser Chime`() {
val sampleChime = Chime("https://domain.com/sample_chime_url#1234567890")
Expand Down
Loading
Loading