Remote Script Executor Component
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / blueprints / 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.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
29 import org.junit.Test
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
35
36 /**
37  *
38  *
39  * @author Brinda Santh
40  */
41 class CustomFunctionsTest {
42
43     @Test
44     fun testFormat() {
45         val returnValue: String = format("This is {} for times {}", "test", 2)
46         assertEquals("This is test for times 2", returnValue, "Failed to format String")
47
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")
50     }
51
52     @Test
53     fun testStringAsJsonPrimitive() {
54         val returnValue: TextNode = "hello".asJsonPrimitive()
55         assertEquals("hello", returnValue.textValue())
56     }
57
58     @Test
59     fun testIntAsJsonPrimitive() {
60         val returnValue: IntNode = 1.asJsonPrimitive()
61         assertEquals(1, returnValue.intValue())
62     }
63
64     @Test
65     fun testBooleanAsJsonPrimitive() {
66         val returnValue: BooleanNode = false.asJsonPrimitive()
67         assertFalse(returnValue.asBoolean())
68     }
69
70     @Test
71     fun testAsJsonType() {
72         val nullReturnValue: JsonNode = null.asJsonType()
73         assertEquals(NullNode.instance, nullReturnValue)
74
75         val returnValueString: JsonNode = "hello".asJsonType()
76         assertEquals("hello", returnValueString.textValue())
77
78         val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
79         assertEquals(returnValueString, returnValueJsonNode)
80
81         val returnValueInt: JsonNode = 1.asJsonType()
82         assertEquals(1, returnValueInt.intValue())
83
84         val returnValueBool: JsonNode = false.asJsonType()
85         assertFalse(returnValueBool.asBoolean())
86
87         val returnValue: JsonNode = BluePrintError().asJsonType()
88         assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
89     }
90
91     @Test
92     fun testMapAsObjectNode() {
93         val returnValue: ObjectNode = hashMapOf("test" to BluePrintError()).asObjectNode()
94         assertNotNull(returnValue.get("test"))
95     }
96
97     @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)
101
102         assert(returnValue is Number)
103
104         val returnValueNull = initMap.castOptionalValue("test1", Number::class)
105
106         assertNull(returnValueNull)
107
108         val returnValueString: String? = initMap.castOptionalValue("test2", String::class)
109
110         assertNull(returnValueString)
111     }
112
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)
117
118         assertNotNull(returnValue)
119
120         initMap.castValue("test1", Number::class)
121     }
122
123     @Test
124     fun testAsListOfString() {
125         val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")
126
127         val result: List<String> = arrayNode.asListOfString()
128
129         assertTrue(result.isEmpty())
130     }
131
132     @Test
133     fun testReturnNullIfMissing() {
134         val valueExist = "hello".asJsonType().returnNullIfMissing()
135         assertNotNull(valueExist)
136
137         val valueNull = NullNode.instance.returnNullIfMissing()
138         assertNull(valueNull)
139
140         val missingValue = MissingNode.getInstance().returnNullIfMissing()
141         assertNull(missingValue)
142     }
143
144     @Test
145     fun testIsNullOrMissing() {
146         assertTrue(NullNode.instance.isNullOrMissing())
147         assertTrue(MissingNode.getInstance().isNullOrMissing())
148
149         assertFalse(TextNode("").isNullOrMissing())
150         assertFalse("".asJsonType().isNullOrMissing())
151         assertFalse("hello".asJsonType().isNullOrMissing())
152         assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing())
153         assertFalse(TextNode("hello").isNullOrMissing())
154     }
155
156     @Test
157     fun testIsComplexType() {
158         assertFalse(NullNode.instance.isComplexType())
159         assertFalse(MissingNode.getInstance().isComplexType())
160
161         assertFalse(TextNode("").isComplexType())
162         assertFalse("".asJsonType().isComplexType())
163         assertFalse("hello".asJsonType().isComplexType())
164         assertFalse(TextNode("hello").isComplexType())
165
166         assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType())
167         assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType())
168     }
169
170     @Test(expected = BluePrintException::class)
171     fun testRootFieldsToMap() {
172         1.asJsonType().rootFieldsToMap()
173     }
174
175     @Test
176     fun testPutJsonElement() {
177         val mutMap = mutableMapOf("test" to 2.asJsonType())
178
179         mutMap.putJsonElement("hello", 3)
180
181         assertEquals(3, mutMap["hello"]?.asInt())
182     }
183
184     @Test(expected = BluePrintException::class)
185     fun testMapGetAsString() {
186         val initMap = hashMapOf("test" to "hello".asJsonType())
187
188         assertEquals("hello", initMap.getAsString("test"))
189
190         initMap.getAsString("test2")
191     }
192
193     @Test(expected = BluePrintException::class)
194     fun testMapGetAsBoolean() {
195         val initMap = hashMapOf("test" to true.asJsonType())
196
197         assertTrue(initMap.getAsBoolean("test"))
198
199         initMap.getAsBoolean("test2")
200     }
201
202     @Test(expected = BluePrintException::class)
203     fun testMapGetAsInt() {
204         val initMap = hashMapOf("test" to 1.asJsonType())
205
206         assertEquals(1, initMap.getAsInt("test"))
207
208         initMap.getAsInt("test2")
209     }
210
211     @Test(expected = BluePrintException::class)
212     fun testCheckEquals() {
213         assertTrue(checkEquals("hello", "hello", { -> "error" }))
214
215         checkEquals("hello", "test", { -> "error" })
216     }
217
218     @Test(expected = IllegalStateException::class)
219     fun testCheckNotEmpty() {
220         assertEquals("hello", checkNotEmpty("hello", { -> "error" }))
221
222         checkNotEmpty("", { -> "error" })
223     }
224
225     @Test(expected = IllegalStateException::class)
226     fun testCheckNotBlank() {
227         assertEquals("hello", checkNotBlank("hello", { -> "error" }))
228
229         checkNotBlank("  ", { -> "error" })
230     }
231
232     @Test
233     fun testNullToEmpty() {
234         assertEquals("", nullToEmpty(null))
235
236         assertEquals("hello", nullToEmpty("hello"))
237     }
238 }