added test file to cover AaiVnfInfo.java
[appc.git] / appc-outbound / appc-aai-client / provider / src / test / java / org / onap / appc / aai / client / aai / TestAaiService.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  * Modification Copyright (C) 2018 IBM.
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.aai;
27
28 import static org.junit.Assert.assertEquals;
29
30 import java.util.HashMap;
31 import java.util.Map;
32
33 import org.apache.commons.lang3.StringUtils;
34 import org.junit.Test;
35 import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
36 import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
37
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40
41 public class TestAaiService {
42     // ONAP merging
43
44     private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAaiService.class);
45     private AAIClient aaiClient;
46
47     @Test
48     public void testGetGenericVnfInfo() throws Exception {
49
50         MockAaiService mockAai = new MockAaiService(aaiClient);
51
52         Map<String, String> inParams = new HashMap<String, String>();
53         inParams.put("vnfId", "ibcxvm0000");
54         inParams.put("responsePrefix", "tmp.vnfInfo");
55
56         SvcLogicContext ctx = new SvcLogicContext();
57
58         mockAai.getGenericVnfInfo(inParams, ctx);
59
60         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vnf-type"), "vUSP-Metaswitch");
61         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
62         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcx001vm001-id");
63         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcx000000");
64     }
65
66     @Test
67     public void testGetVmInfo() throws Exception {
68
69         MockAaiService mockAai = new MockAaiService(aaiClient);
70
71         Map<String, String> inParams = new HashMap<String, String>();
72         inParams.put("vserverId", "vserverId1");
73         inParams.put("tenantId", "tenantId1");
74         inParams.put("cloudOwner", "cloudOwner1");
75         inParams.put("cloudRegionId", "cloudRegionId1");
76         inParams.put("responsePrefix", "tmp.vnfInfo");
77
78         SvcLogicContext ctx = new SvcLogicContext();
79
80         mockAai.getVMInfo(inParams, ctx);
81
82         printContext(ctx);
83
84         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vserver-name"), "ibcx0000000");
85         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc-count"), "1");
86         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vf-module-id"), "vfModule1");
87         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm.vnfc[0].vnfc-name"), "ibcx0001vm001vnfc1");
88
89     }
90
91     @Test
92     public void testGetVnfcInfo() throws Exception {
93
94         MockAaiService mockAai = new MockAaiService(aaiClient);
95
96         Map<String, String> inParams = new HashMap<String, String>();
97         inParams.put("vnfcName", "vnfcName1");
98
99         inParams.put("responsePrefix", "tmp.vnfInfo");
100
101         SvcLogicContext ctx = new SvcLogicContext();
102
103         mockAai.getVnfcInfo(inParams, ctx);
104
105         printContext(ctx);
106
107         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-type"), null);
108         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.vnfc-function-code"), null);
109         assertEquals(ctx.getAttribute("tmp.vnfInfo.vnfc.group-notation"), "grpnot1");
110
111     }
112
113     @Test
114     public void testGetFirstVnfcNameForVnfcType() throws Exception {
115
116         MockAaiService mockAai = new MockAaiService(aaiClient);
117
118         String prefix = "tmp.vnfInfo.";
119
120         SvcLogicContext ctx = new SvcLogicContext(); // VNFC with specified vnfc
121                                                         // type found
122         mockAai.populateFirstVnfcData(ctx, prefix);
123         String firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
124         assertEquals(firstVnfcName, "vnfcname3");
125
126         ctx = new SvcLogicContext(); // no VMS found
127         firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "ssc");
128         assertEquals(firstVnfcName, null);
129
130         ctx = new SvcLogicContext(); // no VMS found with specified type
131         mockAai.populateFirstVnfcData(ctx, prefix);
132         firstVnfcName = mockAai.getFirstVnfcNameForVnfcType(ctx, prefix, "test");
133         assertEquals(firstVnfcName, null);
134
135     }
136
137     @Test
138     public void testGroupNotation() throws Exception {
139
140         MockAaiService mockAai = new MockAaiService(aaiClient);
141
142         String prefix = "tmp.vnfInfo.";
143
144         SvcLogicContext ctx = new SvcLogicContext();
145         mockAai.populateGroupNotation(ctx, prefix);
146
147         // printContext(ctx);
148         String grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
149         assertEquals(grpNotation, "grpNot2");
150
151         ctx = new SvcLogicContext(); // no VMS found
152         grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "ibcxvm0002");
153         assertEquals(grpNotation, null);
154
155         ctx = new SvcLogicContext(); // no VMS found with specified name
156         mockAai.populateGroupNotation(ctx, prefix);
157         grpNotation = mockAai.getGroupNotationForVServer(ctx, prefix, "test");
158         assertEquals(grpNotation, null);
159
160     }
161
162     @Test
163     public void testGetGroupNotation() throws Exception {
164
165         MockAaiService mockAai = new MockAaiService(aaiClient);
166
167         String prefix = "tmp.vnfInfo.";
168
169         SvcLogicContext ctx = new SvcLogicContext();
170
171         String grpNotation = mockAai.getGroupNotation("fixed-value", "2", null, null, null, null, null,null, 0);
172         assertEquals(grpNotation, "2");
173
174         mockAai.populateFirstVnfcData(ctx, prefix); // Existing VNFC Found
175         grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
176                 ctx, "ssc", null, 0);
177         assertEquals(grpNotation, "vnfcname32");
178
179         ctx = new SvcLogicContext(); // no vnfcs exist in A&AI- Use current
180                                         // vnfcName
181         grpNotation = mockAai.getGroupNotation("first-vnfc-name", "2", "currentVnfcName", "currentVServerName", prefix,
182                 ctx, "ssc", null, 0);
183         assertEquals(grpNotation, "currentVnfcName2");
184
185         ctx = new SvcLogicContext();
186         mockAai.populateGroupNotation(ctx, prefix);
187         grpNotation = mockAai.getGroupNotation("relative-value", "same", "currentVnfcName", "ibcxvm0003", prefix, ctx,
188                 "ssc", null, 0);
189         assertEquals(grpNotation, "grpNot2");
190
191         ctx = new SvcLogicContext();
192         mockAai.populateGroupNotation(ctx, prefix);
193         grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0006", prefix, ctx,
194                 "ssc",null,0);
195         assertEquals(grpNotation, "5");
196
197         ctx = new SvcLogicContext();
198         mockAai.populateGroupNotation(ctx, prefix);
199         grpNotation = mockAai.getGroupNotation("relative-value", "next", "currentVnfcName", "ibcxvm0003", prefix, ctx,
200                 "ssc",null,0);
201         assertEquals(grpNotation, null); // Null if grpNotation is not numeric
202
203     }
204
205 @Test
206     public void testInsertVnfcs() throws Exception {
207
208         MockAaiService mockAai = new MockAaiService(aaiClient);
209
210         Map<String, String> inParams = new HashMap<String, String>();
211
212         inParams.put("responsePrefix", "tmp.vnfInfo");
213
214         SvcLogicContext ctx = new SvcLogicContext();
215
216         mockAai.populateVnfcRef(ctx);
217
218         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
219
220         // mockAai.insertVnfcs(inParams,ctx,2, 2) ;
221     }
222
223     @Test
224     public void testUpdateVServerStatus() throws Exception {
225
226         MockAaiService mockAai = new MockAaiService(aaiClient);
227
228         Map<String, String> inParams = new HashMap<String, String>();
229
230         inParams.put("responsePrefix", "tmp.vnfInfo");
231
232         SvcLogicContext ctx = new SvcLogicContext();
233
234         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
235
236         mockAai.updateVServerStatus(inParams, ctx, 2);
237     }
238
239     @Test
240     public void testInsertVnfcsForFirstVnfc() throws Exception {
241
242         MockAaiService mockAai = new MockAaiService(aaiClient);
243
244         Map<String, String> inParams = new HashMap<String, String>();
245
246         inParams.put("responsePrefix", "tmp.vnfInfo");
247
248         SvcLogicContext ctx = new SvcLogicContext();
249
250         mockAai.populateVnfcRefFirstVnfcName(ctx);
251
252         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
253
254         mockAai.insertVnfcs(inParams, ctx, 2, 2,"vfModuleId1");
255
256
257     }
258     @Test
259     public void testInsertVnfcsForRelativeValueSame() throws Exception {
260
261         MockAaiService mockAai = new MockAaiService(aaiClient);
262
263         Map<String, String> inParams = new HashMap<String, String>();
264
265         inParams.put("responsePrefix", "tmp.vnfInfo");
266
267         SvcLogicContext ctx = new SvcLogicContext();
268
269         mockAai.populateVnfcRefRelValueSame(ctx);
270
271         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
272
273         mockAai.insertVnfcs(inParams, ctx, 2, 2, null);
274
275         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
276         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
277         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
278         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
279         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
280         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
281         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
282         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "1");
283     }
284
285     @Test
286     public void testInsertVnfcsForRelativeValueNext() throws Exception {
287
288         MockAaiService mockAai = new MockAaiService(aaiClient);
289
290         Map<String, String> inParams = new HashMap<String, String>();
291
292         inParams.put("responsePrefix", "tmp.vnfInfo");
293
294         SvcLogicContext ctx = new SvcLogicContext();
295
296         mockAai.populateVnfcRefRelValueNext(ctx);
297
298         mockAai.populateAllVnfInfo1(ctx, "tmp.vnfInfo");
299
300         mockAai.insertVnfcs(inParams, ctx, 2, 2,null);
301
302
303
304         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"), "dbjx0001vm001dbj001");
305         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-function-code"), "dbj");
306         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-type"), "v-I? - DBJX");
307         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].group-notation"), "1");
308         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "dbjx0001vm002dbj001");
309         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "dbj");
310         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "v-I? - DBJX");
311         assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "2");
312     }
313
314     @Test
315     public void testUpdateVnfStatus() throws Exception {
316
317         MockAaiService mockAai = new MockAaiService(aaiClient);
318
319         Map<String, String> inParams = new HashMap<String, String>();
320
321         inParams.put("responsePrefix", "tmp.vnfInfo");
322
323         SvcLogicContext ctx = new SvcLogicContext();
324
325         mockAai.populateAllVnfInfo(ctx, "tmp.vnfInfo");
326
327         mockAai.updateVnfStatus(inParams, ctx);
328     }
329
330     @Test
331     public void testReadResource() throws Exception {
332
333         MockAaiService mockAai = new MockAaiService(aaiClient);
334         // AaiService mockAai = new AaiService(new AAIClientMock());
335
336         String vnfId = "ibcx0001v";
337         String resourceKey = "generic-vnf.vnf-id = '" + vnfId + "'";
338         String resourceType = "generic-vnf";
339         String queryPrefix = "vnfInfo";
340         SvcLogicContext ctx = mockAai.readResource(resourceKey, queryPrefix, resourceType);
341
342         // System.out.println("VNF TYPE " + queryPrefix + ".vnf.vnf-type");
343
344         assertEquals(ctx.getAttribute("vnfInfo.vnf-type"), "vUSP-Metaswitch");
345
346     }
347     private void printContext(SvcLogicContext ctx) throws Exception {
348         for (String key : ctx.getAttributeKeySet()) {
349             log.info(" KEY " + key);
350             log.info(" VALUE " + ctx.getAttribute(key));
351         }
352     }
353
354     @Test
355     public void testGetGroupNotationForExistingValue() throws Exception {
356
357         SvcLogicContext ctx = new SvcLogicContext();
358         ctx.setAttribute("req-vf-module-id",  "vfmodule01");
359         ctx.setAttribute("tmp.vnfInfo.vm[0].vf-module-id", "vfmodule01");
360         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
361         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
362         ctx.setAttribute("tmp.vnfInfo.vm[1].vf-module-id", "vfmodule01");
363         ctx.setAttribute("tmp.vnfInfo.vm[1].vnfc-function-code", "fc1");
364         ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-function-code", "fc1");
365         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn2");
366         MockAaiService aai=new MockAaiService(aaiClient);
367         String groupNotationValue1 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 2);
368         assertEquals (groupNotationValue1,null);
369
370         ctx.setAttribute("tmp.vnfInfo.vm[0].group-notation", "gn1");
371         ctx.setAttribute("tmp.vnfInfo.vm[1].group-notation", "gn1");
372         String groupNotationValue2 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 2);
373         assertEquals (groupNotationValue2,"gn1");
374
375         ctx.setAttribute("tmp.vnfInfo.vm[2].vf-module-id", "vfmodule01");
376         ctx.setAttribute("tmp.vnfInfo.vm[2].vnfc-function-code", "fc1");
377         ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn2");
378         String groupNotationValue3 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 3);
379         assertEquals (groupNotationValue3,null);
380
381         ctx.setAttribute("tmp.vnfInfo.vm[2].group-notation", "gn1");
382         String groupNotationValue4 = aai.getGroupNotationForExistigValue(ctx, "tmp.vnfInfo", "fc1", 3);
383         assertEquals (groupNotationValue4,"gn1");
384
385     }
386
387     @Test
388     public void testgetIdentityUrl() throws Exception {
389
390         MockAaiService mockAai = new MockAaiService(aaiClient);
391
392         Map<String, String> inParams = new HashMap<String, String>();
393         inParams.put("responsePrefix", "tmp.vnfInfo");
394         inParams.put("cloudOwner", "testCloudOwner");
395         inParams.put("cloudRegionId", "testCloudRegionId");
396
397         SvcLogicContext ctx = new SvcLogicContext();
398         mockAai.getIdentityUrl(inParams, ctx);
399         assertEquals(ctx.getAttribute("tmp.vnfInfo.cloud-region.identity-url"), "TestUrl");
400
401     } 
402     
403     @Test
404     public void testAddvnfc()
405     {
406         MockAaiService mockAai = new MockAaiService(aaiClient);
407         Map<String, String> params=mockAai.getExpectedParams();
408         mockAai.addVnfc("ibcxvm0002func0001",params , "");        
409     }
410
411 }