76be647f116655c8c5b16648dfbfb88c64f9b47a
[ccsdk/cds.git] / ms / controllerblueprints / modules / blueprint-core / src / test / kotlin / org / onap / ccsdk / cds / controllerblueprints / core / CustomFunctionsTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.ccsdk.cds.controllerblueprints.core
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import com.fasterxml.jackson.databind.ObjectMapper
21 import com.fasterxml.jackson.databind.node.*
22 import org.junit.Test
23 import kotlin.test.*
24
25 /**
26  *
27  *
28  * @author Brinda Santh
29  */
30 class CustomFunctionsTest {
31     @Test
32     fun testFormat() {
33         val returnValue : String = format("This is {} for times {}", "test", 2)
34         assertEquals("This is test for times 2", returnValue, "Failed to format String")
35
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")
38     }
39
40     @Test
41     fun testStringAsJsonPrimitive() {
42         val returnValue: TextNode = "hello".asJsonPrimitive()
43         assertEquals("hello", returnValue.textValue())
44     }
45
46     @Test
47     fun testIntAsJsonPrimitive() {
48         val returnValue: IntNode = 1.asJsonPrimitive()
49         assertEquals(1, returnValue.intValue())
50     }
51
52     @Test
53     fun testBooleanAsJsonPrimitive() {
54         val returnValue: BooleanNode = false.asJsonPrimitive()
55         assertFalse(returnValue.asBoolean())
56     }
57
58     @Test
59     fun testAsJsonType() {
60         val nullReturnValue: JsonNode = null.asJsonType()
61         assertEquals(NullNode.instance, nullReturnValue)
62
63         val returnValueString: JsonNode = "hello".asJsonType()
64         assertEquals("hello", returnValueString.textValue())
65
66         val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
67         assertEquals(returnValueString, returnValueJsonNode)
68
69         val returnValueInt: JsonNode = 1.asJsonType()
70         assertEquals(1, returnValueInt.intValue())
71
72         val returnValueBool: JsonNode = false.asJsonType()
73         assertFalse(returnValueBool.asBoolean())
74
75         val returnValue: JsonNode = BluePrintError().asJsonType()
76         assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
77     }
78
79     @Test
80     fun testMapAsObjectNode() {
81         val returnValue: ObjectNode = hashMapOf("test" to BluePrintError()).asObjectNode()
82         assertNotNull(returnValue.get("test"))
83     }
84
85     @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)
89
90         assert(returnValue is Number)
91
92         val returnValueNull = initMap.castOptionalValue("test1", Number::class)
93
94         assertNull(returnValueNull)
95
96         val returnValueString: String? = initMap.castOptionalValue("test2", String::class)
97
98         assertNull(returnValueString)
99     }
100
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)
105
106         assertNotNull(returnValue)
107
108         initMap.castValue("test1", Number::class)
109     }
110
111     @Test
112     fun testAsListOfString() {
113         val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")
114
115         val result: List<String> = arrayNode.asListOfString()
116
117         assertTrue(result.isEmpty())
118     }
119
120     @Test
121     fun testReturnNullIfMissing() {
122         val valueExist = "hello".asJsonType().returnNullIfMissing()
123         assertNotNull(valueExist)
124
125         val valueNull = NullNode.instance.returnNullIfMissing()
126         assertNull(valueNull)
127
128         val missingValue = MissingNode.getInstance().returnNullIfMissing()
129         assertNull(missingValue)
130     }
131
132     @Test
133     fun testIsNullOrMissing() {
134         assertTrue(NullNode.instance.isNullOrMissing())
135         assertTrue(MissingNode.getInstance().isNullOrMissing())
136
137         assertFalse(TextNode("").isNullOrMissing())
138         assertFalse("".asJsonType().isNullOrMissing())
139         assertFalse("hello".asJsonType().isNullOrMissing())
140         assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing())
141         assertFalse(TextNode("hello").isNullOrMissing())
142     }
143
144     @Test
145     fun testIsComplexType() {
146         assertFalse(NullNode.instance.isComplexType())
147         assertFalse(MissingNode.getInstance().isComplexType())
148
149         assertFalse(TextNode("").isComplexType())
150         assertFalse("".asJsonType().isComplexType())
151         assertFalse("hello".asJsonType().isComplexType())
152         assertFalse(TextNode("hello").isComplexType())
153
154         assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType())
155         assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType())
156     }
157
158     @Test(expected = BluePrintException::class)
159     fun testRootFieldsToMap() {
160         1.asJsonType().rootFieldsToMap()
161     }
162
163     @Test
164     fun testPutJsonElement() {
165         val mutMap = mutableMapOf("test" to 2.asJsonType())
166
167         mutMap.putJsonElement("hello", 3)
168
169         assertEquals(3, mutMap["hello"]?.asInt())
170     }
171
172     @Test(expected = BluePrintException::class)
173     fun testMapGetAsString() {
174         val initMap = hashMapOf("test" to "hello".asJsonType())
175
176         assertEquals("hello", initMap.getAsString("test"))
177
178         initMap.getAsString("test2")
179     }
180
181     @Test(expected = BluePrintException::class)
182     fun testMapGetAsBoolean() {
183         val initMap = hashMapOf("test" to true.asJsonType())
184
185         assertTrue(initMap.getAsBoolean("test"))
186
187         initMap.getAsBoolean("test2")
188     }
189
190     @Test(expected = BluePrintException::class)
191     fun testMapGetAsInt() {
192         val initMap = hashMapOf("test" to 1.asJsonType())
193
194         assertEquals(1, initMap.getAsInt("test"))
195
196         initMap.getAsInt("test2")
197     }
198
199     @Test(expected = BluePrintException::class)
200     fun testCheckEquals() {
201         assertTrue(checkEquals("hello", "hello", { -> "error"}))
202
203         checkEquals("hello", "test", { -> "error"})
204     }
205
206     @Test(expected = IllegalStateException::class)
207     fun testCheckNotEmpty() {
208         assertEquals("hello", checkNotEmpty("hello", { -> "error"}))
209
210         checkNotEmpty("", { -> "error"})
211     }
212
213     @Test(expected = IllegalStateException::class)
214     fun testCheckNotBlank() {
215         assertEquals("hello", checkNotBlank("hello", { -> "error"}))
216
217         checkNotBlank("  ", { -> "error"})
218     }
219
220     @Test
221     fun testNullToEmpty() {
222         assertEquals("", nullToEmpty(null))
223
224         assertEquals("hello", nullToEmpty("hello"))
225     }
226 }