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

Updated the plugin feature response with allowed config features #74

Merged
merged 1 commit into from
Sep 23, 2021
Merged
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 @@ -66,7 +66,8 @@ object NotificationConstants {
const val TOTAL_HIT_RELATION_TAG = "total_hit_relation"
const val QUERY_TAG = "query"
const val COMPACT_TAG = "compact"
const val CONFIG_TYPE_LIST_TAG = "config_type_list"
const val ALLOWED_CONFIG_TYPE_LIST_TAG = "allowed_config_type_list"
const val ALLOWED_CONFIG_FEATURE_LIST_TAG = "allowed_config_feature_list"
const val PLUGIN_FEATURES_TAG = "plugin_features"

const val FEATURE_ALERTING = "alerting"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ 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.CONFIG_TYPE_LIST_TAG
import org.opensearch.commons.notifications.NotificationConstants.ALLOWED_CONFIG_FEATURE_LIST_TAG
import org.opensearch.commons.notifications.NotificationConstants.ALLOWED_CONFIG_TYPE_LIST_TAG
import org.opensearch.commons.notifications.NotificationConstants.PLUGIN_FEATURES_TAG
import org.opensearch.commons.utils.STRING_READER
import org.opensearch.commons.utils.STRING_WRITER
Expand All @@ -45,7 +46,8 @@ import java.io.IOException
* Action Response for getting notification plugin features.
*/
class GetPluginFeaturesResponse : BaseResponse {
val configTypeList: List<String>
val allowedConfigTypeList: List<String>
val allowedConfigFeatureList: List<String>
val pluginFeatures: Map<String, String>

companion object {
Expand All @@ -63,7 +65,8 @@ class GetPluginFeaturesResponse : BaseResponse {
@JvmStatic
@Throws(IOException::class)
fun parse(parser: XContentParser): GetPluginFeaturesResponse {
var configTypeList: List<String>? = null
var allowedConfigTypeList: List<String>? = null
var allowedConfigFeatureList: List<String>? = null
var pluginFeatures: Map<String, String>? = null

XContentParserUtils.ensureExpectedToken(
Expand All @@ -75,17 +78,19 @@ class GetPluginFeaturesResponse : BaseResponse {
val fieldName = parser.currentName()
parser.nextToken()
when (fieldName) {
CONFIG_TYPE_LIST_TAG -> configTypeList = parser.stringList()
ALLOWED_CONFIG_TYPE_LIST_TAG -> allowedConfigTypeList = parser.stringList()
ALLOWED_CONFIG_FEATURE_LIST_TAG -> allowedConfigFeatureList = parser.stringList()
PLUGIN_FEATURES_TAG -> pluginFeatures = parser.mapStrings()
else -> {
parser.skipChildren()
log.info("Unexpected field: $fieldName, while parsing DeleteNotificationConfigResponse")
}
}
}
configTypeList ?: throw IllegalArgumentException("$CONFIG_TYPE_LIST_TAG field absent")
allowedConfigTypeList ?: throw IllegalArgumentException("$ALLOWED_CONFIG_TYPE_LIST_TAG field absent")
allowedConfigFeatureList ?: throw IllegalArgumentException("$ALLOWED_CONFIG_TYPE_LIST_TAG field absent")
pluginFeatures ?: throw IllegalArgumentException("$PLUGIN_FEATURES_TAG field absent")
return GetPluginFeaturesResponse(configTypeList, pluginFeatures)
return GetPluginFeaturesResponse(allowedConfigTypeList, allowedConfigFeatureList, pluginFeatures)
}
}

Expand All @@ -94,18 +99,25 @@ class GetPluginFeaturesResponse : BaseResponse {
*/
override fun toXContent(builder: XContentBuilder?, params: ToXContent.Params?): XContentBuilder {
return builder!!.startObject()
.field(CONFIG_TYPE_LIST_TAG, configTypeList)
.field(ALLOWED_CONFIG_TYPE_LIST_TAG, allowedConfigTypeList)
.field(ALLOWED_CONFIG_FEATURE_LIST_TAG, allowedConfigFeatureList)
.field(PLUGIN_FEATURES_TAG, pluginFeatures)
.endObject()
}

/**
* constructor for creating the class
* @param configTypeList the list of config types supported by plugin
* @param allowedConfigTypeList the list of config types supported by plugin
* @param allowedConfigFeatureList the list of config features supported by plugin
* @param pluginFeatures the map of plugin features supported to its value
*/
constructor(configTypeList: List<String>, pluginFeatures: Map<String, String>) {
this.configTypeList = configTypeList
constructor(
allowedConfigTypeList: List<String>,
allowedConfigFeatureList: List<String>,
pluginFeatures: Map<String, String>
) {
this.allowedConfigTypeList = allowedConfigTypeList
this.allowedConfigFeatureList = allowedConfigFeatureList
this.pluginFeatures = pluginFeatures
}

Expand All @@ -114,7 +126,8 @@ class GetPluginFeaturesResponse : BaseResponse {
*/
@Throws(IOException::class)
constructor(input: StreamInput) : super(input) {
configTypeList = input.readStringList()
allowedConfigTypeList = input.readStringList()
allowedConfigFeatureList = input.readStringList()
pluginFeatures = input.readMap(STRING_READER, STRING_READER)
}

Expand All @@ -123,7 +136,8 @@ class GetPluginFeaturesResponse : BaseResponse {
*/
@Throws(IOException::class)
override fun writeTo(output: StreamOutput) {
output.writeStringCollection(configTypeList)
output.writeStringCollection(allowedConfigTypeList)
output.writeStringCollection(allowedConfigFeatureList)
output.writeMap(pluginFeatures, STRING_WRITER, STRING_WRITER)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ internal class NotificationsPluginInterfaceTests {
val request = mock(GetPluginFeaturesRequest::class.java)
val response = GetPluginFeaturesResponse(
listOf("config_type_1", "config_type_2", "config_type_3"),
listOf("config_feature_1", "config_feature_2", "config_feature_3"),
mapOf(
Pair("FeatureKey1", "FeatureValue1"),
Pair("FeatureKey2", "FeatureValue2"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ internal class GetPluginFeaturesResponseTests {
expected: GetPluginFeaturesResponse,
actual: GetPluginFeaturesResponse
) {
assertEquals(expected.configTypeList, actual.configTypeList)
assertEquals(expected.allowedConfigTypeList, actual.allowedConfigTypeList)
assertEquals(expected.pluginFeatures, actual.pluginFeatures)
}

@Test
fun `Get Response serialize and deserialize transport object should be equal`() {
val response = GetPluginFeaturesResponse(
listOf("config_type_1", "config_type_2", "config_type_3"),
listOf("config_feature_1", "config_feature_2", "config_feature_3"),
mapOf(
Pair("FeatureKey1", "FeatureValue1"),
Pair("FeatureKey2", "FeatureValue2"),
Expand All @@ -61,6 +62,7 @@ internal class GetPluginFeaturesResponseTests {
fun `Get Response serialize and deserialize using json config object should be equal`() {
val response = GetPluginFeaturesResponse(
listOf("config_type_1", "config_type_2", "config_type_3"),
listOf("config_feature_1", "config_feature_2", "config_feature_3"),
mapOf(
Pair("FeatureKey1", "FeatureValue1"),
Pair("FeatureKey2", "FeatureValue2"),
Expand All @@ -76,6 +78,7 @@ internal class GetPluginFeaturesResponseTests {
fun `Get Response should safely ignore extra field in json object`() {
val response = GetPluginFeaturesResponse(
listOf("config_type_1", "config_type_2", "config_type_3"),
listOf("config_feature_1", "config_feature_2", "config_feature_3"),
mapOf(
Pair("FeatureKey1", "FeatureValue1"),
Pair("FeatureKey2", "FeatureValue2"),
Expand All @@ -84,7 +87,8 @@ internal class GetPluginFeaturesResponseTests {
)
val jsonString = """
{
"config_type_list":["config_type_1", "config_type_2", "config_type_3"],
"allowed_config_type_list":["config_type_1", "config_type_2", "config_type_3"],
"allowed_config_feature_list":["config_feature_1", "config_feature_2", "config_feature_3"],
"plugin_features":{
"FeatureKey1":"FeatureValue1",
"FeatureKey2":"FeatureValue2",
Expand All @@ -100,9 +104,27 @@ internal class GetPluginFeaturesResponseTests {
}

@Test
fun `Get Response should throw exception if config_type_list is absent in json`() {
fun `Get Response should throw exception if allowed_config_type_list is absent in json`() {
val jsonString = """
{
"allowed_config_feature_list":["config_feature_1", "config_feature_2", "config_feature_3"],
"plugin_features":{
"FeatureKey1":"FeatureValue1",
"FeatureKey2":"FeatureValue2",
"FeatureKey3":"FeatureValue3"
}
}
""".trimIndent()
Assertions.assertThrows(IllegalArgumentException::class.java) {
createObjectFromJsonString(jsonString) { GetPluginFeaturesResponse.parse(it) }
}
}

@Test
fun `Get Response should throw exception if allowed_config_feature_list is absent in json`() {
val jsonString = """
{
"allowed_config_type_list":["config_type_1", "config_type_2", "config_type_3"],
"plugin_features":{
"FeatureKey1":"FeatureValue1",
"FeatureKey2":"FeatureValue2",
Expand All @@ -119,7 +141,8 @@ internal class GetPluginFeaturesResponseTests {
fun `Get Response should throw exception if plugin_features is absent in json`() {
val jsonString = """
{
"config_type_list":["config_type_1", "config_type_2", "config_type_3"]
"config_type_list":["config_type_1", "config_type_2", "config_type_3"],
"allowed_config_feature_list":["config_feature_1", "config_feature_2", "config_feature_3"]
}
""".trimIndent()
Assertions.assertThrows(IllegalArgumentException::class.java) {
Expand Down