2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2017 Amdocs
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=========================================================
20 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.openecomp.appc.dg.common.impl;
25 import ch.qos.logback.core.Appender;
27 import org.junit.Assert;
28 import org.junit.Test;
29 import org.openecomp.appc.dg.common.impl.JsonDgUtilImpl;
30 import org.openecomp.appc.exceptions.APPCException;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import java.util.HashMap;
36 import static org.mockito.Mockito.mock;
38 public class JsonDgUtilImplTest {
40 private final Appender appender = mock(Appender.class);
44 public void testFlatAndAddToContext() throws Exception {
45 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
46 String key = "payload";
47 String testValueKey = "test-key";
48 String testValueValue = "test-value";
49 String testValueKey2 = "test-key2";
50 String testValueValue2 = "test-value2";
51 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
53 SvcLogicContext ctx = new SvcLogicContext();
54 Map<String, String> params = new HashMap<>();
55 params.put(key, payload);
56 jsonDgUtil.flatAndAddToContext(params, ctx);
59 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
60 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
68 public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
69 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
70 String key = "payload";
71 String testValueKey = "test-key";
72 String testValueValue = "test-value";
73 String testValueKey2 = "test-key2";
74 String testValueValue2 = "test-value2";
75 String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
77 SvcLogicContext ctx = new SvcLogicContext();
78 Map<String, String> params = new HashMap<>();
79 params.put(key, payload);
81 jsonDgUtil.flatAndAddToContext(params, ctx);
83 } catch (APPCException e) {
84 Assert.assertNull(ctx.getAttribute(testValueKey));
85 Assert.assertNull(ctx.getAttribute(testValueKey2));
86 Assert.assertNotNull(ctx.getAttribute("error-message"));
94 public void testFlatAndAddToContextPayloadFromContext() throws Exception {
95 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
96 String key = "payload";
97 String testValueKey = "test-key";
98 String testValueValue = "test-value";
99 String testValueKey2 = "test-key2";
100 String testValueValue2 = "test-value2";
101 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
103 SvcLogicContext ctx = new SvcLogicContext();
104 Map<String, String> params = new HashMap<>();
106 ctx.setAttribute("input.payload", payload);
107 jsonDgUtil.flatAndAddToContext(params, ctx);
110 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
111 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
115 public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
116 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
117 String testValueKey = "test-key";
118 String testValueKey2 = "test-key2";
119 SvcLogicContext ctx = new SvcLogicContext();
120 Map<String, String> params = new HashMap<>();
121 jsonDgUtil.flatAndAddToContext(params, ctx);
124 Assert.assertNull(ctx.getAttribute(testValueKey));
125 Assert.assertNull(ctx.getAttribute(testValueKey2));
130 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
132 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
133 String key = "payload";
134 String testValueKey = "test-key";
135 String testValueKey2 = "test-key2";
137 SvcLogicContext ctx = new SvcLogicContext();
138 Map<String, String> params = new HashMap<>();
140 jsonDgUtil.flatAndAddToContext(params, ctx);
142 Assert.assertNull(ctx.getAttribute(testValueKey));
143 Assert.assertNull(ctx.getAttribute(testValueKey2));
148 public void testGenerateOutputPayloadFromContext() throws Exception {
150 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
151 String key = "output.payload";
152 String key1 = "output.payload.test-key[0]";
153 String key2 = "output.payload.test-key[1]";
154 String testValueKey1 = "value1";
155 String testValueKey2 = "value2";
157 String key3 = "output.payload.test-key3";
158 String testValueKey3 = "value3";
160 SvcLogicContext ctx = new SvcLogicContext();
161 Map<String, String> params = new HashMap<>();
162 ctx.setAttribute(key1, testValueKey1);
163 ctx.setAttribute(key2, testValueKey2);
164 ctx.setAttribute(key3, testValueKey3);
165 jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
167 Assert.assertNotNull(ctx.getAttribute(key));