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