From e0e2aa06217ea5299e720e97f4275197551b3e68 Mon Sep 17 00:00:00 2001 From: "Singal, Kapil (ks220y)" Date: Mon, 23 Sep 2019 18:47:29 -0400 Subject: Refactoring ResourceAssignmentUtils Changing isNull condition to isNullOrEmpty to make sure Empty value doesn't get assigned to resource Issue-ID: CCSDK-1748 Signed-off-by: Singal, Kapil (ks220y) Change-Id: I0744537c7ddec80f20ffd7e6545b947439f63743 --- .../controllerblueprints/core/CustomFunctions.kt | 15 +++++-------- .../core/CustomFunctionsTest.kt | 26 ++++++++++++++++++++++ .../resource/dict/ResourceDefinition.kt | 22 ++++++++++-------- 3 files changed, 45 insertions(+), 18 deletions(-) (limited to 'ms/controllerblueprints/modules') 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 { fun JsonNode.asType(clazzType: Class): 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 { @@ -183,20 +183,17 @@ fun JsonNode.asListOfString(): List { return this.asListOfString() } -fun JsonNode.returnNullIfMissing(): JsonNode? { - return if (this is NullNode || this is MissingNode) { +fun T?.returnNullIfMissing(): JsonNode? { + return if (this == null || this is NullNode || this is MissingNode) { null - } else this + } + else this } -fun T?.isNull(): Boolean { +fun T?.isNullOrMissing(): Boolean { return this == null || this is NullNode || this is MissingNode } -fun 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() diff --git a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt index c222de9e5..2966d8b5d 100644 --- a/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt +++ b/ms/controllerblueprints/modules/resource-dict/src/main/kotlin/org/onap/ccsdk/cds/controllerblueprints/resource/dict/ResourceDefinition.kt @@ -84,15 +84,19 @@ open class ResourceAssignment { var updatedBy: String? = null override fun toString(): String { - return StringBuilder() - .append("[") - .append("name=", name) - .append(", status=", status) - .append(", property=", property?.value ?: "") - .append(", dictionaryName=", dictionaryName) - .append(", dictionarySource=", dictionarySource) - .append("]") - .toString() + return """ + [ + name = $name + status = $status + property [ + defaultValue = ${property?.defaultValue} + required = ${property?.required} + metadata = ${property?.metadata} + ] + dictionaryName = $dictionaryName + dictionarySource = $dictionarySource + ] + """.trimIndent() } } -- cgit 1.2.3-korg