formatted the code and added test case
[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  * Modifications Copyright (C) 2018 IBM.
10  * ================================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  * 
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  * 
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * 
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.dg.common.impl;
27
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30
31 import java.text.SimpleDateFormat;
32 import java.util.HashMap;
33 import java.util.Map;
34
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.appc.exceptions.APPCException;
39 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
40
41 public class JsonDgUtilImplTest {
42
43     private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = ThreadLocal
44             .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
45     private JsonDgUtilImpl jsonDgUtil;
46
47     @Before
48     public void setUp() {
49         jsonDgUtil = new JsonDgUtilImpl();
50     }
51
52     @Test
53     public void testFlatAndAddToContext() throws Exception {
54         String key = "payload";
55         String testValueKey = "test-key";
56         String testValueValue = "test-value";
57         String testValueKey2 = "test-key2";
58         String testValueValue2 = "test-value2";
59         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\"" + testValueKey2 + "\": \""
60                 + testValueValue2 + "\"}";
61
62         SvcLogicContext ctx = new SvcLogicContext();
63         Map<String, String> params = new HashMap<>();
64         params.put(key, payload);
65         jsonDgUtil.flatAndAddToContext(params, ctx);
66
67         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
68         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
69
70     }
71
72     @Test
73     public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
74         String key = "payload";
75         String testValueKey = "test-key";
76         String testValueValue = "test-value";
77         String testValueKey2 = "test-key2";
78         String testValueValue2 = "test-value2";
79         String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\"" + testValueKey2 + "\": \""
80                 + testValueValue2 + "\"}";
81
82         SvcLogicContext ctx = new SvcLogicContext();
83         Map<String, String> params = new HashMap<>();
84         params.put(key, payload);
85         try {
86             jsonDgUtil.flatAndAddToContext(params, ctx);
87
88         } catch (APPCException e) {
89             Assert.assertNull(ctx.getAttribute(testValueKey));
90             Assert.assertNull(ctx.getAttribute(testValueKey2));
91             Assert.assertNotNull(ctx.getAttribute("error-message"));
92         }
93
94     }
95
96     @Test
97     public void testFlatAndAddToContextPayloadFromContext() throws Exception {
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 + "\": \""
104                 + testValueValue2 + "\"}";
105
106         SvcLogicContext ctx = new SvcLogicContext();
107         Map<String, String> params = new HashMap<>();
108         params.put(key, "");
109         ctx.setAttribute("input.payload", payload);
110         jsonDgUtil.flatAndAddToContext(params, ctx);
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         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         Assert.assertNull(ctx.getAttribute(testValueKey));
125         Assert.assertNull(ctx.getAttribute(testValueKey2));
126     }
127
128     @Test
129     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
130
131         String key = "payload";
132         String testValueKey = "test-key";
133         String testValueKey2 = "test-key2";
134
135         SvcLogicContext ctx = new SvcLogicContext();
136         Map<String, String> params = new HashMap<>();
137         params.put(key, "");
138         jsonDgUtil.flatAndAddToContext(params, ctx);
139
140         Assert.assertNull(ctx.getAttribute(testValueKey));
141         Assert.assertNull(ctx.getAttribute(testValueKey2));
142     }
143
144     @Test
145     public void testGenerateOutputPayloadFromContext() throws Exception {
146
147         String key = "output.payload";
148         String key1 = "output.payload.test-key[0]";
149         String key2 = "output.payload.test-key[1]";
150         String testValueKey1 = "value1";
151         String testValueKey2 = "value2";
152
153         String key3 = "output.payload.test-key3";
154         String testValueKey3 = "value3";
155
156         SvcLogicContext ctx = new SvcLogicContext();
157         Map<String, String> params = new HashMap<>();
158         ctx.setAttribute(key1, testValueKey1);
159         ctx.setAttribute(key2, testValueKey2);
160         ctx.setAttribute(key3, testValueKey3);
161         jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
162
163         Assert.assertNotNull(ctx.getAttribute(key));
164
165     }
166
167     @Test
168     public void testCvaasFileNameAndFileContentToContext() throws Exception {
169
170         String key1 = "running-config.upload-date";
171         String testValueKey1 = "2004-02-09 00:00:00:000";
172         Long epochUploadTimestamp = DATE_TIME_PARSER_THREAD_LOCAL.get().parse(testValueKey1).getTime();
173         SvcLogicContext ctx = new SvcLogicContext();
174         Map<String, String> params = new HashMap<>();
175         ctx.setAttribute(key1, testValueKey1);
176         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
177         assertNotNull(ctx.getAttribute("cvaas-file-content"));
178         assertTrue(ctx.getAttribute("cvaas-file-content").contains(epochUploadTimestamp.toString()));
179     }
180
181     @Test(expected = APPCException.class)
182     public void testCvaasFileNameAndFileContentToContextForEmptyParams() throws Exception {
183         SvcLogicContext ctx = new SvcLogicContext();
184         Map<String, String> params = new HashMap<>();
185         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
186     }
187
188     @Test(expected = APPCException.class)
189     public void testCheckFileCreated() throws APPCException {
190         SvcLogicContext ctx = new SvcLogicContext();
191         ctx.setAttribute("cvaas-file-name", "testCvaasFile");
192         Map<String, String> params = new HashMap<>();
193         params.put("vnf-id", "testVnfId");
194         jsonDgUtil.checkFileCreated(params, ctx);
195     }
196 }