aboutsummaryrefslogtreecommitdiffstats
path: root/appc-outbound/appc-aai-client/provider/src/test/java/org/onap/appc/aai/client/node/TestAAIResourceNode.java
blob: cb075840602bcd2770c99b67657477eb87d5b090 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*-
 * ============LICENSE_START=======================================================
 * ONAP : APPC
 * ================================================================================
 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Copyright (C) 2017 Amdocs
 * ================================================================================
 * Modifications Copyright (C) 2019 Ericsson
 * =============================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * ============LICENSE_END=========================================================
 */

package org.onap.appc.aai.client.node;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mockito;
import org.onap.appc.aai.client.AppcAaiClientConstant;
import org.onap.appc.aai.client.aai.AaiService;
import org.onap.ccsdk.sli.adaptors.aai.AAIClient;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
import org.onap.ccsdk.sli.core.sli.SvcLogicException;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;



public class TestAAIResourceNode {

    //Removed for ONAP integration

    private static final EELFLogger log = EELFManager.getInstance().getLogger(TestAAIResourceNode.class);

    @Rule
    public ExpectedException expectedEx = ExpectedException.none();

    @Test
    public void testGetVnfInfo() throws Exception {
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        SvcLogicContext ctx = new SvcLogicContext();
        ctx.setAttribute("vm[0].cloud-owner", "TEST");
        ctx.setAttribute("vm[0].cloud-region-id", "TEST");
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        aai.getVnfInfo(inParams, ctx);
        Mockito.verify(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
    }

    @Test
    public void sortVServer() throws Exception{
        ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
        Map<String, String> vserverMap = new HashMap<>();
        vserverMap.put("vserver-id", "vserverId9");
        vserverMap.put("tenant-id", "tenantId9");
        vserverMap.put("cloud-owner", "cloudOwner9");
        vserverMap.put("cloud-region-id", "cloudRegionId9");
        vserverMap.put("vserver-name", "vServerName9");
        vservers.add(vserverMap);
        vserverMap = new HashMap<>();
        vserverMap.put("vserver-id", "vserverId1");
        vserverMap.put("tenant-id", "tenantId1");
        vserverMap.put("cloud-owner", "cloudOwner1");
        vserverMap.put("cloud-region-id", "cloudRegionId1");
        vserverMap.put("vserver-name", "vServerName1");
        vservers.add(vserverMap);
        vserverMap = new HashMap<>();
        vserverMap.put("vserver-id", "vserverId3");
        vserverMap.put("tenant-id", "tenantId3");
        vserverMap.put("cloud-owner", "cloudOwner3");
        vserverMap.put("cloud-region-id", "cloudRegionId3");
        vserverMap.put("vserver-name", "vServerName3");
        vservers.add(vserverMap);
        Collections.sort(vservers, new Comparator<Map<String, String>>() {
            @Override
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                return o1.get("vserver-name").compareTo(o2.get("vserver-name"));
            }
        });

        SvcLogicContext ctx = new SvcLogicContext();
        AAIResourceNode aai = new AAIResourceNode();
        aai.populateContext(vservers, ctx, "vserver.");
        log.info(ctx.getAttribute("vserver.vm[0].vserver-name"));
    }

    @Test
    public void testAllVServer() throws Exception{

        MockAAIResourceNode mrn = new MockAAIResourceNode();
        SvcLogicContext ctx = new SvcLogicContext();
        populateAllVServerInfo(ctx, "tmp.vnfInfo");
        Map<String, String> inParams = new HashMap<>();
        inParams.put("responsePrefix", "tmp.vnfInfo");
        mrn.getAllVServersVnfcsInfo(inParams, ctx);
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm-count"), "2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count"), "2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count"), "0");
        // VM1
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "ibcsm0002id");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantid2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vserverName2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vf-module-id"), "vfModule2");
        //assertNull(ctx.getAttribute("tmp.vnfInfo.vm[0].vnfc-name"));

