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