Refactor features to separate installer
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / onap / ccsdk / sli / core / slipluginutils / CheckParametersTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                         reserved.
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
13  * 
14  *      http://www.apache.org/licenses/LICENSE-2.0
15  * 
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=========================================================
22  */ 
23
24 package org.onap.ccsdk.sli.core.slipluginutils;
25
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
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;
40
41 import com.google.gson.JsonObject;
42
43 public class CheckParametersTest {
44
45     @Test
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);
51     }
52
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);
59     }
60
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);
68     }
69
70     @Test
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", "!");
76
77         Map<String, String> parameters = new HashMap<String, String>();
78         parameters.put("param1", "dog");
79         parameters.put("param2", "cat");
80         parameters.put("param3", "fish");
81
82         SliPluginUtils.requiredParameters(parameters, ctx);
83     }
84
85     @Test
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", "!");
92
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");
98
99         SliPluginUtils.requiredParameters(parameters, ctx);
100     }
101
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", "!");
107
108         Map<String, String> parameters = new HashMap<String, String>();
109         parameters.put("param1", null);
110         parameters.put("param2", null);
111         parameters.put("param3", null);
112
113         SliPluginUtils.requiredParameters(parameters, ctx);
114     }
115
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", "!");
121
122         Map<String, String> parameters = new HashMap<String, String>();
123
124         SliPluginUtils.requiredParameters(parameters, ctx);
125     }
126
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);
135     }
136
137     @Test
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")));
145     }
146
147     @Test
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));
156
157     }
158     
159     @Test(expected= SvcLogicException.class)
160     public void testPrintContextForNullParameters() throws SvcLogicException
161     {
162         SvcLogicContext ctx = new SvcLogicContext();
163         Map<String, String> parameters = new HashMap<String, String>();
164         SliPluginUtils.printContext(parameters, ctx);
165     }
166     
167     @Test
168     public void testPrintContext() throws SvcLogicException
169     {
170         SvcLogicContext ctx = new SvcLogicContext();
171         Map<String, String> parameters = new HashMap<String, String>();
172         parameters.put("filename","testFileName");
173         SliPluginUtils.printContext(parameters, ctx);
174     }
175     
176     @Test
177     public void testWriteJsonObject() throws SvcLogicException
178     {
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"));
188     }
189     
190     @Test
191     public void testCtxKeyEmpty()
192     {
193         SvcLogicContext ctx = new SvcLogicContext();
194         ctx.setAttribute("key", "");
195         assertTrue(SliPluginUtils.ctxKeyEmpty(ctx, "key"));
196     }
197     
198     @Test
199     public void testGetArrayLength()
200     {
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");
205     }
206     
207     @Test
208     public void testSetPropertiesForRoot()
209     {
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));
215     }
216 }