08adc973155f276d5d296e5e64ec97d0feee01e0
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / onap / ccsdk / sli / core / slipluginutils / SliPluginUtils_StaticFunctionsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.ccsdk.sli.core.slipluginutils;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import java.util.HashMap;
28 import java.util.Map;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
32 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class SliPluginUtils_StaticFunctionsTest {
37     private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctionsTest.class);
38     SliPluginUtils utils = new SliPluginUtils();
39     private SvcLogicContext ctx;
40     private HashMap<String, String> parameters;
41
42     @Before
43     public void setUp() throws Exception {
44         this.ctx = new SvcLogicContext();
45         parameters = new HashMap<String, String>();
46     }
47
48     // TODO: javadoc
49     @Test
50     public final void testCtxGetBeginsWith() {
51         ctx.setAttribute("service-data.oper-status.order-status", "InProgress");
52         ctx.setAttribute("service-data.service-information.service-instance-id", "my-instance");
53         ctx.setAttribute("service-data.service-information.service-type", "my-service");
54
55         Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
56
57         assertEquals("my-instance", entries.get("service-data.service-information.service-instance-id"));
58         assertEquals("my-service", entries.get("service-data.service-information.service-type"));
59         assertFalse(entries.containsKey("service-data.oper-status.order-status"));
60     }
61
62     // TODO: javadoc
63     @Test
64     public final void testCtxListRemove_index() throws SvcLogicException {
65         LOG.trace("=== testCtxListRemove_index ===");
66         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
67         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
68         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
69         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
70         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
71         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
72         ctx.setAttribute("service-data.vnf-l3_length", "3");
73
74         parameters.put("index", "1");
75         parameters.put("list_pfx", "service-data.vnf-l3");
76
77         utils.ctxListRemove(parameters, ctx);
78         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
79
80         assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
81         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
82         assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
83         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
84         assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
85     }
86
87     // TODO: javadoc
88     @Test
89     public final void textCtxListRemove_keyValue() throws SvcLogicException {
90         LOG.trace("=== textCtxListRemove_keyValue ===");
91         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
92         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
93         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
94         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
95         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
96         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
97         // 2nd entry
98         ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
99         ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
100         ctx.setAttribute("service-data.vnf-l3_length", "4");
101
102         parameters.put("list_pfx", "service-data.vnf-l3");
103         parameters.put("key", "vnf-host-name");
104         parameters.put("value", "vnf-host-name_1");
105
106         utils.ctxListRemove(parameters, ctx);
107         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
108
109         assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
110         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
111         assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
112         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
113         assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
114     }
115
116     // TODO: javadoc
117     @Test
118     public final void textCtxListRemove_keyValue_nullkey() throws SvcLogicException {
119         LOG.trace("=== textCtxListRemove_keyValue_nullkey ===");
120         ctx.setAttribute("service-data.vnf-l3[0]", "vnf-host-name_0");
121         ctx.setAttribute("service-data.vnf-l3[1]", "vnf-host-name_1");
122         ctx.setAttribute("service-data.vnf-l3[2]", "vnf-host-name_2");
123         ctx.setAttribute("service-data.vnf-l3_length", "3");
124
125         parameters.put("list_pfx", "service-data.vnf-l3");
126         parameters.put("value", "vnf-host-name_1");
127
128         utils.ctxListRemove(parameters, ctx);
129         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
130
131         assertEquals("2", ctx.getAttribute("service-data.vnf-l3_length"));
132         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0]"));
133         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1]"));
134     }
135
136     // TODO: javadoc
137     @Test
138     public final void textCtxListRemove_keyValueList() throws SvcLogicException {
139         LOG.trace("=== textCtxListRemove_keyValueList ===");
140         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
141         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
142         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
143         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
144         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
145         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
146         // 2nd entry
147         ctx.setAttribute("service-data.vnf-l3[3].vnf-host-name", "vnf-host-name_1");
148         ctx.setAttribute("service-data.vnf-l3[3].device-host-name", "device-host-name_1");
149         // entries with only 1 of 2 key-value pairs matching
150         ctx.setAttribute("service-data.vnf-l3[4].vnf-host-name", "vnf-host-name_1");
151         ctx.setAttribute("service-data.vnf-l3[4].device-host-name", "device-host-name_4");
152         ctx.setAttribute("service-data.vnf-l3[5].vnf-host-name", "vnf-host-name_5");
153         ctx.setAttribute("service-data.vnf-l3[5].device-host-name", "device-host-name_1");
154         ctx.setAttribute("service-data.vnf-l3_length", "6");
155
156         parameters.put("list_pfx", "service-data.vnf-l3");
157         parameters.put("keys_length", "2");
158         parameters.put("keys[0].key", "vnf-host-name");
159         parameters.put("keys[0].value", "vnf-host-name_1");
160         parameters.put("keys[1].key", "device-host-name");
161         parameters.put("keys[1].value", "device-host-name_1");
162
163         utils.ctxListRemove(parameters, ctx);
164         SliPluginUtils.logContextMemory(ctx, LOG, SliPluginUtils.LogLevel.TRACE);
165
166         assertEquals("4", ctx.getAttribute("service-data.vnf-l3_length"));
167         assertEquals("vnf-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].vnf-host-name"));
168         assertEquals("device-host-name_0", ctx.getAttribute("service-data.vnf-l3[0].device-host-name"));
169         assertEquals("vnf-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].vnf-host-name"));
170         assertEquals("device-host-name_2", ctx.getAttribute("service-data.vnf-l3[1].device-host-name"));
171         assertEquals("vnf-host-name_1", ctx.getAttribute("service-data.vnf-l3[2].vnf-host-name"));
172         assertEquals("device-host-name_4", ctx.getAttribute("service-data.vnf-l3[2].device-host-name"));
173         assertEquals("vnf-host-name_5", ctx.getAttribute("service-data.vnf-l3[3].vnf-host-name"));
174         assertEquals("device-host-name_1", ctx.getAttribute("service-data.vnf-l3[3].device-host-name"));
175     }
176
177     // TODO: javadoc
178     @Test(expected = SvcLogicException.class)
179     public final void testCtxListRemove_nullListLength() throws SvcLogicException {
180         LOG.trace("=== testCtxListRemove_nullListLength ===");
181         ctx.setAttribute("service-data.vnf-l3[0].vnf-host-name", "vnf-host-name_0");
182         ctx.setAttribute("service-data.vnf-l3[0].device-host-name", "device-host-name_0");
183         ctx.setAttribute("service-data.vnf-l3[1].vnf-host-name", "vnf-host-name_1");
184         ctx.setAttribute("service-data.vnf-l3[1].device-host-name", "device-host-name_1");
185         ctx.setAttribute("service-data.vnf-l3[2].vnf-host-name", "vnf-host-name_2");
186         ctx.setAttribute("service-data.vnf-l3[2].device-host-name", "device-host-name_2");
187
188         parameters.put("index", "1");
189         parameters.put("list_pfx", "service-data.vnf-l3");
190
191         utils.ctxListRemove(parameters, ctx);
192     }
193
194     // TODO: javadoc
195     @Test
196     public final void testCtxPutAll() {
197         HashMap<String, Object> entries = new HashMap<String, Object>();
198         entries.put("service-data.oper-status.order-status", "InProgress");
199         entries.put("service-data.service-information.service-instance-id", "my-instance");
200         entries.put("service-data.request-information.order-number", 1234);
201         entries.put("service-data.request-information.request-id", null);
202
203         SliPluginUtils.ctxPutAll(ctx, entries);
204
205         assertEquals("InProgress", ctx.getAttribute("service-data.oper-status.order-status"));
206         assertEquals("my-instance", ctx.getAttribute("service-data.service-information.service-instance-id"));
207         assertEquals("1234", ctx.getAttribute("service-data.request-information.order-number"));
208         assertFalse(ctx.getAttributeKeySet().contains("service-data.request-information.request-id"));
209     }
210
211     // TODO: javadoc
212     @Test
213     public final void testCtxSetAttribute_LOG() {
214         LOG.debug("=== testCtxSetAttribute_LOG ===");
215         Integer i = new Integer(3);
216         SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
217     }
218
219     /*@Test
220     public void printContext() throws SvcLogicException, IOException {
221         String filePath = "/src/test/resources/printContext.txt";
222         parameters.put("filename", filePath);
223         File f = new File(filePath);
224         assert (f.exists());
225         assert (!f.isDirectory());
226         ctx.setAttribute("hello", "world");
227         ctx.setAttribute("name", "value");
228
229         SliPluginUtils.printContext(parameters, ctx);
230         BufferedReader br = new BufferedReader(new FileReader(f));
231         String line = br.readLine();
232         assertEquals("#######################################", line);
233         line = br.readLine();
234         assertEquals("hello = world", line);
235         line = br.readLine();
236         assertEquals("name = value", line);
237         br.close();
238         Files.delete(Paths.get(filePath));
239     }*/
240
241     @Test
242     public void setTime() throws SvcLogicException {
243         String outputPath = "output";
244         parameters.put("outputPath", outputPath);
245         SliPluginUtils.setTime(parameters, ctx);
246         assertNotNull(ctx.getAttribute(outputPath));
247     }
248
249     @Test
250     public void containsKey() throws Exception {
251         ctx = new SvcLogicContext();
252         parameters.put(SliStringUtils.INPUT_PARAM_KEY, "key_does_not_exist");
253         String result = SliPluginUtils.containsKey(parameters, ctx);
254         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
255
256         ctx.setAttribute("a", null);
257         parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
258         result = SliPluginUtils.containsKey(parameters, ctx);
259         assertEquals(SliStringUtils.FALSE_CONSTANT, result);
260
261         ctx.setAttribute("a", "hellworld");
262         parameters.put(SliStringUtils.INPUT_PARAM_KEY, "a");
263         result = SliPluginUtils.containsKey(parameters, ctx);
264         assertEquals(SliStringUtils.TRUE_CONSTANT, result);
265     }
266
267 }