3ef368bc40c00ed67f9e34e3cb1c191689ce0ac5
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.dg.aai.impl;
24
25 import org.junit.runner.RunWith;
26 import org.openecomp.appc.dg.aai.Constants;
27 import org.openecomp.appc.dg.aai.impl.AAIPluginImpl;
28 import org.openecomp.appc.dg.common.dao.DAOService;
29 import org.openecomp.appc.dg.common.impl.LicenseManagerImpl;
30 import org.openecomp.appc.exceptions.APPCException;
31 import org.openecomp.sdnc.sli.SvcLogicContext;
32 import org.openecomp.sdnc.sli.SvcLogicResource;
33 import org.openecomp.sdnc.sli.aai.AAIClient;
34 import org.osgi.framework.Bundle;
35 import org.osgi.framework.BundleContext;
36 import org.osgi.framework.FrameworkUtil;
37 import org.osgi.framework.ServiceReference;
38 import org.powermock.api.mockito.PowerMockito;
39 import org.powermock.core.classloader.annotations.PrepareForTest;
40 import org.powermock.modules.junit4.PowerMockRunner;
41
42 import java.util.HashMap;
43 import java.util.Map;
44
45 import static org.junit.Assert.*;
46
47
48 @RunWith(PowerMockRunner.class)
49 @PrepareForTest({AAIPluginImpl.class, FrameworkUtil.class})
50 public class AAIPluginImplTest {
51     private AAIPluginImpl aaiPlugin;
52     private AAIClientMock aaiClient;
53
54     private final BundleContext bundleContext= Mockito.mock(BundleContext.class);
55     private final Bundle bundleService=Mockito.mock(Bundle.class);
56     private final ServiceReference sref=Mockito.mock(ServiceReference.class);
57
58     String prefix = "aai.input.data";
59     String vnfId = "test_VNF";
60     String vnfId1 = "test_VNF1";
61     String vnfId2 = "test_VNF2";
62     String vnfId3 = "test_VNF3";
63
64     @Before
65     public void setUp() throws NoSuchFieldException, IllegalAccessException {
66         aaiClient = new AAIClientMock();
67         PowerMockito.mockStatic(FrameworkUtil.class);
68         PowerMockito.when(FrameworkUtil.getBundle(Matchers.any(Class.class))).thenReturn(bundleService);
69         PowerMockito.when(bundleService.getBundleContext()).thenReturn(bundleContext);
70         PowerMockito.when(bundleContext.getServiceReference(Matchers.any(Class.class))).thenReturn(sref);
71         PowerMockito.when(bundleContext.getService(sref)).thenReturn(aaiClient);
72         aaiPlugin = new AAIPluginImpl();
73
74
75     }
76
77
78
79
80     @Test
81     public void testPostGenericVnfData() throws Exception {
82         Map<String, String> params = new HashMap<>();
83         params.put(prefix+"."+"license-key-uuid", "123");
84         params.put(prefix+"."+"license-assignment-group-uuid", "1234");
85         params.put(prefix+"."+"data.license-key", "12345");
86
87         HashMap<String, String> mockAAI = new HashMap<>();
88         aaiClient.setMockAAI(mockAAI);
89         SvcLogicContext ctx = new SvcLogicContext();
90         ctx.setAttribute("aai.vnfID", vnfId);
91         ctx.setAttribute("aai.prefix", prefix);
92
93         aaiPlugin.postGenericVnfData(params, ctx);
94
95         Assert.assertEquals("wrong license-key-uuid","123", mockAAI.get("license-key-uuid"));
96         Assert.assertEquals("wrong license-assignment-group-uuid","1234", mockAAI.get("license-assignment-group-uuid"));
97         Assert.assertEquals("wrong data.license-key","12345", mockAAI.get("data.license-key"));
98     }
99
100
101     @Test
102     public void testPostGenericVnfDataNegativeVnfNotFound() throws Exception {
103         Map<String, String> params = new HashMap<>();
104         params.put(prefix+"."+"license-key-uuid", "123");
105         params.put(prefix+"."+"license-assignment-group-uuid", "1234");
106         params.put(prefix+"."+"data.license-key", "12345");
107
108         HashMap<String, String> mockAAI = new HashMap<>();
109
110         aaiClient.setMockAAI(mockAAI);
111         SvcLogicContext ctx = new SvcLogicContext();
112         ctx.setAttribute("aai.vnfID", vnfId1);
113         ctx.setAttribute("aai.prefix", prefix);
114
115         try {
116             aaiPlugin.postGenericVnfData(params, ctx);
117             Assert.assertTrue(false);
118         } catch (APPCException e) {
119             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
120         }
121
122     }
123
124
125     @Test
126     public void testPostGenericVnfDataNegativeFailure() throws Exception {
127         Map<String, String> params = new HashMap<>();
128         params.put(prefix+"."+"license-key-uuid", "123");
129         params.put(prefix+"."+"license-assignment-group-uuid", "1234");
130         params.put(prefix+"."+"data.license-key", "12345");
131
132         HashMap<String, String> mockAAI = new HashMap<>();
133
134         aaiClient.setMockAAI(mockAAI);
135         SvcLogicContext ctx = new SvcLogicContext();
136         ctx.setAttribute("aai.vnfID", vnfId2);
137         ctx.setAttribute("aai.prefix", prefix);
138
139         try {
140             aaiPlugin.postGenericVnfData(params, ctx);
141             Assert.assertTrue(false);
142         } catch (APPCException e) {
143             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
144         }
145
146     }
147
148
149     @Test
150     public void testPostGenericVnfDataNegativeSvcLogicException() throws Exception {
151         Map<String, String> params = new HashMap<>();
152         params.put(prefix+"."+"license-key-uuid", "123");
153         params.put(prefix+"."+"license-assignment-group-uuid", "1234");
154         params.put(prefix+"."+"data.license-key", "12345");
155
156         HashMap<String, String> mockAAI = new HashMap<>();
157
158         aaiClient.setMockAAI(mockAAI);
159         SvcLogicContext ctx = new SvcLogicContext();
160         ctx.setAttribute("aai.vnfID", vnfId3);
161         ctx.setAttribute("aai.prefix", prefix);
162
163         try {
164             aaiPlugin.postGenericVnfData(params, ctx);
165             Assert.assertTrue(false);
166         } catch (APPCException e) {
167             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
168         }
169
170     }
171
172     @Test
173     public void testGetGenericVnfData() throws Exception {
174         String vnfNameKey = "vnf-name";
175         String vnfType = "VSCP";
176         String vnfTypeKey = "vnf-type";
177         String provStatus = "Active";
178         String provStatusKey = "prov-status";
179         String orchestrationStatus = "Running";
180         String orchestrationStatusKey = "orchestration-status";
181
182         Map<String, String> params = new HashMap<>();
183         HashMap<String, String> mockAAI = new HashMap<>();
184         mockAAI.put(vnfNameKey,vnfId);
185         mockAAI.put(vnfTypeKey,vnfType);
186         mockAAI.put(provStatusKey, provStatus);
187         mockAAI.put(orchestrationStatusKey, orchestrationStatus);
188         aaiClient.setMockAAI(mockAAI);
189
190         SvcLogicContext ctx = new SvcLogicContext();
191         ctx.setAttribute("aai.vnfID", vnfId);
192         ctx.setAttribute("aai.prefix", prefix);
193
194
195         aaiPlugin.getGenericVnfData(params, ctx);
196
197         Assert.assertEquals("wrong "+vnfNameKey,vnfId, ctx.getAttribute(prefix + "." + vnfNameKey));
198         Assert.assertEquals("wrong "+orchestrationStatusKey,orchestrationStatus, ctx.getAttribute(prefix + "." + orchestrationStatusKey));
199         Assert.assertEquals("wrong "+vnfTypeKey,vnfType, ctx.getAttribute(prefix + "." +  vnfTypeKey));
200         Assert.assertEquals("wrong "+provStatusKey,provStatus, ctx.getAttribute(prefix + "." + provStatusKey ));
201     }
202
203
204
205
206     @Test
207     public void testGetGenericVnfDataNegativeVnfNotFound() throws Exception {
208
209         Map<String, String> params = new HashMap<>();
210         SvcLogicContext ctx = new SvcLogicContext();
211         ctx.setAttribute("aai.vnfID", vnfId1);
212         ctx.setAttribute("aai.prefix", prefix);
213
214
215         try {
216             aaiPlugin.getGenericVnfData(params, ctx);
217             Assert.assertTrue(false);
218         } catch (APPCException e) {
219             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
220         }
221     }
222
223
224     @Test
225     public void testGetGenericVnfDataNegativeFailure() throws Exception {
226
227         Map<String, String> params = new HashMap<>();
228         SvcLogicContext ctx = new SvcLogicContext();
229         ctx.setAttribute("aai.vnfID", vnfId2);
230         ctx.setAttribute("aai.prefix", prefix);
231
232         try {
233             aaiPlugin.getGenericVnfData(params, ctx);
234             Assert.assertTrue(false);
235         } catch (APPCException e) {
236             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
237         }
238     }
239
240
241     @Test
242     public void testGetGenericVnfDataNegativeSvcLogicException() throws Exception {
243
244         Map<String, String> params = new HashMap<>();
245         SvcLogicContext ctx = new SvcLogicContext();
246         ctx.setAttribute("aai.vnfID", vnfId3);
247         ctx.setAttribute("aai.prefix", prefix);
248
249         try {
250             aaiPlugin.getGenericVnfData(params, ctx);
251             Assert.assertTrue(false);
252         } catch (APPCException e) {
253             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
254         }
255     }
256
257 }