2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.dg.util.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.Spy;
33 import org.onap.ccsdk.sli.adaptors.aai.AAIService;
34 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
36 import org.openecomp.appc.exceptions.APPCException;
37 import org.osgi.framework.Bundle;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.FrameworkUtil;
40 import org.osgi.framework.ServiceReference;
41 import org.powermock.api.mockito.PowerMockito;
42 import org.powermock.core.classloader.annotations.PrepareForTest;
43 import org.powermock.modules.junit4.PowerMockRunner;
44 import org.powermock.reflect.Whitebox;
46 import java.util.HashMap;
47 import java.util.HashSet;
51 import static org.junit.Assert.assertEquals;
52 import static org.junit.Assert.assertTrue;
53 import static org.mockito.Matchers.any;
54 import static org.mockito.Matchers.anyMap;
55 import static org.mockito.Matchers.anyString;
56 import static org.mockito.Matchers.eq;
57 import static org.mockito.Mockito.mock;
58 import static org.mockito.Mockito.times;
59 import static org.mockito.Mockito.verify;
60 import static org.powermock.api.mockito.PowerMockito.mockStatic;
61 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
62 import static org.powermock.api.mockito.PowerMockito.verifyStatic;
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({ExecuteNodeActionImpl.class, FrameworkUtil.class, Thread.class})
66 public class ExecuteNodeActionImplTest {
68 private ExecuteNodeActionImpl mockedExecuteNodeActionImpl = new ExecuteNodeActionImpl();
70 private EELFLogger eelfLogger;
72 private AAIService aaiService;
74 private final String resourceType = "resourceType";
75 private final String prefix = "prefix";
76 private final String resourceKey = "resourceKey";
77 private final String attributeName = "attributeName";
78 private final String attributeValue = "attributeValue";
80 private Map<String, String> params = new HashMap<>();
81 private SvcLogicContext svcLogicContext = new SvcLogicContext();
82 private SvcLogicResource.QueryStatus queryStatus = SvcLogicResource.QueryStatus.SUCCESS;
86 public void setUp() throws Exception {
87 Whitebox.setInternalState(mockedExecuteNodeActionImpl, "aaiService", aaiService);
89 params.put("resourceType", resourceType);
90 params.put("prefix", prefix);
91 params.put("resourceKey", resourceKey);
92 params.put("attributeName", attributeName);
93 params.put("attributeValue", attributeValue);
97 public void testInitialize() throws Exception {
98 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "getAAIservice");
99 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "initialize");
100 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("getAAIservice");
104 public void testGetAAIservice() throws Exception {
106 mockStatic(FrameworkUtil.class);
107 Bundle mockedBundle = mock(Bundle.class);
108 BundleContext mockedBundleContext = mock(BundleContext.class);
109 ServiceReference mockedServiceReference = mock(ServiceReference.class);
110 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
111 PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
112 PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
113 .getServiceReference(AAIService.class.getName());
115 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
116 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
119 PowerMockito.doReturn(null).when(mockedBundleContext)
120 .getServiceReference(AAIService.class.getName());
121 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
122 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
126 public void testWaitMethod() throws Exception {
127 mockStatic(Thread.class);
128 params.put("waitTime", "1");
129 mockedExecuteNodeActionImpl.waitMethod(params, svcLogicContext);
130 verifyStatic(times(1));
134 public void testGetResource() throws Exception {
135 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
136 PowerMockito.doReturn(queryStatus).when(aaiService).query(resourceType, false, null,
137 resourceKey, prefix, null, svcLogicContext);
139 mockedExecuteNodeActionImpl.getResource(params, svcLogicContext);
141 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
142 verify(aaiService, times(1)).query(resourceType, false, null,
143 resourceKey, prefix, null, svcLogicContext);
144 assertEquals(queryStatus.toString(), svcLogicContext.getAttribute("getResource_result"));
148 public void testPostResource() throws Exception {
151 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
152 PowerMockito.doReturn(queryStatus).when(aaiService).update(eq(resourceType), eq(resourceKey), anyMap(),
153 eq(prefix), eq(svcLogicContext));
155 mockedExecuteNodeActionImpl.postResource(params, svcLogicContext);
157 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
158 verify(aaiService, times(1)).update(eq(resourceType), eq(resourceKey), anyMap(),
159 eq(prefix), eq(svcLogicContext));
160 assertEquals(svcLogicContext.getAttribute("postResource_result"), queryStatus.toString());
164 public void testDeleteResource() throws Exception {
165 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
167 PowerMockito.doReturn(queryStatus).when(aaiService).delete(eq(resourceType), eq(resourceKey),
168 eq(svcLogicContext));
170 mockedExecuteNodeActionImpl.deleteResource(params, svcLogicContext);
172 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
173 verify(aaiService, times(1)).delete(eq(resourceType), eq(resourceKey),
174 eq(svcLogicContext));
175 assertEquals(svcLogicContext.getAttribute("deleteResource_result"), queryStatus.toString());
179 public void testGetVnfHierarchySuccess() throws Exception {
180 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
181 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(), eq
183 PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
184 any(SvcLogicContext.class))).thenReturn(queryStatus);
186 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
188 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
189 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
190 assertEquals("SUCCESS", svcLogicContext.getAttribute("getVnfHierarchy_result"));
193 @Test(expected = APPCException.class)
194 public void testGetVnfHierarchyFailure() throws Exception {
195 queryStatus = SvcLogicResource.QueryStatus.FAILURE;
196 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
197 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(),
198 eq(svcLogicContext));
199 PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
200 any(SvcLogicContext.class))).thenReturn(queryStatus);
202 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
204 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
205 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
206 assertEquals("FAILURE", svcLogicContext.getAttribute("getVnfHierarchy_result"));
207 assertTrue(svcLogicContext.getAttribute("output.status.message") != null);
211 public void testPopulateVnfcsDetailsinContext() throws Exception {
212 Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
213 Set<String> vServersList = new HashSet<>();
214 vnfcHierarchyMap.put("SMP", vServersList);
215 vServersList.add("smp-0-url");
216 vServersList.add("smp-1-url");
218 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
219 PowerMockito.when(aaiService.query(eq("vnfc"), eq(false), anyString(),
220 eq("vnfc-name = 'SMP'"), eq("vnfcRetrived"), anyString(), any(SvcLogicContext.class)))
221 .thenReturn(queryStatus);
223 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext",
224 vnfcHierarchyMap, svcLogicContext);
226 verify(mockedExecuteNodeActionImpl, times(1)).getResource(anyMap(),
227 any(SvcLogicContext.class));
228 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
229 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].TYPE"));
230 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].NAME"));
231 assertEquals("2", svcLogicContext.getAttribute("VNF.VNFC[0].VM_COUNT"));
232 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[0].URL")));
233 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[1].URL")));