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