266603d1d45bce0eba703602a25b0ad267b11cbb
[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 public class CheckParametersTest {
39
40         @Test
41         public void nullRequiredParameters() throws Exception {
42                 Map<String, String> parametersMap = new HashMap<String, String>();
43                 String[] requiredParams = null;
44                 Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
45                 SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
46         }
47
48         @Test(expected = SvcLogicException.class)
49         public void emptyParametersMap() throws Exception {
50                 Map<String, String> parametersMap = new HashMap<String, String>();
51                 String[] requiredParams = new String[] { "param1", "param2", "param3" };
52                 Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
53                 SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
54         }
55
56         @Test(expected = SvcLogicException.class)
57         public void paramNotFound() throws Exception {
58                 Map<String, String> parametersMap = new HashMap<String, String>();
59                 parametersMap.put("tst", "me");
60                 String[] requiredParams = new String[] { "param1", "parm2", "param3" };
61                 Logger Log = LoggerFactory.getLogger(SliPluginUtils.class);
62                 SliPluginUtils.checkParameters(parametersMap, requiredParams, Log);
63         }
64
65         @Test
66         public void testSunnyRequiredParameters() throws Exception {
67                 SvcLogicContext ctx = new SvcLogicContext();
68                 ctx.setAttribute("param1", "hello");
69                 ctx.setAttribute("param2", "world");
70                 ctx.setAttribute("param3", "!");
71
72                 Map<String, String> parameters = new HashMap<String, String>();
73                 parameters.put("param1", "dog");
74                 parameters.put("param2", "cat");
75                 parameters.put("param3", "fish");
76
77                 SliPluginUtils.requiredParameters(parameters, ctx);
78         }
79
80         @Test
81         public void testSunnyRequiredParametersWithPrefix() throws Exception {
82                 String prefixValue = "my.unique.path.";
83                 SvcLogicContext ctx = new SvcLogicContext();
84                 ctx.setAttribute(prefixValue + "param1", "hello");
85                 ctx.setAttribute(prefixValue + "param2", "world");
86                 ctx.setAttribute(prefixValue + "param3", "!");
87
88                 Map<String, String> parameters = new HashMap<String, String>();
89                 parameters.put("prefix", prefixValue);
90                 parameters.put("param1", "dog");
91                 parameters.put("param2", "cat");
92                 parameters.put("param3", "fish");
93
94                 SliPluginUtils.requiredParameters(parameters, ctx);
95         }
96
97         @Test(expected = SvcLogicException.class)
98         public void testRainyMissingRequiredParameters() throws Exception {
99                 SvcLogicContext ctx = new SvcLogicContext();
100                 ctx.setAttribute("param1", "hello");
101                 ctx.setAttribute("param3", "!");
102
103                 Map<String, String> parameters = new HashMap<String, String>();
104                 parameters.put("param1", null);
105                 parameters.put("param2", null);
106                 parameters.put("param3", null);
107
108                 SliPluginUtils.requiredParameters(parameters, ctx);
109         }
110
111         @Test(expected = SvcLogicException.class)
112         public void testEmptyRequiredParameters() throws Exception {
113                 SvcLogicContext ctx = new SvcLogicContext();
114                 ctx.setAttribute("param1", "hello");
115                 ctx.setAttribute("param3", "!");
116
117                 Map<String, String> parameters = new HashMap<String, String>();
118
119                 SliPluginUtils.requiredParameters(parameters, ctx);
120         }
121
122         @Test(expected = SvcLogicException.class)
123         public void testJsonStringToCtx() throws Exception {
124                 SvcLogicContext ctx = new SvcLogicContext();
125                 Map<String, String> parameters = new HashMap<String, String>();
126                 parameters.put("outputPath", "testPath");
127                 parameters.put("isEscaped", "true");
128                 parameters.put("source", "//{/name1/:value1/}//");
129                 SliPluginUtils.jsonStringToCtx(parameters, ctx);
130         }
131
132         @Test
133         public void testGetAttributeValue() throws Exception {
134                 SvcLogicContext ctx = new SvcLogicContext();
135                 Map<String, String> parameters = new HashMap<String, String>();
136                 parameters.put("outputPath", "testPath");
137                 parameters.put("source", "testSource");
138                 SliPluginUtils.getAttributeValue(parameters, ctx);
139                 assertNull(ctx.getAttribute(parameters.get("outputPath")));
140         }
141
142         @Test
143         public void testCtxListContains() throws Exception {
144                 SvcLogicContext ctx = new SvcLogicContext();
145                 Map<String, String> parameters = new HashMap<String, String>();
146                 parameters.put("list", "10_length");
147                 parameters.put("keyName", "testName");
148                 parameters.put("keyValue", "testValue");
149                 ctx.setAttribute("10_length", "10");
150                 assertEquals("false", SliPluginUtils.ctxListContains(parameters, ctx));
151
152         }
153 }