Refactor dblib
[ccsdk/sli/core.git] / sliPluginUtils / provider / src / test / java / org / openecomp / sdnc / sli / SliPluginUtils / SvcLogicContextListTest.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.assertNull;
25
26 import java.util.HashMap;
27
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
31
32 public class SvcLogicContextListTest {
33         //private static final Logger LOG = LoggerFactory.getLogger(SvcLogicContextTest.class);
34         private SvcLogicContext ctx;
35
36         @Before
37         public void setUp() throws Exception {
38                 this.ctx = new SvcLogicContext();
39         }
40
41         // TODO: javadoc
42         @Test
43         public final void testSvcLogicContextList_SingleValueList() {
44                 ctx.setAttribute("list[0]", "0");
45                 ctx.setAttribute("list[1]", "1");
46                 ctx.setAttribute("list[2]", "2");
47                 ctx.setAttribute("list[3]", "3");
48                 ctx.setAttribute("list[4]", "4");
49                 ctx.setAttribute("list_length", "5");
50
51                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
52
53                 // Check that size of list is 5
54                 assertEquals(5, list.size());
55
56                 // Check that each HashMap has it's list value in the empty string key
57                 // and has no other values
58                 assertEquals(1, list.get(0).size());
59                 assertEquals("0", list.get(0).get(""));
60                 assertEquals(1, list.get(1).size());
61                 assertEquals("1", list.get(1).get(""));
62                 assertEquals(1, list.get(2).size());
63                 assertEquals("2", list.get(2).get(""));
64                 assertEquals(1, list.get(3).size());
65                 assertEquals("3", list.get(3).get(""));
66                 assertEquals(1, list.get(4).size());
67                 assertEquals("4", list.get(4).get(""));
68         }
69
70         // TODO: javadoc
71         @Test
72         public final void testSvcLogicContextList_ObjectList() {
73                 ctx.setAttribute("list[0].ipv4", "1.1.1.0");
74                 ctx.setAttribute("list[0].ipv6", "2001::0");
75                 ctx.setAttribute("list[1].ipv4", "1.1.1.1");
76                 ctx.setAttribute("list[1].ipv6", "2001::1");
77                 ctx.setAttribute("list[2].ipv4", "1.1.1.2");
78                 ctx.setAttribute("list[2].ipv6", "2001::2");
79                 ctx.setAttribute("list[3].ipv4", "1.1.1.3");
80                 ctx.setAttribute("list[3].ipv6", "2001::3");
81                 ctx.setAttribute("list[4].ipv4", "1.1.1.4");
82                 ctx.setAttribute("list[4].ipv6", "2001::4");
83                 ctx.setAttribute("list_length", "5");
84
85                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
86
87                 // Check that size of list is 5
88                 assertEquals(5, list.size());
89
90                 assertEquals(2, list.get(0).size());
91                 assertEquals("1.1.1.0", list.get(0).get("ipv4"));
92                 assertEquals("2001::0", list.get(0).get("ipv6"));
93                 assertEquals(2, list.get(1).size());
94                 assertEquals("1.1.1.1", list.get(1).get("ipv4"));
95                 assertEquals("2001::1", list.get(1).get("ipv6"));
96                 assertEquals(2, list.get(2).size());
97                 assertEquals("1.1.1.2", list.get(2).get("ipv4"));
98                 assertEquals("2001::2", list.get(2).get("ipv6"));
99                 assertEquals(2, list.get(3).size());
100                 assertEquals("1.1.1.3", list.get(3).get("ipv4"));
101                 assertEquals("2001::3", list.get(3).get("ipv6"));
102                 assertEquals(2, list.get(4).size());
103                 assertEquals("1.1.1.4", list.get(4).get("ipv4"));
104                 assertEquals("2001::4", list.get(4).get("ipv6"));
105         }
106
107         // TODO: javadoc
108         @Test
109         public final void testExtract() {
110                 ctx.setAttribute("list[0]", "0");
111                 ctx.setAttribute("list[1]", "1");
112                 ctx.setAttribute("list[2]", "2");
113                 ctx.setAttribute("list[3]", "3");
114                 ctx.setAttribute("list[4]", "4");
115                 ctx.setAttribute("list_length", "5");
116                 ctx.setAttribute("Other", "other");
117
118                 SvcLogicContextList list = SvcLogicContextList.extract(ctx, "list");
119
120                 // Check that size of list is 5
121                 assertEquals(5, list.size());
122
123                 // Check that all list values exist in list object
124                 assertEquals(1, list.get(0).size());
125                 assertEquals("0", list.get(0).get(""));
126                 assertEquals(1, list.get(1).size());
127                 assertEquals("1", list.get(1).get(""));
128                 assertEquals(1, list.get(2).size());
129                 assertEquals("2", list.get(2).get(""));
130                 assertEquals(1, list.get(3).size());
131                 assertEquals("3", list.get(3).get(""));
132                 assertEquals(1, list.get(4).size());
133                 assertEquals("4", list.get(4).get(""));
134
135                 // Check that all list values no longer exist in ctx
136                 assertNull(ctx.getAttribute("list[0]"));
137                 assertNull(ctx.getAttribute("list[1]"));
138                 assertNull(ctx.getAttribute("list[2]"));
139                 assertNull(ctx.getAttribute("list[3]"));
140                 assertNull(ctx.getAttribute("list[4]"));
141                 assertNull(ctx.getAttribute("list_length"));
142
143                 // Check that non-list values still exist in ctx
144                 assertEquals("other", ctx.getAttribute("Other"));
145         }
146
147         // TODO: javadoc
148         @Test
149         public final void testRemove_int() {
150                 ctx.setAttribute("list[0]", "0");
151                 ctx.setAttribute("list[1]", "1");
152                 ctx.setAttribute("list[2]", "2");
153                 ctx.setAttribute("list[3]", "3");
154                 ctx.setAttribute("list[4]", "4");
155                 ctx.setAttribute("list_length", "5");
156
157                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
158                 list.remove(2);
159
160                 // Check that size of list is 4 (1 less than original)
161                 assertEquals(4, list.size());
162
163                 // Check that value was remove from list
164                 assertEquals(1, list.get(0).size());
165                 assertEquals("0", list.get(0).get(""));
166                 assertEquals(1, list.get(1).size());
167                 assertEquals("1", list.get(1).get(""));
168                 assertEquals(1, list.get(2).size());
169                 assertEquals("3", list.get(2).get(""));
170                 assertEquals(1, list.get(3).size());
171                 assertEquals("4", list.get(3).get(""));
172         }
173
174         // TODO: javadoc
175         @Test
176         public final void testRemove_StringString() {
177                 ctx.setAttribute("list[0].ipv4", "1.1.1.0");
178                 ctx.setAttribute("list[0].ipv6", "2001::0");
179                 ctx.setAttribute("list[1].ipv4", "1.1.1.1");
180                 ctx.setAttribute("list[1].ipv6", "2001::1");
181                 ctx.setAttribute("list[2].ipv4", "1.1.1.2");
182                 ctx.setAttribute("list[2].ipv6", "2001::2");
183                 ctx.setAttribute("list[3].ipv4", "1.1.1.3");
184                 ctx.setAttribute("list[3].ipv6", "2001::3");
185                 ctx.setAttribute("list[4].ipv4", "1.1.1.4");
186                 ctx.setAttribute("list[4].ipv6", "2001::4");
187                 ctx.setAttribute("list[5].ipv4", "1.1.1.2");
188                 ctx.setAttribute("list[5].ipv6", "2001::2");
189                 ctx.setAttribute("list_length", "6");
190
191                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
192                 list.remove("ipv4", "1.1.1.2");
193
194                 // Check that size of list is 4 (2 less than original)
195                 assertEquals(4, list.size());
196
197                 // Check that all elements with values ending in 2 were removed
198                 assertEquals("1.1.1.0", list.get(0).get("ipv4"));
199                 assertEquals("2001::0", list.get(0).get("ipv6"));
200                 assertEquals("1.1.1.1", list.get(1).get("ipv4"));
201                 assertEquals("2001::1", list.get(1).get("ipv6"));
202                 assertEquals("1.1.1.3", list.get(2).get("ipv4"));
203                 assertEquals("2001::3", list.get(2).get("ipv6"));
204                 assertEquals("1.1.1.4", list.get(3).get("ipv4"));
205                 assertEquals("2001::4", list.get(3).get("ipv6"));
206         }
207
208         // TODO: javadoc
209         @Test
210         public final void testRemove_StringString_ValueList() {
211                 ctx.setAttribute("list[0]", "5");
212                 ctx.setAttribute("list[1]", "6");
213                 ctx.setAttribute("list[2]", "7");
214                 ctx.setAttribute("list[3]", "8");
215                 ctx.setAttribute("list[4]", "9");
216                 ctx.setAttribute("list_length", "5");
217
218                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
219                 list.remove("", "6");
220
221                 // Check that size of list is 4 (1 less than original)
222                 assertEquals(4, list.size());
223
224                 // Check that value was remove from list
225                 assertEquals(1, list.get(0).size());
226                 assertEquals("5", list.get(0).get(""));
227                 assertEquals(1, list.get(1).size());
228                 assertEquals("7", list.get(1).get(""));
229                 assertEquals(1, list.get(2).size());
230                 assertEquals("8", list.get(2).get(""));
231                 assertEquals(1, list.get(3).size());
232                 assertEquals("9", list.get(3).get(""));
233         }
234
235         // TODO: javadoc
236         @Test
237         public final void testRemove_Map() {
238                 ctx.setAttribute("list[0].ipv4", "1.1.1.0");
239                 ctx.setAttribute("list[0].ipv6", "2001::0");
240                 ctx.setAttribute("list[1].ipv4", "1.1.1.1");
241                 ctx.setAttribute("list[1].ipv6", "2001::1");
242                 ctx.setAttribute("list[2].ipv4", "1.1.1.2");
243                 ctx.setAttribute("list[2].ipv6", "2001::2");
244                 ctx.setAttribute("list[3].ipv4", "1.1.1.3");
245                 ctx.setAttribute("list[3].ipv6", "2001::3");
246                 ctx.setAttribute("list[4].ipv4", "1.1.1.4");
247                 ctx.setAttribute("list[4].ipv6", "2001::4");
248                 ctx.setAttribute("list[5].ipv4", "1.1.1.2");
249                 ctx.setAttribute("list[5].ipv6", "2001::2");
250                 ctx.setAttribute("list_length", "6");
251
252                 HashMap<String,String> remove_key = new HashMap<String,String>();
253                 remove_key.put("ipv4", "1.1.1.2");
254                 remove_key.put("ipv6", "2001::2");
255
256                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
257                 list.remove(remove_key);
258
259                 // Check that size of list is 4 (2 less than original)
260                 assertEquals(4, list.size());
261
262                 // Check that all elements with values ending in 2 were removed
263                 assertEquals("1.1.1.0", list.get(0).get("ipv4"));
264                 assertEquals("2001::0", list.get(0).get("ipv6"));
265                 assertEquals("1.1.1.1", list.get(1).get("ipv4"));
266                 assertEquals("2001::1", list.get(1).get("ipv6"));
267                 assertEquals("1.1.1.3", list.get(2).get("ipv4"));
268                 assertEquals("2001::3", list.get(2).get("ipv6"));
269                 assertEquals("1.1.1.4", list.get(3).get("ipv4"));
270                 assertEquals("2001::4", list.get(3).get("ipv6"));
271         }
272
273         // TODO: javadoc
274         @Test
275         public final void testWriteToContext() {
276                 ctx.setAttribute("list[0]", "0");
277                 ctx.setAttribute("list[1]", "1");
278                 ctx.setAttribute("list[2]", "2");
279                 ctx.setAttribute("list[3]", "3");
280                 ctx.setAttribute("list[4]", "4");
281                 ctx.setAttribute("list_length", "5");
282                 ctx.setAttribute("Other", "other");
283
284                 SvcLogicContextList list = new SvcLogicContextList( ctx, "list" );
285
286                 // Erase context memory
287                 ctx = new SvcLogicContext();
288
289                 // Write list back into context memory
290                 list.writeToContext(ctx);
291
292                 // Check that size of list is 5
293                 assertEquals(5, list.size());
294
295                 // Check that all list values exist in list object
296                 assertEquals("0", ctx.getAttribute("list[0]"));
297                 assertEquals("1", ctx.getAttribute("list[1]"));
298                 assertEquals("2", ctx.getAttribute("list[2]"));
299                 assertEquals("3", ctx.getAttribute("list[3]"));
300                 assertEquals("4", ctx.getAttribute("list[4]"));
301                 assertEquals("5", ctx.getAttribute("list_length"));
302
303                 // Check that old list values aren't in new list
304                 assertNull(ctx.getAttribute("Other"));
305         }
306 }