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;
 
  30 import java.text.SimpleDateFormat;
 
  31 import java.util.HashMap;
 
  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;
 
  39 public class JsonDgUtilImplTest {
 
  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;
 
  47         jsonDgUtil = new JsonDgUtilImpl();
 
  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 + "\"}";
 
  60         SvcLogicContext ctx = new SvcLogicContext();
 
  61         Map<String, String> params = new HashMap<>();
 
  62         params.put(key, payload);
 
  63         jsonDgUtil.flatAndAddToContext(params, ctx);
 
  65         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
 
  66         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
 
  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 + "\"}";
 
  80         SvcLogicContext ctx = new SvcLogicContext();
 
  81         Map<String, String> params = new HashMap<>();
 
  82         params.put(key, payload);
 
  84             jsonDgUtil.flatAndAddToContext(params, ctx);
 
  86         } catch (APPCException e) {
 
  87             Assert.assertNull(ctx.getAttribute(testValueKey));
 
  88             Assert.assertNull(ctx.getAttribute(testValueKey2));
 
  89             Assert.assertNotNull(ctx.getAttribute("error-message"));
 
  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 + "\"}";
 
 104         SvcLogicContext ctx = new SvcLogicContext();
 
 105         Map<String, String> params = new HashMap<>();
 
 107         ctx.setAttribute("input.payload", payload);
 
 108         jsonDgUtil.flatAndAddToContext(params, ctx);
 
 110         Assert.assertEquals(ctx.getAttribute(testValueKey), testValueValue);
 
 111         Assert.assertEquals(ctx.getAttribute(testValueKey2), testValueValue2);
 
 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);
 
 122         Assert.assertNull(ctx.getAttribute(testValueKey));
 
 123         Assert.assertNull(ctx.getAttribute(testValueKey2));
 
 127     public void testFlatAndAddToContextNegativeEmptyPayload() throws Exception {
 
 129         String key = "payload";
 
 130         String testValueKey = "test-key";
 
 131         String testValueKey2 = "test-key2";
 
 133         SvcLogicContext ctx = new SvcLogicContext();
 
 134         Map<String, String> params = new HashMap<>();
 
 136         jsonDgUtil.flatAndAddToContext(params, ctx);
 
 138         Assert.assertNull(ctx.getAttribute(testValueKey));
 
 139         Assert.assertNull(ctx.getAttribute(testValueKey2));
 
 143     public void testGenerateOutputPayloadFromContext() throws Exception {
 
 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";
 
 151         String key3 = "output.payload.test-key3";
 
 152         String testValueKey3 = "value3";
 
 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);
 
 161         Assert.assertNotNull(ctx.getAttribute(key));
 
 166     public void testCvaasFileNameAndFileContentToContext() throws Exception {
 
 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()));
 
 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);
 
 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);