2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.dg.aai.impl;
25 import org.junit.runner.RunWith;
27 import org.openecomp.appc.dg.aai.Constants;
28 import org.openecomp.appc.dg.aai.impl.AAIPluginImpl;
29 import org.openecomp.appc.dg.common.dao.DAOService;
30 import org.openecomp.appc.dg.common.impl.LicenseManagerImpl;
31 import org.openecomp.appc.exceptions.APPCException;
32 import org.openecomp.sdnc.sli.SvcLogicContext;
33 import org.openecomp.sdnc.sli.SvcLogicResource;
34 import org.openecomp.sdnc.sli.aai.AAIClient;
35 import org.osgi.framework.Bundle;
36 import org.osgi.framework.BundleContext;
37 import org.osgi.framework.FrameworkUtil;
38 import org.osgi.framework.ServiceReference;
39 import org.powermock.api.mockito.PowerMockito;
40 import org.powermock.core.classloader.annotations.PrepareForTest;
41 import org.powermock.modules.junit4.PowerMockRunner;
43 import java.lang.reflect.Field;
44 import java.util.HashMap;
47 import static org.junit.Assert.*;
48 import static org.powermock.api.support.SuppressCode.suppressConstructor;
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({AAIPluginImpl.class, FrameworkUtil.class})
53 public class AAIPluginImplTest {
54 private AAIPluginImpl aaiPlugin;
55 private AAIClientMock aaiClient;
57 private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
58 private final Bundle bundleService=Mockito.mock(Bundle.class);
59 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
61 String prefix = "aai.input.data";
62 String vnfId = "test_VNF";
63 String vnfId1 = "test_VNF1";
64 String vnfId2 = "test_VNF2";
65 String vnfId3 = "test_VNF3";
68 public void setUp() throws NoSuchFieldException, IllegalAccessException {
69 aaiClient = new AAIClientMock();
70 PowerMockito.mockStatic(FrameworkUtil.class);
71 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
72 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
73 PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
74 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiClient);
75 aaiPlugin = new AAIPluginImpl();
84 public void testPostGenericVnfData() throws Exception {
85 Map<String, String> params = new HashMap<>();
86 params.put(prefix+"."+"license-key-uuid", "123");
87 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
88 params.put(prefix+"."+"data.license-key", "12345");
90 HashMap<String, String> mockAAI = new HashMap<>();
91 aaiClient.setMockAAI(mockAAI);
92 SvcLogicContext ctx = new SvcLogicContext();
93 ctx.setAttribute("aai.vnfID", vnfId);
94 ctx.setAttribute("aai.prefix", prefix);
96 aaiPlugin.postGenericVnfData(params, ctx);
98 Assert.assertEquals("wrong license-key-uuid","123", mockAAI.get("license-key-uuid"));
99 Assert.assertEquals("wrong license-assignment-group-uuid","1234", mockAAI.get("license-assignment-group-uuid"));
100 Assert.assertEquals("wrong data.license-key","12345", mockAAI.get("data.license-key"));
105 public void testPostGenericVnfDataNegativeVnfNotFound() throws Exception {
106 Map<String, String> params = new HashMap<>();
107 params.put(prefix+"."+"license-key-uuid", "123");
108 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
109 params.put(prefix+"."+"data.license-key", "12345");
111 HashMap<String, String> mockAAI = new HashMap<>();
113 aaiClient.setMockAAI(mockAAI);
114 SvcLogicContext ctx = new SvcLogicContext();
115 ctx.setAttribute("aai.vnfID", vnfId1);
116 ctx.setAttribute("aai.prefix", prefix);
119 aaiPlugin.postGenericVnfData(params, ctx);
120 Assert.assertTrue(false);
121 } catch (APPCException e) {
122 Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
129 public void testPostGenericVnfDataNegativeFailure() throws Exception {
130 Map<String, String> params = new HashMap<>();
131 params.put(prefix+"."+"license-key-uuid", "123");
132 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
133 params.put(prefix+"."+"data.license-key", "12345");
135 HashMap<String, String> mockAAI = new HashMap<>();
137 aaiClient.setMockAAI(mockAAI);
138 SvcLogicContext ctx = new SvcLogicContext();
139 ctx.setAttribute("aai.vnfID", vnfId2);
140 ctx.setAttribute("aai.prefix", prefix);
143 aaiPlugin.postGenericVnfData(params, ctx);
144 Assert.assertTrue(false);
145 } catch (APPCException e) {
146 Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
153 public void testPostGenericVnfDataNegativeSvcLogicException() throws Exception {
154 Map<String, String> params = new HashMap<>();
155 params.put(prefix+"."+"license-key-uuid", "123");
156 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
157 params.put(prefix+"."+"data.license-key", "12345");
159 HashMap<String, String> mockAAI = new HashMap<>();
161 aaiClient.setMockAAI(mockAAI);
162 SvcLogicContext ctx = new SvcLogicContext();
163 ctx.setAttribute("aai.vnfID", vnfId3);
164 ctx.setAttribute("aai.prefix", prefix);
167 aaiPlugin.postGenericVnfData(params, ctx);
168 Assert.assertTrue(false);
169 } catch (APPCException e) {
170 Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
176 public void testGetGenericVnfData() throws Exception {
177 String vnfNameKey = "vnf-name";
178 String vnfType = "VSCP";
179 String vnfTypeKey = "vnf-type";
180 String provStatus = "Active";
181 String provStatusKey = "prov-status";
182 String orchestrationStatus = "Running";
183 String orchestrationStatusKey = "orchestration-status";
185 Map<String, String> params = new HashMap<>();
186 HashMap<String, String> mockAAI = new HashMap<>();
187 mockAAI.put(vnfNameKey,vnfId);
188 mockAAI.put(vnfTypeKey,vnfType);
189 mockAAI.put(provStatusKey, provStatus);
190 mockAAI.put(orchestrationStatusKey, orchestrationStatus);
191 aaiClient.setMockAAI(mockAAI);
193 SvcLogicContext ctx = new SvcLogicContext();
194 ctx.setAttribute("aai.vnfID", vnfId);
195 ctx.setAttribute("aai.prefix", prefix);
198 aaiPlugin.getGenericVnfData(params, ctx);
200 Assert.assertEquals("wrong "+vnfNameKey,vnfId, ctx.getAttribute(prefix + "." + vnfNameKey));
201 Assert.assertEquals("wrong "+orchestrationStatusKey,orchestrationStatus, ctx.getAttribute(prefix + "." + orchestrationStatusKey));
202 Assert.assertEquals("wrong "+vnfTypeKey,vnfType, ctx.getAttribute(prefix + "." + vnfTypeKey));
203 Assert.assertEquals("wrong "+provStatusKey,provStatus, ctx.getAttribute(prefix + "." + provStatusKey ));
210 public void testGetGenericVnfDataNegativeVnfNotFound() throws Exception {
212 Map<String, String> params = new HashMap<>();
213 SvcLogicContext ctx = new SvcLogicContext();
214 ctx.setAttribute("aai.vnfID", vnfId1);
215 ctx.setAttribute("aai.prefix", prefix);
219 aaiPlugin.getGenericVnfData(params, ctx);
220 Assert.assertTrue(false);
221 } catch (APPCException e) {
222 Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
228 public void testGetGenericVnfDataNegativeFailure() throws Exception {
230 Map<String, String> params = new HashMap<>();
231 SvcLogicContext ctx = new SvcLogicContext();
232 ctx.setAttribute("aai.vnfID", vnfId2);
233 ctx.setAttribute("aai.prefix", prefix);
236 aaiPlugin.getGenericVnfData(params, ctx);
237 Assert.assertTrue(false);
238 } catch (APPCException e) {
239 Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
245 public void testGetGenericVnfDataNegativeSvcLogicException() throws Exception {
247 Map<String, String> params = new HashMap<>();
248 SvcLogicContext ctx = new SvcLogicContext();
249 ctx.setAttribute("aai.vnfID", vnfId3);
250 ctx.setAttribute("aai.prefix", prefix);
253 aaiPlugin.getGenericVnfData(params, ctx);
254 Assert.assertTrue(false);
255 } catch (APPCException e) {
256 Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));