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;
24 import org.junit.runner.RunWith;
25 import org.openecomp.appc.dg.aai.Constants;
26 import org.openecomp.appc.dg.aai.impl.AAIPluginImpl;
27 import org.openecomp.appc.dg.common.dao.DAOService;
28 import org.openecomp.appc.dg.common.impl.LicenseManagerImpl;
29 import org.openecomp.appc.exceptions.APPCException;
30 import org.openecomp.sdnc.sli.SvcLogicContext;
31 import org.openecomp.sdnc.sli.SvcLogicResource;
32 import org.openecomp.sdnc.sli.aai.AAIClient;
33 import org.osgi.framework.Bundle;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.FrameworkUtil;
36 import org.osgi.framework.ServiceReference;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.PrepareForTest;
39 import org.powermock.modules.junit4.PowerMockRunner;
41 import java.util.HashMap;
44 import static org.junit.Assert.*;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({AAIPluginImpl.class, FrameworkUtil.class})
49 public class AAIPluginImplTest {
50 private AAIPluginImpl aaiPlugin;
51 private AAIClientMock aaiClient;
53 private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
54 private final Bundle bundleService=Mockito.mock(Bundle.class);
55 private final ServiceReference sref=Mockito.mock(ServiceReference.class);
57 String prefix = "aai.input.data";
58 String vnfId = "test_VNF";
59 String vnfId1 = "test_VNF1";
60 String vnfId2 = "test_VNF2";
61 String vnfId3 = "test_VNF3";
64 public void setUp() throws NoSuchFieldException, IllegalAccessException {
65 aaiClient = new AAIClientMock();
66 PowerMockito.mockStatic(FrameworkUtil.class);
67 PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
68 PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
69 PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
70 PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiClient);
71 aaiPlugin = new AAIPluginImpl();
80 public void testPostGenericVnfData() throws Exception {
81 Map<String, String> params = new HashMap<>();
82 params.put(prefix+"."+"license-key-uuid", "123");
83 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
84 params.put(prefix+"."+"data.license-key", "12345");
86 HashMap<String, String> mockAAI = new HashMap<>();
87 aaiClient.setMockAAI(mockAAI);
88 SvcLogicContext ctx = new SvcLogicContext();
89 ctx.setAttribute("aai.vnfID", vnfId);
90 ctx.setAttribute("aai.prefix", prefix);
92 aaiPlugin.postGenericVnfData(params, ctx);
94 Assert.assertEquals("wrong license-key-uuid","123", mockAAI.get("license-key-uuid"));
95 Assert.assertEquals("wrong license-assignment-group-uuid","1234", mockAAI.get("license-assignment-group-uuid"));
96 Assert.assertEquals("wrong data.license-key","12345", mockAAI.get("data.license-key"));
101 public void testPostGenericVnfDataNegativeVnfNotFound() throws Exception {
102 Map<String, String> params = new HashMap<>();
103 params.put(prefix+"."+"license-key-uuid", "123");
104 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
105 params.put(prefix+"."+"data.license-key", "12345");
107 HashMap<String, String> mockAAI = new HashMap<>();
109 aaiClient.setMockAAI(mockAAI);
110 SvcLogicContext ctx = new SvcLogicContext();
111 ctx.setAttribute("aai.vnfID", vnfId1);
112 ctx.setAttribute("aai.prefix", prefix);
115 aaiPlugin.postGenericVnfData(params, ctx);
116 Assert.assertTrue(false);
117 } catch (APPCException e) {
118 Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
125 public void testPostGenericVnfDataNegativeFailure() throws Exception {
126 Map<String, String> params = new HashMap<>();
127 params.put(prefix+"."+"license-key-uuid", "123");
128 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
129 params.put(prefix+"."+"data.license-key", "12345");
131 HashMap<String, String> mockAAI = new HashMap<>();
133 aaiClient.setMockAAI(mockAAI);
134 SvcLogicContext ctx = new SvcLogicContext();
135 ctx.setAttribute("aai.vnfID", vnfId2);
136 ctx.setAttribute("aai.prefix", prefix);
139 aaiPlugin.postGenericVnfData(params, ctx);
140 Assert.assertTrue(false);
141 } catch (APPCException e) {
142 Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
149 public void testPostGenericVnfDataNegativeSvcLogicException() throws Exception {
150 Map<String, String> params = new HashMap<>();
151 params.put(prefix+"."+"license-key-uuid", "123");
152 params.put(prefix+"."+"license-assignment-group-uuid", "1234");
153 params.put(prefix+"."+"data.license-key", "12345");
155 HashMap<String, String> mockAAI = new HashMap<>();
157 aaiClient.setMockAAI(mockAAI);
158 SvcLogicContext ctx = new SvcLogicContext();
159 ctx.setAttribute("aai.vnfID", vnfId3);
160 ctx.setAttribute("aai.prefix", prefix);
163 aaiPlugin.postGenericVnfData(params, ctx);
164 Assert.assertTrue(false);
165 } catch (APPCException e) {
166 Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
172 public void testGetGenericVnfData() throws Exception {
173 String vnfNameKey = "vnf-name";
174 String vnfType = "VSCP";
175 String vnfTypeKey = "vnf-type";
176 String provStatus = "Active";
177 String provStatusKey = "prov-status";
178 String orchestrationStatus = "Running";
179 String orchestrationStatusKey = "orchestration-status";
181 Map<String, String> params = new HashMap<>();
182 HashMap<String, String> mockAAI = new HashMap<>();
183 mockAAI.put(vnfNameKey,vnfId);
184 mockAAI.put(vnfTypeKey,vnfType);
185 mockAAI.put(provStatusKey, provStatus);
186 mockAAI.put(orchestrationStatusKey, orchestrationStatus);
187 aaiClient.setMockAAI(mockAAI);
189 SvcLogicContext ctx = new SvcLogicContext();
190 ctx.setAttribute("aai.vnfID", vnfId);
191 ctx.setAttribute("aai.prefix", prefix);
194 aaiPlugin.getGenericVnfData(params, ctx);
196 Assert.assertEquals("wrong "+vnfNameKey,vnfId, ctx.getAttribute(prefix + "." + vnfNameKey));
197 Assert.assertEquals("wrong "+orchestrationStatusKey,orchestrationStatus, ctx.getAttribute(prefix + "." + orchestrationStatusKey));
198 Assert.assertEquals("wrong "+vnfTypeKey,vnfType, ctx.getAttribute(prefix + "." + vnfTypeKey));
199 Assert.assertEquals("wrong "+provStatusKey,provStatus, ctx.getAttribute(prefix + "." + provStatusKey ));
206 public void testGetGenericVnfDataNegativeVnfNotFound() throws Exception {
208 Map<String, String> params = new HashMap<>();
209 SvcLogicContext ctx = new SvcLogicContext();
210 ctx.setAttribute("aai.vnfID", vnfId1);
211 ctx.setAttribute("aai.prefix", prefix);
215 aaiPlugin.getGenericVnfData(params, ctx);
216 Assert.assertTrue(false);
217 } catch (APPCException e) {
218 Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
224 public void testGetGenericVnfDataNegativeFailure() throws Exception {
226 Map<String, String> params = new HashMap<>();
227 SvcLogicContext ctx = new SvcLogicContext();
228 ctx.setAttribute("aai.vnfID", vnfId2);
229 ctx.setAttribute("aai.prefix", prefix);
232 aaiPlugin.getGenericVnfData(params, ctx);
233 Assert.assertTrue(false);
234 } catch (APPCException e) {
235 Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
241 public void testGetGenericVnfDataNegativeSvcLogicException() throws Exception {
243 Map<String, String> params = new HashMap<>();
244 SvcLogicContext ctx = new SvcLogicContext();
245 ctx.setAttribute("aai.vnfID", vnfId3);
246 ctx.setAttribute("aai.prefix", prefix);
249 aaiPlugin.getGenericVnfData(params, ctx);
250 Assert.assertTrue(false);
251 } catch (APPCException e) {
252 Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));