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.Spy;
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;
45 import java.util.HashMap;
46 import java.util.HashSet;
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertTrue;
52 import static org.mockito.Matchers.any;
53 import static org.mockito.Matchers.anyMap;
54 import static org.mockito.Matchers.anyString;
55 import static org.mockito.Matchers.eq;
56 import static org.mockito.Mockito.mock;
57 import static org.mockito.Mockito.times;
58 import static org.mockito.Mockito.verify;
59 import static org.powermock.api.mockito.PowerMockito.mockStatic;
60 import static org.powermock.api.mockito.PowerMockito.verifyPrivate;
61 import static org.powermock.api.mockito.PowerMockito.verifyStatic;
63 @RunWith(PowerMockRunner.class)
64 @PrepareForTest({ExecuteNodeActionImpl.class, FrameworkUtil.class, Thread.class})
65 public class ExecuteNodeActionImplTest {
67 private ExecuteNodeActionImpl mockedExecuteNodeActionImpl = new ExecuteNodeActionImpl();
69 private EELFLogger eelfLogger;
71 private AAIService aaiService;
73 private final String resourceType = "resourceType";
74 private final String prefix = "prefix";
75 private final String resourceKey = "resourceKey";
76 private final String attributeName = "attributeName";
77 private final String attributeValue = "attributeValue";
79 private Map<String, String> params = new HashMap<>();
80 private SvcLogicContext svcLogicContext = new SvcLogicContext();
81 private SvcLogicResource.QueryStatus queryStatus = SvcLogicResource.QueryStatus.SUCCESS;
85 public void setUp() throws Exception {
86 Whitebox.setInternalState(mockedExecuteNodeActionImpl, "aaiService", aaiService);
88 params.put("resourceType", resourceType);
89 params.put("prefix", prefix);
90 params.put("resourceKey", resourceKey);
91 params.put("attributeName", attributeName);
92 params.put("attributeValue", attributeValue);
96 public void testInitialize() throws Exception {
97 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "getAAIservice");
98 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "initialize");
99 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("getAAIservice");
103 public void testGetAAIservice() throws Exception {
105 mockStatic(FrameworkUtil.class);
106 Bundle mockedBundle = mock(Bundle.class);
107 BundleContext mockedBundleContext = mock(BundleContext.class);
108 ServiceReference mockedServiceReference = mock(ServiceReference.class);
109 PowerMockito.when(FrameworkUtil.getBundle(AAIService.class)).thenReturn(mockedBundle);
110 PowerMockito.doReturn(mockedBundleContext).when(mockedBundle).getBundleContext();
111 PowerMockito.doReturn(mockedServiceReference).when(mockedBundleContext)
112 .getServiceReference(AAIService.class.getName());
114 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
115 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
118 PowerMockito.doReturn(null).when(mockedBundleContext)
119 .getServiceReference(AAIService.class.getName());
120 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
121 verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
125 public void testWaitMethod() throws Exception {
126 mockStatic(Thread.class);
127 params.put("waitTime", "1");
128 mockedExecuteNodeActionImpl.waitMethod(params, svcLogicContext);
129 verifyStatic(times(1));
133 public void testGetResource() throws Exception {
134 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
135 PowerMockito.doReturn(queryStatus).when(aaiService).query(resourceType, false, null,
136 resourceKey, prefix, null, svcLogicContext);
138 mockedExecuteNodeActionImpl.getResource(params, svcLogicContext);
140 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
141 verify(aaiService, times(1)).query(resourceType, false, null,
142 resourceKey, prefix, null, svcLogicContext);
143 assertEquals(queryStatus.toString(), svcLogicContext.getAttribute("getResource_result"));
147 public void testPostResource() throws Exception {
150 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
151 PowerMockito.doReturn(queryStatus).when(aaiService).update(eq(resourceType), eq(resourceKey), anyMap(),
152 eq(prefix), eq(svcLogicContext));
154 mockedExecuteNodeActionImpl.postResource(params, svcLogicContext);
156 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
157 verify(aaiService, times(1)).update(eq(resourceType), eq(resourceKey), anyMap(),
158 eq(prefix), eq(svcLogicContext));
159 assertEquals(svcLogicContext.getAttribute("postResource_result"), queryStatus.toString());
163 public void testDeleteResource() throws Exception {
164 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
166 PowerMockito.doReturn(queryStatus).when(aaiService).delete(eq(resourceType), eq(resourceKey),
167 eq(svcLogicContext));
169 mockedExecuteNodeActionImpl.deleteResource(params, svcLogicContext);
171 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
172 verify(aaiService, times(1)).delete(eq(resourceType), eq(resourceKey),
173 eq(svcLogicContext));
174 assertEquals(svcLogicContext.getAttribute("deleteResource_result"), queryStatus.toString());
178 public void testGetVnfHierarchySuccess() throws Exception {
179 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
180 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(), eq
182 PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
183 any(SvcLogicContext.class))).thenReturn(queryStatus);
185 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
187 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
188 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
189 assertEquals("SUCCESS", svcLogicContext.getAttribute("getVnfHierarchy_result"));
192 @Test(expected = APPCException.class)
193 public void testGetVnfHierarchyFailure() throws Exception {
194 queryStatus = SvcLogicResource.QueryStatus.FAILURE;
195 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
196 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(),
197 eq(svcLogicContext));
198 PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
199 any(SvcLogicContext.class))).thenReturn(queryStatus);
201 mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
203 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
204 assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
205 assertEquals("FAILURE", svcLogicContext.getAttribute("getVnfHierarchy_result"));
206 assertTrue(svcLogicContext.getAttribute("output.status.message") != null);
210 public void testPopulateVnfcsDetailsinContext() throws Exception {
211 Map<String, Set<String>> vnfcHierarchyMap = new HashMap<>();
212 Set<String> vServersList = new HashSet<>();
213 vnfcHierarchyMap.put("SMP", vServersList);
214 vServersList.add("smp-0-url");
215 vServersList.add("smp-1-url");
217 PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
218 PowerMockito.when(aaiService.query(eq("vnfc"), eq(false), anyString(),
219 eq("vnfc-name = 'SMP'"), eq("vnfcRetrived"), anyString(), any(SvcLogicContext.class)))
220 .thenReturn(queryStatus);
222 Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext",
223 vnfcHierarchyMap, svcLogicContext);
225 verify(mockedExecuteNodeActionImpl, times(1)).getResource(anyMap(),
226 any(SvcLogicContext.class));
227 verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
228 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].TYPE"));
229 assertEquals(null, svcLogicContext.getAttribute("VNF.VNFC[0].NAME"));
230 assertEquals("2", svcLogicContext.getAttribute("VNF.VNFC[0].VM_COUNT"));
231 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[0].URL")));
232 assertTrue(vServersList.contains(svcLogicContext.getAttribute("VNF.VNFC[0].VM[1].URL")));