Test coverage in aai-client-provider
[appc.git] / appc-outbound / appc-aai-client / provider / src / test / java / org / onap / appc / aai / client / node / TestAAIResourceNode.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.aai.client.node;
27
28
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertNotNull;
31 import java.util.ArrayList;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.HashMap;
35 import java.util.Map;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.mockito.Mockito;
40 import org.onap.appc.aai.client.AppcAaiClientConstant;
41 import org.onap.appc.aai.client.aai.AaiService;
42 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
43 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
44 import org.onap.ccsdk.sli.core.sli.SvcLogicException;
45 import com.att.eelf.configuration.EELFLogger;
46 import com.att.eelf.configuration.EELFManager;
47
48
49
50 public class TestAAIResourceNode {
51
52     //Removed for ONAP integration
53
54     private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAAIResourceNode.class);
55
56     @Rule
57     public ExpectedException expectedEx = ExpectedException.none();
58
59     @Test
60     public void testGetVnfInfo() throws Exception {
61         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
62         Map<String, String> inParams = new HashMap<>();
63         SvcLogicContext ctx = new SvcLogicContext();
64         ctx.setAttribute("vm[0].cloud-owner", "TEST");
65         ctx.setAttribute("vm[0].cloud-region-id", "TEST");
66         AaiService aaiService = Mockito.mock(AaiService.class);
67         Mockito.doReturn(aaiService).when(aai).getAaiService();
68         aai.getVnfInfo(inParams, ctx);
69         Mockito.verify(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
70     }
71
72     @Test
73     public void sortVServer() throws Exception{
74         ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
75         Map<String, String> vserverMap = new HashMap<>();
76         vserverMap.put("vserver-id", "vserverId9");
77         vserverMap.put("tenant-id", "tenantId9");
78         vserverMap.put("cloud-owner", "cloudOwner9");
79         vserverMap.put("cloud-region-id", "cloudRegionId9");
80         vserverMap.put("vserver-name", "vServerName9");
81         vservers.add(vserverMap);
82         vserverMap = new HashMap<>();
83         vserverMap.put("vserver-id", "vserverId1");
84         vserverMap.put("tenant-id", "tenantId1");
85         vserverMap.put("cloud-owner", "cloudOwner1");
86         vserverMap.put("cloud-region-id", "cloudRegionId1");
87         vserverMap.put("vserver-name", "vServerName1");
88         vservers.add(vserverMap);
89         vserverMap = new HashMap<>();
90         vserverMap.put("vserver-id", "vserverId3");
91         vserverMap.put("tenant-id", "tenantId3");
92         vserverMap.put("cloud-owner", "cloudOwner3");
93         vserverMap.put("cloud-region-id", "cloudRegionId3");
94         vserverMap.put("vserver-name", "vServerName3");
95         vservers.add(vserverMap);
96         Collections.sort(vservers, new Comparator<Map<String, String>>() {
97             @Override
98             public int compare(Map<String, String> o1, Map<String, String> o2) {
99                 return o1.get("vserver-name").compareTo(o2.get("vserver-name"));
100             }
101         });
102
103         SvcLogicContext ctx = new SvcLogicContext();
104         AAIResourceNode aai = new AAIResourceNode();
105         aai.populateContext(vservers, ctx, "vserver.");
106         log.info(ctx.getAttribute("vserver.vm[0].vserver-name"));
107     }
108
109     @Test
110     public void testAllVServer() throws Exception{
111
112         MockAAIResourceNode mrn = new MockAAIResourceNode();
113         SvcLogicContext ctx = new SvcLogicContext();
114         populateAllVServerInfo(ctx, "tmp.vnfInfo");
115         Map<String, String> inParams = new HashMap<>();
116         inParams.put("responsePrefix", "tmp.vnfInfo");
117         mrn.getAllVServersVnfcsInfo(inParams, ctx);
118         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
119         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count"), "2");
120         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count"), "0");
121         // VM1
122         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcsm0002id");
123         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantid2");
124         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner2");
125         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId2");
126         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vserverName2");
127         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vf-module-id"), "vfModule2");
128         //assertNull(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"));
129
130         // VM2
131         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcxvm0001id");
132         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].tenant-id"), "tenantid1");
133         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-owner"), "cloudOwner1");
134         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-region-id"), "cloudRegionId1");
135         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-name"), "vserverName2");
136         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vf-module-id"), "vfModule2");
137         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "vnfcName2");
138         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "vnfcType2");
139         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "vnfcFuncCode2");
140         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "vnfcGrpNot2");
141
142         ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id","ibcm0001id");
143         ctx.setAttribute("req-vf-module-id","vfModule1");
144         mrn.getAllVServersVnfcsInfo(inParams, ctx);
145         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count-vf-module"),"1");
146         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count-for-vf-module"),"1");
147     }
148
149     @Test
150     public void testAllVServerExceptionFlow() throws Exception{
151         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
152         Map<String, String> inParams = new HashMap<>();
153         SvcLogicContext ctx = new SvcLogicContext();
154         AaiService aaiService = Mockito.mock(AaiService.class);
155         Mockito.doReturn(aaiService).when(aai).getAaiService();
156         expectedEx.expect(SvcLogicException.class);
157         expectedEx.expectMessage("Unable to get VServers for the VNF");
158         aai.getAllVServersVnfcsInfo(inParams, ctx);
159     }
160
161     public void populateAllVServerInfo(SvcLogicContext ctx, String prefix) throws Exception {
162          ctx.setAttribute("vnf-id", "ibcx0001v");
163          ctx.setAttribute("vnf-host-ip-address", "000.00.00.00");
164          ctx.setAttribute(prefix + ".vm-count", "2");
165          ctx.setAttribute(prefix+ ".vm[0].vserver-id", "ibcsm0002id");
166          ctx.setAttribute(prefix+ ".vm[0].tenant-id", "tenantid2");
167          ctx.setAttribute(prefix+ ".vm[0].cloud-owner", "cloudOwner2");
168          ctx.setAttribute(prefix+ ".vm[0].cloud-region-id", "cloudRegionId2");
169          ctx.setAttribute(prefix+ ".vm[1].vserver-id", "ibcxvm0001id");
170          ctx.setAttribute(prefix+ ".vm[1].tenant-id", "tenantid1");
171          ctx.setAttribute(prefix+ ".vm[1].cloud-owner", "cloudOwner1");
172          ctx.setAttribute(prefix+ ".vm[1].cloud-region-id", "cloudRegionId1");
173     }
174
175     public static class MockAAIResourceNode extends AAIResourceNode {
176         private static final EELFLogger log = EELFManager.getInstance().getLogger(MockAAIResourceNode.class);
177          private AAIClient aaiClient;
178
179         public AaiService getAaiService() {
180             log.info("In MockAAI");
181             return new MockAaiService(aaiClient);
182         }
183     }
184
185     @Test
186     public void testPopulateContext() throws Exception{
187         ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
188         Map<String, String> vserverMap = new HashMap<>();
189         vserverMap = new HashMap<>();
190         vserverMap.put("vserver-id", "vserverId1");
191         vserverMap.put("tenant-id", "tenantId1");
192         vserverMap.put("cloud-owner", "cloudOwner1");
193         vserverMap.put("cloud-region-id", "cloudRegionId1");
194         vserverMap.put("vserver-name", "vServerName1");
195         vserverMap.put("vnfc-name", "vnfcName1");
196         vservers.add(vserverMap);
197         vserverMap = new HashMap<>();
198         vserverMap.put("vserver-id", "vserverId3");
199         vserverMap.put("tenant-id", "tenantId3");
200         vserverMap.put("cloud-owner", "cloudOwner3");
201         vserverMap.put("cloud-region-id", "cloudRegionId3");
202         vserverMap.put("vserver-name", "vServerName3");
203         vservers.add(vserverMap);
204         vserverMap = new HashMap<>();
205         vserverMap.put("vserver-id", "vserverId9");
206         vserverMap.put("tenant-id", "tenantId9");
207         vserverMap.put("cloud-owner", "cloudOwner9");
208         vserverMap.put("cloud-region-id", "cloudRegionId9");
209         vserverMap.put("vserver-name", "vServerName9");
210         vservers.add(vserverMap);
211         SvcLogicContext ctx = new SvcLogicContext();
212         AAIResourceNode aai = new AAIResourceNode();
213         aai.populateContext(vservers, ctx, "tmp.vnfInfo.");
214         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vServerName1");
215         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantId1");
216         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner1");
217         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId1");
218         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "vserverId1");
219         assertEquals(ctx.getAttribute("vm-name"), "vServerName3");
220     }
221
222     @Test
223     public final void testGetVnfInfoExceptionFlow() throws Exception {
224         SvcLogicContext ctx = new SvcLogicContext();
225         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
226         Map<String, String> inParams = new HashMap<>();
227         ctx.setAttribute("vm[0].cloud-owner", "TEST");
228         ctx.setAttribute("vm[0].cloud-region-id", "TEST");
229         AaiService aaiService = Mockito.mock(AaiService.class);
230         Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
231         Mockito.doReturn(aaiService).when(aai).getAaiService();
232         expectedEx.expect(SvcLogicException.class);
233         expectedEx.expectMessage("TEST");
234         aai.getVnfInfo(inParams, ctx);
235     }
236
237     @Test
238     public final void testaddVnfcs() throws SvcLogicException
239     {
240         SvcLogicContext ctx = new SvcLogicContext();
241         ctx.setAttribute("vnfcReference_length", "1");
242         ctx.setAttribute("vnf.vm-count", "1");
243         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
244         Map<String, String> inParams = new HashMap<>();
245         AaiService aaiService = Mockito.mock(AaiService.class);
246         Mockito.doReturn(aaiService).when(aai).getAaiService();
247         aai.addVnfcs(inParams, ctx);
248     }
249
250     @Test
251     public final void testaddVnfcsExceptionFlow() throws Exception
252     {
253         SvcLogicContext ctx = new SvcLogicContext();
254         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
255         Map<String, String> inParams = new HashMap<>();
256         inParams.put("responsePrefix", "tmp.vnfInfo");
257         AaiService aaiService = Mockito.mock(AaiService.class);
258         Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
259         Mockito.doReturn(aaiService).when(aai).getAaiService();
260         expectedEx.expect(SvcLogicException.class);
261         aai.addVnfcs(inParams, ctx);
262     }
263
264     @Test
265     public final void  testupdateVnfAndVServerStatus() throws SvcLogicException{
266         SvcLogicContext ctx = new SvcLogicContext();
267         ctx.setAttribute("vnf.vm-count", "1");
268         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
269         Map<String, String> inParams = new HashMap<>();
270         AaiService aaiService = Mockito.mock(AaiService.class);
271         Mockito.doReturn(aaiService).when(aai).getAaiService();
272         aai.updateVnfAndVServerStatus(inParams, ctx);
273         assertEquals(AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute(AppcAaiClientConstant.OUTPUT_PARAM_STATUS));
274     }
275
276     @Test
277     public final void  testupdateVnfAndVServerStatusExceptionFlow() throws SvcLogicException{
278         SvcLogicContext ctx = new SvcLogicContext();
279         AAIResourceNode aai = new AAIResourceNode();
280         Map<String, String> inParams = new HashMap<>();
281         expectedEx.expect(SvcLogicException.class);
282         aai.updateVnfAndVServerStatus(inParams, ctx);
283     }
284
285     @Test
286     public void testgetVfModduleModelInfo() throws Exception{
287         SvcLogicContext ctx = new SvcLogicContext();
288         AAIResourceNode aai = new AAIResourceNode();
289         AAIClient aaic = null;
290         MockAaiService aaiService = new MockAaiService(aaic);
291         Map<String, String> inParams = new HashMap<>();
292         inParams.put("responsePrefix", "tmp.vnfInfo");
293         aai.processForVfModuleModelInfo(aaiService, inParams, ctx);
294         assertEquals(ctx.getAttribute("template-model-id"), "model0001");
295     }
296
297     @Test
298     public final void testSetVmParams() {
299         SvcLogicContext ctx = new SvcLogicContext();
300         String vServerId = "vserver02";
301         ctx.setAttribute("tmp.vnfInfo.vm-count","3");
302         ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id", "vserver01");
303         ctx.setAttribute("tmp.vnfInfo.vm[1].vserver-id", "vserver02");
304         ctx.setAttribute("tmp.vnfInfo.vm[1].tenant-id", "ten01");
305         ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-region-id", "cr01");
306         ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-owner", "co01");
307         AAIResourceNode aairn= new AAIResourceNode();
308         Map <String, String> params = aairn.setVmParams(ctx, vServerId);
309         assertNotNull(params);
310     }
311
312     @Test
313     public final void testGetVnfcInformationForVserver() throws Exception{
314         MockAAIResourceNode aairn = new MockAAIResourceNode();
315         SvcLogicContext ctx = new SvcLogicContext();
316         SvcLogicContext newVnfcCtx = new SvcLogicContext();
317         Map<String, String> inParams = new HashMap<>();
318         Map<String, String> vnfcParams = new HashMap<>();
319         String responsePrefix = "test.";
320         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
321         vnfcParams.put("vnfcName", "vnfcName2");
322         aairn.getVnfcInformationForVserver(vnfcParams, newVnfcCtx, inParams, ctx, aairn.getAaiService(), responsePrefix);
323         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-name"), "vnfcName2");
324         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-type"), "vnfcType2");
325         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-function-code"), "vnfcFuncCode2");
326         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-group-notation"), "vnfcGrpNot2");
327     }
328
329     @Test
330     public final void testGetFormattedValue() throws Exception{
331         MockAAIResourceNode aairn = new MockAAIResourceNode();
332         SvcLogicContext ctx = new SvcLogicContext();
333         Map<String, String> inParams = new HashMap<>();
334         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
335         inParams.put("inputParameter", "Some/Value/With/ Many Spaces");
336         aairn.getFormattedValue(inParams, ctx);
337         assertEquals(ctx.getAttribute("template-model-id"), "Some_Value_With_ManySpaces");
338     }
339
340     @Test
341     public final void testProcessCheckForVfModule() throws Exception{
342         MockAAIResourceNode aairn = new MockAAIResourceNode();
343         SvcLogicContext ctx = new SvcLogicContext();
344         Map<String,String> inParams = new HashMap<String, String>();
345         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
346         inParams.put("inputParameter", "Some/Value/With/ Many Spaces");
347         ctx.setAttribute("test.vnf.vm-with-no-vnfcs-count-vf-module", "0");
348         ctx.setAttribute("test.vnf.vm-count-for-vf-module", "2");
349         aairn.processCheckForVfModule("vfmoduleId1", ctx, "test.", 2);
350     }
351
352     @Test
353     public void testGetVserverInfo() throws SvcLogicException {
354         SvcLogicContext ctx = new SvcLogicContext();
355         ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
356         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
357         Map<String, String> inParams = new HashMap<>();
358         AaiService aaiService = Mockito.mock(AaiService.class);
359         Mockito.doReturn(aaiService).when(aai).getAaiService();
360         aai.getVserverInfo(inParams, ctx);
361         Mockito.verify(aaiService).getVMInfo(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
362     }
363
364     @Test
365     public void testGetVserverInfoExceptionFlow() throws Exception {
366         SvcLogicContext ctx = new SvcLogicContext();
367         ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
368         Map<String, String> inParams = new HashMap<>();
369         AaiService aaiService = Mockito.mock(AaiService.class);
370         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
371         Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getVMInfo(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
372         Mockito.doReturn(aaiService).when(aai).getAaiService();
373         aai.getVserverInfo(inParams, ctx);
374         assertEquals("TEST", ctx.getAttribute(AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE));
375     }
376 }