added test case to JsonDgutilImplTest.java
[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 ch.qos.logback.core.Appender;
29 import java.text.SimpleDateFormat;
30
31 import org.junit.Assert;
32 import org.junit.Test;
33 import org.onap.appc.dg.common.impl.JsonDgUtilImpl;
34 import org.onap.appc.exceptions.APPCException;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import static org.junit.Assert.assertNotNull;
37 import static org.junit.Assert.assertTrue;
38 import java.util.HashMap;
39 import java.util.Map;
40
41 import static org.mockito.Mockito.mock;
42
43 public class JsonDgUtilImplTest {
44
45     private final Appender appender = mock(Appender.class);
46     private static final ThreadLocal<SimpleDateFormat> DATE_TIME_PARSER_THREAD_LOCAL = ThreadLocal
47             .withInitial(() -> new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
48
49     @Test
50     public void testFlatAndAddToContext() throws Exception {
51         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
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+"\": \""+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
65             Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
66             Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
67
68
69
70     }
71
72
73     @Test
74     public void testFlatAndAddToContextNegativeWrongPayload() throws Exception {
75         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
76         String key = "payload";
77         String testValueKey = "test-key";
78         String testValueValue = "test-value";
79         String testValueKey2 = "test-key2";
80         String testValueValue2 = "test-value2";
81         String payload = "{{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
82
83         SvcLogicContext ctx = new SvcLogicContext();
84         Map<String, String> params = new HashMap<>();
85         params.put(key, payload);
86         try {
87             jsonDgUtil.flatAndAddToContext(params, ctx);
88
89         } catch (APPCException e) {
90             Assert.assertNull(ctx.getAttribute(testValueKey));
91             Assert.assertNull(ctx.getAttribute(testValueKey2));
92             Assert.assertNotNull(ctx.getAttribute("error-message"));
93         }
94
95
96     }
97
98
99     @Test
100     public void testFlatAndAddToContextPayloadFromContext() throws Exception {
101         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
102         String key = "payload";
103         String testValueKey = "test-key";
104         String testValueValue = "test-value";
105         String testValueKey2 = "test-key2";
106         String testValueValue2 = "test-value2";
107         String payload = "{\"" + testValueKey + "\": \"" + testValueValue + "\",\""+testValueKey2+"\": \""+testValueValue2+"\"}";
108
109         SvcLogicContext ctx = new SvcLogicContext();
110         Map<String, String> params = new HashMap<>();
111         params.put(key, "");
112         ctx.setAttribute("input.payload", payload);
113         jsonDgUtil.flatAndAddToContext(params, ctx);
114
115
116         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
117         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
118     }
119
120     @Test
121     public void testFlatAndAddToContextNegativeNullPayload() throws Exception {
122         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
123         String testValueKey = "test-key";
124         String testValueKey2 = "test-key2";
125         SvcLogicContext ctx = new SvcLogicContext();
126         Map<String, String> params = new HashMap<>();
127         jsonDgUtil.flatAndAddToContext(params, ctx);
128
129
130         Assert.assertNull(ctx.getAttribute(testValueKey));
131         Assert.assertNull(ctx.getAttribute(testValueKey2));
132     }
133
134
135     @Test
136     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
137
138         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
139         String key = "payload";
140         String testValueKey = "test-key";
141         String testValueKey2 = "test-key2";
142
143         SvcLogicContext ctx = new SvcLogicContext();
144         Map<String, String> params = new HashMap<>();
145         params.put(key, "");
146         jsonDgUtil.flatAndAddToContext(params, ctx);
147
148         Assert.assertNull(ctx.getAttribute(testValueKey));
149         Assert.assertNull(ctx.getAttribute(testValueKey2));
150     }
151
152
153     @Test
154     public void testGenerateOutputPayloadFromContext() throws Exception {
155
156         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
157         String key = "output.payload";
158         String key1 = "output.payload.test-key[0]";
159         String key2 = "output.payload.test-key[1]";
160         String testValueKey1 = "value1";
161         String testValueKey2 = "value2";
162
163         String key3 = "output.payload.test-key3";
164         String testValueKey3 = "value3";
165
166         SvcLogicContext ctx = new SvcLogicContext();
167         Map<String, String> params = new HashMap<>();
168         ctx.setAttribute(key1, testValueKey1);
169         ctx.setAttribute(key2, testValueKey2);
170         ctx.setAttribute(key3, testValueKey3);
171         jsonDgUtil.generateOutputPayloadFromContext(params, ctx);
172
173         Assert.assertNotNull(ctx.getAttribute(key));
174
175     }
176     
177     @Test
178     public void testCvaasFileNameAndFileContentToContext() throws Exception {
179
180         JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
181         String key1 = "running-config.upload-date";
182         String testValueKey1 = "2004-02-09 00:00:00:000";
183         Long epochUploadTimestamp = DATE_TIME_PARSER_THREAD_LOCAL.get().parse(testValueKey1).getTime();
184         SvcLogicContext ctx = new SvcLogicContext();
185         Map<String, String> params = new HashMap<>();
186         ctx.setAttribute(key1, testValueKey1);
187         jsonDgUtil.cvaasFileNameAndFileContentToContext(params, ctx);
188         assertNotNull(ctx.getAttribute("cvaas-file-content"));
189         assertTrue(ctx.getAttribute("cvaas-file-content").contains(epochUploadTimestamp.toString()));
190     }
191 }