Increase sonar coverage for common
[appc.git] / appc-dg-util / appc-dg-util-bundle / src / test / org / openecomp / appc / dg / util / impl / ExecuteNodeActionImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.dg.util.impl;
26
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;
45
46 import java.util.HashMap;
47 import java.util.HashSet;
48 import java.util.Map;
49 import java.util.Set;
50
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;
63
64 @RunWith(PowerMockRunner.class)
65 @PrepareForTest({ExecuteNodeActionImpl.class, FrameworkUtil.class, Thread.class})
66 public class ExecuteNodeActionImplTest {
67     @Spy
68     private ExecuteNodeActionImpl mockedExecuteNodeActionImpl = new ExecuteNodeActionImpl();
69     @Mock
70     private EELFLogger eelfLogger;
71     @Mock
72     private AAIService aaiService;
73
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";
79
80     private Map<String, String> params = new HashMap<>();
81     private SvcLogicContext svcLogicContext = new SvcLogicContext();
82     private SvcLogicResource.QueryStatus queryStatus = SvcLogicResource.QueryStatus.SUCCESS;
83
84
85     @Before
86     public void setUp() throws Exception {
87         Whitebox.setInternalState(mockedExecuteNodeActionImpl, "logger", eelfLogger);
88         Whitebox.setInternalState(mockedExecuteNodeActionImpl, "aaiService", aaiService);
89
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);
95     }
96
97     @Test
98     public void testInitialize() throws Exception {
99         PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "getAAIservice");
100         Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "initialize");
101         verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("getAAIservice");
102     }
103
104     @Test
105     public void testGetAAIservice() throws Exception {
106         // sref is not null
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());
115
116         Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
117         verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
118
119         // sref is null
120         PowerMockito.doReturn(null).when(mockedBundleContext)
121             .getServiceReference(AAIService.class.getName());
122         Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "getAAIservice");
123         verify(mockedBundleContext, times(1)).getService(mockedServiceReference);
124     }
125
126     @Test
127     public void testWaitMethod() throws Exception {
128         mockStatic(Thread.class);
129         params.put("waitTime", "1");
130         mockedExecuteNodeActionImpl.waitMethod(params, svcLogicContext);
131         verifyStatic(times(1));
132     }
133
134     @Test
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);
139
140         mockedExecuteNodeActionImpl.getResource(params, svcLogicContext);
141
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"));
146     }
147
148     @Test
149     public void testPostResource() throws Exception {
150
151
152         PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
153         PowerMockito.doReturn(queryStatus).when(aaiService).update(eq(resourceType), eq(resourceKey), anyMap(),
154             eq(prefix), eq(svcLogicContext));
155
156         mockedExecuteNodeActionImpl.postResource(params, svcLogicContext);
157
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());
162     }
163
164     @Test
165     public void testDeleteResource() throws Exception {
166         PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
167
168         PowerMockito.doReturn(queryStatus).when(aaiService).delete(eq(resourceType), eq(resourceKey),
169             eq(svcLogicContext));
170
171         mockedExecuteNodeActionImpl.deleteResource(params, svcLogicContext);
172
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());
177     }
178
179     @Test
180     public void testGetVnfHierarchySuccess() throws Exception {
181         PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "initialize");
182         PowerMockito.doNothing().when(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext", anyMap(), eq
183             (svcLogicContext));
184         PowerMockito.when(aaiService.query(any(), eq(false), anyString(), any(), any(), anyString(),
185             any(SvcLogicContext.class))).thenReturn(queryStatus);
186
187         mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
188
189         verifyPrivate(mockedExecuteNodeActionImpl, times(1)).invoke("initialize");
190         assertEquals("0", svcLogicContext.getAttribute("VNF.VNFCCount"));
191         assertEquals("SUCCESS", svcLogicContext.getAttribute("getVnfHierarchy_result"));
192     }
193
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);
202
203         mockedExecuteNodeActionImpl.getVnfHierarchy(params, svcLogicContext);
204
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);
209     }
210
211     @Test
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");
218
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);
223
224         Whitebox.invokeMethod(mockedExecuteNodeActionImpl, "populateVnfcsDetailsinContext",
225             vnfcHierarchyMap, svcLogicContext);
226
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")));
235     }
236 }