2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
 
   7  * ================================================================================
 
   8  * Modifications Copyright (C) 2018 IBM.
 
   9  * ================================================================================
 
  10  * Licensed under the Apache License, Version 2.0 (the "License");
 
  11  * you may not use this file except in compliance with the License.
 
  12  * You may obtain a copy of the License at
 
  14  *      http://www.apache.org/licenses/LICENSE-2.0
 
  16  * Unless required by applicable law or agreed to in writing, software
 
  17  * distributed under the License is distributed on an "AS IS" BASIS,
 
  18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  19  * See the License for the specific language governing permissions and
 
  20  * limitations under the License.
 
  21  * ============LICENSE_END=========================================================
 
  24 package org.onap.ccsdk.sli.core.slipluginutils;
 
  26 import static org.junit.Assert.assertEquals;
 
  27 import static org.junit.Assert.assertNull;
 
  28 import static org.junit.Assert.assertTrue;
 
  30 import java.util.HashMap;
 
  33 import org.junit.Test;
 
  34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  35 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  36 import org.onap.ccsdk.sli.core.slipluginutils.SliPluginUtils.LogLevel;
 
  37 import org.slf4j.Logger;
 
  38 import org.slf4j.LoggerFactory;
 
  39 import org.slf4j.Marker;
 
  41 import com.google.gson.JsonObject;
 
  43 public class CheckParametersTest {
 
  46     public void nullRequiredParameters() throws Exception {
 
  47         Map<String, String> parametersMap = new HashMap<String, String>();
 
  48         String[] requiredParams = null;
 
  49         Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
 
  50         SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
 
  53     @Test(expected = SvcLogicException.class)
 
  54     public void emptyParametersMap() throws Exception {
 
  55         Map<String, String> parametersMap = new HashMap<String, String>();
 
  56         String[] requiredParams = new String[] { "param1", "param2", "param3" };
 
  57         Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
 
  58         SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
 
  61     @Test(expected = SvcLogicException.class)
 
  62     public void paramNotFound() throws Exception {
 
  63         Map<String, String> parametersMap = new HashMap<String, String>();
 
  64         parametersMap.put("tst", "me");
 
  65         String[] requiredParams = new String[] { "param1", "parm2", "param3" };
 
  66         Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
 
  67         SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
 
  71     public void testSunnyRequiredParameters() throws Exception {
 
  72         SvcLogicContext ctx = new SvcLogicContext();
 
  73         ctx.setAttribute("param1", "hello");
 
  74         ctx.setAttribute("param2", "world");
 
  75         ctx.setAttribute("param3", "!");
 
  77         Map<String, String> parameters = new HashMap<String, String>();
 
  78         parameters.put("param1", "dog");
 
  79         parameters.put("param2", "cat");
 
  80         parameters.put("param3", "fish");
 
  82         SliPluginUtils.requiredParameters(parameters, ctx);
 
  86     public void testSunnyRequiredParametersWithPrefix() throws Exception {
 
  87         String prefixValue = "my.unique.path.";
 
  88         SvcLogicContext ctx = new SvcLogicContext();
 
  89         ctx.setAttribute(prefixValue + "param1", "hello");
 
  90         ctx.setAttribute(prefixValue + "param2", "world");
 
  91         ctx.setAttribute(prefixValue + "param3", "!");
 
  93         Map<String, String> parameters = new HashMap<String, String>();
 
  94         parameters.put("prefix", prefixValue);
 
  95         parameters.put("param1", "dog");
 
  96         parameters.put("param2", "cat");
 
  97         parameters.put("param3", "fish");
 
  99         SliPluginUtils.requiredParameters(parameters, ctx);
 
 102     @Test(expected = SvcLogicException.class)
 
 103     public void testRainyMissingRequiredParameters() throws Exception {
 
 104         SvcLogicContext ctx = new SvcLogicContext();
 
 105         ctx.setAttribute("param1", "hello");
 
 106         ctx.setAttribute("param3", "!");
 
 108         Map<String, String> parameters = new HashMap<String, String>();
 
 109         parameters.put("param1", null);
 
 110         parameters.put("param2", null);
 
 111         parameters.put("param3", null);
 
 113         SliPluginUtils.requiredParameters(parameters, ctx);
 
 116     @Test(expected = SvcLogicException.class)
 
 117     public void testEmptyRequiredParameters() throws Exception {
 
 118         SvcLogicContext ctx = new SvcLogicContext();
 
 119         ctx.setAttribute("param1", "hello");
 
 120         ctx.setAttribute("param3", "!");
 
 122         Map<String, String> parameters = new HashMap<String, String>();
 
 124         SliPluginUtils.requiredParameters(parameters, ctx);
 
 127     @Test(expected = SvcLogicException.class)
 
 128     public void testJsonStringToCtx() throws Exception {
 
 129         SvcLogicContext ctx = new SvcLogicContext();
 
 130         Map<String, String> parameters = new HashMap<String, String>();
 
 131         parameters.put("outputPath", "testPath");
 
 132         parameters.put("isEscaped", "true");
 
 133         parameters.put("source", "//{/name1/:value1/}//");
 
 134         SliPluginUtils.jsonStringToCtx(parameters, ctx);
 
 138     public void testGetAttributeValue() throws Exception {
 
 139         SvcLogicContext ctx = new SvcLogicContext();
 
 140         Map<String, String> parameters = new HashMap<String, String>();
 
 141         parameters.put("outputPath", "testPath");
 
 142         parameters.put("source", "testSource");
 
 143         SliPluginUtils.getAttributeValue(parameters, ctx);
 
 144         assertNull(ctx.getAttribute(parameters.get("outputPath")));
 
 148     public void testCtxListContains() throws Exception {
 
 149         SvcLogicContext ctx = new SvcLogicContext();
 
 150         Map<String, String> parameters = new HashMap<String, String>();
 
 151         parameters.put("list", "10_length");
 
 152         parameters.put("keyName", "testName");
 
 153         parameters.put("keyValue", "testValue");
 
 154         ctx.setAttribute("10_length", "10");
 
 155         assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx));
 
 159     @Test(expected= SvcLogicException.class)
 
 160     public void testPrintContextForNullParameters() throws SvcLogicException
 
 162         SvcLogicContext ctx = new SvcLogicContext();
 
 163         Map<String, String> parameters = new HashMap<String, String>();
 
 164         SliPluginUtils.printContext(parameters, ctx);
 
 168     public void testPrintContext() throws SvcLogicException
 
 170         SvcLogicContext ctx = new SvcLogicContext();
 
 171         Map<String, String> parameters = new HashMap<String, String>();
 
 172         parameters.put("filename","testFileName");
 
 173         SliPluginUtils.printContext(parameters, ctx);
 
 177     public void testWriteJsonObject() throws SvcLogicException
 
 179         JsonObject obj=new JsonObject();
 
 180         obj.addProperty("name","testName");
 
 181         obj.addProperty("age",27);
 
 182         obj.addProperty("salary",600000);
 
 183         SvcLogicContext ctx = new SvcLogicContext();
 
 184         SliPluginUtils.writeJsonObject(obj, ctx,"root");
 
 185         assertEquals("testName", ctx.getAttribute("root.name"));
 
 186         assertEquals("27", ctx.getAttribute("root.age"));
 
 187         assertEquals("600000", ctx.getAttribute("root.salary"));
 
 191     public void testCtxKeyEmpty()
 
 193         SvcLogicContext ctx = new SvcLogicContext();
 
 194         ctx.setAttribute("key", "");
 
 195         assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
 
 199     public void testGetArrayLength()
 
 201         SvcLogicContext ctx = new SvcLogicContext();
 
 202         ctx.setAttribute("key_length", "test");
 
 203         Logger log = LoggerFactory.getLogger(getClass());
 
 204         SliPluginUtils.getArrayLength(ctx, "key", log , LogLevel.INFO, "invalid input");
 
 208     public void testSetPropertiesForRoot()
 
 210         SvcLogicContext ctx = new SvcLogicContext();
 
 211         Map<String, String> parameters= new HashMap<>();
 
 212         parameters.put("root","RootVal");
 
 213         parameters.put("valueRoot", "ValueRootVal");
 
 214         assertEquals("success",SliPluginUtils.setPropertiesForRoot(parameters,ctx));