Revert "Renaming Files having BluePrint to have Blueprint"
[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 testByteArrayJsonType() {
72         val jsonNode = """{"Name" :"Value"}""".jsonAsJsonType()
73
74         val byteArray = jsonNode.asByteArray()
75         assertNotNull(byteArray, "failed to get ByteArray form Json")
76
77         val reverseJsonNode = byteArray.asJsonType()
78         assertNotNull(reverseJsonNode, "failed to get Json type from ByteArray")
79     }
80
81     @Test
82     fun testAsJsonType() {
83         val nullReturnValue: JsonNode = null.asJsonType()
84         assertEquals(NullNode.instance, nullReturnValue)
85
86         val returnValueString: JsonNode = "hello".asJsonType()
87         assertEquals("hello", returnValueString.textValue())
88
89         val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
90         assertEquals(returnValueString, returnValueJsonNode)
91
92         val returnValueInt: JsonNode = 1.asJsonType()
93         assertEquals(1, returnValueInt.intValue())
94
95         val returnValueBool: JsonNode = false.asJsonType()
96         assertFalse(returnValueBool.asBoolean())
97
98         val returnValue: JsonNode = BluePrintError().asJsonType()
99         assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
100     }
101
102     @Test
103     fun testMapAsObjectNode() {
104         val returnValue: ObjectNode = hashMapOf("test" to BluePrintError()).asObjectNode()
105         assertNotNull(returnValue.get("test"))
106     }
107
108     @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)
112
113         assert(returnValue is Number)
114
115         val returnValueNull = initMap.castOptionalValue("test1", Number::class)
116
117         assertNull(returnValueNull)
118
119         val returnValueString: String? = initMap.castOptionalValue("test2", String::class)
120
121         assertNull(returnValueString)
122     }
123
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)
128
129         assertNotNull(returnValue)
130
131         initMap.castValue("test1", Number::class)
132     }
133
134     @Test
135     fun testAsListOfString() {
136         val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")
137
138         val result: List<String> = arrayNode.asListOfString()
139
140         assertTrue(result.isEmpty())
141     }
142
143     @Test
144     fun testReturnNullIfMissing() {
145         val valueExist = "hello".asJsonType().returnNullIfMissing()
146         assertNotNull(valueExist)
147
148         val valueNull = NullNode.instance.returnNullIfMissing()
149         assertNull(valueNull)
150
151         val missingValue = MissingNode.getInstance().returnNullIfMissing()
152         assertNull(missingValue)
153     }
154
155     @Test
156     fun testIsNullOrMissing() {
157         assertTrue(NullNode.instance.isNullOrMissing())
158         assertTrue(MissingNode.getInstance().isNullOrMissing())
159
160         assertFalse(TextNode("").isNullOrMissing())
161         assertFalse("".asJsonType().isNullOrMissing())
162         assertFalse("hello".asJsonType().isNullOrMissing())
163         assertFalse("{\"key\": \"value\"}".asJsonType().isNullOrMissing())
164         assertFalse(TextNode("hello").isNullOrMissing())
165     }
166
167     @Test
168     fun testIsComplexType() {
169         assertFalse(NullNode.instance.isComplexType())
170         assertFalse(MissingNode.getInstance().isComplexType())
171
172         assertFalse(TextNode("").isComplexType())
173         assertFalse("".asJsonType().isComplexType())
174         assertFalse("hello".asJsonType().isComplexType())
175         assertFalse(TextNode("hello").isComplexType())
176
177         assertTrue("{\"key\": \"value\"}".asJsonType().isComplexType())
178         assertTrue("[{\"key\": \"value\"},{\"key\": \"value\"}]".asJsonType().isComplexType())
179     }
180
181     @Test(expected = BluePrintException::class)
182     fun testRootFieldsToMap() {
183         1.asJsonType().rootFieldsToMap()
184     }
185
186     @Test
187     fun testPutJsonElement() {
188         val mutMap = mutableMapOf("test" to 2.asJsonType())
189
190         mutMap.putJsonElement("hello", 3)
191
192         assertEquals(3, mutMap["hello"]?.asInt())
193     }
194
195     @Test(expected = BluePrintException::class)
196     fun testMapGetAsString() {
197         val initMap = hashMapOf("test" to "hello".asJsonType())
198
199         assertEquals("hello", initMap.getAsString("test"))
200
201         initMap.getAsString("test2")
202     }
203
204     @Test(expected = BluePrintException::class)
205     fun testMapGetAsBoolean() {
206         val initMap = hashMapOf("test" to true.asJsonType())
207
208         assertTrue(initMap.getAsBoolean("test"))
209
210         initMap.getAsBoolean("test2")
211     }
212
213     @Test(expected = BluePrintException::class)
214     fun testMapGetAsInt() {
215         val initMap = hashMapOf("test" to 1.asJsonType())
216
217         assertEquals(1, initMap.getAsInt("test"))
218
219         initMap.getAsInt("test2")
220     }
221
222     @Test(expected = BluePrintException::class)
223     fun testCheckEquals() {
224         assertTrue(checkEquals("hello", "hello", { -> "error" }))
225
226         checkEquals("hello", "test", { -> "error" })
227     }
228
229     @Test(expected = IllegalStateException::class)
230     fun testCheckNotEmpty() {
231         assertEquals("hello", checkNotEmpty("hello", { -> "error" }))
232
233         checkNotEmpty("", { -> "error" })
234     }
235
236     @Test(expected = IllegalStateException::class)
237     fun testCheckNotBlank() {
238         assertEquals("hello", checkNotBlank("hello", { -> "error" }))
239
240         checkNotBlank("  ", { -> "error" })
241     }
242
243     @Test
244     fun testNullToEmpty() {
245         assertEquals("", nullToEmpty(null))
246
247         assertEquals("hello", nullToEmpty("hello"))
248     }
249 }