        // VM2
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-id"), "ibcxvm0001id");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].tenant-id"), "tenantid1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-owner"), "cloudOwner1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].cloud-region-id"), "cloudRegionId1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vserver-name"), "vserverName2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vf-module-id"), "vfModule2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-name"), "vnfcName2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-type"), "vnfcType2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].vnfc-function-code"), "vnfcFuncCode2");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[1].group-notation"), "vnfcGrpNot2");

        ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id","ibcm0001id");
        ctx.setAttribute("req-vf-module-id","vfModule1");
        mrn.getAllVServersVnfcsInfo(inParams, ctx);
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-with-no-vnfcs-count-vf-module"),"1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vnf.vm-count-for-vf-module"),"1");
    }

    @Test
    public void testAllVServerExceptionFlow() throws Exception{
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        SvcLogicContext ctx = new SvcLogicContext();
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        expectedEx.expect(SvcLogicException.class);
        expectedEx.expectMessage("Unable to get VServers for the VNF");
        aai.getAllVServersVnfcsInfo(inParams, ctx);
    }

    public void populateAllVServerInfo(SvcLogicContext ctx, String prefix) throws Exception {
         ctx.setAttribute("vnf-id", "ibcx0001v");
         ctx.setAttribute("vnf-host-ip-address", "000.00.00.00");
         ctx.setAttribute(prefix + ".vm-count", "2");
         ctx.setAttribute(prefix+ ".vm[0].vserver-id", "ibcsm0002id");
         ctx.setAttribute(prefix+ ".vm[0].tenant-id", "tenantid2");
         ctx.setAttribute(prefix+ ".vm[0].cloud-owner", "cloudOwner2");
         ctx.setAttribute(prefix+ ".vm[0].cloud-region-id", "cloudRegionId2");
         ctx.setAttribute(prefix+ ".vm[1].vserver-id", "ibcxvm0001id");
         ctx.setAttribute(prefix+ ".vm[1].tenant-id", "tenantid1");
         ctx.setAttribute(prefix+ ".vm[1].cloud-owner", "cloudOwner1");
         ctx.setAttribute(prefix+ ".vm[1].cloud-region-id", "cloudRegionId1");
    }

    public static class MockAAIResourceNode extends AAIResourceNode {
        private static final EELFLogger log = EELFManager.getInstance().getLogger(MockAAIResourceNode.class);
         private AAIClient aaiClient;

        public AaiService getAaiService() {
            log.info("In MockAAI");
            return new MockAaiService(aaiClient);
        }
    }

    @Test
    public void testPopulateContext() throws Exception{
        ArrayList<Map<String, String>> vservers = new ArrayList<Map<String, String>>();
        Map<String, String> vserverMap = new HashMap<>();
        vserverMap = new HashMap<>();
        vserverMap.put("vserver-id", "vserverId1");
        vserverMap.put("tenant-id", "tenantId1");
        vserverMap.put("cloud-owner", "cloudOwner1");
        vserverMap.put("cloud-region-id", "cloudRegionId1");
        vserverMap.put("vserver-name", "vServerName1");
        vserverMap.put("vnfc-name", "vnfcName1");
        vservers.add(vserverMap);
        vserverMap = new HashMap<>();
        vserverMap.put("vserver-id", "vserverId3");
        vserverMap.put("tenant-id", "tenantId3");
        vserverMap.put("cloud-owner", "cloudOwner3");
        vserverMap.put("cloud-region-id", "cloudRegionId3");
        vserverMap.put("vserver-name", "vServerName3");
        vservers.add(vserverMap);
        vserverMap = new HashMap<>();
        vserverMap.put("vserver-id", "vserverId9");
        vserverMap.put("tenant-id", "tenantId9");
        vserverMap.put("cloud-owner", "cloudOwner9");
        vserverMap.put("cloud-region-id", "cloudRegionId9");
        vserverMap.put("vserver-name", "vServerName9");
        vservers.add(vserverMap);
        SvcLogicContext ctx = new SvcLogicContext();
        AAIResourceNode aai = new AAIResourceNode();
        aai.populateContext(vservers, ctx, "tmp.vnfInfo.");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-name"), "vServerName1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].tenant-id"), "tenantId1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-owner"), "cloudOwner1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].cloud-region-id"), "cloudRegionId1");
        assertEquals(ctx.getAttribute("tmp.vnfInfo.vm[0].vserver-id"), "vserverId1");
        assertEquals(ctx.getAttribute("vm-name"), "vServerName3");
    }

    @Test
    public final void testGetVnfInfoExceptionFlow() throws Exception {
        SvcLogicContext ctx = new SvcLogicContext();
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        ctx.setAttribute("vm[0].cloud-owner", "TEST");
        ctx.setAttribute("vm[0].cloud-region-id", "TEST");
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        expectedEx.expect(SvcLogicException.class);
        expectedEx.expectMessage("TEST");
        aai.getVnfInfo(inParams, ctx);
    }

    @Test
    public final void testaddVnfcs() throws SvcLogicException
    {
        SvcLogicContext ctx = new SvcLogicContext();
        ctx.setAttribute("vnfcReference_length", "1");
        ctx.setAttribute("vnf.vm-count", "1");
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        aai.addVnfcs(inParams, ctx);
    }

    @Test
    public final void testaddVnfcsExceptionFlow() throws Exception
    {
        SvcLogicContext ctx = new SvcLogicContext();
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        inParams.put("responsePrefix", "tmp.vnfInfo");
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getIdentityUrl(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        expectedEx.expect(SvcLogicException.class);
        aai.addVnfcs(inParams, ctx);
    }

    @Test
    public final void  testupdateVnfAndVServerStatus() throws SvcLogicException{
        SvcLogicContext ctx = new SvcLogicContext();
        ctx.setAttribute("vnf.vm-count", "1");
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        aai.updateVnfAndVServerStatus(inParams, ctx);
        assertEquals(AppcAaiClientConstant.OUTPUT_STATUS_SUCCESS, ctx.getAttribute(AppcAaiClientConstant.OUTPUT_PARAM_STATUS));
    }

    @Test
    public final void  testupdateVnfAndVServerStatusExceptionFlow() throws SvcLogicException{
        SvcLogicContext ctx = new SvcLogicContext();
        AAIResourceNode aai = new AAIResourceNode();
        Map<String, String> inParams = new HashMap<>();
        expectedEx.expect(SvcLogicException.class);
        aai.updateVnfAndVServerStatus(inParams, ctx);
    }

    @Test
    public void testgetVfModduleModelInfo() throws Exception{
        SvcLogicContext ctx = new SvcLogicContext();
        AAIResourceNode aai = new AAIResourceNode();
        AAIClient aaic = null;
        MockAaiService aaiService = new MockAaiService(aaic);
        Map<String, String> inParams = new HashMap<>();
        inParams.put("responsePrefix", "tmp.vnfInfo");
        aai.processForVfModuleModelInfo(aaiService, inParams, ctx);
        assertEquals(ctx.getAttribute("template-model-id"), "model0001");
    }

    @Test
    public final void testSetVmParams() {
        SvcLogicContext ctx = new SvcLogicContext();
        String vServerId = "vserver02";
        ctx.setAttribute("tmp.vnfInfo.vm-count","3");
        ctx.setAttribute("tmp.vnfInfo.vm[0].vserver-id", "vserver01");
        ctx.setAttribute("tmp.vnfInfo.vm[1].vserver-id", "vserver02");
        ctx.setAttribute("tmp.vnfInfo.vm[1].tenant-id", "ten01");
        ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-region-id", "cr01");
        ctx.setAttribute("tmp.vnfInfo.vm[1].cloud-owner", "co01");
        AAIResourceNode aairn= new AAIResourceNode();
        Map <String, String> params = aairn.setVmParams(ctx, vServerId);
        assertNotNull(params);
    }

    @Test
    public final void testGetVnfcInformationForVserver() throws Exception{
        MockAAIResourceNode aairn = new MockAAIResourceNode();
        SvcLogicContext ctx = new SvcLogicContext();
        SvcLogicContext newVnfcCtx = new SvcLogicContext();
        Map<String, String> inParams = new HashMap<>();
        Map<String, String> vnfcParams = new HashMap<>();
        String responsePrefix = "test.";
        inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
        vnfcParams.put("vnfcName", "vnfcName2");
        aairn.getVnfcInformationForVserver(vnfcParams, newVnfcCtx, inParams, ctx, aairn.getAaiService(), responsePrefix);
        assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-name"), "vnfcName2");
        assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-type"), "vnfcType2");
        assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-function-code"), "vnfcFuncCode2");
        assertEquals(ctx.getAttribute("test.vm.vnfc.vnfc-group-notation"), "vnfcGrpNot2");
    }

    @Test
    public final void testGetFormattedValue() throws Exception{
        MockAAIResourceNode aairn = new MockAAIResourceNode();
        SvcLogicContext ctx = new SvcLogicContext();
        Map<String, String> inParams = new HashMap<>();
        inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
        inParams.put("inputParameter", "Some/Value/With/ Many Spaces");
        aairn.getFormattedValue(inParams, ctx);
        assertEquals(ctx.getAttribute("template-model-id"), "Some_Value_With_ManySpaces");
    }

    @Test
    public final void testProcessCheckForVfModule() throws Exception{
        MockAAIResourceNode aairn = new MockAAIResourceNode();
        SvcLogicContext ctx = new SvcLogicContext();
        Map<String,String> inParams = new HashMap<String, String>();
        inParams.put(AppcAaiClientConstant.INPUT_PARAM_RESPONSE_PREFIX, "test");
        inParams.put("inputParameter", "Some/Value/With/ Many Spaces");
        ctx.setAttribute("test.vnf.vm-with-no-vnfcs-count-vf-module", "0");
        ctx.setAttribute("test.vnf.vm-count-for-vf-module", "2");
        aairn.processCheckForVfModule("vfmoduleId1", ctx, "test.", 2);
    }

    @Test
    public void testGetVserverInfo() throws SvcLogicException {
        SvcLogicContext ctx = new SvcLogicContext();
        ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Map<String, String> inParams = new HashMap<>();
        AaiService aaiService = Mockito.mock(AaiService.class);
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        aai.getVserverInfo(inParams, ctx);
        Mockito.verify(aaiService).getVMInfo(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
    }

    @Test
    public void testGetVserverInfoExceptionFlow() throws Exception {
        SvcLogicContext ctx = new SvcLogicContext();
        ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
        Map<String, String> inParams = new HashMap<>();
        AaiService aaiService = Mockito.mock(AaiService.class);
        AAIResourceNode aai = Mockito.spy(new AAIResourceNode());
        Mockito.doThrow(new SvcLogicException("TEST")).when(aaiService).getVMInfo(Mockito.anyMap(), Mockito.any(SvcLogicContext.class));
        Mockito.doReturn(aaiService).when(aai).getAaiService();
        aai.getVserverInfo(inParams, ctx);
        assertEquals("TEST", ctx.getAttribute(AppcAaiClientConstant.OUTPUT_PARAM_ERROR_MESSAGE));
    }
}