bf22606d3ca67b795c39bb164acf4c358cdae743
[appc.git] / appc-outbound / appc-aai-client / provider / src / test / java / org / onap / appc / aai / client / aai / TestAaiService.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Modification Copyright (C) 2018 IBM.
10  * ================================================================================
11  * Modifications Copyright (C) 2019 Ericsson
12  * =============================================================================
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  *      http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  *
25  * ============LICENSE_END=========================================================
26  */
27
28 package org.onap.appc.aai.client.aai;
29
30 import static org.junit.Assert.assertEquals;
31
32 import java.util.HashMap;
33 import java.util.Map;
34 import org.junit.Before;
35 import org.junit.Rule;
36 import org.junit.Test;
37 import org.junit.rules.ExpectedException;
38 import org.junit.runner.RunWith;
39 import org.mockito.Matchers;
40 import org.mockito.Mockito;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
44 import org.osgi.framework.Bundle;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.FrameworkUtil;
47 import org.osgi.framework.ServiceReference;
48 import org.powermock.api.mockito.PowerMockito;
49 import org.powermock.core.classloader.annotations.PrepareForTest;
50 import org.powermock.modules.junit4.PowerMockRunner;
51 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
52
53 import com.att.eelf.configuration.EELFLogger;
54 import com.att.eelf.configuration.EELFManager;
55
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({FrameworkUtil.class})
58 public class TestAaiService {
59     // ONAP merging
60
61     private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAaiService.class);
62     private AAIClient aaiClient = Mockito.mock(AAIClient.class);
63     private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
64     private final Bundle bundleService=Mockito.mock(Bundle.class);
65     private final ServiceReference sref=Mockito.mock(ServiceReference.class);
66
67     @Rule
68     public ExpectedException expectedEx = ExpectedException.none();
69
70     @Before
71     public void setup() {
72         PowerMockito.mockStatic(FrameworkUtil.class);
73         PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
74         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
75         PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
76         PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiClient);
77     }
78
79     @Test
80     public void testMissingVnfId() throws AaiServiceInternalException, SvcLogicException {
81         AaiService aaiService = new AaiService();
82         expectedEx.expect(AaiServiceInternalException.class);
83         expectedEx.expectMessage("VnfId is missing");
84         aaiService.getGenericVnfInfo(new HashMap<String, String>(), new SvcLogicContext());
85     }
86
87     @Test
88     public void testGetVmInfoExceptionFlow() throws SvcLogicException, AaiServiceInternalException {
89         AaiService aaiService = Mockito.spy(new AaiService());
90         Map<String, String> inParams = new HashMap<String, String>();
91         inParams.put("vserverId", "vserverId1");
92         inParams.put("tenantId", "tenantId1");
93         inParams.put("cloudOwner", "cloudOwner1");
94         inParams.put("cloudRegionId", "cloudRegionId1");
95         inParams.put("responsePrefix", "tmp.vnfInfo");
96
97         SvcLogicContext ctx = new SvcLogicContext();
98         Mockito.doThrow(new SvcLogicException()).when(aaiService).readResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
99         expectedEx.expect(SvcLogicException.class);
100         expectedEx.expectMessage("Failed to fetch VM info");
101         aaiService.getVMInfo(inParams, ctx);
102     }
103
104     @Test
105     public void testGetVnfcInfoExceptionFlow() throws Exception {
106         AaiService aaiService = new AaiService();
107         Map<String, String> inParams = new HashMap<String, String>();
108         inParams.put("vnfcName", "");
109         inParams.put("responsePrefix", "tmp.vnfInfo");
110         SvcLogicContext ctx = new SvcLogicContext();
111         expectedEx.expect(AaiServiceInternalException.class);
112         expectedEx.expectMessage("Vnfc Name is missing");
113         aaiService.getVnfcInfo(inParams, ctx);
114     }
115
116     @Test
117     public void testInsertVnfcsExceptionFlow() throws Exception {
118         AaiService aaiService = new AaiService();
119         Map<String, String> inParams = new HashMap<String, String>();
120         inParams.put("responsePrefix", "tmp.vnfInfo");
121         SvcLogicContext ctx = new SvcLogicContext();
122         inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
123         ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-name", "nullnull001");
124         aaiService.insertVnfcs(inParams, ctx, 2, null);
125     }
126
127     @Test
128     public void testGetVnfcData() {
129         AaiService aaiService = new AaiService();
130         Map<String, String> inParams = new HashMap<String, String>();
131         inParams.put("responsePrefix", "tmp.vnfInfo");
132         SvcLogicContext ctx = new SvcLogicContext();
133         inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
134         ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-name", "nullnull001");
135         aaiService.getVnfcData(inParams, ctx, 1);
136     }
137
138     @Test
139     public void testReadResource() throws AaiServiceInternalException, SvcLogicException {
140         AaiService aaiService = Mockito.spy(new AaiService());
141         Map<String, String> inParams = new HashMap<String, String>();
142         inParams.put("responsePrefix", "tmp.vnfInfo");
143         inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
144         Mockito.doReturn(QueryStatus.FAILURE).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
145                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
146         expectedEx.expect(AaiServiceInternalException.class);
147         expectedEx.expectMessage("Error Retrieving null from A&AI");
148         aaiService.readResource(null, null, null);
149     }
150
151     @Test
152     public void testCheckAndUpdateVnfc() throws AaiServiceInternalException, SvcLogicException {
153         AaiService aaiService = Mockito.spy(new AaiService());
154         Map<String, String> inParams = new HashMap<String, String>();
155         inParams.put("responsePrefix", "tmp.vnfInfo");
156         SvcLogicContext ctx = new SvcLogicContext();
157         inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
158         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-name", "nullnull001");
159         aaiService.checkAndUpdateVnfc(inParams, ctx, 1, 1);
160         Mockito.verify(aaiService).updateVnfcStatus(Mockito.anyString(), Mockito.anyMap(), Mockito.anyString());
161     }
162
163     @Test
164     public void testGetGenericVnfInfo() throws Exception {
165
166         MockAaiService mockAai = new MockAaiService(aaiClient);
167
168         Map<String, String> inParams = new HashMap<String, String>();
169         inParams.put("vnfId", "ibcxvm0000");
170         inParams.put("responsePrefix", "tmp.vnfInfo");
171
172         SvcLogicContext ctx = new SvcLogicContext();
173
174         mockAai.getGenericVnfInfo(inParams, ctx);
175
176         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"), "vUSP-Metaswitch");
177         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
178         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcx001vm001-id");
179         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcx000000");
180     }
181
182     @Test
183     public void testGetVmInfo() throws Exception {
184
185         MockAaiService mockAai = new MockAaiService(aaiClient);
186
187         Map<String, String> inParams = new HashMap<String, String>();
188         inParams.put("vserverId", "vserverId1");
189         inParams.put("tenantId", "tenantId1");
190         inParams.put("cloudOwner", "cloudOwner1");
191         inParams.put("cloudRegionId", "cloudRegionId1");
192         inParams.put("responsePrefix", "tmp.vnfInfo");
193
194         SvcLogicContext ctx = new SvcLogicContext();
195
196         mockAai.getVMInfo(inParams, ctx);
197
198         printContext(ctx);
199
200         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vserver-name"), "ibcx0000000");
201         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc-count"), "1");
202         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vf-module-id"), "vfModule1");
203         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc[0].vnfc-name"), "ibcx0001vm001vnfc1");
204
205     }
206
207     @Test
208     public void testGetVnfcInfo() throws Exception {
209
210         MockAaiService mockAai = new MockAaiService(aaiClient);
211
212         Map<String, String> inParams = new HashMap<String, String>();
213         inParams.put("vnfcName", "vnfcName1");
214
215         inParams.put("responsePrefix", "tmp.vnfInfo");
216
217         SvcLogicContext ctx = new SvcLogicContext();
218
219         mockAai.getVnfcInfo(inParams, ctx);
220
221         printContext(ctx);
222
223         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-type"), null);
224         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-function-code"), null);
225         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.group-notation"), "grpnot1");
226
227     }
228
229     @Test
230     public void testGetFirstVnfcNameForVnfcType() throws Exception {
231
232         MockAaiService mockAai = new MockAaiService(aaiClient);
233
234         String prefix = "tmp.vnfInfo.";
235
236         SvcLogicContext ctx = new SvcLogicContext(); // VNFC with specified vnfc
237                                                         // type found
238         mockAai.populateFirstVnfcData(ctx, prefix);
239         String firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
240         assertEquals(firstVnfcName, "vnfcname3");
241
242         ctx = new SvcLogicContext(); // no VMS found
243         firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
244         assertEquals(firstVnfcName, null);
245
246         ctx = new SvcLogicContext(); // no VMS found with specified type
247         mockAai.populateFirstVnfcData(ctx, prefix);
248         firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "test");
249         assertEquals(firstVnfcName, null);
250
251     }
252
253     @Test
254     public void testGroupNotation() throws Exception {
255
256         MockAaiService mockAai = new MockAaiService(aaiClient);
257
258         String prefix = "tmp.vnfInfo.";
259
260         SvcLogicContext ctx = new SvcLogicContext();
261         mockAai.populateGroupNotation(ctx, prefix);
262
263         // printContext(ctx);
264         String grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
265         assertEquals(grpNotation, "grpNot2");
266
267         ctx = new SvcLogicContext(); // no VMS found
268         grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
269         assertEquals(grpNotation, null);
270
271         ctx = new SvcLogicContext(); // no VMS found with specified name
272         mockAai.populateGroupNotation(ctx, prefix);
273         grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "test");
274         assertEquals(grpNotation, null);
275
276     }
277
278     @Test
279     public void testGetGroupNotation() throws Exception {
280
281         MockAaiService mockAai = new MockAaiService(aaiClient);
282
283         String prefix = "tmp.vnfInfo.";
284
285         SvcLogicContext ctx = new SvcLogicContext();
286
287         String grpNotation = mockAai.getGroupNotation("fixed-value", "2", null, null, null, null, null,null, 0);
288         assertEquals(grpNotation, "2");
289
290         mockAai.populateFirstVnfcData(ctx, prefix); // Existing VNFC Found
291         grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
292                 ctx, "ssc", null, 0);
293         assertEquals(grpNotation, "vnfcname32");
294
295         ctx = new SvcLogicContext(); // no vnfcs exist in A&AI- Use current
296                                         // vnfcName
297         grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
298                 ctx, "ssc", null, 0);
299         assertEquals(grpNotation, "currentVnfcName2");
300
301         ctx = new SvcLogicContext();
302         mockAai.populateGroupNotation(ctx, prefix);
303         grpNotation = mockAai.getGroupNotation("relative-value", "same", "currentVnfcName", "ibcxvm0003", prefix, ctx,
304                 "ssc", null, 0);
305         assertEquals(grpNotation, "grpNot2");
306
307         ctx = new SvcLogicContext();
308         mockAai.populateGroupNotation(ctx, prefix);
309         grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0006", prefix, ctx,
310                 "ssc",null,0);
311         assertEquals(grpNotation, "5");
312
313         ctx = new SvcLogicContext();
314         mockAai.populateGroupNotation(ctx, prefix);
315         grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0003", prefix, ctx,
316                 "ssc",null,0);
317         assertEquals(grpNotation, null); // Null if grpNotation is not numeric
318
319     }
320
321 @Test
322     public void testInsertVnfcs() throws Exception {
323
324         MockAaiService mockAai = new MockAaiService(aaiClient);
325
326         Map<String, String> inParams = new HashMap<String, String>();
327
328         inParams.put("responsePrefix", "tmp.vnfInfo");
329
330         SvcLogicContext ctx = new SvcLogicContext();
331
332         mockAai.populateVnfcRef(ctx);
333
334         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
335
336         // mockAai.insertVnfcs(inParams,ctx,2, 2) ;
337     }
338
339     @Test
340     public void testUpdateVServerStatus() throws Exception {
341
342         MockAaiService mockAai = new MockAaiService(aaiClient);
343
344         Map<String, String> inParams = new HashMap<String, String>();
345
346         inParams.put("responsePrefix", "tmp.vnfInfo");
347
348         SvcLogicContext ctx = new SvcLogicContext();
349
350         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
351
352         mockAai.updateVServerStatus(inParams, ctx, 2);
353     }
354
355     @Test
356     public void testInsertVnfcsForFirstVnfc() throws Exception {
357
358         MockAaiService mockAai = new MockAaiService(aaiClient);
359
360         Map<String, String> inParams = new HashMap<String, String>();
361
362         inParams.put("responsePrefix", "tmp.vnfInfo");
363
364         SvcLogicContext ctx = new SvcLogicContext();
365
366         mockAai.populateVnfcRefFirstVnfcName(ctx);
367
368         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
369
370         mockAai.insertVnfcs(inParams, ctx, 2,"vfModuleId1");
371
372
373     }
374
375     @Test
376     public void testInsertVnfcsForRelativeValueSame() throws Exception {
377
378         MockAaiService mockAai = new MockAaiService(aaiClient);
379
380         Map<String, String> inParams = new HashMap<String, String>();
381
382         inParams.put("responsePrefix", "tmp.vnfInfo");
383
384         SvcLogicContext ctx = new SvcLogicContext();
385
386         mockAai.populateVnfcRefRelValueSame(ctx);
387
388         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
389
390         mockAai.insertVnfcs(inParams, ctx, 2, null);
391
392         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
393         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
394         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
395         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
396         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
397         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
398         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
399         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "1");
400     }
401
402     @Test
403     public void testInsertVnfcsForRelativeValueNext() throws Exception {
404
405         MockAaiService mockAai = new MockAaiService(aaiClient);
406
407         Map<String, String> inParams = new HashMap<String, String>();
408
409         inParams.put("responsePrefix", "tmp.vnfInfo");
410
411         SvcLogicContext ctx = new SvcLogicContext();
412
413         mockAai.populateVnfcRefRelValueNext(ctx);
414
415         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
416
417         mockAai.insertVnfcs(inParams, ctx, 2,null);
418
419
420
421         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
422         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
423         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
424         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
425         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
426         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
427         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
428         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "2");
429     }
430
431     @Test
432     public void testUpdateVnfStatus() throws Exception {
433
434         MockAaiService mockAai = new MockAaiService(aaiClient);
435
436         Map<String, String> inParams = new HashMap<String, String>();
437
438         inParams.put("responsePrefix", "tmp.vnfInfo");
439
440         SvcLogicContext ctx = new SvcLogicContext();
441
442         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
443
444         mockAai.updateVnfStatus(inParams, ctx);
445     }
446
447     private void printContext(SvcLogicContext ctx) throws Exception {
448         for (String key : ctx.getAttributeKeySet()) {
449             log.info(" KEY " + key);
450             log.info(" VALUE " + ctx.getAttribute(key));
451         }
452     }
453
454     @Test
455     public void testGetGroupNotationForExistingValue() throws Exception {
456
457         SvcLogicContext ctx = new SvcLogicContext();
458         ctx.setAttribute("req-vf-module-id",  "vfmodule01");
459         ctx.setAttribute("tmp.vnfInfo.vm[0].vf-module-id", "vfmodule01");
460         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
461         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
462         ctx.setAttribute("tmp.vnfInfo.vm[1].vf-module-id", "vfmodule01");
463         ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-function-code", "fc1");
464         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
465         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn2");
466         MockAaiService aai=new MockAaiService(aaiClient);
467         String groupNotationValue1 = aai.getGroupNotationForExistigValue(ctx, "fc1", 2);
468         assertEquals (groupNotationValue1,null);
469
470         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
471         ctx.setAttribute("tmp.vnfInfo.vm[1].group-notation", "gn1");
472         String groupNotationValue2 = aai.getGroupNotationForExistigValue(ctx, "fc1", 2);
473         assertEquals (groupNotationValue2,"gn1");
474
475         ctx.setAttribute("tmp.vnfInfo.vm[2].vf-module-id", "vfmodule01");
476         ctx.setAttribute("tmp.vnfInfo.vm[2].vnfc-function-code", "fc1");
477         ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn2");
478         String groupNotationValue3 = aai.getGroupNotationForExistigValue(ctx, "fc1", 3);
479         assertEquals (groupNotationValue3,null);
480
481         ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn1");
482         String groupNotationValue4 = aai.getGroupNotationForExistigValue(ctx, "fc1", 3);
483         assertEquals (groupNotationValue4,"gn1");
484
485     }
486
487     @Test
488     public void testgetIdentityUrl() throws Exception {
489
490         MockAaiService mockAai = new MockAaiService(aaiClient);
491
492         Map<String, String> inParams = new HashMap<String, String>();
493         inParams.put("responsePrefix", "tmp.vnfInfo");
494         inParams.put("cloudOwner", "testCloudOwner");
495         inParams.put("cloudRegionId", "testCloudRegionId");
496
497         SvcLogicContext ctx = new SvcLogicContext();
498         mockAai.getIdentityUrl(inParams, ctx);
499         assertEquals(ctx.getAttribute("tmp.vnfInfo.cloud-region.identity-url"), "TestUrl");
500
501     } 
502
503     @Test
504     public void testAddvnfc()
505     {
506         MockAaiService mockAai = new MockAaiService(aaiClient);
507         Map<String, String> params=mockAai.getExpectedParams();
508         mockAai.addVnfc("ibcxvm0002func0001",params , "");        
509     }
510
511 }