Update license header in appc-dg-common files
[appc.git] / appc-dg / appc-dg-shared / appc-dg-common / src / test / java / org / onap / appc / dg / common / impl / JsonDgUtilImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
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  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.dg.common.impl;
25
26 import ch.qos.logback.core.Appender;
27
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;
33
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import static org.mockito.Mockito.mock;
38
39 public class JsonDgUtilImplTest {
40
41     private final Appender appender = mock(Appender.class);
42
43
44     @Test
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+"\"}";
53
54         SvcLogicContext ctx = new SvcLogicContext();
55         Map<String, String> params = new HashMap<>();
56         params.put(key, payload);
57         jsonDgUtil.flatAndAddToContext(params, ctx);
58
59
60             Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
61             Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
62
63
64
65     }
66
67
68     @Test
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+"\"}";
77
78         SvcLogicContext ctx = new SvcLogicContext();
79         Map<String, String> params = new HashMap<>();
80         params.put(key, payload);
81         try {
82             jsonDgUtil.flatAndAddToContext(params, ctx);
83
84         } catch (APPCException e) {
85             Assert.assertNull(ctx.getAttribute(testValueKey));
86             Assert.assertNull(ctx.getAttribute(testValueKey2));
87             Assert.assertNotNull(ctx.getAttribute("error-message"));
88         }
89
90
91     }
92
93
94     @Test
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+"\"}";
103
104         SvcLogicContext ctx = new SvcLogicContext();
105         Map<String, String> params = new HashMap<>();
106         params.put(key, "");
107         ctx.setAttribute("input.payload", payload);
108         jsonDgUtil.flatAndAddToContext(params, ctx);
109
110
111         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
112         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
113     }
114
115     @Test
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);
123
124
125         Assert.assertNull(ctx.getAttribute(testValueKey));
126         Assert.assertNull(ctx.getAttribute(testValueKey2));
127     }
128
129
130     @Test
131     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
132
133         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
134         String key = "payload";
135         String testValueKey = "test-key";
136         String testValueKey2 = "test-key2";
137
138         SvcLogicContext ctx = new SvcLogicContext();
139         Map<String, String> params = new HashMap<>();
140         params.put(key, "");
141         jsonDgUtil.flatAndAddToContext(params, ctx);
142
143         Assert.assertNull(ctx.getAttribute(testValueKey));
144         Assert.assertNull(ctx.getAttribute(testValueKey2));
145     }
146
147
148     @Test
149     public void testGenerateOutputPayloadFromContext() throws Exception {
150
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";
157
158         String key3 = "output.payload.test-key3";
159         String testValueKey3 = "value3";
160
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);
167
168         Assert.assertNotNull(ctx.getAttribute(key));
169
170     }
171 }