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 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"));
300 public void testCtxKeyEmpty() {
301 ctx.setAttribute("key", "");
302 assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
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");
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));
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");
331 SliPluginUtils.jsonStringToCtx(parameters, ctx);
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("2000", 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"));
364 public void testJsonStringToCtx() throws Exception {
365 String path = "src/test/resources/ObjectMenu.json";
366 String content = new String(Files.readAllBytes(Paths.get(path)));
368 SvcLogicContext ctx = new SvcLogicContext();
369 ctx.setAttribute("input", content);
371 Map<String, String> parameters = new HashMap<String, String>();
372 parameters.put("outputPath", "testPath");
373 parameters.put("isEscaped", "false");
374 parameters.put("source", "input");
376 SliPluginUtils.jsonStringToCtx(parameters, ctx);
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"));
410 public void test2dJsonStringToCtx() throws Exception {
411 String path = "src/test/resources/2dArray.json";
412 String content = new String(Files.readAllBytes(Paths.get(path)));
414 SvcLogicContext ctx = new SvcLogicContext();
415 ctx.setAttribute("input", content);
417 Map<String, String> parameters = new HashMap<String, String>();
418 parameters.put("outputPath", "testPath");
419 parameters.put("isEscaped", "false");
420 parameters.put("source", "input");
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("3", ctx.getAttribute("testPath.[0]_length"));
427 assertEquals("squash", ctx.getAttribute("testPath.[1][0]"));
428 assertEquals("broccoli", ctx.getAttribute("testPath.[1][1]"));
429 assertEquals("cauliflower", ctx.getAttribute("testPath.[1][2]"));
430 assertEquals("3", ctx.getAttribute("testPath.[1]_length"));
431 assertEquals("2", ctx.getAttribute("testPath._length"));
435 public void test3dJsonStringToCtx() throws Exception {
436 String path = "src/test/resources/3dArray.json";
437 String content = new String(Files.readAllBytes(Paths.get(path)));
439 SvcLogicContext ctx = new SvcLogicContext();
440 ctx.setAttribute("input", content);
442 Map<String, String> parameters = new HashMap<String, String>();
443 parameters.put("outputPath", "testPath");
444 parameters.put("isEscaped", "false");
445 parameters.put("source", "input");
447 SliPluginUtils.jsonStringToCtx(parameters, ctx);
448 assertEquals("a", ctx.getAttribute("testPath.[0][0][0]"));
449 assertEquals("b", ctx.getAttribute("testPath.[0][0][1]"));
450 assertEquals("c", ctx.getAttribute("testPath.[0][0][2]"));
451 assertEquals("3", ctx.getAttribute("testPath.[0][0]_length"));
452 assertEquals("d", ctx.getAttribute("testPath.[0][1][0]"));
453 assertEquals("e", ctx.getAttribute("testPath.[0][1][1]"));
454 assertEquals("f", ctx.getAttribute("testPath.[0][1][2]"));
455 assertEquals("3", ctx.getAttribute("testPath.[0][1]_length"));
456 assertEquals("2", ctx.getAttribute("testPath.[0]_length"));
457 assertEquals("x", ctx.getAttribute("testPath.[1][0][0]"));
458 assertEquals("y", ctx.getAttribute("testPath.[1][0][1]"));
459 assertEquals("z", ctx.getAttribute("testPath.[1][0][2]"));
460 assertEquals("3", ctx.getAttribute("testPath.[1][0]_length"));
461 assertEquals("1", ctx.getAttribute("testPath.[1]_length"));
462 assertEquals("2", ctx.getAttribute("testPath._length"));
466 public void testJsonWidgetStringToCtx() throws Exception {
467 String path = "src/test/resources/Widget.json";
468 String content = new String(Files.readAllBytes(Paths.get(path)));
470 SvcLogicContext ctx = new SvcLogicContext();
471 ctx.setAttribute("input", content);
473 Map<String, String> parameters = new HashMap<String, String>();
474 parameters.put("outputPath", "testPath");
475 parameters.put("isEscaped", "false");
476 parameters.put("source", "input");
478 SliPluginUtils.jsonStringToCtx(parameters, ctx);
479 assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
480 assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
481 assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
482 assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
483 assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
484 assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
485 assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
486 assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
487 assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
488 assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
489 assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
490 assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
491 assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
492 assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
493 assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
494 assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
495 assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));
499 public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception {
500 String path = "src/test/resources/EmbeddedEscapedJson.json";
501 String content = new String(Files.readAllBytes(Paths.get(path)));
503 SvcLogicContext ctx = new SvcLogicContext();
504 ctx.setAttribute("input", content);
506 Map<String, String> parameters = new HashMap<String, String>();
507 parameters.put("outputPath", "testPath");
508 parameters.put("isEscaped", "false");
509 parameters.put("source", "input");
511 SliPluginUtils.jsonStringToCtx(parameters, ctx);
513 assertEquals("escapedJsonObject", ctx.getAttribute("testPath.input.parameters[0].name"));
514 assertEquals("[{\"id\":\"0.2.0.0/16\"},{\"id\":\"ge04::/64\"}]",
515 ctx.getAttribute("testPath.input.parameters[0].value"));
516 assertEquals("Hello/World", ctx.getAttribute("testPath.input.parameters[1].value"));
517 assertEquals("resourceName", ctx.getAttribute("testPath.input.parameters[2].name"));
518 assertEquals("The\t\"Best\"\tName", ctx.getAttribute("testPath.input.parameters[2].value"));
519 assertEquals("3", ctx.getAttribute("testPath.input.parameters_length"));
522 // Break the embedded json object into properties
523 parameters.put("outputPath", "testPath.input.parameters[0].value");
524 parameters.put("source", "testPath.input.parameters[0].value");
525 SliPluginUtils.jsonStringToCtx(parameters, ctx);
527 assertEquals("0.2.0.0/16", ctx.getAttribute("testPath.input.parameters[0].value.[0].id"));
528 assertEquals("ge04::/64", ctx.getAttribute("testPath.input.parameters[0].value.[1].id"));
529 assertEquals("2", ctx.getAttribute("testPath.input.parameters[0].value._length"));
533 public void testEscapedJsonStringToCtx() throws Exception {
534 String path = "src/test/resources/EscapedJson.json";
535 String content = new String(Files.readAllBytes(Paths.get(path)));
537 SvcLogicContext ctx = new SvcLogicContext();
538 ctx.setAttribute("input", content);
540 Map<String, String> parameters = new HashMap<String, String>();
541 parameters.put("outputPath", "testPath");
542 parameters.put("isEscaped", "true"); // set to true because the entire json content has been escaped
543 parameters.put("source", "input");
546 SliPluginUtils.jsonStringToCtx(parameters, ctx);
547 assertEquals("false", ctx.getAttribute("testPath.widget.debug"));
548 assertEquals("center", ctx.getAttribute("testPath.widget.image.alignment"));
549 assertEquals("150", ctx.getAttribute("testPath.widget.image.hOffset"));
550 assertEquals("moon", ctx.getAttribute("testPath.widget.image.name"));
551 assertEquals("images/moon.png", ctx.getAttribute("testPath.widget.image.src"));
552 assertEquals("150", ctx.getAttribute("testPath.widget.image.vOffset"));
553 assertEquals("center", ctx.getAttribute("testPath.widget.text.alignment"));
554 assertEquals("Click Me", ctx.getAttribute("testPath.widget.text.data"));
555 assertEquals("350", ctx.getAttribute("testPath.widget.text.hOffset"));
556 assertEquals("text1", ctx.getAttribute("testPath.widget.text.name"));
557 assertEquals("21", ctx.getAttribute("testPath.widget.text.size"));
558 assertEquals("bold", ctx.getAttribute("testPath.widget.text.style"));
559 assertEquals("200", ctx.getAttribute("testPath.widget.text.vOffset"));
560 assertEquals("300", ctx.getAttribute("testPath.widget.window.height"));
561 assertEquals("main_window", ctx.getAttribute("testPath.widget.window.name"));
562 assertEquals("ONAP Widget", ctx.getAttribute("testPath.widget.window.title"));
563 assertEquals("200", ctx.getAttribute("testPath.widget.window.width"));