2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.common.impl;
26 import ch.qos.logback.core.Appender;
28 import org.junit.Assert;
29 import org.junit.Test;
30 import org.onap.appc.dg.common.impl.JsonDgUtilImpl;
31 import org.onap.appc.exceptions.APPCException;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import java.util.HashMap;
37 import static org.mockito.Mockito.mock;
39 public class JsonDgUtilImplTest {
41 private final Appender appender = mock(Appender.class);
45 public void testFlatAndAddToContext() throws Exception {
46 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
47 String key = "payload";
48 String testValueKey = "test-key";
49 String testValueValue = "test-value";
50 String testValueKey2 = "test-key2";
51 String testValueValue2 = "test-value2";
52 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
54 SvcLogicContext ctx = new SvcLogicContext();
55 Map<String, String> params = new HashMap<>();
56 params.put(key, payload);
57 jsonDgUtil.flatAndAddToContext(params, ctx);
60 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
61 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
69 public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
70 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
71 String key = "payload";
72 String testValueKey = "test-key";
73 String testValueValue = "test-value";
74 String testValueKey2 = "test-key2";
75 String testValueValue2 = "test-value2";
76 String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
78 SvcLogicContext ctx = new SvcLogicContext();
79 Map<String, String> params = new HashMap<>();
80 params.put(key, payload);
82 jsonDgUtil.flatAndAddToContext(params, ctx);
84 } catch (APPCException e) {
85 Assert.assertNull(ctx.getAttribute(testValueKey));
86 Assert.assertNull(ctx.getAttribute(testValueKey2));
87 Assert.assertNotNull(ctx.getAttribute("error-message"));
95 public void testFlatAndAddToContextPayloadFromContext() throws Exception {
96 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
97 String key = "payload";
98 String testValueKey = "test-key";
99 String testValueValue = "test-value";
100 String testValueKey2 = "test-key2";
101 String testValueValue2 = "test-value2";
102 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
104 SvcLogicContext ctx = new SvcLogicContext();
105 Map<String, String> params = new HashMap<>();
107 ctx.setAttribute("input.payload", payload);
108 jsonDgUtil.flatAndAddToContext(params, ctx);
111 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
112 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
116 public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
117 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
118 String testValueKey = "test-key";
119 String testValueKey2 = "test-key2";
120 SvcLogicContext ctx = new SvcLogicContext();
121 Map<String, String> params = new HashMap<>();
122 jsonDgUtil.flatAndAddToContext(params, ctx);
125 Assert.assertNull(ctx.getAttribute(testValueKey));
126 Assert.assertNull(ctx.getAttribute(testValueKey2));
131 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
133 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
134 String key = "payload";
135 String testValueKey = "test-key";
136 String testValueKey2 = "test-key2";
138 SvcLogicContext ctx = new SvcLogicContext();
139 Map<String, String> params = new HashMap<>();
141 jsonDgUtil.flatAndAddToContext(params, ctx);
143 Assert.assertNull(ctx.getAttribute(testValueKey));
144 Assert.assertNull(ctx.getAttribute(testValueKey2));
149 public void testGenerateOutputPayloadFromContext() throws Exception {
151 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
152 String key = "output.payload";
153 String key1 = "output.payload.test-key[0]";
154 String key2 = "output.payload.test-key[1]";
155 String testValueKey1 = "value1";
156 String testValueKey2 = "value2";
158 String key3 = "output.payload.test-key3";
159 String testValueKey3 = "value3";
161 SvcLogicContext ctx = new SvcLogicContext();
162 Map<String, String> params = new HashMap<>();
163 ctx.setAttribute(key1, testValueKey1);
164 ctx.setAttribute(key2, testValueKey2);
165 ctx.setAttribute(key3, testValueKey3);
166 jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
168 Assert.assertNotNull(ctx.getAttribute(key));