Changed to unmaintained
[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         assertNotNull(vserverMap);
108     }
109
110     @Test
111     public void testAllVServer() throws Exception{
112
113         MockAAIResourceNode mrn = new MockAAIResourceNode();
114         SvcLogicContext ctx = new SvcLogicContext();
115         populateAllVServerInfo(ctx, "tmp.vnfInfo");
116         Map<String, String> inParams = new HashMap<>();
117         inParams.put("responsePrefix", "tmp.vnfInfo");
118         mrn.getAllVServersVnfcsInfo(inParams, ctx);
119         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
120         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count"), "2");
121         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count"), "0");
122         // VM1
123         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcsm0002id");
124         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantid2");
125         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner2");
126         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId2");
127         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vserverName2");
128         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vf-module-id"), "vfModule2");
129         //assertNull(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"));
130
131         // VM2
132         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcxvm0001id");
133         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].tenant-id"), "tenantid1");
134         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-owner"), "cloudOwner1");
135         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-region-id"), "cloudRegionId1");
136         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-name"), "vserverName2");
137         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vf-module-id"), "vfModule2");
138         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "vnfcName2");
139         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "vnfcType2");
140         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "vnfcFuncCode2");
141         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "vnfcGrpNot2");
142
143         ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id","ibcm0001id");
144         ctx.setAttribute("req-vf-module-id","vfModule1");
145         mrn.getAllVServersVnfcsInfo(inParams, ctx);
146         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count-vf-module"),"1");
147         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count-for-vf-module"),"1");
148     }
149
150     @Test
151     public void testAllVServerExceptionFlow() throws Exception{
152         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
153         Map<String, String> inParams = new HashMap<>();
154         SvcLogicContext ctx = new SvcLogicContext();
155         AaiService aaiService = Mockito.mock(AaiService.class);
156         Mockito.doReturn(aaiService).when(aai).getAaiService();
157         expectedEx.expect(SvcLogicException.class);
158         expectedEx.expectMessage("Unable to get VServers for the VNF");
159         aai.getAllVServersVnfcsInfo(inParams, ctx);
160     }
161
162     public void populateAllVServerInfo(SvcLogicContext ctx, String prefix) throws Exception {
163          ctx.setAttribute("vnf-id", "ibcx0001v");
164          ctx.setAttribute("vnf-host-ip-address", "000.00.00.00");
165          ctx.setAttribute(prefix + ".vm-count", "2");
166          ctx.setAttribute(prefix+ ".vm[0].vserver-id", "ibcsm0002id");
167          ctx.setAttribute(prefix+ ".vm[0].tenant-id", "tenantid2");
168          ctx.setAttribute(prefix+ ".vm[0].cloud-owner", "cloudOwner2");
169          ctx.setAttribute(prefix+ ".vm[0].cloud-region-id", "cloudRegionId2");
170          ctx.setAttribute(prefix+ ".vm[1].vserver-id", "ibcxvm0001id");
171          ctx.setAttribute(prefix+ ".vm[1].tenant-id", "tenantid1");
172          ctx.setAttribute(prefix+ ".vm[1].cloud-owner", "cloudOwner1");
173          ctx.setAttribute(prefix+ ".vm[1].cloud-region-id", "cloudRegionId1");
174     }
175
176     public static class MockAAIResourceNode extends AAIResourceNode {
177         private static final EELFLogger log = EELFManager.getInstance().getLogger(MockAAIResourceNode.class);
178          private AAIClient aaiClient;
179
180         public AaiService getAaiService() {
181             log.info("In MockAAI");
182             return new MockAaiService(aaiClient);
183         }
184     }
185
186     @Test
187     public void testPopulateContext() throws Exception{
188         ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
189         Map<String, String> vserverMap = new HashMap<>();
190         vserverMap = new HashMap<>();
191         vserverMap.put("vserver-id", "vserverId1");
192         vserverMap.put("tenant-id", "tenantId1");
193         vserverMap.put("cloud-owner", "cloudOwner1");
194         vserverMap.put("cloud-region-id", "cloudRegionId1");
195         vserverMap.put("vserver-name", "vServerName1");
196         vserverMap.put("vnfc-name", "vnfcName1");
197         vservers.add(vserverMap);
198         vserverMap = new HashMap<>();
199         vserverMap.put("vserver-id", "vserverId3");
200         vserverMap.put("tenant-id", "tenantId3");
201         vserverMap.put("cloud-owner", "cloudOwner3");
202         vserverMap.put("cloud-region-id", "cloudRegionId3");
203         vserverMap.put("vserver-name", "vServerName3");
204         vservers.add(vserverMap);
205         vserverMap = new HashMap<>();
206         vserverMap.put("vserver-id", "vserverId9");
207         vserverMap.put("tenant-id", "tenantId9");
208         vserverMap.put("cloud-owner", "cloudOwner9");
209         vserverMap.put("cloud-region-id", "cloudRegionId9");
210         vserverMap.put("vserver-name", "vServerName9");
211         vservers.add(vserverMap);
212         SvcLogicContext ctx = new SvcLogicContext();
213         AAIResourceNode aai = new AAIResourceNode();
214         aai.populateContext(vservers, ctx, "tmp.vnfInfo.");
215         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vServerName1");
216         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantId1");
217         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner1");
218         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId1");
219         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "vserverId1");
220         assertEquals(ctx.getAttribute("vm-name"), "vServerName3");
221     }
222
223     @Test
224     public final void testGetVnfInfoExceptionFlow() throws Exception {
225         SvcLogicContext ctx = new SvcLogicContext();
226         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
227         Map<String, String> inParams = new HashMap<>();
228         ctx.setAttribute("vm[0].cloud-owner", "TEST");
229         ctx.setAttribute("vm[0].cloud-region-id", "TEST");
230         AaiService aaiService = Mockito.mock(AaiService.class);
231         Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
232         Mockito.doReturn(aaiService).when(aai).getAaiService();
233         expectedEx.expect(SvcLogicException.class);
234         expectedEx.expectMessage("TEST");
235         aai.getVnfInfo(inParams, ctx);
236     }
237
238     @Test
239     public final void testaddVnfcs() throws SvcLogicException
240     {
241         SvcLogicContext ctx = new SvcLogicContext();
242         ctx.setAttribute("vnfcReference_length", "1");
243         ctx.setAttribute("vnf.vm-count", "1");
244         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
245         Map<String, String> inParams = new HashMap<>();
246         AaiService aaiService = Mockito.mock(AaiService.class);
247         Mockito.doReturn(aaiService).when(aai).getAaiService();
248         aai.addVnfcs(inParams, ctx);
249         assertNotNull(ctx);
250     }
251
252     @Test
253     public final void testaddVnfcsExceptionFlow() throws Exception
254     {
255         SvcLogicContext ctx = new SvcLogicContext();
256         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
257         Map<String, String> inParams = new HashMap<>();
258         inParams.put("responsePrefix", "tmp.vnfInfo");
259         AaiService aaiService = Mockito.mock(AaiService.class);
260         Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
261         Mockito.doReturn(aaiService).when(aai).getAaiService();
262         expectedEx.expect(SvcLogicException.class);
263         aai.addVnfcs(inParams, ctx);
264     }
265
266     @Test
267     public final void  testupdateVnfAndVServerStatus() throws SvcLogicException{
268         SvcLogicContext ctx = new SvcLogicContext();
269         ctx.setAttribute("vnf.vm-count", "1");
270         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
271         Map<String, String> inParams = new HashMap<>();
272         AaiService aaiService = Mockito.mock(AaiService.class);
273         Mockito.doReturn(aaiService).when(aai).getAaiService();
274         aai.updateVnfAndVServerStatus(inParams, ctx);
275         assertEquals(AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute(AppcAaiClientConstant.OUTPUT_PARAM_STATUS));
276     }
277
278     @Test
279     public final void  testupdateVnfAndVServerStatusExceptionFlow() throws SvcLogicException{
280         SvcLogicContext ctx = new SvcLogicContext();
281         AAIResourceNode aai = new AAIResourceNode();
282         Map<String, String> inParams = new HashMap<>();
283         expectedEx.expect(SvcLogicException.class);
284         aai.updateVnfAndVServerStatus(inParams, ctx);
285     }
286
287     @Test
288     public void testgetVfModduleModelInfo() throws Exception{
289         SvcLogicContext ctx = new SvcLogicContext();
290         AAIResourceNode aai = new AAIResourceNode();
291         AAIClient aaic = null;
292         MockAaiService aaiService = new MockAaiService(aaic);
293         Map<String, String> inParams = new HashMap<>();
294         inParams.put("responsePrefix", "tmp.vnfInfo");
295         aai.processForVfModuleModelInfo(aaiService, inParams, ctx);
296         assertEquals(ctx.getAttribute("template-model-id"), "model0001");
297     }
298
299     @Test
300     public final void testSetVmParams() {
301         SvcLogicContext ctx = new SvcLogicContext();
302         String vServerId = "vserver02";
303         ctx.setAttribute("tmp.vnfInfo.vm-count","3");
304         ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id", "vserver01");
305         ctx.setAttribute("tmp.vnfInfo.vm[1].vserver-id", "vserver02");
306         ctx.setAttribute("tmp.vnfInfo.vm[1].tenant-id", "ten01");
307         ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-region-id", "cr01");
308         ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-owner", "co01");
309         AAIResourceNode aairn= new AAIResourceNode();
310         Map <String, String> params = aairn.setVmParams(ctx, vServerId);
311         assertNotNull(params);
312     }
313
314     @Test
315     public final void testGetVnfcInformationForVserver() throws Exception{
316         MockAAIResourceNode aairn = new MockAAIResourceNode();
317         SvcLogicContext ctx = new SvcLogicContext();
318         SvcLogicContext newVnfcCtx = new SvcLogicContext();
319         Map<String, String> inParams = new HashMap<>();
320         Map<String, String> vnfcParams = new HashMap<>();
321         String responsePrefix = "test.";
322         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
323         vnfcParams.put("vnfcName", "vnfcName2");
324         aairn.getVnfcInformationForVserver(vnfcParams, newVnfcCtx, inParams, ctx, aairn.getAaiService(), responsePrefix);
325         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-name"), "vnfcName2");
326         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-type"), "vnfcType2");
327         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-function-code"), "vnfcFuncCode2");
328         assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-group-notation"), "vnfcGrpNot2");
329     }
330
331     @Test
332     public final void testGetFormattedValue() throws Exception{
333         MockAAIResourceNode aairn = new MockAAIResourceNode();
334         SvcLogicContext ctx = new SvcLogicContext();
335         Map<String, String> inParams = new HashMap<>();
336         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
337         inParams.put("inputParameter", "Some/Value/With/ Many Spaces");
338         aairn.getFormattedValue(inParams, ctx);
339         assertEquals(ctx.getAttribute("template-model-id"), "Some_Value_With_ManySpaces");
340     }
341
342     @Test
343     public final void testProcessCheckForVfModule() throws Exception{
344         MockAAIResourceNode aairn = new MockAAIResourceNode();
345         SvcLogicContext ctx = new SvcLogicContext();
346         Map<String,String> inParams = new HashMap<String, String>();
347         inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
348         inParams.put("inputParameter", "Some/Value/With/ Many Spaces");
349         ctx.setAttribute("test.vnf.vm-with-no-vnfcs-count-vf-module", "0");
350         ctx.setAttribute("test.vnf.vm-count-for-vf-module", "2");
351         aairn.processCheckForVfModule("vfmoduleId1", ctx, "test.", 2);
352         assertNotNull(aairn);
353     }
354
355     @Test
356     public void testGetVserverInfo() throws SvcLogicException {
357         SvcLogicContext ctx = new SvcLogicContext();
358         ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
359         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
360         Map<String, String> inParams = new HashMap<>();
361         AaiService aaiService = Mockito.mock(AaiService.class);
362         Mockito.doReturn(aaiService).when(aai).getAaiService();
363         aai.getVserverInfo(inParams, ctx);
364         Mockito.verify(aaiService).getVMInfo(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
365     }
366
367     @Test
368     public void testGetVserverInfoExceptionFlow() throws Exception {
369         SvcLogicContext ctx = new SvcLogicContext();
370         ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
371         Map<String, String> inParams = new HashMap<>();
372         AaiService aaiService = Mockito.mock(AaiService.class);
373         AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
374         Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getVMInfo(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
375         Mockito.doReturn(aaiService).when(aai).getAaiService();
376         aai.getVserverInfo(inParams, ctx);
377         assertEquals("TEST", ctx.getAttribute(AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE));
378     }
379 }