Merge "Fixed defect CCSDK-1299"
[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(expected = BluePrintException::class)
133     fun testRootFieldsToMap() {
134         1.asJsonType().rootFieldsToMap()
135     }
136
137     @Test
138     fun testPutJsonElement() {
139         val mutMap = mutableMapOf("test" to 2.asJsonType())
140
141         mutMap.putJsonElement("hello", 3)
142
143         assertEquals(3, mutMap["hello"]?.asInt())
144     }
145
146     @Test(expected = BluePrintException::class)
147     fun testMapGetAsString() {
148         val initMap = hashMapOf("test" to "hello".asJsonType())
149
150         assertEquals("hello", initMap.getAsString("test"))
151
152         initMap.getAsString("test2")
153     }
154
155     @Test(expected = BluePrintException::class)
156     fun testMapGetAsBoolean() {
157         val initMap = hashMapOf("test" to true.asJsonType())
158
159         assertTrue(initMap.getAsBoolean("test"))
160
161         initMap.getAsBoolean("test2")
162     }
163
164     @Test(expected = BluePrintException::class)
165     fun testMapGetAsInt() {
166         val initMap = hashMapOf("test" to 1.asJsonType())
167
168         assertEquals(1, initMap.getAsInt("test"))
169
170         initMap.getAsInt("test2")
171     }
172
173     @Test(expected = BluePrintException::class)
174     fun testCheckEquals() {
175         assertTrue(checkEquals("hello", "hello", { -> "error"}))
176
177         checkEquals("hello", "test", { -> "error"})
178     }
179
180     @Test(expected = IllegalStateException::class)
181     fun testCheckNotEmpty() {
182         assertEquals("hello", checkNotEmpty("hello", { -> "error"}))
183
184         checkNotEmpty("", { -> "error"})
185     }
186
187     @Test(expected = IllegalStateException::class)
188     fun testCheckNotBlank() {
189         assertEquals("hello", checkNotBlank("hello", { -> "error"}))
190
191         checkNotBlank("  ", { -> "error"})
192     }
193
194     @Test
195     fun testNullToEmpty() {
196         assertEquals("", nullToEmpty(null))
197
198         assertEquals("hello", nullToEmpty("hello"))
199     }
200 }