2 * ============LICENSE_START=======================================================
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
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.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.common.impl;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import java.text.SimpleDateFormat;
30 import java.util.HashMap;
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;
38 public class JsonDgUtilImplTest {
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;
46 jsonDgUtil = new JsonDgUtilImpl();
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 + "\"}";
59 SvcLogicContext ctx = new SvcLogicContext();
60 Map<String, String> params = new HashMap<>();
61 params.put(key, payload);
62 jsonDgUtil.flatAndAddToContext(params, ctx);
64 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
65 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
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 + "\"}";
79 SvcLogicContext ctx = new SvcLogicContext();
80 Map<String, String> params = new HashMap<>();
81 params.put(key, payload);
83 jsonDgUtil.flatAndAddToContext(params, ctx);
85 } catch (APPCException e) {
86 Assert.assertNull(ctx.getAttribute(testValueKey));
87 Assert.assertNull(ctx.getAttribute(testValueKey2));
88 Assert.assertNotNull(ctx.getAttribute("error-message"));
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 + "\"}";
103 SvcLogicContext ctx = new SvcLogicContext();
104 Map<String, String> params = new HashMap<>();
106 ctx.setAttribute("input.payload", payload);
107 jsonDgUtil.flatAndAddToContext(params, ctx);
109 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
110 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
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);
121 Assert.assertNull(ctx.getAttribute(testValueKey));
122 Assert.assertNull(ctx.getAttribute(testValueKey2));
126 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
128 String key = "payload";
129 String testValueKey = "test-key";
130 String testValueKey2 = "test-key2";
132 SvcLogicContext ctx = new SvcLogicContext();
133 Map<String, String> params = new HashMap<>();
135 jsonDgUtil.flatAndAddToContext(params, ctx);
137 Assert.assertNull(ctx.getAttribute(testValueKey));
138 Assert.assertNull(ctx.getAttribute(testValueKey2));
142 public void testGenerateOutputPayloadFromContext() throws Exception {
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";
150 String key3 = "output.payload.test-key3";
151 String testValueKey3 = "value3";
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);
160 Assert.assertNotNull(ctx.getAttribute(key));
165 public void testCvaasFileNameAndFileContentToContext() throws Exception {
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()));
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);
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);