Merge "Fix unchecked cast warning"
[sdnc/core.git] / sliPluginUtils / provider / src / test / java / org / openecomp / sdnc / sli / SliPluginUtils / SliPluginUtils_StaticFunctions.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
27 import java.util.HashMap;
28 import java.util.Map;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import org.openecomp.sdnc.sli.SvcLogicException;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 public class SliPluginUtils_StaticFunctions {
38     private static final Logger LOG = LoggerFactory.getLogger(SliPluginUtils_StaticFunctions.class);
39     SliPluginUtils utils = new SliPluginUtils();
40     private SvcLogicContext ctx;
41     private HashMap<String,String> parameters;
42
43
44     @Before
45     public void setUp() throws Exception {
46         this.ctx = new SvcLogicContext();
47         parameters = new HashMap<>();
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", "USOSTCDALTX0101UJZZ01");
55         ctx.setAttribute("service-data.service-information.service-type", "VMS");
56
57         Map<String, String> entries = SliPluginUtils.ctxGetBeginsWith(ctx, "service-data.service-information");
58
59         assertEquals( "USOSTCDALTX0101UJZZ01", entries.get("service-data.service-information.service-instance-id") );
60         assertEquals( "VMS", 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
191         parameters.put("index", "1");
192         parameters.put("list_pfx", "service-data.vnf-l3");
193
194         utils.ctxListRemove( parameters, ctx );
195     }
196
197     // TODO: javadoc
198     @Test
199     public final void testCtxPutAll() {
200         HashMap<String, Object> entries = new HashMap<>();
201         entries.put("service-data.oper-status.order-status", "InProgress");
202         entries.put("service-data.service-information.service-instance-id", "USOSTCDALTX0101UJZZ01");
203         entries.put("service-data.request-information.order-number", 1234);
204         entries.put("service-data.request-information.request-id", null);
205
206         SliPluginUtils.ctxPutAll(ctx, entries);
207
208         assertEquals( "InProgress", ctx.getAttribute("service-data.oper-status.order-status") );
209         assertEquals( "USOSTCDALTX0101UJZZ01", ctx.getAttribute("service-data.service-information.service-instance-id") );
210         assertEquals( "1234", ctx.getAttribute("service-data.request-information.order-number") );
211         assertFalse( ctx.getAttributeKeySet().contains("service-data.request-information.request-id") );
212     }
213
214     // TODO: javadoc
215     @Test
216     public final void testCtxSetAttribute_LOG() {
217         LOG.debug("=== testCtxSetAttribute_LOG ===");
218         Integer i = new Integer(3);
219         SliPluginUtils.ctxSetAttribute(ctx, "test", i, LOG, SliPluginUtils.LogLevel.TRACE);
220     }
221 }