Skip to content

Commit

Permalink
BugFix: Allow null values for role_arn in json parsing (opensearch-pr…
Browse files Browse the repository at this point in the history
…oject#60)

Signed-off-by: @akbhatta
  • Loading branch information
akbhatta authored Aug 16, 2021
1 parent f3d0302 commit 0d91574
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.opensearch.common.xcontent.XContentParserUtils
import org.opensearch.commons.notifications.NotificationConstants.FROM_ADDRESS_TAG
import org.opensearch.commons.notifications.NotificationConstants.REGION_TAG
import org.opensearch.commons.notifications.NotificationConstants.ROLE_ARN_TAG
import org.opensearch.commons.utils.fieldIfNotNull
import org.opensearch.commons.utils.logger
import org.opensearch.commons.utils.validateEmail
import org.opensearch.commons.utils.validateIamRoleArn
Expand Down Expand Up @@ -74,7 +75,7 @@ data class SesAccount(
parser.nextToken()
when (fieldName) {
REGION_TAG -> awsRegion = parser.text()
ROLE_ARN_TAG -> roleArn = parser.text()
ROLE_ARN_TAG -> roleArn = parser.textOrNull()
FROM_ADDRESS_TAG -> fromAddress = parser.text()
else -> {
parser.skipChildren()
Expand All @@ -98,7 +99,7 @@ data class SesAccount(
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
return builder!!.startObject()
.field(REGION_TAG, awsRegion)
.field(ROLE_ARN_TAG, roleArn)
.fieldIfNotNull(ROLE_ARN_TAG, roleArn)
.field(FROM_ADDRESS_TAG, fromAddress)
.endObject()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ internal class SesAccountTests {
assertEquals(sesAccount, recreatedObject)
}

@Test
fun `SES serialize and deserialize using json object should be equal with null roleArn`() {
val sesAccount = SesAccount("us-east-1", null, "[email protected]")
val jsonString = getJsonString(sesAccount)
val recreatedObject = createObjectFromJsonString(jsonString) { SesAccount.parse(it) }
assertEquals(sesAccount, recreatedObject)
}

@Test
fun `SES should deserialize json object using parser`() {
val sesAccount = SesAccount("us-east-1", "arn:aws:iam::012345678912:role/iam-test", "[email protected]")
Expand All @@ -96,6 +104,33 @@ internal class SesAccountTests {
assertEquals(sesAccount, recreatedObject)
}

@Test
fun `SES should deserialize json object will null role_arn using parser`() {
val sesAccount = SesAccount("us-east-1", null, "[email protected]")
val jsonString = """
{
"region":"${sesAccount.awsRegion}",
"role_arn":null,
"from_address":"${sesAccount.fromAddress}"
}
""".trimIndent()
val recreatedObject = createObjectFromJsonString(jsonString) { SesAccount.parse(it) }
assertEquals(sesAccount, recreatedObject)
}

@Test
fun `SES should deserialize json object will missing role_arn using parser`() {
val sesAccount = SesAccount("us-east-1", null, "[email protected]")
val jsonString = """
{
"region":"${sesAccount.awsRegion}",
"from_address":"${sesAccount.fromAddress}"
}
""".trimIndent()
val recreatedObject = createObjectFromJsonString(jsonString) { SesAccount.parse(it) }
assertEquals(sesAccount, recreatedObject)
}

@Test
fun `SES should throw exception when invalid json object is passed`() {
val jsonString = "sample message"
Expand Down

0 comments on commit 0d91574

Please sign in to comment.