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, "logger", eelfLogger);
88 Whitebox.setInternalState(mockedExecuteNodeActionImpl, "aaiService", aaiService);
90 params.put("resourceType", resourceType);
91 params.put("prefix", prefix);
92 params.put("resourceKey", resourceKey);
93 params.put("attributeName", attributeName);
94 params.put("attributeValue", attributeValue);
98 public void testInitialize() throws Exception {
99 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "getAAIservice");
100 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "initialize");
101 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("getAAIservice");
105 public void testGetAAIservice() throws Exception {
107 mockStatic(FrameworkUtil.class);
108 Bundle mockedBundle = mock(Bundle.class);
109 BundleContext mockedBundleContext = mock(BundleContext.class);
110 ServiceReference mockedServiceReference = mock(ServiceReference.class);
111 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
112 PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
113 PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
114 .getServiceReference(AAIService.class.getName());
116 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
117 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
120 PowerMockito.doReturn(null).when(mockedBundleContext)
121 .getServiceReference(AAIService.class.getName());
122 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
123 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
127 public void testWaitMethod() throws Exception {
128 mockStatic(Thread.class);
129 params.put("waitTime", "1");
130 mockedExecuteNodeActionImpl.waitMethod(params, svcLogicContext);
131 verifyStatic(times(1));
135 public void testGetResource() throws Exception {
136 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
137 PowerMockito.doReturn(queryStatus).when(aaiService).query(resourceType, false, null,
138 resourceKey, prefix, null, svcLogicContext);
140 mockedExecuteNodeActionImpl.getResource(params, svcLogicContext);
142 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
143 verify(aaiService, times(1)).query(resourceType, false, null,
144 resourceKey, prefix, null, svcLogicContext);
145 assertEquals(queryStatus.toString(), svcLogicContext.getAttribute("getResource_result"));
149 public void testPostResource() throws Exception {
152 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
153 PowerMockito.doReturn(queryStatus).when(aaiService).update(eq(resourceType), eq(resourceKey), anyMap(),
154 eq(prefix), eq(svcLogicContext));
156 mockedExecuteNodeActionImpl.postResource(params, svcLogicContext);
158 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
159 verify(aaiService, times(1)).update(eq(resourceType), eq(resourceKey), anyMap(),
160 eq(prefix), eq(svcLogicContext));
161 assertEquals(svcLogicContext.getAttribute("postResource_result"), queryStatus.toString());
165 public void testDeleteResource() throws Exception {
166 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
168 PowerMockito.doReturn(queryStatus).when(aaiService).delete(eq(resourceType), eq(resourceKey),
169 eq(svcLogicContext));
171 mockedExecuteNodeActionImpl.deleteResource(params, svcLogicContext);
173 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
174 verify(aaiService, times(1)).delete(eq(resourceType), eq(resourceKey),
175 eq(svcLogicContext));
176 assertEquals(svcLogicContext.getAttribute("deleteResource_result"), queryStatus.toString());
180 public void testGetVnfHierarchySuccess() throws Exception {
181 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
182 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(), eq
184 PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
185 any(SvcLogicContext.class))).thenReturn(queryStatus);
187 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
189 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
190 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
191 assertEquals("SUCCESS", svcLogicContext.getAttribute("getVnfHierarchy_result"));
194 @Test(expected = APPCException.class)
195 public void testGetVnfHierarchyFailure() throws Exception {
196 queryStatus = SvcLogicResource.QueryStatus.FAILURE;
197 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
198 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(),
199 eq(svcLogicContext));
200 PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
201 any(SvcLogicContext.class))).thenReturn(queryStatus);
203 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
205 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
206 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
207 assertEquals("FAILURE", svcLogicContext.getAttribute("getVnfHierarchy_result"));
208 assertTrue(svcLogicContext.getAttribute("output.status.message") != null);
212 public void testPopulateVnfcsDetailsinContext() throws Exception {
213 Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
214 Set<String> vServersList = new HashSet<>();
215 vnfcHierarchyMap.put("SMP", vServersList);
216 vServersList.add("smp-0-url");
217 vServersList.add("smp-1-url");
219 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
220 PowerMockito.when(aaiService.query(eq("vnfc"), eq(false), anyString(),
221 eq("vnfc-name = 'SMP'"), eq("vnfcRetrived"), anyString(), any(SvcLogicContext.class)))
222 .thenReturn(queryStatus);
224 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext",
225 vnfcHierarchyMap, svcLogicContext);
227 verify(mockedExecuteNodeActionImpl, times(1)).getResource(anyMap(),
228 any(SvcLogicContext.class));
229 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
230 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].TYPE"));
231 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].NAME"));
232 assertEquals("2", svcLogicContext.getAttribute("VNF.VNFC[0].VM_COUNT"));
233 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[0].URL")));
234 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[1].URL")));