2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.sli.core.slipluginutils;
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;
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;
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;
50 public void setUp() throws Exception {
51 this.ctx = new SvcLogicContext();
52 parameters = new HashMap<String, String>();
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");
62 Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
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"));
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");
81 parameters.put("index", "1");
82 parameters.put("list_pfx", "service-data.vnf-l3");
84 utils.ctxListRemove(parameters, ctx);
85 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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"));
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");
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");
109 parameters.put("list_pfx", "service-data.vnf-l3");
110 parameters.put("key", "vnf-host-name");
111 parameters.put("value", "vnf-host-name_1");
113 utils.ctxListRemove(parameters, ctx);
114 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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"));
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");
132 parameters.put("list_pfx", "service-data.vnf-l3");
133 parameters.put("value", "vnf-host-name_1");
135 utils.ctxListRemove(parameters, ctx);
136 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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]"));
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");
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");
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");
170 utils.ctxListRemove(parameters, ctx);
171 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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"));
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");
195 parameters.put("index", "1");
196 parameters.put("list_pfx", "service-data.vnf-l3");
198 utils.ctxListRemove(parameters, ctx);
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);
210 SliPluginUtils.ctxPutAll(ctx, entries);
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"));
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);
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));
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);
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);
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);
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")));
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));
270 @Test(expected = SvcLogicException.class)
271 public void testPrintContextForEmptyParameters() throws SvcLogicException {
272 SliPluginUtils.printContext(parameters, ctx);
275 @Test(expected = SvcLogicException.class)
276 public void testPrintContextForNullParameters() throws SvcLogicException {
277 SliPluginUtils.printContext(null, ctx);
281 public void testPrintContext() throws SvcLogicException {
282 parameters.put("filename", "testFileName");
283 SliPluginUtils.printContext(parameters, ctx);
287 public void testCtxKeyEmpty() {
288 ctx.setAttribute("key", "");
289 assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
293 public void testGetArrayLength() {
294 ctx.setAttribute("key_length", "test");
295 Logger log = LoggerFactory.getLogger(getClass());
296 SliPluginUtils.getArrayLength(ctx, "key", log, LogLevel.INFO, "invalid input");
300 public void testSetPropertiesForRoot() {
301 Map<String, String> parameters = new HashMap<>();
302 parameters.put("root", "RootVal");
303 parameters.put("valueRoot", "ValueRootVal");
304 assertEquals(SvcLogicConstants.SUCCESS, SliPluginUtils.setPropertiesForRoot(parameters, ctx));
308 public void testJsonStringToCtxToplevelArray() throws Exception {
309 String path = "src/test/resources/ArrayMenu.json";
310 String content = new String(Files.readAllBytes(Paths.get(path)));
311 SvcLogicContext ctx = new SvcLogicContext();
312 ctx.setAttribute("input", content);
313 Map<String, String> parameters = new HashMap<String, String>();
314 parameters.put("outputPath", "testPath");
315 parameters.put("isEscaped", "false");
316 parameters.put("source", "input");
318 SliPluginUtils.jsonStringToCtx(parameters, ctx);
320 assertEquals("1000", ctx.getAttribute("testPath.[0].calories"));
321 assertEquals("1", ctx.getAttribute("testPath.[0].id"));
322 assertEquals("plain", ctx.getAttribute("testPath.[0].name"));
323 assertEquals("pizza", ctx.getAttribute("testPath.[0].type"));
324 assertEquals("true", ctx.getAttribute("testPath.[0].vegetarian"));
325 assertEquals(SliPluginUtils.CTX_NULL_VALUE, ctx.getAttribute("testPath.[1].calories"));
326 assertEquals("2", ctx.getAttribute("testPath.[1].id"));
327 assertEquals("Tuesday Special", ctx.getAttribute("testPath.[1].name"));
328 assertEquals("1", ctx.getAttribute("testPath.[1].topping[0].id"));
329 assertEquals("onion", ctx.getAttribute("testPath.[1].topping[0].name"));
330 assertEquals("2", ctx.getAttribute("testPath.[1].topping[1].id"));
331 assertEquals("pepperoni", ctx.getAttribute("testPath.[1].topping[1].name"));
332 assertEquals("2", ctx.getAttribute("testPath.[1].topping_length"));
333 assertEquals("pizza", ctx.getAttribute("testPath.[1].type"));
334 assertEquals("false", ctx.getAttribute("testPath.[1].vegetarian"));
335 assertEquals("1500", ctx.getAttribute("testPath.[2].calories"));
336 assertEquals("3", ctx.getAttribute("testPath.[2].id"));
337 assertEquals("House Special", ctx.getAttribute("testPath.[2].name"));
338 assertEquals("3", ctx.getAttribute("testPath.[2].topping[0].id"));
339 assertEquals("basil", ctx.getAttribute("testPath.[2].topping[0].name"));
340 assertEquals("4", ctx.getAttribute("testPath.[2].topping[1].id"));
341 assertEquals("fresh mozzarella", ctx.getAttribute("testPath.[2].topping[1].name"));
342 assertEquals("5", ctx.getAttribute("testPath.[2].topping[2].id"));
343 assertEquals("tomato", ctx.getAttribute("testPath.[2].topping[2].name"));
344 assertEquals("3", ctx.getAttribute("testPath.[2].topping_length"));
345 assertEquals("pizza", ctx.getAttribute("testPath.[2].type"));
346 assertEquals("true", ctx.getAttribute("testPath.[2].vegetarian"));
347 assertEquals("3", ctx.getAttribute("testPath._length"));
351 public void testJsonStringToCtx() throws Exception {
352 String path = "src/test/resources/ObjectMenu.json";
353 String content = new String(Files.readAllBytes(Paths.get(path)));
355 SvcLogicContext ctx = new SvcLogicContext();
356 ctx.setAttribute("input", content);
358 Map<String, String> parameters = new HashMap<String, String>();
359 parameters.put("outputPath", "testPath");
360 parameters.put("isEscaped", "false");
361 parameters.put("source", "input");
363 SliPluginUtils.jsonStringToCtx(parameters, ctx);
366 assertEquals("1000", ctx.getAttribute("testPath.menu[0].calories"));
367 assertEquals("1", ctx.getAttribute("testPath.menu[0].id"));
368 assertEquals("plain", ctx.getAttribute("testPath.menu[0].name"));
369 assertEquals("pizza", ctx.getAttribute("testPath.menu[0].type"));
370 assertEquals("true", ctx.getAttribute("testPath.menu[0].vegetarian"));
371 assertEquals("2000", ctx.getAttribute("testPath.menu[1].calories"));
372 assertEquals("2", ctx.getAttribute("testPath.menu[1].id"));
373 assertEquals("Tuesday Special", ctx.getAttribute("testPath.menu[1].name"));
374 assertEquals("1", ctx.getAttribute("testPath.menu[1].topping[0].id"));
375 assertEquals("onion", ctx.getAttribute("testPath.menu[1].topping[0].name"));
376 assertEquals("2", ctx.getAttribute("testPath.menu[1].topping[1].id"));
377 assertEquals("pepperoni", ctx.getAttribute("testPath.menu[1].topping[1].name"));
378 assertEquals("2", ctx.getAttribute("testPath.menu[1].topping_length"));
379 assertEquals("pizza", ctx.getAttribute("testPath.menu[1].type"));
380 assertEquals("false", ctx.getAttribute("testPath.menu[1].vegetarian"));
381 assertEquals("1500", ctx.getAttribute("testPath.menu[2].calories"));
382 assertEquals("3", ctx.getAttribute("testPath.menu[2].id"));
383 assertEquals("House Special", ctx.getAttribute("testPath.menu[2].name"));
384 assertEquals("3", ctx.getAttribute("testPath.menu[2].topping[0].id"));
385 assertEquals("basil", ctx.getAttribute("testPath.menu[2].topping[0].name"));
386 assertEquals("4", ctx.getAttribute("testPath.menu[2].topping[1].id"));
387 assertEquals("fresh mozzarella", ctx.getAttribute("testPath.menu[2].topping[1].name"));
388 assertEquals("5", ctx.getAttribute("testPath.menu[2].topping[2].id"));
389 assertEquals("tomato", ctx.getAttribute("testPath.menu[2].topping[2].name"));
390 assertEquals("3", ctx.getAttribute("testPath.menu[2].topping_length"));
391 assertEquals("pizza", ctx.getAttribute("testPath.menu[2].type"));
392 assertEquals("true", ctx.getAttribute("testPath.menu[2].vegetarian"));
393 assertEquals("3", ctx.getAttribute("testPath.menu_length"));
397 public void test2dJsonStringToCtx() throws Exception {
398 String path = "src/test/resources/2dArray.json";
399 String content = new String(Files.readAllBytes(Paths.get(path)));
401 SvcLogicContext ctx = new SvcLogicContext();
402 ctx.setAttribute("input", content);
404 Map<String, String> parameters = new HashMap<String, String>();
405 parameters.put("outputPath", "testPath");
406 parameters.put("isEscaped", "false");
407 parameters.put("source", "input");
409 SliPluginUtils.jsonStringToCtx(parameters, ctx);
410 assertEquals("apple", ctx.getAttribute("testPath.[0][0]"));
411 assertEquals("orange", ctx.getAttribute("testPath.[0][1]"));
412 assertEquals("banana", ctx.getAttribute("testPath.[0][2]"));
413 assertEquals(SliPluginUtils.CTX_NULL_VALUE, ctx.getAttribute("testPath.[0][3]"));
414 assertEquals("4", ctx.getAttribute("testPath.[0]_length"));
415 assertEquals("squash", ctx.getAttribute("testPath.[1][0]"));
416 assertEquals("broccoli", ctx.getAttribute("testPath.[1][1]"));
417 assertEquals("cauliflower", ctx.getAttribute("testPath.[1][2]"));
418 assertEquals("3", ctx.getAttribute("testPath.[1]_length"));
419 assertEquals("2", ctx.getAttribute("testPath._length"));
423 public void test3dJsonStringToCtx() throws Exception {
424 String path = "src/test/resources/3dArray.json";
425 String content = new String(Files.readAllBytes(Paths.get(path)));
427 SvcLogicContext ctx = new SvcLogicContext();
428 ctx.setAttribute("input", content);
430 Map<String, String> parameters = new HashMap<String, String>();
431 parameters.put("outputPath", "testPath");
432 parameters.put("isEscaped", "false");
433 parameters.put("source", "input");
435 SliPluginUtils.jsonStringToCtx(parameters, ctx);
436 assertEquals("a", ctx.getAttribute("testPath.[0][0][0]"));
437 assertEquals("b", ctx.getAttribute("testPath.[0][0][1]"));
438 assertEquals("c", ctx.getAttribute("testPath.[0][0][2]"));
439 assertEquals("3", ctx.getAttribute("testPath.[0][0]_length"));
440 assertEquals("d", ctx.getAttribute("testPath.[0][1][0]"));
441 assertEquals("e", ctx.getAttribute("testPath.[0][1][1]"));
442 assertEquals("f", ctx.getAttribute("testPath.[0][1][2]"));
443 assertEquals("3", ctx.getAttribute("testPath.[0][1]_length"));
444 assertEquals("2", ctx.getAttribute("testPath.[0]_length"));
445 assertEquals("x", ctx.getAttribute("testPath.[1][0][0]"));
446 assertEquals("y", ctx.getAttribute("testPath.[1][0][1]"));
447 assertEquals("z", ctx.getAttribute("testPath.[1][0][2]"));
448 assertEquals("3", ctx.getAttribute("testPath.[1][0]_length"));
449 assertEquals("1", ctx.getAttribute("testPath.[1]_length"));
450 assertEquals("2", ctx.getAttribute("testPath._length"));
454 public void testJsonWidgetStringToCtx() throws Exception {
455 String path = "src/test/resources/Widget.json";
456 String content = new String(Files.readAllBytes(Paths.get(path)));
458 SvcLogicContext ctx = new SvcLogicContext();
459 ctx.setAttribute("input", content);
461 Map<String, String> parameters = new HashMap<String, String>();
462 parameters.put("outputPath", "testPath");
463 parameters.put("isEscaped", "false");
464 parameters.put("source", "input");
466 SliPluginUtils.jsonStringToCtx(parameters, ctx);
467 assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
468 assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
469 assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
470 assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
471 assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
472 assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
473 assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
474 assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
475 assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
476 assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
477 assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
478 assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
479 assertEquals(SliPluginUtils.CTX_NULL_VALUE, ctx.getAttribute("testPath.widget.text.vOffset"));
480 assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
481 assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
482 assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
483 assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));
487 public void testUpdateJsonObjectString() throws Exception {
488 String path = "src/test/resources/JsonObject.json";
489 String content = new String(Files.readAllBytes(Paths.get(path)));
491 SvcLogicContext ctx = new SvcLogicContext();
492 ctx.setAttribute("input", content);
494 Map<String, String> parametersUpdateJson = new HashMap<String, String>();
495 parametersUpdateJson.put("source", "input");
496 parametersUpdateJson.put("outputPath", "newJsonString");
498 // add key "ccc" and its value
499 parametersUpdateJson.put("add.ccc", "abcxyz");
501 // update keys and their values of "aaa" and "c.d"
502 parametersUpdateJson.put("add.aaa", "4567");
503 parametersUpdateJson.put("add.c.d", "defg");
506 parametersUpdateJson.put("delete.bbb", "");
508 SliPluginUtils.updateJsonObjectString(parametersUpdateJson, ctx);
510 Map<String, String> parametersJsonToCtx = new HashMap<String, String>();
511 parametersJsonToCtx.put("source", "newJsonString");
512 parametersJsonToCtx.put("outputPath", "testPath");
513 parametersJsonToCtx.put("isEscaped", "false");
515 SliPluginUtils.jsonStringToCtx(parametersJsonToCtx, ctx);
517 assertEquals("abcxyz", ctx.getAttribute("testPath.ccc"));
518 assertEquals("4567", ctx.getAttribute("testPath.aaa"));
519 assertEquals("defg", ctx.getAttribute("testPath.c.d"));
520 assertEquals(null, ctx.getAttribute("testPath.bbb"));
524 public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception {
525 String path = "src/test/resources/EmbeddedEscapedJson.json";
526 String content = new String(Files.readAllBytes(Paths.get(path)));
528 SvcLogicContext ctx = new SvcLogicContext();
529 ctx.setAttribute("input", content);
531 Map<String, String> parameters = new HashMap<String, String>();
532 parameters.put("outputPath", "testPath");
533 parameters.put("isEscaped", "false");
534 parameters.put("source", "input");
536 SliPluginUtils.jsonStringToCtx(parameters, ctx);
538 assertEquals("escapedJsonObject", ctx.getAttribute("testPath.input.parameters[0].name"));
539 assertEquals("[{\"id\":\"0.2.0.0/16\"},{\"id\":\"ge04::/64\"}]",
540 ctx.getAttribute("testPath.input.parameters[0].value"));
541 assertEquals("Hello/World", ctx.getAttribute("testPath.input.parameters[1].value"));
542 assertEquals("resourceName", ctx.getAttribute("testPath.input.parameters[2].name"));
543 assertEquals("The\t\"Best\"\tName", ctx.getAttribute("testPath.input.parameters[2].value"));
544 assertEquals("3", ctx.getAttribute("testPath.input.parameters_length"));
547 // Break the embedded json object into properties
548 parameters.put("outputPath", "testPath.input.parameters[0].value");
549 parameters.put("source", "testPath.input.parameters[0].value");
550 SliPluginUtils.jsonStringToCtx(parameters, ctx);
552 assertEquals("0.2.0.0/16", ctx.getAttribute("testPath.input.parameters[0].value.[0].id"));
553 assertEquals("ge04::/64", ctx.getAttribute("testPath.input.parameters[0].value.[1].id"));
554 assertEquals("2", ctx.getAttribute("testPath.input.parameters[0].value._length"));
558 public void testEscapedJsonStringToCtx() throws Exception {
559 String path = "src/test/resources/EscapedJson.json";
560 String content = new String(Files.readAllBytes(Paths.get(path)));
562 SvcLogicContext ctx = new SvcLogicContext();
563 ctx.setAttribute("input", content);
565 Map<String, String> parameters = new HashMap<String, String>();
566 parameters.put("outputPath", "testPath");
567 parameters.put("isEscaped", "true"); // set to true because the entire json content has been escaped
568 parameters.put("source", "input");
571 SliPluginUtils.jsonStringToCtx(parameters, ctx);
572 assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
573 assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
574 assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
575 assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
576 assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
577 assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
578 assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
579 assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
580 assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
581 assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
582 assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
583 assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
584 assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
585 assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
586 assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
587 assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
588 assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));