enhance SliPluginUtils
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / onap / ccsdk / sli / core / slipluginutils / SliPluginUtils_StaticFunctionsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.slipluginutils;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.HashMap;
32 import java.util.Map;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
37 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils.LogLevel;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import com.google.gson.JsonObject;
41
42 public class SliPluginUtils_StaticFunctionsTest {
43     private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class);
44     SliPluginUtils utils = new SliPluginUtils();
45     private SvcLogicContext ctx;
46     private HashMap<String, String> parameters;
47
48     @Before
49     public void setUp() throws Exception {
50         this.ctx = new SvcLogicContext();
51         parameters = new HashMap<String, String>();
52     }
53
54     // TODO: javadoc
55     @Test
56     public final void testCtxGetBeginsWith() {
57         ctx.setAttribute("service-data.oper-status.order-status", "InProgress");
58         ctx.setAttribute("service-data.service-information.service-instance-id", "my-instance");
59         ctx.setAttribute("service-data.service-information.service-type", "my-service");
60
61         Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
62
63         assertEquals("my-instance", entries.get("service-data.service-information.service-instance-id"));
64         assertEquals("my-service", entries.get("service-data.service-information.service-type"));
65         assertFalse(entries.containsKey("service-data.oper-status.order-status"));
66     }
67
68     // TODO: javadoc
69     @Test
70     public final void testCtxListRemove_index() throws SvcLogicException {
71         LOG.trace("=== testCtxListRemove_index ===");
72         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
73         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
74         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
75         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
76         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
77         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
78         ctx.setAttribute("service-data.vnf-l3_length", "3");
79
80         parameters.put("index", "1");
81         parameters.put("list_pfx", "service-data.vnf-l3");
82
83         utils.ctxListRemove(parameters, ctx);
84         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
85
86         assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
87         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
88         assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
89         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
90         assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
91     }
92
93     // TODO: javadoc
94     @Test
95     public final void textCtxListRemove_keyValue() throws SvcLogicException {
96         LOG.trace("=== textCtxListRemove_keyValue ===");
97         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
98         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
99         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
100         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
101         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
102         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
103         // 2nd entry
104         ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
105         ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
106         ctx.setAttribute("service-data.vnf-l3_length", "4");
107
108         parameters.put("list_pfx", "service-data.vnf-l3");
109         parameters.put("key", "vnf-host-name");
110         parameters.put("value", "vnf-host-name_1");
111
112         utils.ctxListRemove(parameters, ctx);
113         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
114
115         assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
116         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
117         assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
118         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
119         assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
120     }
121
122     // TODO: javadoc
123     @Test
124     public final void textCtxListRemove_keyValue_nullkey() throws SvcLogicException {
125         LOG.trace("=== textCtxListRemove_keyValue_nullkey ===");
126         ctx.setAttribute("service-data.vnf-l3[0]", "vnf-host-name_0");
127         ctx.setAttribute("service-data.vnf-l3[1]", "vnf-host-name_1");
128         ctx.setAttribute("service-data.vnf-l3[2]", "vnf-host-name_2");
129         ctx.setAttribute("service-data.vnf-l3_length", "3");
130
131         parameters.put("list_pfx", "service-data.vnf-l3");
132         parameters.put("value", "vnf-host-name_1");
133
134         utils.ctxListRemove(parameters, ctx);
135         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
136
137         assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
138         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0]"));
139         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1]"));
140     }
141
142     // TODO: javadoc
143     @Test
144     public final void textCtxListRemove_keyValueList() throws SvcLogicException {
145         LOG.trace("=== textCtxListRemove_keyValueList ===");
146         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
147         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
148         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
149         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
150         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
151         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
152         // 2nd entry
153         ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
154         ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
155         // entries with only 1 of 2 key-value pairs matching
156         ctx.setAttribute("service-data.vnf-l3[4].vnf-host-name", "vnf-host-name_1");
157         ctx.setAttribute("service-data.vnf-l3[4].device-host-name", "device-host-name_4");
158         ctx.setAttribute("service-data.vnf-l3[5].vnf-host-name", "vnf-host-name_5");
159         ctx.setAttribute("service-data.vnf-l3[5].device-host-name", "device-host-name_1");
160         ctx.setAttribute("service-data.vnf-l3_length", "6");
161
162         parameters.put("list_pfx", "service-data.vnf-l3");
163         parameters.put("keys_length", "2");
164         parameters.put("keys[0].key", "vnf-host-name");
165         parameters.put("keys[0].value", "vnf-host-name_1");
166         parameters.put("keys[1].key", "device-host-name");
167         parameters.put("keys[1].value", "device-host-name_1");
168
169         utils.ctxListRemove(parameters, ctx);
170         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
171
172         assertEquals("4", ctx.getAttribute("service-data.vnf-l3_length"));
173         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
174         assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
175         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
176         assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
177         assertEquals("vnf-host-name_1", ctx.getAttribute("service-data.vnf-l3[2].vnf-host-name"));
178         assertEquals("device-host-name_4", ctx.getAttribute("service-data.vnf-l3[2].device-host-name"));
179         assertEquals("vnf-host-name_5", ctx.getAttribute("service-data.vnf-l3[3].vnf-host-name"));
180         assertEquals("device-host-name_1", ctx.getAttribute("service-data.vnf-l3[3].device-host-name"));
181     }
182
183     // TODO: javadoc
184     @Test(expected = SvcLogicException.class)
185     public final void testCtxListRemove_nullListLength() throws SvcLogicException {
186         LOG.trace("=== testCtxListRemove_nullListLength ===");
187         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
188         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
189         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
190         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
191         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
192         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
193
194         parameters.put("index", "1");
195         parameters.put("list_pfx", "service-data.vnf-l3");
196
197         utils.ctxListRemove(parameters, ctx);
198     }
199
200     // TODO: javadoc
201     @Test
202     public final void testCtxPutAll() {
203         HashMap<String, Object> entries = new HashMap<String, Object>();
204         entries.put("service-data.oper-status.order-status", "InProgress");
205         entries.put("service-data.service-information.service-instance-id", "my-instance");
206         entries.put("service-data.request-information.order-number", 1234);
207         entries.put("service-data.request-information.request-id", null);
208
209         SliPluginUtils.ctxPutAll(ctx, entries);
210
211         assertEquals("InProgress", ctx.getAttribute("service-data.oper-status.order-status"));
212         assertEquals("my-instance", ctx.getAttribute("service-data.service-information.service-instance-id"));
213         assertEquals("1234", ctx.getAttribute("service-data.request-information.order-number"));
214         assertFalse(ctx.getAttributeKeySet().contains("service-data.request-information.request-id"));
215     }
216
217     // TODO: javadoc
218     @Test
219     public final void testCtxSetAttribute_LOG() {
220         LOG.debug("=== testCtxSetAttribute_LOG ===");
221         Integer i = new Integer(3);
222         SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
223     }
224
225     @Test
226     public void setTime() throws SvcLogicException {
227         String outputPath = "output";
228         parameters.put("outputPath", outputPath);
229         SliPluginUtils.setTime(parameters, ctx);
230         assertNotNull(ctx.getAttribute(outputPath));
231     }
232
233     @Test
234     public void containsKey() throws Exception {
235         ctx = new SvcLogicContext();
236         parameters.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
237         String result = SliPluginUtils.containsKey(parameters, ctx);
238         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
239
240         ctx.setAttribute("a", null);
241         parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
242         result = SliPluginUtils.containsKey(parameters, ctx);
243         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
244
245         ctx.setAttribute("a", "hellworld");
246         parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
247         result = SliPluginUtils.containsKey(parameters, ctx);
248         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
249     }
250
251     @Test
252     public void testGetAttributeValue() throws Exception {
253         parameters.put("outputPath", "testPath");
254         parameters.put("source", "testSource");
255         SliPluginUtils.getAttributeValue(parameters, ctx);
256         assertNull(ctx.getAttribute(parameters.get("outputPath")));
257     }
258
259     @Test
260     public void testCtxListContains() throws Exception {
261         parameters.put("list", "10_length");
262         parameters.put("keyName", "testName");
263         parameters.put("keyValue", "testValue");
264         ctx.setAttribute("10_length", "10");
265         assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx));
266
267     }
268
269     @Test(expected = SvcLogicException.class)
270     public void testPrintContextForEmptyParameters() throws SvcLogicException {
271         SliPluginUtils.printContext(parameters, ctx);
272     }
273
274     @Test(expected = SvcLogicException.class)
275     public void testPrintContextForNullParameters() throws SvcLogicException {
276         SliPluginUtils.printContext(null, ctx);
277     }
278
279     @Test
280     public void testPrintContext() throws SvcLogicException {
281         parameters.put("filename", "testFileName");
282         SliPluginUtils.printContext(parameters, ctx);
283     }
284
285     @Test
286     public void testWriteJsonObject() throws SvcLogicException {
287         JsonObject obj = new JsonObject();
288         obj.addProperty("name", "testName");
289         obj.addProperty("age", 27);
290         obj.addProperty("salary", 600000);
291         SvcLogicContext ctx = new SvcLogicContext();
292         SliPluginUtils.writeJsonObject(obj, ctx, "root");
293         assertEquals("testName", ctx.getAttribute("root.name"));
294         assertEquals("27", ctx.getAttribute("root.age"));
295         assertEquals("600000", ctx.getAttribute("root.salary"));
296     }
297
298     @Test
299     public void testCtxKeyEmpty() {
300         ctx.setAttribute("key", "");
301         assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
302     }
303
304     @Test
305     public void testGetArrayLength() {
306         ctx.setAttribute("key_length", "test");
307         Logger log = LoggerFactory.getLogger(getClass());
308         SliPluginUtils.getArrayLength(ctx, "key", log, LogLevel.INFO, "invalid input");
309     }
310
311     @Test
312     public void testSetPropertiesForRoot() {
313         Map<String, String> parameters = new HashMap<>();
314         parameters.put("root", "RootVal");
315         parameters.put("valueRoot", "ValueRootVal");
316         assertEquals("success", SliPluginUtils.setPropertiesForRoot(parameters, ctx));
317     }
318
319     @Test
320     public void testJsonStringToCtxToplevelArray() throws Exception {
321         String path = "src/test/resources/ArrayMenu.json";
322         String content = new String(Files.readAllBytes(Paths.get(path)));
323         SvcLogicContext ctx = new SvcLogicContext();
324         ctx.setAttribute("input", content);
325         Map<String, String> parameters = new HashMap<String, String>();
326         parameters.put("outputPath", "testPath");
327         parameters.put("isEscaped", "false");
328         parameters.put("source", "input");
329
330         SliPluginUtils.jsonStringToCtx(parameters, ctx);
331
332         assertEquals("1000", ctx.getAttribute("testPath.[0].calories"));
333         assertEquals("1", ctx.getAttribute("testPath.[0].id"));
334         assertEquals("plain", ctx.getAttribute("testPath.[0].name"));
335         assertEquals("pizza", ctx.getAttribute("testPath.[0].type"));
336         assertEquals("true", ctx.getAttribute("testPath.[0].vegetarian"));
337         assertEquals("2000", ctx.getAttribute("testPath.[1].calories"));
338         assertEquals("2", ctx.getAttribute("testPath.[1].id"));
339         assertEquals("Tuesday Special", ctx.getAttribute("testPath.[1].name"));
340         assertEquals("1", ctx.getAttribute("testPath.[1].topping[0].id"));
341         assertEquals("onion", ctx.getAttribute("testPath.[1].topping[0].name"));
342         assertEquals("2", ctx.getAttribute("testPath.[1].topping[1].id"));
343         assertEquals("pepperoni", ctx.getAttribute("testPath.[1].topping[1].name"));
344         assertEquals("2", ctx.getAttribute("testPath.[1].topping_length"));
345         assertEquals("pizza", ctx.getAttribute("testPath.[1].type"));
346         assertEquals("false", ctx.getAttribute("testPath.[1].vegetarian"));
347         assertEquals("1500", ctx.getAttribute("testPath.[2].calories"));
348         assertEquals("3", ctx.getAttribute("testPath.[2].id"));
349         assertEquals("House Special", ctx.getAttribute("testPath.[2].name"));
350         assertEquals("3", ctx.getAttribute("testPath.[2].topping[0].id"));
351         assertEquals("basil", ctx.getAttribute("testPath.[2].topping[0].name"));
352         assertEquals("4", ctx.getAttribute("testPath.[2].topping[1].id"));
353         assertEquals("fresh mozzarella", ctx.getAttribute("testPath.[2].topping[1].name"));
354         assertEquals("5", ctx.getAttribute("testPath.[2].topping[2].id"));
355         assertEquals("tomato", ctx.getAttribute("testPath.[2].topping[2].name"));
356         assertEquals("3", ctx.getAttribute("testPath.[2].topping_length"));
357         assertEquals("pizza", ctx.getAttribute("testPath.[2].type"));
358         assertEquals("true", ctx.getAttribute("testPath.[2].vegetarian"));
359         assertEquals("3", ctx.getAttribute("testPath._length"));
360     }
361
362     @Test
363     public void testJsonStringToCtx() throws Exception {
364         String path = "src/test/resources/ObjectMenu.json";
365         String content = new String(Files.readAllBytes(Paths.get(path)));
366
367         SvcLogicContext ctx = new SvcLogicContext();
368         ctx.setAttribute("input", content);
369
370         Map<String, String> parameters = new HashMap<String, String>();
371         parameters.put("outputPath", "testPath");
372         parameters.put("isEscaped", "false");
373         parameters.put("source", "input");
374
375         SliPluginUtils.jsonStringToCtx(parameters, ctx);
376
377
378         assertEquals("1000", ctx.getAttribute("testPath.menu[0].calories"));
379         assertEquals("1", ctx.getAttribute("testPath.menu[0].id"));
380         assertEquals("plain", ctx.getAttribute("testPath.menu[0].name"));
381         assertEquals("pizza", ctx.getAttribute("testPath.menu[0].type"));
382         assertEquals("true", ctx.getAttribute("testPath.menu[0].vegetarian"));
383         assertEquals("2000", ctx.getAttribute("testPath.menu[1].calories"));
384         assertEquals("2", ctx.getAttribute("testPath.menu[1].id"));
385         assertEquals("Tuesday Special", ctx.getAttribute("testPath.menu[1].name"));
386         assertEquals("1", ctx.getAttribute("testPath.menu[1].topping[0].id"));
387         assertEquals("onion", ctx.getAttribute("testPath.menu[1].topping[0].name"));
388         assertEquals("2", ctx.getAttribute("testPath.menu[1].topping[1].id"));
389         assertEquals("pepperoni", ctx.getAttribute("testPath.menu[1].topping[1].name"));
390         assertEquals("2", ctx.getAttribute("testPath.menu[1].topping_length"));
391         assertEquals("pizza", ctx.getAttribute("testPath.menu[1].type"));
392         assertEquals("false", ctx.getAttribute("testPath.menu[1].vegetarian"));
393         assertEquals("1500", ctx.getAttribute("testPath.menu[2].calories"));
394         assertEquals("3", ctx.getAttribute("testPath.menu[2].id"));
395         assertEquals("House Special", ctx.getAttribute("testPath.menu[2].name"));
396         assertEquals("3", ctx.getAttribute("testPath.menu[2].topping[0].id"));
397         assertEquals("basil", ctx.getAttribute("testPath.menu[2].topping[0].name"));
398         assertEquals("4", ctx.getAttribute("testPath.menu[2].topping[1].id"));
399         assertEquals("fresh mozzarella", ctx.getAttribute("testPath.menu[2].topping[1].name"));
400         assertEquals("5", ctx.getAttribute("testPath.menu[2].topping[2].id"));
401         assertEquals("tomato", ctx.getAttribute("testPath.menu[2].topping[2].name"));
402         assertEquals("3", ctx.getAttribute("testPath.menu[2].topping_length"));
403         assertEquals("pizza", ctx.getAttribute("testPath.menu[2].type"));
404         assertEquals("true", ctx.getAttribute("testPath.menu[2].vegetarian"));
405         assertEquals("3", ctx.getAttribute("testPath.menu_length"));
406     }
407
408     @Test
409     public void test2dJsonStringToCtx() throws Exception {
410         String path = "src/test/resources/2dArray.json";
411         String content = new String(Files.readAllBytes(Paths.get(path)));
412
413         SvcLogicContext ctx = new SvcLogicContext();
414         ctx.setAttribute("input", content);
415
416         Map<String, String> parameters = new HashMap<String, String>();
417         parameters.put("outputPath", "testPath");
418         parameters.put("isEscaped", "false");
419         parameters.put("source", "input");
420
421         SliPluginUtils.jsonStringToCtx(parameters, ctx);
422         assertEquals("apple", ctx.getAttribute("testPath.[0][0]"));
423         assertEquals("orange", ctx.getAttribute("testPath.[0][1]"));
424         assertEquals("banana", ctx.getAttribute("testPath.[0][2]"));
425         assertEquals("3", ctx.getAttribute("testPath.[0]_length"));
426         assertEquals("squash", ctx.getAttribute("testPath.[1][0]"));
427         assertEquals("broccoli", ctx.getAttribute("testPath.[1][1]"));
428         assertEquals("cauliflower", ctx.getAttribute("testPath.[1][2]"));
429         assertEquals("3", ctx.getAttribute("testPath.[1]_length"));
430         assertEquals("2", ctx.getAttribute("testPath._length"));
431     }
432
433     @Test
434     public void test3dJsonStringToCtx() throws Exception {
435         String path = "src/test/resources/3dArray.json";
436         String content = new String(Files.readAllBytes(Paths.get(path)));
437
438         SvcLogicContext ctx = new SvcLogicContext();
439         ctx.setAttribute("input", content);
440
441         Map<String, String> parameters = new HashMap<String, String>();
442         parameters.put("outputPath", "testPath");
443         parameters.put("isEscaped", "false");
444         parameters.put("source", "input");
445
446         SliPluginUtils.jsonStringToCtx(parameters, ctx);
447         assertEquals("a", ctx.getAttribute("testPath.[0][0][0]"));
448         assertEquals("b", ctx.getAttribute("testPath.[0][0][1]"));
449         assertEquals("c", ctx.getAttribute("testPath.[0][0][2]"));
450         assertEquals("3", ctx.getAttribute("testPath.[0][0]_length"));
451         assertEquals("d", ctx.getAttribute("testPath.[0][1][0]"));
452         assertEquals("e", ctx.getAttribute("testPath.[0][1][1]"));
453         assertEquals("f", ctx.getAttribute("testPath.[0][1][2]"));
454         assertEquals("3", ctx.getAttribute("testPath.[0][1]_length"));
455         assertEquals("2", ctx.getAttribute("testPath.[0]_length"));
456         assertEquals("x", ctx.getAttribute("testPath.[1][0][0]"));
457         assertEquals("y", ctx.getAttribute("testPath.[1][0][1]"));
458         assertEquals("z", ctx.getAttribute("testPath.[1][0][2]"));
459         assertEquals("3", ctx.getAttribute("testPath.[1][0]_length"));
460         assertEquals("1", ctx.getAttribute("testPath.[1]_length"));
461         assertEquals("2", ctx.getAttribute("testPath._length"));
462     }
463
464     @Test
465     public void testJsonWidgetStringToCtx() throws Exception {
466         String path = "src/test/resources/Widget.json";
467         String content = new String(Files.readAllBytes(Paths.get(path)));
468
469         SvcLogicContext ctx = new SvcLogicContext();
470         ctx.setAttribute("input", content);
471
472         Map<String, String> parameters = new HashMap<String, String>();
473         parameters.put("outputPath", "testPath");
474         parameters.put("isEscaped", "false");
475         parameters.put("source", "input");
476
477         SliPluginUtils.jsonStringToCtx(parameters, ctx);
478         assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
479         assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
480         assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
481         assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
482         assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
483         assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
484         assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
485         assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
486         assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
487         assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
488         assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
489         assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
490         assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
491         assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
492         assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
493         assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
494         assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));
495     }
496
497     @Test
498     public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception {
499         String path = "src/test/resources/EmbeddedEscapedJson.json";
500         String content = new String(Files.readAllBytes(Paths.get(path)));
501
502         SvcLogicContext ctx = new SvcLogicContext();
503         ctx.setAttribute("input", content);
504
505         Map<String, String> parameters = new HashMap<String, String>();
506         parameters.put("outputPath", "testPath");
507         parameters.put("isEscaped", "false");
508         parameters.put("source", "input");
509
510         SliPluginUtils.jsonStringToCtx(parameters, ctx);
511
512         assertEquals("escapedJsonObject", ctx.getAttribute("testPath.input.parameters[0].name"));
513         assertEquals("[{\"id\":\"0.2.0.0/16\"},{\"id\":\"ge04::/64\"}]",
514                 ctx.getAttribute("testPath.input.parameters[0].value"));
515         assertEquals("Hello/World", ctx.getAttribute("testPath.input.parameters[1].value"));
516         assertEquals("resourceName", ctx.getAttribute("testPath.input.parameters[2].name"));
517         assertEquals("The\t\"Best\"\tName", ctx.getAttribute("testPath.input.parameters[2].value"));
518         assertEquals("3", ctx.getAttribute("testPath.input.parameters_length"));
519
520
521         // Break the embedded json object into properties
522         parameters.put("outputPath", "testPath.input.parameters[0].value");
523         parameters.put("source", "testPath.input.parameters[0].value");
524         SliPluginUtils.jsonStringToCtx(parameters, ctx);
525
526         assertEquals("0.2.0.0/16", ctx.getAttribute("testPath.input.parameters[0].value.[0].id"));
527         assertEquals("ge04::/64", ctx.getAttribute("testPath.input.parameters[0].value.[1].id"));
528         assertEquals("2", ctx.getAttribute("testPath.input.parameters[0].value._length"));
529     }
530
531     @Test
532     public void testEscapedJsonStringToCtx() throws Exception {
533         String path = "src/test/resources/EscapedJson.json";
534         String content = new String(Files.readAllBytes(Paths.get(path)));
535
536         SvcLogicContext ctx = new SvcLogicContext();
537         ctx.setAttribute("input", content);
538
539         Map<String, String> parameters = new HashMap<String, String>();
540         parameters.put("outputPath", "testPath");
541         parameters.put("isEscaped", "true"); // set to true because the entire json content has been escaped
542         parameters.put("source", "input");
543
544
545         SliPluginUtils.jsonStringToCtx(parameters, ctx);
546         assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
547         assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
548         assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
549         assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
550         assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
551         assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
552         assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
553         assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
554         assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
555         assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
556         assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
557         assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
558         assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
559         assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
560         assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
561         assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
562         assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));
563     }
564
565 }