Skip to content

Commit

Permalink
Add index refresh after config creation and deletion in integration t…
Browse files Browse the repository at this point in the history
…ests (#590) (#594)

* Add refresh to config creation in tests

Signed-off-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>

* Remove duplicate createConfig utility methods for tests

Signed-off-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>

* Replace delete config calls in tests with helper method with indices refresh

Signed-off-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>

* Replace creating config from JSON string calls with helper method that refreshes indices

Signed-off-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>

* Update security test back to correct request with expected response assertion

Signed-off-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>

Signed-off-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>
(cherry picked from commit 62b8c36)

Co-authored-by: Mohammad Qureshi <47198598+qreshi@users.noreply.github.com>
  • Loading branch information
opensearch-trigger-bot[bot] and qreshi authored Jan 6, 2023
1 parent 8d71ee4 commit 3a5b1b2
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 404 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,12 +287,59 @@ abstract class PluginRestTestCase : OpenSearchRestTestCase() {
RestStatus.OK.status,
client
)
refreshAllIndices()
val configId = createResponse.get("config_id").asString
Assert.assertNotNull(configId)
Thread.sleep(100)
return configId
}

fun createConfigWithRequestJsonString(
createRequestJsonString: String,
client: RestClient = client()
): String {
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status,
client
)
refreshAllIndices()
Thread.sleep(100)
return createResponse.get("config_id").asString
}

fun deleteConfig(
configId: String,
client: RestClient = client()
): JsonObject {
val deleteResponse = executeRequest(
RestRequest.Method.DELETE.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs/$configId",
"",
RestStatus.OK.status,
client
)
refreshAllIndices()
return deleteResponse
}

fun deleteConfigs(
configIds: Set<String>,
client: RestClient = client()
): JsonObject {
val deleteResponse = executeRequest(
RestRequest.Method.DELETE.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs?config_id_list=${configIds.joinToString(separator = ",")}",
"",
RestStatus.OK.status,
client
)
refreshAllIndices()
return deleteResponse
}

@After
open fun wipeAllSettings() {
wipeAllClusterSettings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,7 @@ class SecurityNotificationIT : PluginRestTestCase() {
}
""".trimIndent()
try {
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status,
userClient!!
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString, userClient!!)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -159,13 +152,7 @@ class SecurityNotificationIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -278,13 +265,7 @@ class SecurityNotificationIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -340,24 +321,12 @@ class SecurityNotificationIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

// Delete Slack notification config
executeRequest(
RestRequest.Method.DELETE.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs/$configId",
"",
RestStatus.OK.status,
userClient!!
)
deleteConfig(configId, userClient!!)

// Should not be able to find config
executeRequest(
Expand Down Expand Up @@ -477,13 +446,7 @@ class SecurityNotificationIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -521,13 +484,7 @@ class SecurityNotificationIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,7 @@ class NotificationsBackwardsCompatibilityIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/configs",
requestJsonString,
RestStatus.OK.status
)
val createdConfigId = createResponse.get("config_id").asString
val createdConfigId = createConfigWithRequestJsonString(requestJsonString)
assertNotNull(createdConfigId)
Thread.sleep(100)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ class ChimeNotificationConfigCrudIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"$PLUGIN_BASE_URI/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -114,12 +108,7 @@ class ChimeNotificationConfigCrudIT : PluginRestTestCase() {
Thread.sleep(100)

// Delete chime notification config
val deleteResponse = executeRequest(
RestRequest.Method.DELETE.name,
"$PLUGIN_BASE_URI/configs/$configId",
"",
RestStatus.OK.status
)
val deleteResponse = deleteConfig(configId)
Assert.assertEquals("OK", deleteResponse.get("delete_response_list").asJsonObject.get(configId).asString)
Thread.sleep(1000)

Expand Down Expand Up @@ -189,13 +178,7 @@ class ChimeNotificationConfigCrudIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"$PLUGIN_BASE_URI/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -283,13 +266,7 @@ class ChimeNotificationConfigCrudIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"$PLUGIN_BASE_URI/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ class CreateNotificationConfigIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"$PLUGIN_BASE_URI/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)

Expand Down Expand Up @@ -90,13 +84,8 @@ class CreateNotificationConfigIT : PluginRestTestCase() {
}
}
""".trimIndent()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"$PLUGIN_BASE_URI/configs",
createRequestJsonString,
RestStatus.OK.status
)
Assert.assertEquals(configId, createResponse.get("config_id").asString)
val createdConfigId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertEquals(configId, createdConfigId)
Thread.sleep(1000)

