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.openecomp.appc.dg.common.impl;
24 import ch.qos.logback.core.Appender;
26 import org.junit.Assert;
27 import org.junit.Test;
28 import org.openecomp.appc.dg.common.impl.JsonDgUtilImpl;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.sdnc.sli.SvcLogicContext;
32 import java.util.HashMap;
35 import static org.mockito.Mockito.mock;
37 public class JsonDgUtilImplTest {
39 private final Appender appender = mock(Appender.class);
43 public void testFlatAndAddToContext() throws Exception {
44 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
45 String key = "payload";
46 String testValueKey = "test-key";
47 String testValueValue = "test-value";
48 String testValueKey2 = "test-key2";
49 String testValueValue2 = "test-value2";
50 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
52 SvcLogicContext ctx = new SvcLogicContext();
53 Map<String, String> params = new HashMap<>();
54 params.put(key, payload);
55 jsonDgUtil.flatAndAddToContext(params, ctx);
58 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
59 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
67 public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
68 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
69 String key = "payload";
70 String testValueKey = "test-key";
71 String testValueValue = "test-value";
72 String testValueKey2 = "test-key2";
73 String testValueValue2 = "test-value2";
74 String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
76 SvcLogicContext ctx = new SvcLogicContext();
77 Map<String, String> params = new HashMap<>();
78 params.put(key, payload);
80 jsonDgUtil.flatAndAddToContext(params, ctx);
82 } catch (APPCException e) {
83 Assert.assertNull(ctx.getAttribute(testValueKey));
84 Assert.assertNull(ctx.getAttribute(testValueKey2));
85 Assert.assertNotNull(ctx.getAttribute("error-message"));
93 public void testFlatAndAddToContextPayloadFromContext() throws Exception {
94 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
95 String key = "payload";
96 String testValueKey = "test-key";
97 String testValueValue = "test-value";
98 String testValueKey2 = "test-key2";
99 String testValueValue2 = "test-value2";
100 String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
102 SvcLogicContext ctx = new SvcLogicContext();
103 Map<String, String> params = new HashMap<>();
105 ctx.setAttribute("input.payload", payload);
106 jsonDgUtil.flatAndAddToContext(params, ctx);
109 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
110 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
114 public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
115 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
116 String testValueKey = "test-key";
117 String testValueKey2 = "test-key2";
118 SvcLogicContext ctx = new SvcLogicContext();
119 Map<String, String> params = new HashMap<>();
120 jsonDgUtil.flatAndAddToContext(params, ctx);
123 Assert.assertNull(ctx.getAttribute(testValueKey));
124 Assert.assertNull(ctx.getAttribute(testValueKey2));
129 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
131 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
132 String key = "payload";
133 String testValueKey = "test-key";
134 String testValueKey2 = "test-key2";
136 SvcLogicContext ctx = new SvcLogicContext();
137 Map<String, String> params = new HashMap<>();
139 jsonDgUtil.flatAndAddToContext(params, ctx);
141 Assert.assertNull(ctx.getAttribute(testValueKey));
142 Assert.assertNull(ctx.getAttribute(testValueKey2));
147 public void testGenerateOutputPayloadFromContext() throws Exception {
149 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
150 String key = "output.payload";
151 String key1 = "output.payload.test-key[0]";
152 String key2 = "output.payload.test-key[1]";
153 String testValueKey1 = "value1";
154 String testValueKey2 = "value2";
156 String key3 = "output.payload.test-key3";
157 String testValueKey3 = "value3";
159 SvcLogicContext ctx = new SvcLogicContext();
160 Map<String, String> params = new HashMap<>();
161 ctx.setAttribute(key1, testValueKey1);
162 ctx.setAttribute(key2, testValueKey2);
163 ctx.setAttribute(key3, testValueKey3);
164 jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
166 Assert.assertNotNull(ctx.getAttribute(key));