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.ArrayNode
22 import com.fasterxml.jackson.databind.node.BooleanNode
23 import com.fasterxml.jackson.databind.node.IntNode
24 import com.fasterxml.jackson.databind.node.JsonNodeType
25 import com.fasterxml.jackson.databind.node.MissingNode
26 import com.fasterxml.jackson.databind.node.NullNode
27 import com.fasterxml.jackson.databind.node.ObjectNode
28 import com.fasterxml.jackson.databind.node.TextNode
30 import kotlin.test.assertEquals
31 import kotlin.test.assertFalse
32 import kotlin.test.assertNotNull
33 import kotlin.test.assertNull
34 import kotlin.test.assertTrue
39 * @author Brinda Santh
41 class CustomFunctionsTest {
45 val returnValue: String = format("This is {} for times {}", "test", 2)
46 assertEquals("This is test for times 2", returnValue, "Failed to format String")
48 val returnValue1: String = format("This is test for times 2")
49 assertEquals("This is test for times 2", returnValue1, "Failed to format empty args")
53 fun testStringAsJsonPrimitive() {
54 val returnValue: TextNode = "hello".asJsonPrimitive()
55 assertEquals("hello", returnValue.textValue())
59 fun testIntAsJsonPrimitive() {
60 val returnValue: IntNode = 1.asJsonPrimitive()
61 assertEquals(1, returnValue.intValue())
65 fun testBooleanAsJsonPrimitive() {
66 val returnValue: BooleanNode = false.asJsonPrimitive()
67 assertFalse(returnValue.asBoolean())
71 fun testByteArrayJsonType() {
72 val jsonNode = """{"Name" :"Value"}""".jsonAsJsonType()
74 val byteArray = jsonNode.asByteArray()
75 assertNotNull(byteArray, "failed to get ByteArray form Json")
77 val reverseJsonNode = byteArray.asJsonType()
78 assertNotNull(reverseJsonNode, "failed to get Json type from ByteArray")
82 fun testAsJsonType() {
83 val nullReturnValue: JsonNode = null.asJsonType()
84 assertEquals(NullNode.instance, nullReturnValue)
86 val returnValueString: JsonNode = "hello".asJsonType()
87 assertEquals("hello", returnValueString.textValue())
89 val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
90 assertEquals(returnValueString, returnValueJsonNode)
92 val returnValueInt: JsonNode = 1.asJsonType()
93 assertEquals(1, returnValueInt.intValue())
95 val returnValueBool: JsonNode = false.asJsonType()
96 assertFalse(returnValueBool.asBoolean())
98 val returnValue: JsonNode = BlueprintError().asJsonType()
99 assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
103 fun testMapAsObjectNode() {
104 val returnValue: ObjectNode = hashMapOf("test" to BlueprintError()).asObjectNode()
105 assertNotNull(returnValue.get("test"))
109 fun testCastOptionalValue() {
110 val initMap: Map<String, *> = hashMapOf("test" to 1.1, "test2" to null)
111 val returnValue = initMap.castOptionalValue("test", Number::class)
113 assert(returnValue is Number)
115 val returnValueNull = initMap.castOptionalValue("test1", Number::class)
117 assertNull(returnValueNull)
119 val returnValueString: String? = initMap.castOptionalValue("test2", String::class)
121 assertNull(returnValueString)
124 @Test(expected = BlueprintException::class)
125 fun testCastValue() {
126 val initMap: Map<String, Double> = hashMapOf("test" to 1.1)
127 val returnValue = initMap.castValue("test", Number::class)
129 assertNotNull(returnValue)
131 initMap.castValue("test1", Number::class)
135 fun testAsListOfString() {
136 val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")
138 val result: List<String> = arrayNode.asListOfString()
140 assertTrue(result.isEmpty())
144 fun testReturnNullIfMissing() {
145 val valueExist = "hello".asJsonType().returnNullIfMissing()
146 assertNotNull(valueExist)
148 val valueNull = NullNode.instance.returnNullIfMissing()
149 assertNull(valueNull)
151 val missingValue = MissingNode.getInstance().returnNullIfMissing()
152 assertNull(missingValue)
156 fun testIsNullOrMissing() {
157 assertTrue(NullNode.instance.isNullOrMissing())
158 assertTrue(MissingNode.getInstance().isNullOrMissing())
160 assertFalse(TextNode("").isNullOrMissing())
161 assertFalse("".asJsonType().isNullOrMissing())
162 assertFalse("hello".asJsonType().isNullOrMissing())
163 assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing())
164 assertFalse(TextNode("hello").isNullOrMissing())
168 fun testIsComplexType() {
169 assertFalse(NullNode.instance.isComplexType())
170 assertFalse(MissingNode.getInstance().isComplexType())
172 assertFalse(TextNode("").isComplexType())
173 assertFalse("".asJsonType().isComplexType())
174 assertFalse("hello".asJsonType().isComplexType())
175 assertFalse(TextNode("hello").isComplexType())
177 assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType())
178 assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType())
181 @Test(expected = BlueprintException::class)
182 fun testRootFieldsToMap() {
183 1.asJsonType().rootFieldsToMap()
187 fun testPutJsonElement() {
188 val mutMap = mutableMapOf("test" to 2.asJsonType())
190 mutMap.putJsonElement("hello", 3)
192 assertEquals(3, mutMap["hello"]?.asInt())
195 @Test(expected = BlueprintException::class)
196 fun testMapGetAsString() {
197 val initMap = hashMapOf("test" to "hello".asJsonType())
199 assertEquals("hello", initMap.getAsString("test"))
201 initMap.getAsString("test2")
204 @Test(expected = BlueprintException::class)
205 fun testMapGetAsBoolean() {
206 val initMap = hashMapOf("test" to true.asJsonType())
208 assertTrue(initMap.getAsBoolean("test"))
210 initMap.getAsBoolean("test2")
213 @Test(expected = BlueprintException::class)
214 fun testMapGetAsInt() {
215 val initMap = hashMapOf("test" to 1.asJsonType())
217 assertEquals(1, initMap.getAsInt("test"))
219 initMap.getAsInt("test2")
222 @Test(expected = BlueprintException::class)
223 fun testCheckEquals() {
224 assertTrue(checkEquals("hello", "hello", { -> "error" }))
226 checkEquals("hello", "test", { -> "error" })
229 @Test(expected = IllegalStateException::class)
230 fun testCheckNotEmpty() {
231 assertEquals("hello", checkNotEmpty("hello", { -> "error" }))
233 checkNotEmpty("", { -> "error" })
236 @Test(expected = IllegalStateException::class)
237 fun testCheckNotBlank() {
238 assertEquals("hello", checkNotBlank("hello", { -> "error" }))
240 checkNotBlank(" ", { -> "error" })
244 fun testNullToEmpty() {
245 assertEquals("", nullToEmpty(null))
247 assertEquals("hello", nullToEmpty("hello"))