Merge of new rebased code
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / openecomp / appc / dg / common / impl / JsonDgUtilImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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  */
21
22 package org.openecomp.appc.dg.common.impl;
23
24 import ch.qos.logback.core.Appender;
25
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;
31
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import static org.mockito.Mockito.mock;
36
37 public class JsonDgUtilImplTest {
38
39     private final Appender appender = mock(Appender.class);
40
41
42     @Test
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+"\"}";
51
52         SvcLogicContext ctx = new SvcLogicContext();
53         Map<String, String> params = new HashMap<>();
54         params.put(key, payload);
55         jsonDgUtil.flatAndAddToContext(params, ctx);
56
57
58             Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
59             Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
60
61
62
63     }
64
65
66     @Test
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+"\"}";
75
76         SvcLogicContext ctx = new SvcLogicContext();
77         Map<String, String> params = new HashMap<>();
78         params.put(key, payload);
79         try {
80             jsonDgUtil.flatAndAddToContext(params, ctx);
81
82         } catch (APPCException e) {
83             Assert.assertNull(ctx.getAttribute(testValueKey));
84             Assert.assertNull(ctx.getAttribute(testValueKey2));
85             Assert.assertNotNull(ctx.getAttribute("error-message"));
86         }
87
88
89     }
90
91
92     @Test
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+"\"}";
101
102         SvcLogicContext ctx = new SvcLogicContext();
103         Map<String, String> params = new HashMap<>();
104         params.put(key, "");
105         ctx.setAttribute("input.payload", payload);
106         jsonDgUtil.flatAndAddToContext(params, ctx);
107
108
109         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
110         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
111     }
112
113     @Test
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);
121
122
123         Assert.assertNull(ctx.getAttribute(testValueKey));
124         Assert.assertNull(ctx.getAttribute(testValueKey2));
125     }
126
127
128     @Test
129     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
130
131         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
132         String key = "payload";
133         String testValueKey = "test-key";
134         String testValueKey2 = "test-key2";
135
136         SvcLogicContext ctx = new SvcLogicContext();
137         Map<String, String> params = new HashMap<>();
138         params.put(key, "");
139         jsonDgUtil.flatAndAddToContext(params, ctx);
140
141         Assert.assertNull(ctx.getAttribute(testValueKey));
142         Assert.assertNull(ctx.getAttribute(testValueKey2));
143     }
144
145
146     @Test
147     public void testGenerateOutputPayloadFromContext() throws Exception {
148
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";
155
156         String key3 = "output.payload.test-key3";
157         String testValueKey3 = "value3";
158
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);
165
166         Assert.assertNotNull(ctx.getAttribute(key));
167
168     }
169 }