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;
 
  29 import java.util.HashMap;
 
  32 import org.junit.Test;
 
  33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 
  34 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
 
  35 import org.slf4j.Logger;
 
  36 import org.slf4j.LoggerFactory;
 
  38 import com.google.gson.JsonObject;
 
  40 public class CheckParametersTest {
 
  43     public void nullRequiredParameters() throws Exception {
 
  44         Map<String, String> parametersMap = new HashMap<String, String>();
 
  45         String[] requiredParams = null;
 
  46         Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
 
  47         SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
 
  50     @Test(expected = SvcLogicException.class)
 
  51     public void emptyParametersMap() throws Exception {
 
  52         Map<String, String> parametersMap = new HashMap<String, String>();
 
  53         String[] requiredParams = new String[] { "param1", "param2", "param3" };
 
  54         Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
 
  55         SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
 
  58     @Test(expected = SvcLogicException.class)
 
  59     public void paramNotFound() throws Exception {
 
  60         Map<String, String> parametersMap = new HashMap<String, String>();
 
  61         parametersMap.put("tst", "me");
 
  62         String[] requiredParams = new String[] { "param1", "parm2", "param3" };
 
  63         Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
 
  64         SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
 
  68     public void testSunnyRequiredParameters() throws Exception {
 
  69         SvcLogicContext ctx = new SvcLogicContext();
 
  70         ctx.setAttribute("param1", "hello");
 
  71         ctx.setAttribute("param2", "world");
 
  72         ctx.setAttribute("param3", "!");
 
  74         Map<String, String> parameters = new HashMap<String, String>();
 
  75         parameters.put("param1", "dog");
 
  76         parameters.put("param2", "cat");
 
  77         parameters.put("param3", "fish");
 
  79         SliPluginUtils.requiredParameters(parameters, ctx);
 
  83     public void testSunnyRequiredParametersWithPrefix() throws Exception {
 
  84         String prefixValue = "my.unique.path.";
 
  85         SvcLogicContext ctx = new SvcLogicContext();
 
  86         ctx.setAttribute(prefixValue + "param1", "hello");
 
  87         ctx.setAttribute(prefixValue + "param2", "world");
 
  88         ctx.setAttribute(prefixValue + "param3", "!");
 
  90         Map<String, String> parameters = new HashMap<String, String>();
 
  91         parameters.put("prefix", prefixValue);
 
  92         parameters.put("param1", "dog");
 
  93         parameters.put("param2", "cat");
 
  94         parameters.put("param3", "fish");
 
  96         SliPluginUtils.requiredParameters(parameters, ctx);
 
  99     @Test(expected = SvcLogicException.class)
 
 100     public void testRainyMissingRequiredParameters() throws Exception {
 
 101         SvcLogicContext ctx = new SvcLogicContext();
 
 102         ctx.setAttribute("param1", "hello");
 
 103         ctx.setAttribute("param3", "!");
 
 105         Map<String, String> parameters = new HashMap<String, String>();
 
 106         parameters.put("param1", null);
 
 107         parameters.put("param2", null);
 
 108         parameters.put("param3", null);
 
 110         SliPluginUtils.requiredParameters(parameters, ctx);
 
 113     @Test(expected = SvcLogicException.class)
 
 114     public void testEmptyRequiredParameters() throws Exception {
 
 115         SvcLogicContext ctx = new SvcLogicContext();
 
 116         ctx.setAttribute("param1", "hello");
 
 117         ctx.setAttribute("param3", "!");
 
 119         Map<String, String> parameters = new HashMap<String, String>();
 
 121         SliPluginUtils.requiredParameters(parameters, ctx);
 
 124     @Test(expected = SvcLogicException.class)
 
 125     public void testJsonStringToCtx() throws Exception {
 
 126         SvcLogicContext ctx = new SvcLogicContext();
 
 127         Map<String, String> parameters = new HashMap<String, String>();
 
 128         parameters.put("outputPath", "testPath");
 
 129         parameters.put("isEscaped", "true");
 
 130         parameters.put("source", "//{/name1/:value1/}//");
 
 131         SliPluginUtils.jsonStringToCtx(parameters, ctx);
 
 135     public void testGetAttributeValue() throws Exception {
 
 136         SvcLogicContext ctx = new SvcLogicContext();
 
 137         Map<String, String> parameters = new HashMap<String, String>();
 
 138         parameters.put("outputPath", "testPath");
 
 139         parameters.put("source", "testSource");
 
 140         SliPluginUtils.getAttributeValue(parameters, ctx);
 
 141         assertNull(ctx.getAttribute(parameters.get("outputPath")));
 
 145     public void testCtxListContains() throws Exception {
 
 146         SvcLogicContext ctx = new SvcLogicContext();
 
 147         Map<String, String> parameters = new HashMap<String, String>();
 
 148         parameters.put("list", "10_length");
 
 149         parameters.put("keyName", "testName");
 
 150         parameters.put("keyValue", "testValue");
 
 151         ctx.setAttribute("10_length", "10");
 
 152         assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx));
 
 156     @Test(expected= SvcLogicException.class)
 
 157     public void testPrintContextForNullParameters() throws SvcLogicException
 
 159         SvcLogicContext ctx = new SvcLogicContext();
 
 160         Map<String, String> parameters = new HashMap<String, String>();
 
 161         SliPluginUtils.printContext(parameters, ctx);
 
 165     public void testPrintContext() throws SvcLogicException
 
 167         SvcLogicContext ctx = new SvcLogicContext();
 
 168         Map<String, String> parameters = new HashMap<String, String>();
 
 169         parameters.put("filename","testFileName");
 
 170         SliPluginUtils.printContext(parameters, ctx);
 
 174     public void testWriteJsonObject() throws SvcLogicException
 
 176         JsonObject obj=new JsonObject();
 
 177         obj.addProperty("name","testName");
 
 178         obj.addProperty("age",27);
 
 179         obj.addProperty("salary",600000);
 
 180         SvcLogicContext ctx = new SvcLogicContext();
 
 181         SliPluginUtils.writeJsonObject(obj, ctx,"root");
 
 182         assertEquals("testName", ctx.getAttribute("root.name"));
 
 183         assertEquals("27", ctx.getAttribute("root.age"));
 
 184         assertEquals("600000", ctx.getAttribute("root.salary"));