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