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;
55 import static org.junit.Assert.assertNotNull;
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest({FrameworkUtil.class})
59 public class TestAaiService {
62 private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAaiService.class);
63 private AAIClient aaiClient = Mockito.mock(AAIClient.class);
64 private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
65 private final Bundle bundleService=Mockito.mock(Bundle.class);
66 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
69 public ExpectedException expectedEx = ExpectedException.none();
73 PowerMockito.mockStatic(FrameworkUtil.class);
74 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
75 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
76 PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
77 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiClient);
81 public void testMissingVnfId() throws AaiServiceInternalException, SvcLogicException {
82 AaiService aaiService = new AaiService();
83 expectedEx.expect(AaiServiceInternalException.class);
84 expectedEx.expectMessage("VnfId is missing");
85 aaiService.getGenericVnfInfo(new HashMap<String, String>(), new SvcLogicContext());
89 public void testGetVmInfoExceptionFlow() throws SvcLogicException, AaiServiceInternalException {
90 AaiService aaiService = Mockito.spy(new AaiService());
91 Map<String, String> inParams = new HashMap<String, String>();
92 inParams.put("vserverId", "vserverId1");
93 inParams.put("tenantId", "tenantId1");
94 inParams.put("cloudOwner", "cloudOwner1");
95 inParams.put("cloudRegionId", "cloudRegionId1");
96 inParams.put("responsePrefix", "tmp.vnfInfo");
98 SvcLogicContext ctx = new SvcLogicContext();
99 Mockito.doThrow(new SvcLogicException()).when(aaiService).readResource(Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
100 expectedEx.expect(SvcLogicException.class);
101 expectedEx.expectMessage("Failed to fetch VM info");
102 aaiService.getVMInfo(inParams, ctx);
106 public void testGetVnfcInfoExceptionFlow() throws Exception {
107 AaiService aaiService = new AaiService();
108 Map<String, String> inParams = new HashMap<String, String>();
109 inParams.put("vnfcName", "");
110 inParams.put("responsePrefix", "tmp.vnfInfo");
111 SvcLogicContext ctx = new SvcLogicContext();
112 expectedEx.expect(AaiServiceInternalException.class);
113 expectedEx.expectMessage("Vnfc Name is missing");
114 aaiService.getVnfcInfo(inParams, ctx);
118 public void testInsertVnfcsExceptionFlow() throws Exception {
119 AaiService aaiService = new AaiService();
120 Map<String, String> inParams = new HashMap<String, String>();
121 inParams.put("responsePrefix", "tmp.vnfInfo");
122 SvcLogicContext ctx = new SvcLogicContext();
123 inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
124 ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-name", "nullnull001");
125 aaiService.insertVnfcs(inParams, ctx, 2, null);
126 assertNotNull(aaiService);
130 public void testGetVnfcData() {
131 AaiService aaiService = new AaiService();
132 Map<String, String> inParams = new HashMap<String, String>();
133 inParams.put("responsePrefix", "tmp.vnfInfo");
134 SvcLogicContext ctx = new SvcLogicContext();
135 inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
136 ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-name", "nullnull001");
137 aaiService.getVnfcData(inParams, ctx, 1);
142 public void testReadResource() throws AaiServiceInternalException, SvcLogicException {
143 AaiService aaiService = Mockito.spy(new AaiService());
144 Map<String, String> inParams = new HashMap<String, String>();
145 inParams.put("responsePrefix", "tmp.vnfInfo");
146 inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
147 Mockito.doReturn(QueryStatus.FAILURE).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
148 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
149 expectedEx.expect(AaiServiceInternalException.class);
150 expectedEx.expectMessage("Error Retrieving null from A&AI");
151 aaiService.readResource(null, null, null);
155 public void testCheckAndUpdateVnfc() throws AaiServiceInternalException, SvcLogicException {
156 AaiService aaiService = Mockito.spy(new AaiService());
157 Map<String, String> inParams = new HashMap<String, String>();
158 inParams.put("responsePrefix", "tmp.vnfInfo");
159 SvcLogicContext ctx = new SvcLogicContext();
160 inParams.put("AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX", "prefix.");
161 ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-name", "nullnull001");
162 aaiService.checkAndUpdateVnfc(inParams, ctx, 1, 1);
163 Mockito.verify(aaiService).updateVnfcStatus(Mockito.anyString(), Mockito.anyMap(), Mockito.anyString());
167 public void testGetGenericVnfInfo() throws Exception {
169 MockAaiService mockAai = new MockAaiService(aaiClient);
171 Map<String, String> inParams = new HashMap<String, String>();
172 inParams.put("vnfId", "ibcxvm0000");
173 inParams.put("responsePrefix", "tmp.vnfInfo");
175 SvcLogicContext ctx = new SvcLogicContext();
177 mockAai.getGenericVnfInfo(inParams, ctx);
179 assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"), "vUSP-Metaswitch");
180 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
181 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcx001vm001-id");
182 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcx000000");
186 public void testGetVmInfo() throws Exception {
188 MockAaiService mockAai = new MockAaiService(aaiClient);
190 Map<String, String> inParams = new HashMap<String, String>();
191 inParams.put("vserverId", "vserverId1");
192 inParams.put("tenantId", "tenantId1");
193 inParams.put("cloudOwner", "cloudOwner1");
194 inParams.put("cloudRegionId", "cloudRegionId1");
195 inParams.put("responsePrefix", "tmp.vnfInfo");
197 SvcLogicContext ctx = new SvcLogicContext();
199 mockAai.getVMInfo(inParams, ctx);
203 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vserver-name"), "ibcx0000000");
204 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc-count"), "1");
205 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vf-module-id"), "vfModule1");
206 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc[0].vnfc-name"), "ibcx0001vm001vnfc1");
211 public void testGetVnfcInfo() throws Exception {
213 MockAaiService mockAai = new MockAaiService(aaiClient);
215 Map<String, String> inParams = new HashMap<String, String>();
216 inParams.put("vnfcName", "vnfcName1");
218 inParams.put("responsePrefix", "tmp.vnfInfo");
220 SvcLogicContext ctx = new SvcLogicContext();
222 mockAai.getVnfcInfo(inParams, ctx);
226 assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-type"), null);
227 assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-function-code"), null);
228 assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.group-notation"), "grpnot1");
233 public void testGetFirstVnfcNameForVnfcType() throws Exception {
235 MockAaiService mockAai = new MockAaiService(aaiClient);
237 String prefix = "tmp.vnfInfo.";
239 SvcLogicContext ctx = new SvcLogicContext(); // VNFC with specified vnfc
241 mockAai.populateFirstVnfcData(ctx, prefix);
242 String firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
243 assertEquals(firstVnfcName, "vnfcname3");
245 ctx = new SvcLogicContext(); // no VMS found
246 firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
247 assertEquals(firstVnfcName, null);
249 ctx = new SvcLogicContext(); // no VMS found with specified type
250 mockAai.populateFirstVnfcData(ctx, prefix);
251 firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "test");
252 assertEquals(firstVnfcName, null);
257 public void testGroupNotation() throws Exception {
259 MockAaiService mockAai = new MockAaiService(aaiClient);
261 String prefix = "tmp.vnfInfo.";
263 SvcLogicContext ctx = new SvcLogicContext();
264 mockAai.populateGroupNotation(ctx, prefix);
266 // printContext(ctx);
267 String grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
268 assertEquals(grpNotation, "grpNot2");
270 ctx = new SvcLogicContext(); // no VMS found
271 grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
272 assertEquals(grpNotation, null);
274 ctx = new SvcLogicContext(); // no VMS found with specified name
275 mockAai.populateGroupNotation(ctx, prefix);
276 grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "test");
277 assertEquals(grpNotation, null);
282 public void testGetGroupNotation() throws Exception {
284 MockAaiService mockAai = new MockAaiService(aaiClient);
286 String prefix = "tmp.vnfInfo.";
288 SvcLogicContext ctx = new SvcLogicContext();
290 String grpNotation = mockAai.getGroupNotation("fixed-value", "2", null, null, null, null, null,null, 0);
291 assertEquals(grpNotation, "2");
293 mockAai.populateFirstVnfcData(ctx, prefix); // Existing VNFC Found
294 grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
295 ctx, "ssc", null, 0);
296 assertEquals(grpNotation, "vnfcname32");
298 ctx = new SvcLogicContext(); // no vnfcs exist in A&AI- Use current
300 grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
301 ctx, "ssc", null, 0);
302 assertEquals(grpNotation, "currentVnfcName2");
304 ctx = new SvcLogicContext();
305 mockAai.populateGroupNotation(ctx, prefix);
306 grpNotation = mockAai.getGroupNotation("relative-value", "same", "currentVnfcName", "ibcxvm0003", prefix, ctx,
308 assertEquals(grpNotation, "grpNot2");
310 ctx = new SvcLogicContext();
311 mockAai.populateGroupNotation(ctx, prefix);
312 grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0006", prefix, ctx,
314 assertEquals(grpNotation, "5");
316 ctx = new SvcLogicContext();
317 mockAai.populateGroupNotation(ctx, prefix);
318 grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0003", prefix, ctx,
320 assertEquals(grpNotation, null); // Null if grpNotation is not numeric
325 public void testInsertVnfcs() throws Exception {
327 MockAaiService mockAai = new MockAaiService(aaiClient);
329 Map<String, String> inParams = new HashMap<String, String>();
331 inParams.put("responsePrefix", "tmp.vnfInfo");
333 SvcLogicContext ctx = new SvcLogicContext();
335 mockAai.populateVnfcRef(ctx);
337 mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
339 // mockAai.insertVnfcs(inParams,ctx,2, 2) ;
340 assertNotNull(mockAai);
344 public void testUpdateVServerStatus() throws Exception {
346 MockAaiService mockAai = new MockAaiService(aaiClient);
348 Map<String, String> inParams = new HashMap<String, String>();
350 inParams.put("responsePrefix", "tmp.vnfInfo");
352 SvcLogicContext ctx = new SvcLogicContext();
354 mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
356 mockAai.updateVServerStatus(inParams, ctx, 2);
358 assertNotNull(mockAai);
362 public void testInsertVnfcsForFirstVnfc() throws Exception {
364 MockAaiService mockAai = new MockAaiService(aaiClient);
366 Map<String, String> inParams = new HashMap<String, String>();
368 inParams.put("responsePrefix", "tmp.vnfInfo");
370 SvcLogicContext ctx = new SvcLogicContext();
372 mockAai.populateVnfcRefFirstVnfcName(ctx);
374 mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
376 mockAai.insertVnfcs(inParams, ctx, 2,"vfModuleId1");
377 assertNotNull(mockAai);
383 public void testInsertVnfcsForRelativeValueSame() throws Exception {
385 MockAaiService mockAai = new MockAaiService(aaiClient);
387 Map<String, String> inParams = new HashMap<String, String>();
389 inParams.put("responsePrefix", "tmp.vnfInfo");
391 SvcLogicContext ctx = new SvcLogicContext();
393 mockAai.populateVnfcRefRelValueSame(ctx);
395 mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
397 mockAai.insertVnfcs(inParams, ctx, 2, null);
399 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
400 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
401 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
402 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
403 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
404 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
405 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
406 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "1");
410 public void testInsertVnfcsForRelativeValueNext() throws Exception {
412 MockAaiService mockAai = new MockAaiService(aaiClient);
414 Map<String, String> inParams = new HashMap<String, String>();
416 inParams.put("responsePrefix", "tmp.vnfInfo");
418 SvcLogicContext ctx = new SvcLogicContext();
420 mockAai.populateVnfcRefRelValueNext(ctx);
422 mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
424 mockAai.insertVnfcs(inParams, ctx, 2,null);
428 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
429 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
430 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
431 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
432 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
433 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
434 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
435 assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "2");
439 public void testUpdateVnfStatus() throws Exception {
441 MockAaiService mockAai = new MockAaiService(aaiClient);
443 Map<String, String> inParams = new HashMap<String, String>();
445 inParams.put("responsePrefix", "tmp.vnfInfo");
447 SvcLogicContext ctx = new SvcLogicContext();
449 mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
451 mockAai.updateVnfStatus(inParams, ctx);
456 private void printContext(SvcLogicContext ctx) throws Exception {
457 for (String key : ctx.getAttributeKeySet()) {
458 log.info(" KEY " + key);
459 log.info(" VALUE " + ctx.getAttribute(key));
464 public void testGetGroupNotationForExistingValue() throws Exception {
466 SvcLogicContext ctx = new SvcLogicContext();
467 ctx.setAttribute("req-vf-module-id", "vfmodule01");
468 ctx.setAttribute("tmp.vnfInfo.vm[0].vf-module-id", "vfmodule01");
469 ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
470 ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
471 ctx.setAttribute("tmp.vnfInfo.vm[1].vf-module-id", "vfmodule01");
472 ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-function-code", "fc1");
473 ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
474 ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn2");
475 MockAaiService aai=new MockAaiService(aaiClient);
476 String groupNotationValue1 = aai.getGroupNotationForExistigValue(ctx, "fc1", 2);
477 assertEquals (groupNotationValue1,null);
479 ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
480 ctx.setAttribute("tmp.vnfInfo.vm[1].group-notation", "gn1");
481 String groupNotationValue2 = aai.getGroupNotationForExistigValue(ctx, "fc1", 2);
482 assertEquals (groupNotationValue2,"gn1");
484 ctx.setAttribute("tmp.vnfInfo.vm[2].vf-module-id", "vfmodule01");
485 ctx.setAttribute("tmp.vnfInfo.vm[2].vnfc-function-code", "fc1");
486 ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn2");
487 String groupNotationValue3 = aai.getGroupNotationForExistigValue(ctx, "fc1", 3);
488 assertEquals (groupNotationValue3,null);
490 ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn1");
491 String groupNotationValue4 = aai.getGroupNotationForExistigValue(ctx, "fc1", 3);
492 assertEquals (groupNotationValue4,"gn1");
497 public void testgetIdentityUrl() throws Exception {
499 MockAaiService mockAai = new MockAaiService(aaiClient);
501 Map<String, String> inParams = new HashMap<String, String>();
502 inParams.put("responsePrefix", "tmp.vnfInfo");
503 inParams.put("cloudOwner", "testCloudOwner");
504 inParams.put("cloudRegionId", "testCloudRegionId");
506 SvcLogicContext ctx = new SvcLogicContext();
507 mockAai.getIdentityUrl(inParams, ctx);
508 assertEquals(ctx.getAttribute("tmp.vnfInfo.cloud-region.identity-url"), "TestUrl");
513 public void testAddvnfc()
515 MockAaiService mockAai = new MockAaiService(aaiClient);
516 Map<String, String> params=mockAai.getExpectedParams();
517 mockAai.addVnfc("ibcxvm0002func0001",params , "");
518 assertNotNull(mockAai);