2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.dg.common.impl;
27 import ch.qos.logback.core.Appender;
29 import org.junit.Assert;
30 import org.junit.Test;
31 import org.openecomp.appc.dg.common.impl.JsonDgUtilImpl;
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import java.util.HashMap;
38 import static org.mockito.Mockito.mock;
40 public class JsonDgUtilImplTest {
42 private final Appender appender = mock(Appender.class);
46 public void testFlatAndAddToContext() throws Exception {
47 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
48 String key = "payload";
49 String testValueKey = "test-key";
50 String testValueValue = "test-value";
51 String testValueKey2 = "test-key2";
52 String testValueValue2 = "test-value2";
53 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
55 SvcLogicContext ctx = new SvcLogicContext();
56 Map<String, String> params = new HashMap<>();
57 params.put(key, payload);
58 jsonDgUtil.flatAndAddToContext(params, ctx);
61 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
62 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
70 public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
71 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
72 String key = "payload";
73 String testValueKey = "test-key";
74 String testValueValue = "test-value";
75 String testValueKey2 = "test-key2";
76 String testValueValue2 = "test-value2";
77 String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
79 SvcLogicContext ctx = new SvcLogicContext();
80 Map<String, String> params = new HashMap<>();
81 params.put(key, payload);
83 jsonDgUtil.flatAndAddToContext(params, ctx);
85 } catch (APPCException e) {
86 Assert.assertNull(ctx.getAttribute(testValueKey));
87 Assert.assertNull(ctx.getAttribute(testValueKey2));
88 Assert.assertNotNull(ctx.getAttribute("error-message"));
96 public void testFlatAndAddToContextPayloadFromContext() throws Exception {
97 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
98 String key = "payload";
99 String testValueKey = "test-key";
100 String testValueValue = "test-value";
101 String testValueKey2 = "test-key2";
102 String testValueValue2 = "test-value2";
103 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
105 SvcLogicContext ctx = new SvcLogicContext();
106 Map<String, String> params = new HashMap<>();
108 ctx.setAttribute("input.payload", payload);
109 jsonDgUtil.flatAndAddToContext(params, ctx);
112 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
113 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
117 public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
118 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
119 String testValueKey = "test-key";
120 String testValueKey2 = "test-key2";
121 SvcLogicContext ctx = new SvcLogicContext();
122 Map<String, String> params = new HashMap<>();
123 jsonDgUtil.flatAndAddToContext(params, ctx);
126 Assert.assertNull(ctx.getAttribute(testValueKey));
127 Assert.assertNull(ctx.getAttribute(testValueKey2));
132 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
134 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
135 String key = "payload";
136 String testValueKey = "test-key";
137 String testValueKey2 = "test-key2";
139 SvcLogicContext ctx = new SvcLogicContext();
140 Map<String, String> params = new HashMap<>();
142 jsonDgUtil.flatAndAddToContext(params, ctx);
144 Assert.assertNull(ctx.getAttribute(testValueKey));
145 Assert.assertNull(ctx.getAttribute(testValueKey2));
150 public void testGenerateOutputPayloadFromContext() throws Exception {
152 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
153 String key = "output.payload";
154 String key1 = "output.payload.test-key[0]";
155 String key2 = "output.payload.test-key[1]";
156 String testValueKey1 = "value1";
157 String testValueKey2 = "value2";
159 String key3 = "output.payload.test-key3";
160 String testValueKey3 = "value3";
162 SvcLogicContext ctx = new SvcLogicContext();
163 Map<String, String> params = new HashMap<>();
164 ctx.setAttribute(key1, testValueKey1);
165 ctx.setAttribute(key2, testValueKey2);
166 ctx.setAttribute(key3, testValueKey3);
167 jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
169 Assert.assertNotNull(ctx.getAttribute(key));