2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.dg.util.impl;
26 import com.att.eelf.configuration.EELFLogger;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
33 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
35 import org.onap.appc.exceptions.APPCException;
36 import org.osgi.framework.Bundle;
37 import org.osgi.framework.BundleContext;
38 import org.osgi.framework.FrameworkUtil;
39 import org.osgi.framework.ServiceReference;
40 import org.powermock.api.mockito.PowerMockito;
41 import org.powermock.core.classloader.annotations.PrepareForTest;
42 import org.powermock.modules.junit4.PowerMockRunner;
43 import org.powermock.reflect.Whitebox;
44 import java.util.HashMap;
45 import java.util.HashSet;
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.Matchers.any;
51 import static org.mockito.Matchers.anyMap;
52 import static org.mockito.Matchers.anyString;
53 import static org.mockito.Matchers.eq;
54 import static org.mockito.Mockito.mock;
55 import static org.mockito.Mockito.times;
56 import static org.mockito.Mockito.verify;
57 import static org.powermock.api.mockito.PowerMockito.mockStatic;
58 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
59 import static org.powermock.api.mockito.PowerMockito.verifyStatic;
61 @RunWith(PowerMockRunner.class)
62 @PrepareForTest({FrameworkUtil.class, Thread.class})
63 public class ExecuteNodeActionImplTest {
64 private ExecuteNodeActionImpl mockedExecuteNodeActionImpl = PowerMockito.spy(new ExecuteNodeActionImpl());
66 private EELFLogger eelfLogger;
68 private AAIService aaiService;
70 private final String resourceType = "resourceType";
71 private final String prefix = "prefix";
72 private final String resourceKey = "resourceKey";
73 private final String attributeName = "attributeName";
74 private final String attributeValue = "attributeValue";
76 private Map<String, String> params = new HashMap<>();
77 private SvcLogicContext svcLogicContext = new SvcLogicContext();
78 private SvcLogicResource.QueryStatus queryStatus = SvcLogicResource.QueryStatus.SUCCESS;
81 public void setUp() throws Exception {
82 Whitebox.setInternalState(mockedExecuteNodeActionImpl, "aaiService", aaiService);
84 params.put("resourceType", resourceType);
85 params.put("prefix", prefix);
86 params.put("resourceKey", resourceKey);
87 params.put("attributeName", attributeName);
88 params.put("attributeValue", attributeValue);
92 public void testGetAAIservice() throws Exception {
94 mockStatic(FrameworkUtil.class);
95 Bundle mockedBundle = mock(Bundle.class);
96 BundleContext mockedBundleContext = mock(BundleContext.class);
97 ServiceReference mockedServiceReference = mock(ServiceReference.class);
98 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
99 PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
100 PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
101 .getServiceReference(AAIService.class.getName());
103 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "initialize");
104 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
107 PowerMockito.doReturn(null).when(mockedBundleContext).getServiceReference(AAIService.class.getName());
108 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
109 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
113 public void testWaitMethod() throws Exception {
114 mockStatic(Thread.class);
115 params.put("waitTime", "1");
116 mockedExecuteNodeActionImpl.waitMethod(params, svcLogicContext);
117 verifyStatic(times(1));
121 public void testGetResource() throws Exception {
122 AAIService aaiService = setupForResourceTests();
123 PowerMockito.doReturn(queryStatus).when(aaiService).query(Mockito.<String>any(), Mockito.anyBoolean(),
124 Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(),
125 Mockito.any(SvcLogicContext.class));
127 mockedExecuteNodeActionImpl.getResource(params, svcLogicContext);
129 verify(aaiService, times(1)).query(resourceType, false, null, resourceKey, prefix, null, svcLogicContext);
130 assertEquals(queryStatus.toString(), svcLogicContext.getAttribute("getResource_result"));
134 public void testPostResource() throws Exception {
135 AAIService aaiService = setupForResourceTests();
136 PowerMockito.doReturn(queryStatus).when(aaiService).update(eq(resourceType), eq(resourceKey), anyMap(),
137 eq(prefix), eq(svcLogicContext));
139 mockedExecuteNodeActionImpl.postResource(params, svcLogicContext);
141 verify(aaiService, times(1)).update(eq(resourceType), eq(resourceKey), anyMap(), eq(prefix),
142 eq(svcLogicContext));
143 assertEquals(svcLogicContext.getAttribute("postResource_result"), queryStatus.toString());
147 public void testDeleteResource() throws Exception {
148 AAIService aaiService = setupForResourceTests();
150 PowerMockito.doReturn(queryStatus).when(aaiService).delete(eq(resourceType), eq(resourceKey),
151 eq(svcLogicContext));
153 mockedExecuteNodeActionImpl.deleteResource(params, svcLogicContext);
155 verify(aaiService, times(1)).delete(eq(resourceType), eq(resourceKey), eq(svcLogicContext));
156 assertEquals(svcLogicContext.getAttribute("deleteResource_result"), queryStatus.toString());
160 public void testGetVnfHierarchySuccess() throws Exception {
161 AAIService aaiService = setupForResourceTests();
163 PowerMockito.doReturn(queryStatus).when(aaiService).query(Mockito.<String>any(), Mockito.anyBoolean(),
164 Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(),
165 Mockito.any(SvcLogicContext.class));
167 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
169 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
170 assertEquals("SUCCESS", svcLogicContext.getAttribute("getVnfHierarchy_result"));
173 @Test(expected = APPCException.class)
174 public void testGetVnfHierarchyFailure() throws Exception {
175 queryStatus = SvcLogicResource.QueryStatus.FAILURE;
176 AAIService aaiService = setupForResourceTests();
177 PowerMockito.doReturn(queryStatus).when(aaiService).query(Mockito.<String>any(), Mockito.anyBoolean(),
178 Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(), Mockito.<String>any(),
179 Mockito.any(SvcLogicContext.class));
181 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
183 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
184 assertEquals("FAILURE", svcLogicContext.getAttribute("getVnfHierarchy_result"));
185 assertTrue(svcLogicContext.getAttribute("output.status.message") != null);
189 public void testPopulateVnfcsDetailsinContext() throws Exception {
190 Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
191 Set<String> vServersList = new HashSet<>();
192 vnfcHierarchyMap.put("SMP", vServersList);
193 vServersList.add("smp-0-url");
194 vServersList.add("smp-1-url");
196 AAIService aaiService = setupForResourceTests();
197 PowerMockito.when(aaiService.query(eq("vnfc"), eq(false), anyString(), eq("vnfc-name = 'SMP'"),
198 eq("vnfcRetrived"), anyString(), any(SvcLogicContext.class))).thenReturn(queryStatus);
200 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", vnfcHierarchyMap,
203 verify(mockedExecuteNodeActionImpl, times(1)).getResource(anyMap(), any(SvcLogicContext.class));
204 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].TYPE"));
205 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].NAME"));
206 assertEquals("2", svcLogicContext.getAttribute("VNF.VNFC[0].VM_COUNT"));
207 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[0].URL")));
208 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[1].URL")));
211 private AAIService setupForResourceTests() {
212 mockStatic(FrameworkUtil.class);
213 Bundle mockedBundle = mock(Bundle.class);
214 BundleContext mockedBundleContext = mock(BundleContext.class);
215 ServiceReference mockedServiceReference = mock(ServiceReference.class);
216 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
217 PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
218 PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
219 .getServiceReference(AAIService.class.getName());
220 AAIService aaiService = PowerMockito.mock(AAIService.class);
221 PowerMockito.doReturn(aaiService).when(mockedBundleContext).getService(mockedServiceReference);