950cb2f1abfc6560c47a3885b702ec3f25e78c7b
[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 import java.text.SimpleDateFormat;
31 import java.util.HashMap;
32 import java.util.Map;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.onap.appc.exceptions.APPCException;
37 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
38
39 public class JsonDgUtilImplTest {
40
41     private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = ThreadLocal
42             .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
43     private JsonDgUtilImpl jsonDgUtil;
44
45     @Before
46     public void setUp() {
47         jsonDgUtil = new JsonDgUtilImpl();
48     }
49
50     @Test
51     public void testFlatAndAddToContext() throws Exception {
52         String key = "payload";
53         String testValueKey = "test-key";
54         String testValueValue = "test-value";
55         String testValueKey2 = "test-key2";
56         String testValueValue2 = "test-value2";
57         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\"" + testValueKey2 + "\": \""
58                 + testValueValue2 + "\"}";
59
60         SvcLogicContext ctx = new SvcLogicContext();
61         Map<String, String> params = new HashMap<>();
62         params.put(key, payload);
63         jsonDgUtil.flatAndAddToContext(params, ctx);
64
65         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
66         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
67
68     }
69
70     @Test
71     public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
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 + "\": \""
78                 + testValueValue2 + "\"}";
79
80         SvcLogicContext ctx = new SvcLogicContext();
81         Map<String, String> params = new HashMap<>();
82         params.put(key, payload);
83         try {
84             jsonDgUtil.flatAndAddToContext(params, ctx);
85
86         } catch (APPCException e) {
87             Assert.assertNull(ctx.getAttribute(testValueKey));
88             Assert.assertNull(ctx.getAttribute(testValueKey2));
89             Assert.assertNotNull(ctx.getAttribute("error-message"));
90         }
91
92     }
93
94     @Test
95     public void testFlatAndAddToContextPayloadFromContext() throws Exception {
96         String key = "payload";
97         String testValueKey = "test-key";
98         String testValueValue = "test-value";
99         String testValueKey2 = "test-key2";
100         String testValueValue2 = "test-value2";
101         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\"" + testValueKey2 + "\": \""
102                 + 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         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
111         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
112     }
113
114     @Test
115     public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
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         Assert.assertNull(ctx.getAttribute(testValueKey));
123         Assert.assertNull(ctx.getAttribute(testValueKey2));
124     }
125
126     @Test
127     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
128
129         String key = "payload";
130         String testValueKey = "test-key";
131         String testValueKey2 = "test-key2";
132
133         SvcLogicContext ctx = new SvcLogicContext();
134         Map<String, String> params = new HashMap<>();
135         params.put(key, "");
136         jsonDgUtil.flatAndAddToContext(params, ctx);
137
138         Assert.assertNull(ctx.getAttribute(testValueKey));
139         Assert.assertNull(ctx.getAttribute(testValueKey2));
140     }
141
142     @Test
143     public void testGenerateOutputPayloadFromContext() throws Exception {
144
145         String key = "output.payload";
146         String key1 = "output.payload.test-key[0]";
147         String key2 = "output.payload.test-key[1]";
148         String testValueKey1 = "value1";
149         String testValueKey2 = "value2";
150
151         String key3 = "output.payload.test-key3";
152         String testValueKey3 = "value3";
153
154         SvcLogicContext ctx = new SvcLogicContext();
155         Map<String, String> params = new HashMap<>();
156         ctx.setAttribute(key1, testValueKey1);
157         ctx.setAttribute(key2, testValueKey2);
158         ctx.setAttribute(key3, testValueKey3);
159         jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
160
161         Assert.assertNotNull(ctx.getAttribute(key));
162
163     }
164
165     @Test
166     public void testCvaasFileNameAndFileContentToContext() throws Exception {
167
168         String key1 = "running-config.upload-date";
169         String testValueKey1 = "2004-02-09 00:00:00:000";
170         Long epochUploadTimestamp = DATE_TIME_PARSER_THREAD_LOCAL.get().parse(testValueKey1).getTime();
171         SvcLogicContext ctx = new SvcLogicContext();
172         Map<String, String> params = new HashMap<>();
173         ctx.setAttribute(key1, testValueKey1);
174         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
175         assertNotNull(ctx.getAttribute("cvaas-file-content"));
176         assertTrue(ctx.getAttribute("cvaas-file-content").contains(epochUploadTimestamp.toString()));
177     }
178
179     @Test(expected = APPCException.class)
180     public void testCvaasFileNameAndFileContentToContextForEmptyParams() throws Exception {
181         SvcLogicContext ctx = new SvcLogicContext();
182         Map<String, String> params = new HashMap<>();
183         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
184     }
185
186     @Test(expected = APPCException.class)
187     public void testCheckFileCreated() throws APPCException {
188         SvcLogicContext ctx = new SvcLogicContext();
189         ctx.setAttribute("cvaas-file-name", "testCvaasFile");
190         Map<String, String> params = new HashMap<>();
191         params.put("vnf-id", "testVnfId");
192         jsonDgUtil.checkFileCreated(params, ctx);
193     }
194 }