a7cc1bde54818131de627822d20fab5e8d8462af
[ccsdk/sli.git] /
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
29 import java.util.HashMap;
30 import java.util.Map;
31
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;
37
38 import com.google.gson.JsonObject;
39
40 public class CheckParametersTest {
41
42     @Test
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);
48     }
49
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);
56     }
57
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);
65     }
66
67     @Test
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", "!");
73
74         Map<String, String> parameters = new HashMap<String, String>();
75         parameters.put("param1", "dog");
76         parameters.put("param2", "cat");
77         parameters.put("param3", "fish");
78
79         SliPluginUtils.requiredParameters(parameters, ctx);
80     }
81
82     @Test
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", "!");
89
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");
95
96         SliPluginUtils.requiredParameters(parameters, ctx);
97     }
98
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", "!");
104
105         Map<String, String> parameters = new HashMap<String, String>();
106         parameters.put("param1", null);
107         parameters.put("param2", null);
108         parameters.put("param3", null);
109
110         SliPluginUtils.requiredParameters(parameters, ctx);
111     }
112
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", "!");
118
119         Map<String, String> parameters = new HashMap<String, String>();
120
121         SliPluginUtils.requiredParameters(parameters, ctx);
122     }
123
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);
132     }
133
134     @Test
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")));
142     }
143
144     @Test
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));
153
154     }
155     
156     @Test(expected= SvcLogicException.class)
157     public void testPrintContextForNullParameters() throws SvcLogicException
158     {
159         SvcLogicContext ctx = new SvcLogicContext();
160         Map<String, String> parameters = new HashMap<String, String>();
161         SliPluginUtils.printContext(parameters, ctx);
162     }
163     
164     @Test
165     public void testPrintContext() throws SvcLogicException
166     {
167         SvcLogicContext ctx = new SvcLogicContext();
168         Map<String, String> parameters = new HashMap<String, String>();
169         parameters.put("filename","testFileName");
170         SliPluginUtils.printContext(parameters, ctx);
171     }
172     
173     @Test
174     public void testWriteJsonObject() throws SvcLogicException
175     {
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"));
185     }
186 }