summaryrefslogtreecommitdiffstats
path: root/ms/controllerblueprints/modules/blueprint-core/src/test/kotlin/org/onap/ccsdk/cds/controllerblueprints/core/CustomFunctionsTest.kt
blob: 487b1d15bc9f25fa28c72256dc11984251796f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * Copyright © 2017-2018 AT&T Intellectual Property.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.onap.ccsdk.cds.controllerblueprints.core

import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.node.*
import org.junit.Test
import kotlin.test.*

/**
 *
 *
 * @author Brinda Santh
 */
class CustomFunctionsTest {
    @Test
    fun testFormat() {
        val returnValue : String = format("This is {} for times {}", "test", 2)
        assertEquals("This is test for times 2", returnValue, "Failed to format String")

        val returnValue1 : String = format("This is test for times 2")
        assertEquals("This is test for times 2", returnValue1, "Failed to format empty args")
    }

    @Test
    fun testStringAsJsonPrimitive() {
        val returnValue: TextNode = "hello".asJsonPrimitive()
        assertEquals("hello", returnValue.textValue())
    }

    @Test
    fun testIntAsJsonPrimitive() {
        val returnValue: IntNode = 1.asJsonPrimitive()
        assertEquals(1, returnValue.intValue())
    }

    @Test
    fun testBooleanAsJsonPrimitive() {
        val returnValue: BooleanNode = false.asJsonPrimitive()
        assertFalse(returnValue.asBoolean())
    }

    @Test
    fun testAsJsonType() {
        val nullReturnValue: JsonNode = null.asJsonType()
        assertEquals(NullNode.instance, nullReturnValue)

        val returnValueString: JsonNode = "hello".asJsonType()
        assertEquals("hello", returnValueString.textValue())

        val returnValueJsonNode: JsonNode = returnValueString.asJsonType()
        assertEquals(returnValueString, returnValueJsonNode)

        val returnValueInt: JsonNode = 1.asJsonType()
        assertEquals(1, returnValueInt.intValue())

        val returnValueBool: JsonNode = false.asJsonType()
        assertFalse(returnValueBool.asBoolean())

        val returnValue: JsonNode = BluePrintError().asJsonType()
        assertEquals(JsonNodeType.OBJECT, returnValue.getNodeType())
    }

    @Test
    fun testMapAsObjectNode() {
        val returnValue: ObjectNode = hashMapOf("test" to BluePrintError()).asObjectNode()
        assertNotNull(returnValue.get("test"))
    }

    @Test
    fun testCastOptionalValue() {
        val initMap: Map<String, *> = hashMapOf("test" to 1.1, "test2" to null)
        val returnValue = initMap.castOptionalValue("test", Number::class)

        assert(returnValue is Number)

        val returnValueNull = initMap.castOptionalValue("test1", Number::class)

        assertNull(returnValueNull)

        val returnValueString: String? = initMap.castOptionalValue("test2", String::class)

        assertNull(returnValueString)
    }

    @Test(expected = BluePrintException::class)
    fun testCastValue() {
        val initMap: Map<String, Double> = hashMapOf("test" to 1.1)
        val returnValue = initMap.castValue("test", Number::class)

        assertNotNull(returnValue)

        initMap.castValue("test1", Number::class)
    }

    @Test
    fun testAsListOfString() {
        val arrayNode: ArrayNode = ObjectMapper().createObjectNode().putArray("array")

        val result: List<String> = arrayNode.asListOfString()

        assertTrue(result.isEmpty())
    }

    @Test
    fun testReturnNullIfMissing() {
        val valueExist = "hello".asJsonType().returnNullIfMissing()
        assertNotNull(valueExist)

        val valueNull = NullNode.instance.returnNullIfMissing()
        assertNull(valueNull)

        val missingValue = MissingNode.getInstance().returnNullIfMissing()
        assertNull(missingValue)
    }

    @Test(expected = BluePrintException::class)
    fun testRootFieldsToMap() {
        1.asJsonType().rootFieldsToMap()
    }

    @Test
    fun testPutJsonElement() {
        val mutMap = mutableMapOf("test" to 2.asJsonType())

        mutMap.putJsonElement("hello", 3)

        assertEquals(3, mutMap["hello"]?.asInt())
    }

    @Test(expected = BluePrintException::class)
    fun testMapGetAsString() {
        val initMap = hashMapOf("test" to "hello".asJsonType())

        assertEquals("hello", initMap.getAsString("test"))

        initMap.getAsString("test2")
    }

    @Test(expected = BluePrintException::class)
    fun testMapGetAsBoolean() {
        val initMap = hashMapOf("test" to true.asJsonType())

        assertTrue(initMap.getAsBoolean("test"))

        initMap.getAsBoolean("test2")
    }

    @Test(expected = BluePrintException::class)
    fun testMapGetAsInt() {
        val initMap = hashMapOf("test" to 1.asJsonType())

        assertEquals(1, initMap.getAsInt("test"))

        initMap.getAsInt("test2")
    }

    @Test(expected = BluePrintException::class)
    fun testCheckEquals() {
        assertTrue(checkEquals("hello", "hello", { -> "error"}))

        checkEquals("hello", "test", { -> "error"})
    }

    @Test(expected = IllegalStateException::class)
    fun testCheckNotEmpty() {
        assertEquals("hello", checkNotEmpty("hello", { -> "error"}))

        checkNotEmpty("", { -> "error"})
    }

    @Test(expected = IllegalStateException::class)
    fun testCheckNotBlank() {
        assertEquals("hello", checkNotBlank("hello", { -> "error"}))

        checkNotBlank("  ", { -> "error"})
    }

    @Test
    fun testNullToEmpty() {
        assertEquals("", nullToEmpty(null))

        assertEquals("hello", nullToEmpty("hello"))
    }
}