d89bd47304fc3952a1202fd807fcababab18d30c
[appc.git] / appc-dg / appc-dg-shared / appc-dg-aai / src / test / java / org / onap / appc / dg / aai / impl / AAIPluginImplTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2018 Ericsson
6  * =============================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.appc.dg.aai.impl;
23
24 import static org.hamcrest.CoreMatchers.isA;
25 import java.util.HashMap;
26 import java.util.Map;
27 import org.junit.Before;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.rules.ExpectedException;
31 import org.junit.runner.RunWith;
32 import org.mockito.Matchers;
33 import org.mockito.Mockito;
34 import org.onap.appc.dg.aai.exception.AAIQueryException;
35 import org.onap.appc.dg.aai.objects.AAIQueryResult;
36 import org.onap.appc.dg.aai.objects.Relationship;
37 import org.onap.appc.exceptions.APPCException;
38 import org.onap.appc.i18n.Msg;
39 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
40 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
41 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
42 import org.onap.ccsdk.sli.core.sli.SvcLogicResource;
43 import org.osgi.framework.FrameworkUtil;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
46 import com.att.eelf.i18n.EELFResourceManager;
47 import org.hamcrest.CoreMatchers;
48 import org.junit.Assert;
49 import org.osgi.framework.Bundle;
50 import org.osgi.framework.BundleContext;
51 import org.osgi.framework.ServiceReference;
52 import org.powermock.api.mockito.PowerMockito;
53
54 @RunWith(PowerMockRunner.class)
55 @PrepareForTest(FrameworkUtil.class)
56 public class AAIPluginImplTest {
57
58     private final BundleContext bundleContext = Mockito.mock(BundleContext.class);
59     private final Bundle bundleService = Mockito.mock(Bundle.class);
60     private final ServiceReference sref = Mockito.mock(ServiceReference.class);
61     private final AAIClient aaiClient = Mockito.mock(AAIClient.class);
62     private SvcLogicContext ctx;
63     private Map<String, String> params;
64
65     @Rule
66     public ExpectedException expectedEx = ExpectedException.none();
67
68     @Before
69     public void setUp() throws NoSuchFieldException, IllegalAccessException {
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.<AAIClient>getService(sref)).thenReturn(aaiClient);
75         params = new HashMap<String, String>();
76         params.put(Constants.AAI_INPUT_DATA + ".suffix", "TEST_DATA");
77     }
78
79     @Test
80     public void testPostGenericVnfDataNotFound() throws APPCException, SvcLogicException {
81         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.NOT_FOUND;
82         Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
83                 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
84         ctx = new SvcLogicContext();
85         AAIPluginImpl impl = new AAIPluginImpl();
86         impl.initialize();
87         expectedEx.expect(APPCException.class);
88         expectedEx.expectMessage("VNF not found with vnf_id null");
89         impl.postGenericVnfData(params, ctx);
90     }
91
92     @Test
93     public void testPostGenericVnfDataFailure() throws APPCException, SvcLogicException {
94         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
95         Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
96                 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
97         ctx = new SvcLogicContext();
98         AAIPluginImpl impl = new AAIPluginImpl();
99         impl.initialize();
100         expectedEx.expect(APPCException.class);
101         expectedEx.expectMessage("Error Querying AAI with vnfID = null");
102         impl.postGenericVnfData(params, ctx);
103     }
104
105     @Test
106     public void testPostGenericVnfDataSucces() throws APPCException, SvcLogicException {
107         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
108         Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
109                 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
110         ctx = new SvcLogicContext();
111         AAIPluginImpl impl = new AAIPluginImpl();
112         impl.initialize();
113         impl.postGenericVnfData(params, ctx);
114         Assert.assertThat(ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE), CoreMatchers.containsString(
115                 "Operation PostGenericVnfData succeed for VNF ID null"));
116     }
117
118     @Test
119     public void testPostGenericVnfDataFailureThrownExeption() throws APPCException, SvcLogicException {
120         Mockito.doThrow(new SvcLogicException()).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
121                 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
122         ctx = new SvcLogicContext();
123         AAIPluginImpl impl = new AAIPluginImpl();
124         impl.initialize();
125         expectedEx.expect(APPCException.class);
126         expectedEx.expectCause(isA(SvcLogicException.class));
127         impl.postGenericVnfData(params, ctx);
128     }
129
130     @Test
131     public void testGetGenericVnfDataNotFound() throws APPCException, SvcLogicException {
132         SvcLogicResource.QueryStatus notFound = SvcLogicResource.QueryStatus.NOT_FOUND;
133         Mockito.doReturn(notFound).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
134                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
135         ctx = new SvcLogicContext();
136         AAIPluginImpl impl = new AAIPluginImpl();
137         impl.initialize();
138         expectedEx.expect(APPCException.class);
139         expectedEx.expectMessage("VNF not found with vnf_id null");
140         impl.getGenericVnfData(params, ctx);
141     }
142
143     @Test
144     public void testGetGenericVnfDataFailure() throws APPCException, SvcLogicException {
145         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
146         Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
147                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
148         ctx = new SvcLogicContext();
149         AAIPluginImpl impl = new AAIPluginImpl();
150         impl.initialize();
151         expectedEx.expect(APPCException.class);
152         expectedEx.expectMessage("Error Querying AAI with vnfID = null");
153         impl.getGenericVnfData(params, ctx);
154     }
155
156     @Test
157     public void testGetGenericVnfDataSucces() throws APPCException, SvcLogicException {
158         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
159         Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
160                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
161         ctx = new SvcLogicContext();
162         AAIPluginImpl impl = new AAIPluginImpl();
163         impl.initialize();
164         impl.getGenericVnfData(params, ctx);
165         Assert.assertThat(ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE), CoreMatchers.containsString(
166                 "Operation GetGenericVnfData succeed for VNF ID null"));
167     }
168
169     @Test
170     public void testGetGenericVnfDataFailureThrownExeption() throws APPCException, SvcLogicException {
171         Mockito.doThrow(new SvcLogicException()).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
172                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
173         ctx = new SvcLogicContext();
174         AAIPluginImpl impl = new AAIPluginImpl();
175         impl.initialize();
176         expectedEx.expect(APPCException.class);
177         expectedEx.expectCause(isA(SvcLogicException.class));
178         impl.getGenericVnfData(params, ctx);
179     }
180
181     @Test
182     public void testGetVnfHierarchyAaiExceptionFlow() throws APPCException, SvcLogicException {
183         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.FAILURE;
184         Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
185                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
186         ctx = new SvcLogicContext();
187         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
188         AAIPluginImpl impl = new AAIPluginImpl();
189         impl.initialize();
190         expectedEx.expect(APPCException.class);
191         expectedEx.expectMessage("Error Retrieving VNF hierarchy");
192         impl.getVnfHierarchy(params, ctx);
193     }
194
195     @Test
196     public void testGetVnfHierarchyAaiExceptionFlow2() throws APPCException, SvcLogicException {
197         Mockito.doThrow(new SvcLogicException()).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
198                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
199         ctx = new SvcLogicContext();
200         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
201         AAIPluginImpl impl = new AAIPluginImpl();
202         impl.initialize();
203         expectedEx.expect(APPCException.class);
204         expectedEx.expectMessage("Error Retrieving VNF hierarchy");
205         impl.getVnfHierarchy(params, ctx);
206     }
207
208     @Test
209     public void testGetVnfHierarchyNoVMs() throws APPCException, SvcLogicException {
210         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
211         Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
212                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
213         ctx = new SvcLogicContext();
214         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
215         AAIPluginImpl impl = new AAIPluginImpl();
216         impl.initialize();
217         impl.getVnfHierarchy(params, ctx);
218         Assert.assertEquals("0", ctx.getAttribute("VNF.VMCount"));
219     }
220
221     @Test
222     public void testGetVnfHierarchy() throws APPCException, SvcLogicException, AAIQueryException {
223         String vnfId = "TEST_RESOURCE_KEY";
224         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
225         Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
226                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
227         ctx = new SvcLogicContext();
228         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
229         AAIPluginImpl impl = Mockito.spy(new AAIPluginImpl());
230         AAIQueryResult aaiQueryResult = new AAIQueryResult();
231         aaiQueryResult.getAdditionProperties().put("vnf-type", "TEST_VNF_TYPE");
232         Relationship relationship = new Relationship();
233         relationship.setRelatedTo("vserver");
234         aaiQueryResult.getRelationshipList().add(relationship);
235         Mockito.doReturn(aaiQueryResult).when(impl).readVnf(vnfId);
236         AAIQueryResult vmQueryResult = new AAIQueryResult();
237         vmQueryResult.getAdditionProperties().put("vserver-selflink", "TEST_VM_NAME");
238         Relationship vmRelationship = new Relationship();
239         vmRelationship.setRelatedTo("vnfc");
240         vmQueryResult.getRelationshipList().add(vmRelationship);
241         Mockito.doReturn(vmQueryResult).when(impl).readVM(null, null, null, null);
242         impl.initialize();
243         impl.getVnfHierarchy(params, ctx);
244         Assert.assertEquals(EELFResourceManager.format(Msg.SUCCESS_EVENT_MESSAGE, "GetVNFHierarchy", "VNF ID " + vnfId),
245                 ctx.getAttribute(org.onap.appc.Constants.ATTRIBUTE_SUCCESS_MESSAGE));
246     }
247
248     @Test
249     public void testReadVM() throws AAIQueryException {
250         AAIPluginImpl impl = Mockito.spy(new AAIPluginImpl());
251         ctx = new SvcLogicContext();
252         ctx.setAttribute("VM.relationship-list.relationship_length", "1");
253         ctx.setAttribute("VM.relationship-list.relationship[0].relationship-data_length", "1");
254         ctx.setAttribute("VM.relationship-list.relationship[0].related-to-property_length", "1");
255         Mockito.doReturn(ctx).when(impl).readResource("vserver.vserver-id = 'null' AND tenant.tenant_id = 'null' AND "
256                 + "cloud-region.cloud-owner = 'null' AND cloud-region.cloud-region-id = 'null'", "VM", "vserver");
257         impl.initialize();
258         Assert.assertEquals(null, impl.readVM(null, null, null, null).getAdditionProperties().get("resource-version"));
259     }
260
261     @Test
262     public void testGetResource() throws SvcLogicException, APPCException {
263         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
264         Mockito.doReturn(status).when(aaiClient).query(Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyString(),
265                 Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
266         ctx = new SvcLogicContext();
267         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
268         AAIPluginImpl impl = new AAIPluginImpl();
269         impl.initialize();
270         impl.getResource(params, ctx);
271         Assert.assertEquals("SUCCESS",ctx.getAttribute("getResource_result"));
272     }
273
274     @Test
275     public void testPostResource() throws SvcLogicException, APPCException {
276         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
277         Mockito.doReturn(status).when(aaiClient).update(Mockito.anyString(), Mockito.anyString(),
278                 Mockito.anyMap(), Mockito.anyString(), Mockito.any(SvcLogicContext.class));
279         ctx = new SvcLogicContext();
280         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
281         AAIPluginImpl impl = new AAIPluginImpl();
282         impl.initialize();
283         impl.postResource(params, ctx);
284         Assert.assertEquals("SUCCESS",ctx.getAttribute("postResource_result"));
285     }
286
287     @Test
288     public void testDeleteResource() throws SvcLogicException, APPCException {
289         SvcLogicResource.QueryStatus status = SvcLogicResource.QueryStatus.SUCCESS;
290         Mockito.doReturn(status).when(aaiClient).delete(Mockito.anyString(), Mockito.anyString(),
291                 Mockito.any(SvcLogicContext.class));
292         ctx = new SvcLogicContext();
293         params.put(Constants.RESOURCEKEY, "TEST_RESOURCE_KEY");
294         AAIPluginImpl impl = new AAIPluginImpl();
295         impl.initialize();
296         impl.deleteResource(params, ctx);
297         Assert.assertEquals("SUCCESS",ctx.getAttribute("deleteResource_result"));
298     }
299 }