2 * ============LICENSE_START=======================================================
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
15 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 * ============LICENSE_END=========================================================
26 package org.onap.appc.dg.common.impl;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.text.SimpleDateFormat;
32 import java.util.HashMap;
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;
41 public class JsonDgUtilImplTest {
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;
49 jsonDgUtil = new JsonDgUtilImpl();
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 + "\"}";
62 SvcLogicContext ctx = new SvcLogicContext();
63 Map<String, String> params = new HashMap<>();
64 params.put(key, payload);
65 jsonDgUtil.flatAndAddToContext(params, ctx);
67 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
68 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
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 + "\"}";
82 SvcLogicContext ctx = new SvcLogicContext();
83 Map<String, String> params = new HashMap<>();
84 params.put(key, payload);
86 jsonDgUtil.flatAndAddToContext(params, ctx);
88 } catch (APPCException e) {
89 Assert.assertNull(ctx.getAttribute(testValueKey));
90 Assert.assertNull(ctx.getAttribute(testValueKey2));
91 Assert.assertNotNull(ctx.getAttribute("error-message"));
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 + "\"}";
106 SvcLogicContext ctx = new SvcLogicContext();
107 Map<String, String> params = new HashMap<>();
109 ctx.setAttribute("input.payload", payload);
110 jsonDgUtil.flatAndAddToContext(params, ctx);
112 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
113 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
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);
124 Assert.assertNull(ctx.getAttribute(testValueKey));
125 Assert.assertNull(ctx.getAttribute(testValueKey2));
129 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
131 String key = "payload";
132 String testValueKey = "test-key";
133 String testValueKey2 = "test-key2";
135 SvcLogicContext ctx = new SvcLogicContext();
136 Map<String, String> params = new HashMap<>();
138 jsonDgUtil.flatAndAddToContext(params, ctx);
140 Assert.assertNull(ctx.getAttribute(testValueKey));
141 Assert.assertNull(ctx.getAttribute(testValueKey2));
145 public void testGenerateOutputPayloadFromContext() throws Exception {
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";
153 String key3 = "output.payload.test-key3";
154 String testValueKey3 = "value3";
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);
163 Assert.assertNotNull(ctx.getAttribute(key));
168 public void testCvaasFileNameAndFileContentToContext() throws Exception {
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()));
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);
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);