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.junit.Assert.*;
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("output.status.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));