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 testAsJsonType() {
72 val nullReturnValue: JsonNode = null.asJsonType()
73 assertEquals(NullNode.instance, nullReturnValue)
75 val returnValueString: JsonNode = "hello".asJsonType()
76 assertEquals("hello", returnValueString.textValue())
78 val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
79 assertEquals(returnValueString, returnValueJsonNode)
81 val returnValueInt: JsonNode = 1.asJsonType()
82 assertEquals(1, returnValueInt.intValue())
84 val returnValueBool: JsonNode = false.asJsonType()
85 assertFalse(returnValueBool.asBoolean())
87 val returnValue: JsonNode = BluePrintError().asJsonType()
88 assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
92 fun testMapAsObjectNode() {
93 val returnValue: ObjectNode = hashMapOf("test" to BluePrintError()).asObjectNode()
94 assertNotNull(returnValue.get("test"))
98 fun testCastOptionalValue() {
99 val initMap: Map<String, *> = hashMapOf("test" to 1.1, "test2" to null)
100 val returnValue = initMap.castOptionalValue("test", Number::class)
102 assert(returnValue is Number)
104 val returnValueNull = initMap.castOptionalValue("test1", Number::class)
106 assertNull(returnValueNull)
108 val returnValueString: String? = initMap.castOptionalValue("test2", String::class)
110 assertNull(returnValueString)
113 @Test(expected = BluePrintException::class)
114 fun testCastValue() {
115 val initMap: Map<String, Double> = hashMapOf("test" to 1.1)
116 val returnValue = initMap.castValue("test", Number::class)
118 assertNotNull(returnValue)
120 initMap.castValue("test1", Number::class)
124 fun testAsListOfString() {
125 val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")
127 val result: List<String> = arrayNode.asListOfString()
129 assertTrue(result.isEmpty())
133 fun testReturnNullIfMissing() {
134 val valueExist = "hello".asJsonType().returnNullIfMissing()
135 assertNotNull(valueExist)
137 val valueNull = NullNode.instance.returnNullIfMissing()
138 assertNull(valueNull)
140 val missingValue = MissingNode.getInstance().returnNullIfMissing()
141 assertNull(missingValue)
145 fun testIsNullOrMissing() {
146 assertTrue(NullNode.instance.isNullOrMissing())
147 assertTrue(MissingNode.getInstance().isNullOrMissing())
149 assertFalse(TextNode("").isNullOrMissing())
150 assertFalse("".asJsonType().isNullOrMissing())
151 assertFalse("hello".asJsonType().isNullOrMissing())
152 assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing())
153 assertFalse(TextNode("hello").isNullOrMissing())
157 fun testIsComplexType() {
158 assertFalse(NullNode.instance.isComplexType())
159 assertFalse(MissingNode.getInstance().isComplexType())
161 assertFalse(TextNode("").isComplexType())
162 assertFalse("".asJsonType().isComplexType())
163 assertFalse("hello".asJsonType().isComplexType())
164 assertFalse(TextNode("hello").isComplexType())
166 assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType())
167 assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType())
170 @Test(expected = BluePrintException::class)
171 fun testRootFieldsToMap() {
172 1.asJsonType().rootFieldsToMap()
176 fun testPutJsonElement() {
177 val mutMap = mutableMapOf("test" to 2.asJsonType())
179 mutMap.putJsonElement("hello", 3)
181 assertEquals(3, mutMap["hello"]?.asInt())
184 @Test(expected = BluePrintException::class)
185 fun testMapGetAsString() {
186 val initMap = hashMapOf("test" to "hello".asJsonType())
188 assertEquals("hello", initMap.getAsString("test"))
190 initMap.getAsString("test2")
193 @Test(expected = BluePrintException::class)
194 fun testMapGetAsBoolean() {
195 val initMap = hashMapOf("test" to true.asJsonType())
197 assertTrue(initMap.getAsBoolean("test"))
199 initMap.getAsBoolean("test2")
202 @Test(expected = BluePrintException::class)
203 fun testMapGetAsInt() {
204 val initMap = hashMapOf("test" to 1.asJsonType())
206 assertEquals(1, initMap.getAsInt("test"))
208 initMap.getAsInt("test2")
211 @Test(expected = BluePrintException::class)
212 fun testCheckEquals() {
213 assertTrue(checkEquals("hello", "hello", { -> "error" }))
215 checkEquals("hello", "test", { -> "error" })
218 @Test(expected = IllegalStateException::class)
219 fun testCheckNotEmpty() {
220 assertEquals("hello", checkNotEmpty("hello", { -> "error" }))
222 checkNotEmpty("", { -> "error" })
225 @Test(expected = IllegalStateException::class)
226 fun testCheckNotBlank() {
227 assertEquals("hello", checkNotBlank("hello", { -> "error" }))
229 checkNotBlank(" ", { -> "error" })
233 fun testNullToEmpty() {
234 assertEquals("", nullToEmpty(null))
236 assertEquals("hello", nullToEmpty("hello"))