2 * Copyright © 2017-2018 AT&T Intellectual Property.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.ccsdk.cds.controllerblueprints.core
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.*
28 * @author Brinda Santh
30 class CustomFunctionsTest {
33 val returnValue : String = format("This is {} for times {}", "test", 2)
34 assertEquals("This is test for times 2", returnValue, "Failed to format String")
36 val returnValue1 : String = format("This is test for times 2")
37 assertEquals("This is test for times 2", returnValue1, "Failed to format empty args")
41 fun testStringAsJsonPrimitive() {
42 val returnValue: TextNode = "hello".asJsonPrimitive()
43 assertEquals("hello", returnValue.textValue())
47 fun testIntAsJsonPrimitive() {
48 val returnValue: IntNode = 1.asJsonPrimitive()
49 assertEquals(1, returnValue.intValue())
53 fun testBooleanAsJsonPrimitive() {
54 val returnValue: BooleanNode = false.asJsonPrimitive()
55 assertFalse(returnValue.asBoolean())
59 fun testAsJsonType() {
60 val nullReturnValue: JsonNode = null.asJsonType()
61 assertEquals(NullNode.instance, nullReturnValue)
63 val returnValueString: JsonNode = "hello".asJsonType()
64 assertEquals("hello", returnValueString.textValue())
66 val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
67 assertEquals(returnValueString, returnValueJsonNode)
69 val returnValueInt: JsonNode = 1.asJsonType()
70 assertEquals(1, returnValueInt.intValue())
72 val returnValueBool: JsonNode = false.asJsonType()
73 assertFalse(returnValueBool.asBoolean())
75 val returnValue: JsonNode = BluePrintError().asJsonType()
76 assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
80 fun testMapAsObjectNode() {
81 val returnValue: ObjectNode = hashMapOf("test" to BluePrintError()).asObjectNode()
82 assertNotNull(returnValue.get("test"))
86 fun testCastOptionalValue() {
87 val initMap: Map<String, *> = hashMapOf("test" to 1.1, "test2" to null)
88 val returnValue = initMap.castOptionalValue("test", Number::class)
90 assert(returnValue is Number)
92 val returnValueNull = initMap.castOptionalValue("test1", Number::class)
94 assertNull(returnValueNull)
96 val returnValueString: String? = initMap.castOptionalValue("test2", String::class)
98 assertNull(returnValueString)
101 @Test(expected = BluePrintException::class)
102 fun testCastValue() {
103 val initMap: Map<String, Double> = hashMapOf("test" to 1.1)
104 val returnValue = initMap.castValue("test", Number::class)
106 assertNotNull(returnValue)
108 initMap.castValue("test1", Number::class)
112 fun testAsListOfString() {
113 val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")
115 val result: List<String> = arrayNode.asListOfString()
117 assertTrue(result.isEmpty())
121 fun testReturnNullIfMissing() {
122 val valueExist = "hello".asJsonType().returnNullIfMissing()
123 assertNotNull(valueExist)
125 val valueNull = NullNode.instance.returnNullIfMissing()
126 assertNull(valueNull)
128 val missingValue = MissingNode.getInstance().returnNullIfMissing()
129 assertNull(missingValue)
133 fun testIsNullOrMissing() {
134 assertTrue(NullNode.instance.isNullOrMissing())
135 assertTrue(MissingNode.getInstance().isNullOrMissing())
137 assertFalse(TextNode("").isNullOrMissing())
138 assertFalse("".asJsonType().isNullOrMissing())
139 assertFalse("hello".asJsonType().isNullOrMissing())
140 assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing())
141 assertFalse(TextNode("hello").isNullOrMissing())
145 fun testIsComplexType() {
146 assertFalse(NullNode.instance.isComplexType())
147 assertFalse(MissingNode.getInstance().isComplexType())
149 assertFalse(TextNode("").isComplexType())
150 assertFalse("".asJsonType().isComplexType())
151 assertFalse("hello".asJsonType().isComplexType())
152 assertFalse(TextNode("hello").isComplexType())
154 assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType())
155 assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType())
158 @Test(expected = BluePrintException::class)
159 fun testRootFieldsToMap() {
160 1.asJsonType().rootFieldsToMap()
164 fun testPutJsonElement() {
165 val mutMap = mutableMapOf("test" to 2.asJsonType())
167 mutMap.putJsonElement("hello", 3)
169 assertEquals(3, mutMap["hello"]?.asInt())
172 @Test(expected = BluePrintException::class)
173 fun testMapGetAsString() {
174 val initMap = hashMapOf("test" to "hello".asJsonType())
176 assertEquals("hello", initMap.getAsString("test"))
178 initMap.getAsString("test2")
181 @Test(expected = BluePrintException::class)
182 fun testMapGetAsBoolean() {
183 val initMap = hashMapOf("test" to true.asJsonType())
185 assertTrue(initMap.getAsBoolean("test"))
187 initMap.getAsBoolean("test2")
190 @Test(expected = BluePrintException::class)
191 fun testMapGetAsInt() {
192 val initMap = hashMapOf("test" to 1.asJsonType())
194 assertEquals(1, initMap.getAsInt("test"))
196 initMap.getAsInt("test2")
199 @Test(expected = BluePrintException::class)
200 fun testCheckEquals() {
201 assertTrue(checkEquals("hello", "hello", { -> "error"}))
203 checkEquals("hello", "test", { -> "error"})
206 @Test(expected = IllegalStateException::class)
207 fun testCheckNotEmpty() {
208 assertEquals("hello", checkNotEmpty("hello", { -> "error"}))
210 checkNotEmpty("", { -> "error"})
213 @Test(expected = IllegalStateException::class)
214 fun testCheckNotBlank() {
215 assertEquals("hello", checkNotBlank("hello", { -> "error"}))
217 checkNotBlank(" ", { -> "error"})
221 fun testNullToEmpty() {
222 assertEquals("", nullToEmpty(null))
224 assertEquals("hello", nullToEmpty("hello"))