6febd2679104dffd8e70e0cf1319e10c78ac2a93
[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  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.dg.common.impl;
26
27 import ch.qos.logback.core.Appender;
28
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;
34
35 import java.util.HashMap;
36 import java.util.Map;
37
38 import static org.mockito.Mockito.mock;
39
40 public class JsonDgUtilImplTest {
41
42     private final Appender appender = mock(Appender.class);
43
44
45     @Test
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+"\"}";
54
55         SvcLogicContext ctx = new SvcLogicContext();
56         Map<String, String> params = new HashMap<>();
57         params.put(key, payload);
58         jsonDgUtil.flatAndAddToContext(params, ctx);
59
60
61             Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
62             Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
63
64
65
66     }
67
68
69     @Test
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+"\"}";
78
79         SvcLogicContext ctx = new SvcLogicContext();
80         Map<String, String> params = new HashMap<>();
81         params.put(key, payload);
82         try {
83             jsonDgUtil.flatAndAddToContext(params, ctx);
84
85         } catch (APPCException e) {
86             Assert.assertNull(ctx.getAttribute(testValueKey));
87             Assert.assertNull(ctx.getAttribute(testValueKey2));
88             Assert.assertNotNull(ctx.getAttribute("error-message"));
89         }
90
91
92     }
93
94
95     @Test
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+"\"}";
104
105         SvcLogicContext ctx = new SvcLogicContext();
106         Map<String, String> params = new HashMap<>();
107         params.put(key, "");
108         ctx.setAttribute("input.payload", payload);
109         jsonDgUtil.flatAndAddToContext(params, ctx);
110
111
112         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
113         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
114     }
115
116     @Test
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);
124
125
126         Assert.assertNull(ctx.getAttribute(testValueKey));
127         Assert.assertNull(ctx.getAttribute(testValueKey2));
128     }
129
130
131     @Test
132     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
133
134         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
135         String key = "payload";
136         String testValueKey = "test-key";
137         String testValueKey2 = "test-key2";
138
139         SvcLogicContext ctx = new SvcLogicContext();
140         Map<String, String> params = new HashMap<>();
141         params.put(key, "");
142         jsonDgUtil.flatAndAddToContext(params, ctx);
143
144         Assert.assertNull(ctx.getAttribute(testValueKey));
145         Assert.assertNull(ctx.getAttribute(testValueKey2));
146     }
147
148
149     @Test
150     public void testGenerateOutputPayloadFromContext() throws Exception {
151
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";
158
159         String key3 = "output.payload.test-key3";
160         String testValueKey3 = "value3";
161
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);
168
169         Assert.assertNotNull(ctx.getAttribute(key));
170
171     }
172 }