summaryrefslogtreecommitdiffstats
path: root/so-nssmf-adapter-application/src/test/java/org/onap/so/adapters/nssmf/service/impl/NssmfManagerServiceImplTest.java
blob: df499535007eb3eb08ec876a29241a45189b1b3a (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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 # Copyright (c) 2020, CMCC Technologies Co., Ltd.
 #
 # 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.so.adapters.nssmf.service.impl;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpRequestBase;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.stubbing.Answer;
import org.onap.aai.domain.yang.ServiceInstance;
import org.onap.so.adapters.nssmf.config.NssmfAdapterConfig;
import org.onap.so.adapters.nssmf.consts.NssmfAdapterConsts;
import org.onap.so.adapters.nssmf.entity.NssmfInfo;
import org.onap.so.adapters.nssmf.entity.TokenResponse;
import org.onap.so.adapters.nssmf.enums.ActionType;
import org.onap.so.adapters.nssmf.enums.HttpMethod;
import org.onap.so.adapters.nssmf.exceptions.ApplicationException;
import org.onap.so.adapters.nssmf.util.RestUtil;
import org.onap.so.beans.nsmf.ActDeActNssi;
import org.onap.so.beans.nsmf.AllocateAnNssi;
import org.onap.so.beans.nsmf.AllocateCnNssi;
import org.onap.so.beans.nsmf.AllocateTnNssi;
import org.onap.so.beans.nsmf.AnSliceProfile;
import org.onap.so.beans.nsmf.ConnectionLink;
import org.onap.so.beans.nsmf.CnSliceProfile;
import org.onap.so.beans.nsmf.DeAllocateNssi;
import org.onap.so.beans.nsmf.EsrInfo;
import org.onap.so.beans.nsmf.JobStatusResponse;
import org.onap.so.beans.nsmf.PerfReq;
import org.onap.so.beans.nsmf.PerfReqEmbb;
import org.onap.so.beans.nsmf.QuerySubnetCapability;
import org.onap.so.beans.nsmf.NetworkType;
import org.onap.so.beans.nsmf.NsiInfo;
import org.onap.so.beans.nsmf.NssiResponse;
import org.onap.so.beans.nsmf.NssmfAdapterNBIRequest;
import org.onap.so.beans.nsmf.NssmfRequest;
import org.onap.so.beans.nsmf.ResponseDescriptor;
import org.onap.so.beans.nsmf.ServiceInfo;
import org.onap.so.beans.nsmf.TnSliceProfile;
import org.onap.so.beans.nsmf.TransportSliceNetwork;
import org.onap.so.beans.nsmf.UeMobilityLevel;
import org.onap.so.db.request.beans.ResourceOperationStatus;
import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.initMocks;
import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.unMarshal;
import static org.onap.so.beans.nsmf.NetworkType.ACCESS;
import static org.onap.so.beans.nsmf.NetworkType.CORE;
import static org.onap.so.beans.nsmf.NetworkType.TRANSPORT;
import static org.onap.so.beans.nsmf.ResourceSharingLevel.NON_SHARED;

@RunWith(SpringRunner.class)
public class NssmfManagerServiceImplTest {

    @Mock
    private RestUtil restUtil;


    private NssmfManagerServiceImpl nssiManagerService;

    @Mock
    private HttpResponse tokenResponse;

    @Mock
    private HttpEntity tokenEntity;

    @Mock
    private HttpResponse commonResponse;

    @Mock
    private HttpEntity commonEntity;

    @Mock
    private StatusLine statusLine;

    @Mock
    private HttpClient httpClient;

    private InputStream postStream;

    private InputStream tokenStream;

    @Mock
    private NssmfAdapterConfig adapterConfig;

    @Mock
    private ResourceOperationStatusRepository repository;

    @Before
    public void setUp() throws Exception {
        initMocks(this);

        nssiManagerService = new NssmfManagerServiceImpl();

        Field restUtil = nssiManagerService.getClass().getDeclaredField("restUtil");
        restUtil.setAccessible(true);
        restUtil.set(nssiManagerService, this.restUtil);

        Field adapterConfig = nssiManagerService.getClass().getDeclaredField("nssmfAdapterConfig");
        adapterConfig.setAccessible(true);
        adapterConfig.set(nssiManagerService, this.adapterConfig);

        Field repository = nssiManagerService.getClass().getDeclaredField("repository");
        repository.setAccessible(true);
        repository.set(nssiManagerService, this.repository);
        // nssiManagerService.setRestUtil(this.restUtil);

        when(this.restUtil.send(any(String.class), any(HttpMethod.class), any(), any(Header.class)))
                .thenCallRealMethod();
        when(this.restUtil.createResponse(any(Integer.class), any(String.class))).thenCallRealMethod();
    }

    private void createCommonMock(int statusCode, NssmfInfo nssmf) throws Exception {
        when(restUtil.getToken(any(NssmfInfo.class))).thenReturn("7512eb3feb5249eca5ddd742fedddd39");
        when(restUtil.getHttpsClient()).thenReturn(httpClient);
        when(restUtil.getServiceInstance(any())).thenReturn(new ServiceInstance());

        when(statusLine.getStatusCode()).thenReturn(statusCode);
        when(restUtil.getNssmfHost(any(EsrInfo.class))).thenReturn(nssmf);

        when(tokenResponse.getEntity()).thenReturn(tokenEntity);
        when(tokenResponse.getStatusLine()).thenReturn(statusLine);
        when(tokenEntity.getContent()).thenReturn(tokenStream);

        when(commonResponse.getEntity()).thenReturn(commonEntity);
        when(commonResponse.getStatusLine()).thenReturn(statusLine);
        when(commonEntity.getContent()).thenReturn(postStream);

        when(adapterConfig.getInfraAuth()).thenReturn("SW5mcmFQb3J0YWxDbGllbnQ6cGFzc3dvcmQxJA==");

        Answer<HttpResponse> answer = invocation -> {
            Object[] arguments = invocation.getArguments();
            if (arguments != null && arguments.length == 1 && arguments[0] != null) {

                HttpRequestBase base = (HttpRequestBase) arguments[0];
                if (base.getURI().toString().endsWith("/oauth/token")) {
                    return tokenResponse;
                } else {
                    return commonResponse;
                }
            }
            return commonResponse;
        };

        doAnswer(answer).when(httpClient).execute(any(HttpRequestBase.class));

    }

    @Test
    public void allocateCnNssiTest() throws Exception {
        allocateNssi(CORE);

    }

    @Test
    public void allocateTnNssiTest() throws Exception {
        allocateNssi(TRANSPORT);
    }

    @Test
    public void allocateAnNssiTest() throws Exception {
        allocateNssi(ACCESS);
    }


    public void allocateNssi(NetworkType domainType) throws Exception {
        NssmfInfo nssmf = new NssmfInfo();
        nssmf.setUserName("nssmf-user");
        nssmf.setPassword("nssmf-pass");
        nssmf.setPort("8080");
        nssmf.setIpAddress("127.0.0.1");
        nssmf.setUrl("http://127.0.0.1:8080");

        NssiResponse nssiRes = new NssiResponse();
        nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
        nssiRes.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");

        TokenResponse token = new TokenResponse();
        token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
        token.setExpires(1800);

        postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
        tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));

        createCommonMock(200, nssmf);
        NssmfAdapterNBIRequest nbiRequest = null;

        switch (domainType) {
            case CORE:
                nbiRequest = createCnAllocateNssi();
                break;
            case TRANSPORT:
                nbiRequest = createTnAllocateNssi();
                break;
            case ACCESS:
                nbiRequest = createAnAllocateNssi();
                break;
        }

        assertNotNull(nbiRequest);
        System.out.println(marshal(nbiRequest));
        ResponseEntity res = nssiManagerService.allocateNssi(nbiRequest);
        assertNotNull(res);
        assertNotNull(res.getBody());
        NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
        if (!domainType.equals(ACCESS)) {
            assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
            assertEquals(allRes.getNssiId(), "NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
        }

        System.out.println(res);
    }



    private NssmfAdapterNBIRequest createCnAllocateNssi() {
        CnSliceProfile sP = new CnSliceProfile();
        List<String> sns = new LinkedList<>();
        sns.add("001-100001");
        List<String> plmn = new LinkedList<>();
        plmn.add("460-00");
        plmn.add("460-01");
        PerfReqEmbb embb = new PerfReqEmbb();
        embb.setActivityFactor(50);
        List<PerfReqEmbb> embbList = new LinkedList<>();
        embbList.add(embb);
        PerfReq perfReq = new PerfReq();
        perfReq.setPerfReqEmbbList(embbList);
        List<String> taList = new LinkedList<>();
        taList.add("1");
        taList.add("2");
        taList.add("3");
        sP.setSnssaiList(sns);
        sP.setSliceProfileId("ab9af40f13f721b5f13539d87484098");
        sP.setPLMNIdList(plmn);
        sP.setPerfReq(perfReq);
        sP.setMaxNumberOfUEs(200);
        sP.setCoverageAreaTAList(taList);
        sP.setLatency(6);
        sP.setResourceSharingLevel(NON_SHARED);
        NsiInfo nsiInfo = new NsiInfo();
        nsiInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        nsiInfo.setNsiName("eMBB-001");
        AllocateCnNssi cnNssi = new AllocateCnNssi();
        cnNssi.setNssiId("NSST-C-001-HDBNJ-NSSMF-01-A-ZX");
        cnNssi.setNssiName("eMBB-001");
        cnNssi.setScriptName("CN1");
        cnNssi.setSliceProfile(sP);
        cnNssi.setNsiInfo(nsiInfo);

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(CORE);
        nbiRequest.setAllocateCnNssi(cnNssi);
        return nbiRequest;
    }

    private NssmfAdapterNBIRequest createAnAllocateNssi() {

        AnSliceProfile sP = new AnSliceProfile();
        List<String> sns = new LinkedList<>();
        sns.add("001-100001");
        List<String> plmn = new LinkedList<>();
        plmn.add("460-00");
        plmn.add("460-01");
        PerfReqEmbb embb = new PerfReqEmbb();
        embb.setActivityFactor(50);
        List<PerfReqEmbb> embbList = new LinkedList<>();
        embbList.add(embb);
        PerfReq perfReq = new PerfReq();
        perfReq.setPerfReqEmbbList(embbList);
        List<Integer> taList = new LinkedList<>();
        taList.add(1);
        taList.add(2);
        taList.add(3);
        sP.setSNSSAIList(sns);
        sP.setSliceProfileId("ab9af40f13f721b5f13539d87484098");
        sP.setPLMNIdList(plmn);
        sP.setPerfReq(perfReq);
        sP.setMaxNumberOfUEs(200);
        sP.setCoverageAreaTAList(taList);
        sP.setLatency(6);
        sP.setResourceSharingLevel(NON_SHARED);
        sP.setUeMobilityLevel(UeMobilityLevel.STATIONARY);
        sP.setResourceSharingLevel(NON_SHARED);
        NsiInfo nsiInfo = new NsiInfo();
        nsiInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        nsiInfo.setNsiName("eMBB-001");
        AllocateAnNssi anNssi = new AllocateAnNssi();
        anNssi.setNssiId("NSST-C-001-HDBNJ-NSSMF-01-A-ZX");
        anNssi.setNssiName("eMBB-001");
        anNssi.setScriptName("CN1");
        anNssi.setSliceProfile(sP);
        anNssi.setNsiInfo(nsiInfo);

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(ACCESS);
        nbiRequest.setAllocateAnNssi(anNssi);
        return nbiRequest;
    }


    private NssmfAdapterNBIRequest createTnAllocateNssi() {
        TnSliceProfile sP = new TnSliceProfile();
        List<String> sns = new LinkedList<>();
        sns.add("01-1EB5BA40");
        List<String> plmn = new LinkedList<>();
        plmn.add("460-00");
        PerfReqEmbb embb = new PerfReqEmbb();
        embb.setActivityFactor(50);
        List<PerfReqEmbb> embbList = new LinkedList<>();
        embbList.add(embb);

        sP.setSNSSAIList(sns);
        sP.setSliceProfileId("fec94836-87a0-41dc-a199-0ad9aa3890d1");
        sP.setPLMNIdList(plmn);
        sP.setLatency(10);
        sP.setMaxBandwidth(1000);
        sP.setJitter(10);

        List<TransportSliceNetwork> networks = new LinkedList<>();
        TransportSliceNetwork network = new TransportSliceNetwork();
        List<ConnectionLink> connectionLinks = new LinkedList<>();
        ConnectionLink connectionLink = new ConnectionLink();
        connectionLink.setTransportEndpointA("a47c76e3-c010-4eaf-adbb-0ba264118cab");
        connectionLink.setTransportEndpointB("c0c83e33-80d2-43da-b6cb-17b930420d74");
        connectionLinks.add(connectionLink);
        network.setConnectionLinks(connectionLinks);
        networks.add(network);

        NsiInfo nsiInfo = new NsiInfo();
        nsiInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        nsiInfo.setNsiName("eMBB-001");
        AllocateTnNssi tnNssi = new AllocateTnNssi();
        tnNssi.setTransportSliceNetworks(networks);
        tnNssi.setScriptName("TN");
        tnNssi.setSliceProfile(sP);
        tnNssi.setNsiInfo(nsiInfo);

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(TRANSPORT);
        nbiRequest.setAllocateTnNssi(tnNssi);
        return nbiRequest;
    }

    @Test
    public void deAllocateCnNssi() throws Exception {
        deAllocateNssi(CORE);
    }

    @Test
    public void deAllocateAnNssi() throws Exception {
        deAllocateNssi(ACCESS);
    }

    @Test
    public void deAllocateTnNssi() throws Exception {
        deAllocateNssi(TRANSPORT);
    }

    public void deAllocateNssi(NetworkType domainType) throws Exception {
        DeAllocateNssi deAllocateNssi = new DeAllocateNssi();
        deAllocateNssi.setTerminateNssiOption(0);
        List<String> snssai = new LinkedList<>();
        snssai.add("001-100001");
        deAllocateNssi.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        deAllocateNssi.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
        deAllocateNssi.setScriptName("CN1");
        deAllocateNssi.setSnssaiList(snssai);

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(domainType);
        nbiRequest.setDeAllocateNssi(deAllocateNssi);

        NssmfInfo nssmf = new NssmfInfo();
        nssmf.setUserName("nssmf-user");
        nssmf.setPassword("nssmf-pass");
        nssmf.setPort("8080");
        nssmf.setIpAddress("127.0.0.1");

        NssiResponse nssiRes = new NssiResponse();
        nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");

        TokenResponse token = new TokenResponse();
        token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
        token.setExpires(1800);

        postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
        tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));

        createCommonMock(202, nssmf);
        ResponseEntity res = nssiManagerService.deAllocateNssi(nbiRequest, "ab9af40f13f721b5f13539d87484098");
        assertNotNull(res);
        assertNotNull(res.getBody());
        NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
        if (!domainType.equals(ACCESS)) {
            assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
        }
    }

    @Test
    public void activateAnNssi() throws Exception {
        activateNssi(ACCESS);
    }


    @Test
    public void activateCnNssi() throws Exception {
        activateNssi(CORE);
    }


    @Test
    public void activateTnNssi() throws Exception {
        activateNssi(TRANSPORT);
    }


    private void activateNssi(NetworkType domainType) throws Exception {
        NssmfInfo nssmf = new NssmfInfo();
        nssmf.setUserName("nssmf-user");
        nssmf.setPassword("nssmf-pass");
        nssmf.setPort("8080");
        nssmf.setIpAddress("127.0.0.1");

        NssiResponse nssiRes = new NssiResponse();
        nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");

        TokenResponse token = new TokenResponse();
        token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
        token.setExpires(1800);

        postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
        tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));

        ActDeActNssi act = new ActDeActNssi();
        act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(domainType);
        nbiRequest.setActDeActNssi(act);

        createCommonMock(200, nssmf);
        ResponseEntity res = nssiManagerService.activateNssi(nbiRequest, "001-100001");
        assertNotNull(res);
        assertNotNull(res.getBody());
        NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
        if (!domainType.equals(ACCESS)) {
            assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
        }
    }

    @Test
    public void deActivateNssi() throws Exception {
        NssmfInfo nssmf = new NssmfInfo();
        nssmf.setUserName("nssmf-user");
        nssmf.setPassword("nssmf-pass");
        nssmf.setPort("8080");
        nssmf.setIpAddress("127.0.0.1");

        NssiResponse nssiRes = new NssiResponse();
        nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");

        TokenResponse token = new TokenResponse();
        token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
        token.setExpires(1800);

        postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
        tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));

        ActDeActNssi act = new ActDeActNssi();
        act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(CORE);
        nbiRequest.setActDeActNssi(act);

        createCommonMock(200, nssmf);
        ResponseEntity res = nssiManagerService.deActivateNssi(nbiRequest, "001-100001");
        assertNotNull(res);
        assertNotNull(res.getBody());
        NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
        assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
    }

    @Test
    public void testNssmfRequest() throws ApplicationException {
        NssmfRequest nssmfRequest = new NssmfRequest();
        String sst = marshal(nssmfRequest);
        System.out.println(sst);
    }

    @Test
    public void queryCnAllocateJobStatus() throws Exception {
        queryJobStatus(CORE, ActionType.ALLOCATE.toString());
    }

    @Test
    public void queryCnActivateJobStatus() throws Exception {
        queryJobStatus(CORE, ActionType.ACTIVATE.toString());
    }

    @Test
    public void queryCnDeActivateJobStatus() throws Exception {
        queryJobStatus(CORE, ActionType.DEACTIVATE.toString());
    }

    @Test
    public void queryAnJobStatus() throws Exception {
        queryJobStatus(ACCESS, ActionType.ALLOCATE.toString());
    }

    public void queryJobStatus(NetworkType domainType, String action) throws Exception {
        NssmfInfo nssmf = new NssmfInfo();
        nssmf.setUserName("nssmf-user");
        nssmf.setPassword("nssmf-pass");
        nssmf.setPort("8080");
        nssmf.setIpAddress("127.0.0.1");

        JobStatusResponse jobStatusResponse = new JobStatusResponse();
        ResponseDescriptor descriptor = new ResponseDescriptor();
        descriptor.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
        descriptor.setProgress(100);
        descriptor.setStatusDescription("Initiating VNF Instance");
        descriptor.setStatus("FINISHED");
        jobStatusResponse.setResponseDescriptor(descriptor);

        TokenResponse token = new TokenResponse();
        token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
        token.setExpires(1800);

        postStream = new ByteArrayInputStream(marshal(jobStatusResponse).getBytes(UTF_8));
        tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));

        ResourceOperationStatus operationStatus = new ResourceOperationStatus();
        operationStatus.setOperationId("4b45d919816ccaa2b762df5120f72067");
        operationStatus.setResourceTemplateUUID("8ee5926d-720b-4bb2-86f9-d20e921c143b");
        operationStatus.setServiceId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        operationStatus.setOperType(action);

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(domainType);
        nbiRequest.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
        List<ResourceOperationStatus> optional = new ArrayList<>();
        optional.add(operationStatus);

        doAnswer(invocation -> optional).when(repository).findByServiceIdAndOperationId(any(), any());

        createCommonMock(200, nssmf);

        ResponseEntity res = nssiManagerService.queryJobStatus(nbiRequest, "4b45d919816ccaa2b762df5120f72067");
        assertNotNull(res);
        assertNotNull(res.getBody());
        JobStatusResponse allRes = unMarshal(res.getBody().toString(), JobStatusResponse.class);
        assertEquals(allRes.getResponseDescriptor().getProgress(), 100);
        assertEquals(allRes.getResponseDescriptor().getStatus(), "FINISHED");
        if (!domainType.equals(ACCESS)) {
            assertEquals(allRes.getResponseDescriptor().getResponseId(), "7512eb3feb5249eca5ddd742fedddd39");
        }

    }

    @Test
    public void queryNSSISelectionCapability() throws Exception {

        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(CORE);
        ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
        assertNotNull(res);
        assertNotNull(res.getBody());
        Map allRes = unMarshal(res.getBody().toString(), Map.class);
        assertEquals(allRes.get("selection"), "NSMF");

        System.out.println(res);

        nbiRequest.getEsrInfo().setVendor(NssmfAdapterConsts.ONAP_INTERNAL_TAG);
        res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
        assertNotNull(res);
        assertNotNull(res.getBody());
        allRes = unMarshal(res.getBody().toString(), Map.class);
        assertEquals(allRes.get("selection"), "NSSMF");

        System.out.println(res);

        nbiRequest.getEsrInfo().setNetworkType(NetworkType.ACCESS);
        res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
        assertNotNull(res);
        assertNotNull(res.getBody());
        allRes = unMarshal(res.getBody().toString(), Map.class);
        assertEquals(allRes.get("selection"), "NSSMF");

        System.out.println(res);
    }

    private NssmfAdapterNBIRequest createNbiRequest(NetworkType networkType) {
        NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest();
        EsrInfo esrInfo = new EsrInfo();
        switch (networkType) {
            case CORE:
                esrInfo.setVendor("huawei");
                esrInfo.setNetworkType(CORE);
                break;
            case TRANSPORT:
                esrInfo.setVendor("ONAP_internal");
                esrInfo.setNetworkType(TRANSPORT);
                break;
            case ACCESS:
                esrInfo.setVendor("huawei");
                esrInfo.setNetworkType(ACCESS);
                break;
        }
        ServiceInfo serviceInfo = new ServiceInfo();
        serviceInfo.setServiceUuid("8ee5926d-720b-4bb2-86f9-d20e921c143b");
        serviceInfo.setServiceInvariantUuid("e75698d9-925a-4cdd-a6c0-edacbe6a0b51");
        serviceInfo.setGlobalSubscriberId("5GCustomer");
        serviceInfo.setServiceType("5G");
        serviceInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
        nbiRequest.setEsrInfo(esrInfo);
        nbiRequest.setServiceInfo(serviceInfo);
        return nbiRequest;
    }

    @Test
    public void querySubnetCapability() {
        NssmfAdapterNBIRequest nbiRequest = createNbiRequest(CORE);

        QuerySubnetCapability subnetCapabilityQuery = new QuerySubnetCapability();
        List<String> subnetTypes = Arrays.asList("CN");
        subnetCapabilityQuery.setSubnetTypes(subnetTypes);
        nbiRequest.setSubnetCapabilityQuery(subnetCapabilityQuery);
        ResponseEntity res = nssiManagerService.querySubnetCapability(nbiRequest);
        assertNotNull(res);
        assertNotNull(res.getBody());
    }
}