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.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;
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;
49 public void setUp() throws Exception {
50 this.ctx = new SvcLogicContext();
51 parameters = new HashMap<String, String>();
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");
61 Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
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"));
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");
80 parameters.put("index", "1");
81 parameters.put("list_pfx", "service-data.vnf-l3");
83 utils.ctxListRemove(parameters, ctx);
84 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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"));
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");
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");
108 parameters.put("list_pfx", "service-data.vnf-l3");
109 parameters.put("key", "vnf-host-name");
110 parameters.put("value", "vnf-host-name_1");
112 utils.ctxListRemove(parameters, ctx);
113 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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"));
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");
131 parameters.put("list_pfx", "service-data.vnf-l3");
132 parameters.put("value", "vnf-host-name_1");
134 utils.ctxListRemove(parameters, ctx);
135 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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]"));
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");
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");
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");
169 utils.ctxListRemove(parameters, ctx);
170 SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
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"));
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");
194 parameters.put("index", "1");
195 parameters.put("list_pfx", "service-data.vnf-l3");
197 utils.ctxListRemove(parameters, ctx);
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);
209 SliPluginUtils.ctxPutAll(ctx, entries);
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"));
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);
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));
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);
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);
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);
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")));
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));
269 @Test(expected = SvcLogicException.class)
270 public void testPrintContextForEmptyParameters() throws SvcLogicException {
271 SliPluginUtils.printContext(parameters, ctx);
274 @Test(expected = SvcLogicException.class)
275 public void testPrintContextForNullParameters() throws SvcLogicException {
276 SliPluginUtils.printContext(null, ctx);
280 public void testPrintContext() throws SvcLogicException {
281 parameters.put("filename", "testFileName");
282 SliPluginUtils.printContext(parameters, ctx);
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"));
299 public void testCtxKeyEmpty() {
300 ctx.setAttribute("key", "");
301 assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
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");
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));
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");
330 SliPluginUtils.jsonStringToCtx(parameters, ctx);
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"));
363 public void testJsonStringToCtx() throws Exception {
364 String path = "src/test/resources/ObjectMenu.json";
365 String content = new String(Files.readAllBytes(Paths.get(path)));
367 SvcLogicContext ctx = new SvcLogicContext();
368 ctx.setAttribute("input", content);
370 Map<String, String> parameters = new HashMap<String, String>();
371 parameters.put("outputPath", "testPath");
372 parameters.put("isEscaped", "false");
373 parameters.put("source", "input");
375 SliPluginUtils.jsonStringToCtx(parameters, ctx);
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"));
409 public void test2dJsonStringToCtx() throws Exception {
410 String path = "src/test/resources/2dArray.json";
411 String content = new String(Files.readAllBytes(Paths.get(path)));
413 SvcLogicContext ctx = new SvcLogicContext();
414 ctx.setAttribute("input", content);
416 Map<String, String> parameters = new HashMap<String, String>();
417 parameters.put("outputPath", "testPath");
418 parameters.put("isEscaped", "false");
419 parameters.put("source", "input");
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"));
434 public void test3dJsonStringToCtx() throws Exception {
435 String path = "src/test/resources/3dArray.json";
436 String content = new String(Files.readAllBytes(Paths.get(path)));
438 SvcLogicContext ctx = new SvcLogicContext();
439 ctx.setAttribute("input", content);
441 Map<String, String> parameters = new HashMap<String, String>();
442 parameters.put("outputPath", "testPath");
443 parameters.put("isEscaped", "false");
444 parameters.put("source", "input");
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"));
465 public void testJsonWidgetStringToCtx() throws Exception {
466 String path = "src/test/resources/Widget.json";
467 String content = new String(Files.readAllBytes(Paths.get(path)));
469 SvcLogicContext ctx = new SvcLogicContext();
470 ctx.setAttribute("input", content);
472 Map<String, String> parameters = new HashMap<String, String>();
473 parameters.put("outputPath", "testPath");
474 parameters.put("isEscaped", "false");
475 parameters.put("source", "input");
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"));
498 public void testEmbeddedEscapedJsonJsonStringToCtx() throws Exception {
499 String path = "src/test/resources/EmbeddedEscapedJson.json";
500 String content = new String(Files.readAllBytes(Paths.get(path)));
502 SvcLogicContext ctx = new SvcLogicContext();
503 ctx.setAttribute("input", content);
505 Map<String, String> parameters = new HashMap<String, String>();
506 parameters.put("outputPath", "testPath");
507 parameters.put("isEscaped", "false");
508 parameters.put("source", "input");
510 SliPluginUtils.jsonStringToCtx(parameters, ctx);
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"));
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);
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"));
532 public void testEscapedJsonStringToCtx() throws Exception {
533 String path = "src/test/resources/EscapedJson.json";
534 String content = new String(Files.readAllBytes(Paths.get(path)));
536 SvcLogicContext ctx = new SvcLogicContext();
537 ctx.setAttribute("input", content);
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");
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"));