Moving all files to root directory
[appc.git] / appc-dg / appc-dg-shared / appc-dg-aai / src / main / test / java / org / openecomp / appc / dg / aai / impl / AAIPluginImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
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  */
21
22 package org.openecomp.appc.dg.aai.impl;
23
24 import org.junit.*;
25 import org.junit.runner.RunWith;
26 import org.mockito.*;
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;
42
43 import java.lang.reflect.Field;
44 import java.util.HashMap;
45 import java.util.Map;
46
47 import static org.junit.Assert.*;
48 import static org.powermock.api.support.SuppressCode.suppressConstructor;
49
50
51 @RunWith(PowerMockRunner.class)
52 @PrepareForTest({AAIPluginImpl.class, FrameworkUtil.class})
53 public class AAIPluginImplTest {
54     private AAIPluginImpl aaiPlugin;
55     private AAIClientMock aaiClient;
56
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);
60
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";
66
67     @Before
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();
76
77
78     }
79
80
81
82
83     @Test
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");
89
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);
95
96         aaiPlugin.postGenericVnfData(params, ctx);
97
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"));
101     }
102
103
104     @Test
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");
110
111         HashMap<String, String> mockAAI = new HashMap<>();
112
113         aaiClient.setMockAAI(mockAAI);
114         SvcLogicContext ctx = new SvcLogicContext();
115         ctx.setAttribute("aai.vnfID", vnfId1);
116         ctx.setAttribute("aai.prefix", prefix);
117
118         try {
119             aaiPlugin.postGenericVnfData(params, ctx);
120             Assert.assertTrue(false);
121         } catch (APPCException e) {
122             Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
123         }
124
125     }
126
127
128     @Test
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");
134
135         HashMap<String, String> mockAAI = new HashMap<>();
136
137         aaiClient.setMockAAI(mockAAI);
138         SvcLogicContext ctx = new SvcLogicContext();
139         ctx.setAttribute("aai.vnfID", vnfId2);
140         ctx.setAttribute("aai.prefix", prefix);
141
142         try {
143             aaiPlugin.postGenericVnfData(params, ctx);
144             Assert.assertTrue(false);
145         } catch (APPCException e) {
146             Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
147         }
148
149     }
150
151
152     @Test
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");
158
159         HashMap<String, String> mockAAI = new HashMap<>();
160
161         aaiClient.setMockAAI(mockAAI);
162         SvcLogicContext ctx = new SvcLogicContext();
163         ctx.setAttribute("aai.vnfID", vnfId3);
164         ctx.setAttribute("aai.prefix", prefix);
165
166         try {
167             aaiPlugin.postGenericVnfData(params, ctx);
168             Assert.assertTrue(false);
169         } catch (APPCException e) {
170             Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
171         }
172
173     }
174
175     @Test
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";
184
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);
192
193         SvcLogicContext ctx = new SvcLogicContext();
194         ctx.setAttribute("aai.vnfID", vnfId);
195         ctx.setAttribute("aai.prefix", prefix);
196
197
198         aaiPlugin.getGenericVnfData(params, ctx);
199
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 ));
204     }
205
206
207
208
209     @Test
210     public void testGetGenericVnfDataNegativeVnfNotFound() throws Exception {
211
212         Map<String, String> params = new HashMap<>();
213         SvcLogicContext ctx = new SvcLogicContext();
214         ctx.setAttribute("aai.vnfID", vnfId1);
215         ctx.setAttribute("aai.prefix", prefix);
216
217
218         try {
219             aaiPlugin.getGenericVnfData(params, ctx);
220             Assert.assertTrue(false);
221         } catch (APPCException e) {
222             Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
223         }
224     }
225
226
227     @Test
228     public void testGetGenericVnfDataNegativeFailure() throws Exception {
229
230         Map<String, String> params = new HashMap<>();
231         SvcLogicContext ctx = new SvcLogicContext();
232         ctx.setAttribute("aai.vnfID", vnfId2);
233         ctx.setAttribute("aai.prefix", prefix);
234
235         try {
236             aaiPlugin.getGenericVnfData(params, ctx);
237             Assert.assertTrue(false);
238         } catch (APPCException e) {
239             Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
240         }
241     }
242
243
244     @Test
245     public void testGetGenericVnfDataNegativeSvcLogicException() throws Exception {
246
247         Map<String, String> params = new HashMap<>();
248         SvcLogicContext ctx = new SvcLogicContext();
249         ctx.setAttribute("aai.vnfID", vnfId3);
250         ctx.setAttribute("aai.prefix", prefix);
251
252         try {
253             aaiPlugin.getGenericVnfData(params, ctx);
254             Assert.assertTrue(false);
255         } catch (APPCException e) {
256             Assert.assertNotNull(ctx.getAttribute(Constants.DG_OUTPUT_STATUS_MESSAGE));
257         }
258     }
259
260 }