diff options
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-core')
4 files changed, 47 insertions, 10 deletions
diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt index 34a2d9cd3..2717c3be9 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctions.kt @@ -22,6 +22,7 @@ import com.fasterxml.jackson.databind.node.* import org.apache.commons.lang3.ObjectUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils import org.slf4j.LoggerFactory +import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils import org.slf4j.helpers.MessageFormatter import kotlin.reflect.KClass @@ -41,8 +42,8 @@ fun <T : Any> T.bpClone(): T { } fun String.isJson(): Boolean { - return ((this.startsWith("{") && this.endsWith("}")) - || (this.startsWith("[") && this.endsWith("]"))) + return ((this.trim().startsWith("{") && this.trim().endsWith("}")) + || (this.trim().startsWith("[") && this.trim().endsWith("]"))) } fun Any.asJsonString(intend: Boolean? = false): String { @@ -263,4 +264,16 @@ inline fun <reified T : JsonNode> T.isComplexType(): Boolean { return this is ObjectNode || this is ArrayNode } +// Json Parsing Extensions +fun JsonNode.jsonPathParse(expression: String): JsonNode { + check(this.isComplexType()) { "$this is not complex or array node to apply expression" } + return JsonParserUtils.parse(this, expression) +} + +// Json Path Extensions +fun JsonNode.jsonPaths(expression: String): List<String> { + check(this.isComplexType()) { "$this is not complex or array node to apply expression" } + return JsonParserUtils.paths(this, expression) +} + diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt index 30bd75f7d..e5788a936 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/service/PropertyAssignmentService.kt @@ -24,7 +24,6 @@ import com.fasterxml.jackson.databind.node.NullNode import org.onap.ccsdk.cds.controllerblueprints.core.* import org.onap.ccsdk.cds.controllerblueprints.core.data.* import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils -import org.onap.ccsdk.cds.controllerblueprints.core.utils.JsonParserUtils import org.onap.ccsdk.cds.controllerblueprints.core.utils.ResourceResolverUtils /** @@ -141,7 +140,7 @@ If Property Assignment is Expression. } if (subAttributeName != null) { if (valueNode.isComplexType()) - valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subAttributeName) + valueNode = valueNode.jsonPathParse(subAttributeName) } return valueNode } @@ -174,7 +173,7 @@ If Property Assignment is Expression. if (subPropertyName != null) { if (valueNode.isComplexType()) - valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName) + valueNode = valueNode.jsonPathParse(subPropertyName) } return valueNode } @@ -195,7 +194,7 @@ If Property Assignment is Expression. val subPropertyName: String? = operationOutputExpression.subPropertyName if (subPropertyName != null) { if (valueNode.isComplexType()) - valueNode = JsonParserUtils.parse(valueNode.asJsonString(), subPropertyName) + valueNode = valueNode.jsonPathParse(subPropertyName) } return valueNode } diff --git a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt index e5eef5a41..19686b5df 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtils.kt @@ -24,6 +24,7 @@ import com.jayway.jsonpath.JsonPath import com.jayway.jsonpath.Option import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider +import org.onap.ccsdk.cds.controllerblueprints.core.asJsonString class JsonParserUtils { companion object { @@ -39,7 +40,7 @@ class JsonParserUtils { } fun paths(jsonNode: JsonNode, expression: String): List<String> { - return paths(jsonNode, expression) + return paths(jsonNode.asJsonString(), expression) } fun parse(jsonContent: String, expression: String): JsonNode { @@ -47,7 +48,7 @@ class JsonParserUtils { } fun parse(jsonNode: JsonNode, expression: String): JsonNode { - return parse(jsonNode.toString(), expression) + return parse(jsonNode.asJsonString(), expression) } fun parseNSet(jsonContent: String, expression: String, value: JsonNode): JsonNode { @@ -56,7 +57,7 @@ class JsonParserUtils { fun parseNSet(jsonNode: JsonNode, expression: String, valueNode: JsonNode): JsonNode { - return parseNSet(jsonNode, expression, valueNode) + return parseNSet(jsonNode.asJsonString(), expression, valueNode) } } }
\ No newline at end of file diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt index 9728a2255..810dae738 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/utils/JsonParserUtilsTest.kt @@ -18,6 +18,9 @@ package org.onap.ccsdk.cds.controllerblueprints.core.utils import org.junit.Test import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive +import org.onap.ccsdk.cds.controllerblueprints.core.jsonAsJsonType +import org.onap.ccsdk.cds.controllerblueprints.core.jsonPathParse +import org.onap.ccsdk.cds.controllerblueprints.core.jsonPaths import kotlin.test.assertEquals class JsonParserUtilsTest { @@ -26,8 +29,29 @@ class JsonParserUtilsTest { fun `test parse Node`() { val dataNode = JacksonUtils.jsonNodeFromClassPathFile("data/default-context.json") - val parsedNode = JsonParserUtils.parse(dataNode, "$.request-id") + val parsedNode = dataNode.jsonPathParse("$.request-id") assertEquals(parsedNode, "12345".asJsonPrimitive(), "failed to parse json request-id") } + + @Test + fun testPaths() { + val json: String = """ + { + "data" : { + "prop1" : "1234" + }, + "data2" : { + "prop1" : "12345" + }, + "data3" : [{ + "prop1" : "12345" + } + ] + } + """.trimIndent() + val jsonNode = json.jsonAsJsonType() + val parsedPath = jsonNode.jsonPaths("$..prop1") + println(parsedPath) + } }
\ No newline at end of file |