Change code in appc dispatcher for new LCMs in R6
[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-2019 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  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.dg.common.impl;
26
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import java.text.SimpleDateFormat;
30 import java.util.HashMap;
31 import java.util.Map;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
37
38 public class JsonDgUtilImplTest {
39
40     private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL =
41             ThreadLocal.withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
42     private JsonDgUtilImpl jsonDgUtil;
43
44     @Before
45     public void setUp() {
46         jsonDgUtil = new JsonDgUtilImpl();
47     }
48
49     @Test
50     public void testFlatAndAddToContext() throws Exception {
51         String key = "payload";
52         String testValueKey = "test-key";
53         String testValueValue = "test-value";
54         String testValueKey2 = "test-key2";
55         String testValueValue2 = "test-value2";
56         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\"" + testValueKey2 + "\": \""
57                 + testValueValue2 + "\"}";
58
59         SvcLogicContext ctx = new SvcLogicContext();
60         Map<String, String> params = new HashMap<>();
61         params.put(key, payload);
62         jsonDgUtil.flatAndAddToContext(params, ctx);
63
64         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
65         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
66
67     }
68
69     @Test
70     public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
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 + "\": \""
77                 + 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     @Test
94     public void testFlatAndAddToContextPayloadFromContext() throws Exception {
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 + "\": \""
101                 + testValueValue2 + "\"}";
102
103         SvcLogicContext ctx = new SvcLogicContext();
104         Map<String, String> params = new HashMap<>();
105         params.put(key, "");
106         ctx.setAttribute("input.payload", payload);
107         jsonDgUtil.flatAndAddToContext(params, ctx);
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         String testValueKey = "test-key";
116         String testValueKey2 = "test-key2";
117         SvcLogicContext ctx = new SvcLogicContext();
118         Map<String, String> params = new HashMap<>();
119         jsonDgUtil.flatAndAddToContext(params, ctx);
120
121         Assert.assertNull(ctx.getAttribute(testValueKey));
122         Assert.assertNull(ctx.getAttribute(testValueKey2));
123     }
124
125     @Test
126     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
127
128         String key = "payload";
129         String testValueKey = "test-key";
130         String testValueKey2 = "test-key2";
131
132         SvcLogicContext ctx = new SvcLogicContext();
133         Map<String, String> params = new HashMap<>();
134         params.put(key, "");
135         jsonDgUtil.flatAndAddToContext(params, ctx);
136
137         Assert.assertNull(ctx.getAttribute(testValueKey));
138         Assert.assertNull(ctx.getAttribute(testValueKey2));
139     }
140
141     @Test
142     public void testGenerateOutputPayloadFromContext() throws Exception {
143
144         String key = "output.payload";
145         String key1 = "output.payload.test-key[0]";
146         String key2 = "output.payload.test-key[1]";
147         String testValueKey1 = "value1";
148         String testValueKey2 = "value2";
149
150         String key3 = "output.payload.test-key3";
151         String testValueKey3 = "value3";
152
153         SvcLogicContext ctx = new SvcLogicContext();
154         Map<String, String> params = new HashMap<>();
155         ctx.setAttribute(key1, testValueKey1);
156         ctx.setAttribute(key2, testValueKey2);
157         ctx.setAttribute(key3, testValueKey3);
158         jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
159
160         Assert.assertNotNull(ctx.getAttribute(key));
161
162     }
163
164     @Test
165     public void testCvaasFileNameAndFileContentToContext() throws Exception {
166
167         String key1 = "running-config.upload-date";
168         String testValueKey1 = "2004-02-09 00:00:00:000";
169         Long epochUploadTimestamp = DATE_TIME_PARSER_THREAD_LOCAL.get().parse(testValueKey1).getTime();
170         SvcLogicContext ctx = new SvcLogicContext();
171         Map<String, String> params = new HashMap<>();
172         ctx.setAttribute(key1, testValueKey1);
173         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
174         assertNotNull(ctx.getAttribute("cvaas-file-content"));
175         assertTrue(ctx.getAttribute("cvaas-file-content").contains(epochUploadTimestamp.toString()));
176     }
177
178     @Test(expected = APPCException.class)
179     public void testCvaasFileNameAndFileContentToContextForEmptyParams() throws Exception {
180         SvcLogicContext ctx = new SvcLogicContext();
181         Map<String, String> params = new HashMap<>();
182         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
183     }
184
185     @Test(expected = APPCException.class)
186     public void testCheckFileCreated() throws APPCException {
187         SvcLogicContext ctx = new SvcLogicContext();
188         ctx.setAttribute("cvaas-file-name", "testCvaasFile");
189         Map<String, String> params = new HashMap<>();
190         params.put("vnf-id", "testVnfId");
191         jsonDgUtil.checkFileCreated(params, ctx);
192     }
193 }