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