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 ch.qos.logback.core.Appender;
29 import java.text.SimpleDateFormat;
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;
41 import static org.mockito.Mockito.mock;
43 public class JsonDgUtilImplTest {
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"));
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+"\"}";
59 SvcLogicContext ctx = new SvcLogicContext();
60 Map<String, String> params = new HashMap<>();
61 params.put(key, payload);
62 jsonDgUtil.flatAndAddToContext(params, ctx);
65 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
66 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
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+"\"}";
83 SvcLogicContext ctx = new SvcLogicContext();
84 Map<String, String> params = new HashMap<>();
85 params.put(key, payload);
87 jsonDgUtil.flatAndAddToContext(params, ctx);
89 } catch (APPCException e) {
90 Assert.assertNull(ctx.getAttribute(testValueKey));
91 Assert.assertNull(ctx.getAttribute(testValueKey2));
92 Assert.assertNotNull(ctx.getAttribute("error-message"));
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+"\"}";
109 SvcLogicContext ctx = new SvcLogicContext();
110 Map<String, String> params = new HashMap<>();
112 ctx.setAttribute("input.payload", payload);
113 jsonDgUtil.flatAndAddToContext(params, ctx);
116 Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
117 Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
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);
130 Assert.assertNull(ctx.getAttribute(testValueKey));
131 Assert.assertNull(ctx.getAttribute(testValueKey2));
136 public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
138 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
139 String key = "payload";
140 String testValueKey = "test-key";
141 String testValueKey2 = "test-key2";
143 SvcLogicContext ctx = new SvcLogicContext();
144 Map<String, String> params = new HashMap<>();
146 jsonDgUtil.flatAndAddToContext(params, ctx);
148 Assert.assertNull(ctx.getAttribute(testValueKey));
149 Assert.assertNull(ctx.getAttribute(testValueKey2));
154 public void testGenerateOutputPayloadFromContext() throws Exception {
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";
163 String key3 = "output.payload.test-key3";
164 String testValueKey3 = "value3";
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);
173 Assert.assertNotNull(ctx.getAttribute(key));
178 public void testCvaasFileNameAndFileContentToContext() throws Exception {
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()));
192 @Test(expected=APPCException.class)
193 public void testCheckFileCreated() throws APPCException
195 SvcLogicContext ctx= new SvcLogicContext();
196 ctx.setAttribute("cvaas-file-name", "testCvaasFile");
197 JsonDgUtilImpl jsonDgUtil = new JsonDgUtilImpl();
198 Map<String, String> params = new HashMap<>();
199 params.put("vnf-id", "testVnfId");
200 jsonDgUtil.checkFileCreated(params, ctx);