diff options
Diffstat (limited to 'ms/controllerblueprints/modules/blueprint-core')
2 files changed, 32 insertions, 9 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 b74b7e4cf..1aaf9d8a4 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 @@ -175,7 +175,7 @@ fun ArrayNode.asListOfString(): List<String> { fun <T> JsonNode.asType(clazzType: Class<T>): T { return JacksonUtils.readValue(this, clazzType) - ?: throw BluePrintException("couldn't convert JsonNode of type $clazzType") + ?: throw BluePrintException("couldn't convert JsonNode of type $clazzType") } fun JsonNode.asListOfString(): List<String> { @@ -183,20 +183,17 @@ fun JsonNode.asListOfString(): List<String> { return this.asListOfString() } -fun JsonNode.returnNullIfMissing(): JsonNode? { - return if (this is NullNode || this is MissingNode) { +fun <T : JsonNode> T?.returnNullIfMissing(): JsonNode? { + return if (this == null || this is NullNode || this is MissingNode) { null - } else this + } + else this } -fun <T : JsonNode> T?.isNull(): Boolean { +fun <T : JsonNode> T?.isNullOrMissing(): Boolean { return this == null || this is NullNode || this is MissingNode } -fun <T : JsonNode> T?.isNotNull(): Boolean { - return !(this == null || this is NullNode || this is MissingNode) -} - /** * Convert Json to map of json node, the root fields will be map keys */ diff --git a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt index 487b1d15b..76be647f1 100644 --- a/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt +++ b/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt @@ -129,6 +129,32 @@ class CustomFunctionsTest { assertNull(missingValue) } + @Test + fun testIsNullOrMissing() { + assertTrue(NullNode.instance.isNullOrMissing()) + assertTrue(MissingNode.getInstance().isNullOrMissing()) + + assertFalse(TextNode("").isNullOrMissing()) + assertFalse("".asJsonType().isNullOrMissing()) + assertFalse("hello".asJsonType().isNullOrMissing()) + assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing()) + assertFalse(TextNode("hello").isNullOrMissing()) + } + + @Test + fun testIsComplexType() { + assertFalse(NullNode.instance.isComplexType()) + assertFalse(MissingNode.getInstance().isComplexType()) + + assertFalse(TextNode("").isComplexType()) + assertFalse("".asJsonType().isComplexType()) + assertFalse("hello".asJsonType().isComplexType()) + assertFalse(TextNode("hello").isComplexType()) + + assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType()) + assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType()) + } + @Test(expected = BluePrintException::class) fun testRootFieldsToMap() { 1.asJsonType().rootFieldsToMap() |