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