2 * ============LICENSE_START=======================================================
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
17 * http://www.apache.org/licenses/LICENSE-2.0
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.
25 * ============LICENSE_END=========================================================
28 package org.onap.appc.aai.client.aai;
30 import static org.junit.Assert.assertEquals;
32 import java.util.HashMap;
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;
53 import com.att.eelf.configuration.EELFLogger;
54 import com.att.eelf.configuration.EELFManager;
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({FrameworkUtil.class})
58 public class TestAaiService {
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);
68 public ExpectedException expectedEx = ExpectedException.none();
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);
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());
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");
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);
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);
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, 2, null);
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, 1);
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);
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());
164 public void testGetGenericVnfInfo() throws Exception {
166 MockAaiService mockAai = new MockAaiService(aaiClient);
168 Map<String, String> inParams = new HashMap<String, String>();
169 inParams.put("vnfId", "ibcxvm0000");
170 inParams.put("responsePrefix", "tmp.vnfInfo");
172 SvcLogicContext ctx = new SvcLogicContext();
174 mockAai.getGenericVnfInfo(inParams, ctx);
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");
183 public void testGetVmInfo() throws Exception {
185 MockAaiService mockAai = new MockAaiService(aaiClient);
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");
194 SvcLogicContext ctx = new SvcLogicContext();
196 mockAai.getVMInfo(inParams, ctx);
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");
208 public void testGetVnfcInfo() throws Exception {
210 MockAaiService mockAai = new MockAaiService(aaiClient);
212 Map<String, String> inParams = new HashMap<String, String>();
213 inParams.put("vnfcName", "vnfcName1");
215 inParams.put("responsePrefix", "tmp.vnfInfo");
217 SvcLogicContext ctx = new SvcLogicContext();
219 mockAai.getVnfcInfo(inParams, ctx);
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");
230 public void testGetFirstVnfcNameForVnfcType() throws Exception {
232 MockAaiService mockAai = new MockAaiService(aaiClient);
234 String prefix = "tmp.vnfInfo.";
236 SvcLogicContext ctx = new SvcLogicContext(); // VNFC with specified vnfc
238 mockAai.populateFirstVnfcData(ctx, prefix);
239 String firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
240 assertEquals(firstVnfcName, "vnfcname3");
242 ctx = new SvcLogicContext(); // no VMS found
243 firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
244 assertEquals(firstVnfcName, null);
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);
254 public void testGroupNotation() throws Exception {
256 MockAaiService mockAai = new MockAaiService(aaiClient);
258 String prefix = "tmp.vnfInfo.";
260 SvcLogicContext ctx = new SvcLogicContext();
261 mockAai.populateGroupNotation(ctx, prefix);
263 // printContext(ctx);
264 String grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
265 assertEquals(grpNotation, "grpNot2");
267 ctx = new SvcLogicContext(); // no VMS found
268 grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
269 assertEquals(grpNotation, null);
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);
279 public void testGetGroupNotation() throws Exception {
281 MockAaiService mockAai = new MockAaiService(aaiClient);
283 String prefix = "tmp.vnfInfo.";
285 SvcLogicContext ctx = new SvcLogicContext();
287 String grpNotation = mockAai.getGroupNotation("fixed-value", "2", null, null, null, null, null,null, 0);
288 assertEquals(grpNotation, "2");
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");
295 ctx = new SvcLogicContext(); // no vnfcs exist in A&AI- Use current
297 grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
298 ctx, "ssc", null, 0);
299 assertEquals(grpNotation, "currentVnfcName2");
301 ctx = new SvcLogicContext();
302 mockAai.populateGroupNotation(ctx, prefix);
303 grpNotation = mockAai.getGroupNotation("relative-value", "same", "currentVnfcName", "ibcxvm0003", prefix, ctx,
305 assertEquals(grpNotation, "grpNot2");
307 ctx = new SvcLogicContext();
308 mockAai.populateGroupNotation(ctx, prefix);
309 grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0006", prefix, ctx,
311 assertEquals(grpNotation, "5");
313 ctx = new SvcLogicContext();
314 mockAai.populateGroupNotation(ctx, prefix);
315 grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0003", prefix, ctx,
317 assertEquals(grpNotation, null); // Null if grpNotation is not numeric
322 public void testInsertVnfcs() throws Exception {
324 MockAaiService mockAai = new MockAaiService(aaiClient);
326 Map<String, String> inParams = new HashMap<String, String>();
328 inParams.put("responsePrefix", "tmp.vnfInfo");
330 SvcLogicContext ctx = new SvcLogicContext();
332 mockAai.populateVnfcRef(ctx);
334 mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
336 // mockAai.insertVnfcs(inParams,ctx,2, 2) ;
340 public void testUpdateVServerStatus() throws Exception {
342 MockAaiService mockAai = new MockAaiService(aaiClient);
344 Map<String, String> inParams = new HashMap<String, String>();
346 inParams.put("responsePrefix", "tmp.vnfInfo");
348 SvcLogicContext ctx = new SvcLogicContext();
350 mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
352 mockAai.updateVServerStatus(inParams, ctx, 2);
356 public void testInsertVnfcsForFirstVnfc() throws Exception {
358 MockAaiService mockAai = new MockAaiService(aaiClient);
360 Map<String, String> inParams = new HashMap<String, String>();
362 inParams.put("responsePrefix", "tmp.vnfInfo");
364 SvcLogicContext ctx = new SvcLogicContext();
366 mockAai.populateVnfcRefFirstVnfcName(ctx);
368 mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
370 mockAai.insertVnfcs(inParams, ctx, 2, 2,"vfModuleId1");
376 public void testInsertVnfcsForRelativeValueSame() throws Exception {
378 MockAaiService mockAai = new MockAaiService(aaiClient);
380 Map<String, String> inParams = new HashMap<String, String>();
382 inParams.put("responsePrefix", "tmp.vnfInfo");
384 SvcLogicContext ctx = new SvcLogicContext();
386 mockAai.populateVnfcRefRelValueSame(ctx);
388 mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
390 mockAai.insertVnfcs(inParams, ctx, 2, 2, null);
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");
403 public void testInsertVnfcsForRelativeValueNext() throws Exception {
405 MockAaiService mockAai = new MockAaiService(aaiClient);
407 Map<String, String> inParams = new HashMap<String, String>();
409 inParams.put("responsePrefix", "tmp.vnfInfo");
411 SvcLogicContext ctx = new SvcLogicContext();
413 mockAai.populateVnfcRefRelValueNext(ctx);
415 mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
417 mockAai.insertVnfcs(inParams, ctx, 2, 2,null);
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");
432 public void testUpdateVnfStatus() throws Exception {
434 MockAaiService mockAai = new MockAaiService(aaiClient);
436 Map<String, String> inParams = new HashMap<String, String>();
438 inParams.put("responsePrefix", "tmp.vnfInfo");
440 SvcLogicContext ctx = new SvcLogicContext();
442 mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
444 mockAai.updateVnfStatus(inParams, ctx);
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));
455 public void testGetGroupNotationForExistingValue() throws Exception {
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, "tmp.vnfInfo", "fc1", 2);
468 assertEquals (groupNotationValue1,null);
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, "tmp.vnfInfo", "fc1", 2);
473 assertEquals (groupNotationValue2,"gn1");
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, "tmp.vnfInfo", "fc1", 3);
479 assertEquals (groupNotationValue3,null);
481 ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn1");
482 String groupNotationValue4 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 3);
483 assertEquals (groupNotationValue4,"gn1");
488 public void testgetIdentityUrl() throws Exception {
490 MockAaiService mockAai = new MockAaiService(aaiClient);
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");
497 SvcLogicContext ctx = new SvcLogicContext();
498 mockAai.getIdentityUrl(inParams, ctx);
499 assertEquals(ctx.getAttribute("tmp.vnfInfo.cloud-region.identity-url"), "TestUrl");
504 public void testAddvnfc()
506 MockAaiService mockAai = new MockAaiService(aaiClient);
507 Map<String, String> params=mockAai.getExpectedParams();
508 mockAai.addVnfc("ibcxvm0002func0001",params , "");