// Get chime notification config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,10 @@ import org.opensearch.notifications.verifyMultiConfigIdEquals
import org.opensearch.notifications.verifySingleConfigIdEquals
import org.opensearch.rest.RestRequest
import org.opensearch.rest.RestStatus
import kotlin.random.Random

class DeleteNotificationConfigIT : PluginRestTestCase() {
private val charPool: List<Char> = ('a'..'z') + ('A'..'Z') + ('0'..'9')

private fun getCreateRequestJsonString(): String {
val randomString = (1..20)
.map { Random.nextInt(0, charPool.size) }
.map(charPool::get)
.joinToString("")
return """
{
"config_id":"$randomString",
"config":{
"name":"this is a sample config name $randomString",
"description":"this is a sample config description $randomString",
"config_type":"slack",
"is_enabled":true,
"slack":{"url":"https://domain.com/sample_slack_url#$randomString"}
}
}
""".trimIndent()
}

private fun createConfig(): String {
val createRequestJsonString = getCreateRequestJsonString()
val createResponse = executeRequest(
RestRequest.Method.POST.name,
"$PLUGIN_BASE_URI/configs",
createRequestJsonString,
RestStatus.OK.status
)
val configId = createResponse.get("config_id").asString
Assert.assertNotNull(configId)
Thread.sleep(100)
return configId
}

fun `test Delete single notification config`() {
val configId = createConfig()
Thread.sleep(1000)
Expand All @@ -65,12 +31,7 @@ class DeleteNotificationConfigIT : PluginRestTestCase() {
Thread.sleep(100)

// Delete notification config
val deleteResponse = executeRequest(
RestRequest.Method.DELETE.name,
"$PLUGIN_BASE_URI/configs/$configId",
"",
RestStatus.OK.status
)
val deleteResponse = deleteConfig(configId)
Assert.assertEquals("OK", deleteResponse.get("delete_response_list").asJsonObject.get(configId).asString)
Thread.sleep(100)

Expand Down Expand Up @@ -122,12 +83,7 @@ class DeleteNotificationConfigIT : PluginRestTestCase() {
Thread.sleep(100)

// Delete notification config
val deleteResponse = executeRequest(
RestRequest.Method.DELETE.name,
"$PLUGIN_BASE_URI/configs?config_id_list=${configIds.joinToString(separator = ",")}",
"",
RestStatus.OK.status
)
val deleteResponse = deleteConfigs(configIds)
val deletedObject = deleteResponse.get("delete_response_list").asJsonObject
configIds.forEach {
Assert.assertEquals("OK", deletedObject.get(it).asString)
Expand Down Expand Up @@ -203,12 +159,7 @@ class DeleteNotificationConfigIT : PluginRestTestCase() {
val remainingIds = partitions.second.toSet()

// Delete notification config
val deleteResponse = executeRequest(
RestRequest.Method.DELETE.name,
"$PLUGIN_BASE_URI/configs?config_id_list=${deletedIds.joinToString(separator = ",")}",
"",
RestStatus.OK.status
)
val deleteResponse = deleteConfigs(deletedIds)
val deletedObject = deleteResponse.get("delete_response_list").asJsonObject
deletedIds.forEach {
Assert.assertEquals("OK", deletedObject.get(it).asString)
Expand Down
Loading

0 comments on commit 3a5b1b2

Please sign in to comment.