Merge of new rebased code
[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.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;
40
41 import java.util.HashMap;
42 import java.util.Map;
43
44 import static org.junit.Assert.*;
45
46
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({AAIPluginImpl.class, FrameworkUtil.class})
49 public class AAIPluginImplTest {
50     private AAIPluginImpl aaiPlugin;
51     private AAIClientMock aaiClient;
52
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);
56
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";
62
63     @Before
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();
72
73
74     }
75
76
77
78
79     @Test
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");
85
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);
91
92         aaiPlugin.postGenericVnfData(params, ctx);
93
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"));
97     }
98
99
100     @Test
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");
106
107         HashMap<String, String> mockAAI = new HashMap<>();
108
109         aaiClient.setMockAAI(mockAAI);
110         SvcLogicContext ctx = new SvcLogicContext();
111         ctx.setAttribute("aai.vnfID", vnfId1);
112         ctx.setAttribute("aai.prefix", prefix);
113
114         try {
115             aaiPlugin.postGenericVnfData(params, ctx);
116             Assert.assertTrue(false);
117         } catch (APPCException e) {
118             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
119         }
120
121     }
122
123
124     @Test
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");
130
131         HashMap<String, String> mockAAI = new HashMap<>();
132
133         aaiClient.setMockAAI(mockAAI);
134         SvcLogicContext ctx = new SvcLogicContext();
135         ctx.setAttribute("aai.vnfID", vnfId2);
136         ctx.setAttribute("aai.prefix", prefix);
137
138         try {
139             aaiPlugin.postGenericVnfData(params, ctx);
140             Assert.assertTrue(false);
141         } catch (APPCException e) {
142             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
143         }
144
145     }
146
147
148     @Test
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");
154
155         HashMap<String, String> mockAAI = new HashMap<>();
156
157         aaiClient.setMockAAI(mockAAI);
158         SvcLogicContext ctx = new SvcLogicContext();
159         ctx.setAttribute("aai.vnfID", vnfId3);
160         ctx.setAttribute("aai.prefix", prefix);
161
162         try {
163             aaiPlugin.postGenericVnfData(params, ctx);
164             Assert.assertTrue(false);
165         } catch (APPCException e) {
166             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
167         }
168
169     }
170
171     @Test
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";
180
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);
188
189         SvcLogicContext ctx = new SvcLogicContext();
190         ctx.setAttribute("aai.vnfID", vnfId);
191         ctx.setAttribute("aai.prefix", prefix);
192
193
194         aaiPlugin.getGenericVnfData(params, ctx);
195
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 ));
200     }
201
202
203
204
205     @Test
206     public void testGetGenericVnfDataNegativeVnfNotFound() throws Exception {
207
208         Map<String, String> params = new HashMap<>();
209         SvcLogicContext ctx = new SvcLogicContext();
210         ctx.setAttribute("aai.vnfID", vnfId1);
211         ctx.setAttribute("aai.prefix", prefix);
212
213
214         try {
215             aaiPlugin.getGenericVnfData(params, ctx);
216             Assert.assertTrue(false);
217         } catch (APPCException e) {
218             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
219         }
220     }
221
222
223     @Test
224     public void testGetGenericVnfDataNegativeFailure() throws Exception {
225
226         Map<String, String> params = new HashMap<>();
227         SvcLogicContext ctx = new SvcLogicContext();
228         ctx.setAttribute("aai.vnfID", vnfId2);
229         ctx.setAttribute("aai.prefix", prefix);
230
231         try {
232             aaiPlugin.getGenericVnfData(params, ctx);
233             Assert.assertTrue(false);
234         } catch (APPCException e) {
235             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
236         }
237     }
238
239
240     @Test
241     public void testGetGenericVnfDataNegativeSvcLogicException() throws Exception {
242
243         Map<String, String> params = new HashMap<>();
244         SvcLogicContext ctx = new SvcLogicContext();
245         ctx.setAttribute("aai.vnfID", vnfId3);
246         ctx.setAttribute("aai.prefix", prefix);
247
248         try {
249             aaiPlugin.getGenericVnfData(params, ctx);
250             Assert.assertTrue(false);
251         } catch (APPCException e) {
252             Assert.assertNotNull(ctx.getAttribute(Constants.ATTRIBUTE_ERROR_MESSAGE));
253         }
254     }
255
256 }