2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  # Copyright (c) 2020, CMCC Technologies Co., Ltd.
 
   7  # Licensed under the Apache License, Version 2.0 (the "License")
 
   8  # you may not use this file except in compliance with the License.
 
   9  # You may obtain a copy of the License at
 
  11  #       http://www.apache.org/licenses/LICENSE-2.0
 
  13  # Unless required by applicable law or agreed to in writing, software
 
  14  # distributed under the License is distributed on an "AS IS" BASIS,
 
  15  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  16  # See the License for the specific language governing permissions and
 
  17  # limitations under the License.
 
  18  * ============LICENSE_END=========================================================
 
  21 package org.onap.so.adapters.nssmf.service.impl;
 
  24 import org.apache.http.Header;
 
  25 import org.apache.http.HttpEntity;
 
  26 import org.apache.http.HttpResponse;
 
  27 import org.apache.http.StatusLine;
 
  28 import org.apache.http.client.HttpClient;
 
  29 import org.apache.http.client.methods.HttpRequestBase;
 
  30 import org.junit.Before;
 
  31 import org.junit.Test;
 
  32 import org.junit.runner.RunWith;
 
  33 import org.mockito.Mock;
 
  34 import org.mockito.stubbing.Answer;
 
  35 import org.onap.so.adapters.nssmf.consts.NssmfAdapterConsts;
 
  36 import org.onap.so.adapters.nssmf.entity.NssmfInfo;
 
  37 import org.onap.so.adapters.nssmf.entity.TokenResponse;
 
  38 import org.onap.so.adapters.nssmf.enums.HttpMethod;
 
  39 import org.onap.so.adapters.nssmf.util.RestUtil;
 
  40 import org.onap.so.beans.nsmf.*;
 
  41 import org.onap.so.beans.nsmf.oof.SubnetCapability;
 
  42 import org.onap.so.db.request.beans.ResourceOperationStatus;
 
  43 import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
 
  44 import org.springframework.http.ResponseEntity;
 
  45 import org.springframework.test.context.junit4.SpringRunner;
 
  46 import java.io.ByteArrayInputStream;
 
  47 import java.io.InputStream;
 
  48 import java.lang.reflect.Field;
 
  50 import static java.nio.charset.StandardCharsets.UTF_8;
 
  51 import static org.junit.Assert.assertEquals;
 
  52 import static org.junit.Assert.assertNotNull;
 
  53 import static org.mockito.ArgumentMatchers.any;
 
  54 import static org.mockito.Mockito.doAnswer;
 
  55 import static org.mockito.Mockito.when;
 
  56 import static org.mockito.MockitoAnnotations.initMocks;
 
  57 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
 
  58 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.unMarshal;
 
  59 import static org.onap.so.beans.nsmf.NetworkType.CORE;
 
  60 import static org.onap.so.beans.nsmf.ResourceSharingLevel.NON_SHARED;
 
  62 @RunWith(SpringRunner.class)
 
  63 public class NssmfManagerServiceImplTest {
 
  66     private RestUtil restUtil;
 
  69     private NssmfManagerServiceImpl nssiManagerService;
 
  72     private HttpResponse tokenResponse;
 
  75     private HttpEntity tokenEntity;
 
  78     private HttpResponse commonResponse;
 
  81     private HttpEntity commonEntity;
 
  84     private StatusLine statusLine;
 
  87     private HttpClient httpClient;
 
  89     private InputStream postStream;
 
  91     private InputStream tokenStream;
 
  94     private ResourceOperationStatusRepository repository;
 
  97     public void setUp() throws Exception {
 
 100         nssiManagerService = new NssmfManagerServiceImpl();
 
 102         Field restUtil = nssiManagerService.getClass().getDeclaredField("restUtil");
 
 103         restUtil.setAccessible(true);
 
 104         restUtil.set(nssiManagerService, this.restUtil);
 
 106         Field repository = nssiManagerService.getClass().getDeclaredField("repository");
 
 107         repository.setAccessible(true);
 
 108         repository.set(nssiManagerService, this.repository);
 
 109         // nssiManagerService.setRestUtil(this.restUtil);
 
 111         when(this.restUtil.send(any(String.class), any(HttpMethod.class), any(), any(Header.class)))
 
 112                 .thenCallRealMethod();
 
 113         when(this.restUtil.createResponse(any(Integer.class), any(String.class))).thenCallRealMethod();
 
 116     private void createCommonMock(int statusCode, NssmfInfo nssmf) throws Exception {
 
 117         when(restUtil.getToken(any(NssmfInfo.class))).thenReturn("7512eb3feb5249eca5ddd742fedddd39");
 
 118         when(restUtil.getHttpsClient()).thenReturn(httpClient);
 
 120         when(statusLine.getStatusCode()).thenReturn(statusCode);
 
 121         when(restUtil.getNssmfHost(any(EsrInfo.class))).thenReturn(nssmf);
 
 123         when(tokenResponse.getEntity()).thenReturn(tokenEntity);
 
 124         when(tokenResponse.getStatusLine()).thenReturn(statusLine);
 
 125         when(tokenEntity.getContent()).thenReturn(tokenStream);
 
 127         when(commonResponse.getEntity()).thenReturn(commonEntity);
 
 128         when(commonResponse.getStatusLine()).thenReturn(statusLine);
 
 129         when(commonEntity.getContent()).thenReturn(postStream);
 
 131         Answer<HttpResponse> answer = invocation -> {
 
 132             Object[] arguments = invocation.getArguments();
 
 133             if (arguments != null && arguments.length == 1 && arguments[0] != null) {
 
 135                 HttpRequestBase base = (HttpRequestBase) arguments[0];
 
 136                 if (base.getURI().toString().endsWith("/oauth/token")) {
 
 137                     return tokenResponse;
 
 139                     return commonResponse;
 
 142             return commonResponse;
 
 145         doAnswer(answer).when(httpClient).execute(any(HttpRequestBase.class));
 
 150     public void allocateNssi() throws Exception {
 
 152         NssmfInfo nssmf = new NssmfInfo();
 
 153         nssmf.setUserName("nssmf-user");
 
 154         nssmf.setPassword("nssmf-pass");
 
 155         nssmf.setPort("8080");
 
 156         nssmf.setIpAddress("127.0.0.1");
 
 157         nssmf.setUrl("http://127.0.0.1:8080");
 
 159         NssiResponse nssiRes = new NssiResponse();
 
 160         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
 
 161         nssiRes.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
 
 163         TokenResponse token = new TokenResponse();
 
 164         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
 
 165         token.setExpires(1800);
 
 167         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
 
 168         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
 
 170         createCommonMock(200, nssmf);
 
 173         NssmfAdapterNBIRequest nbiRequest = createAllocateNssi();
 
 174         assertNotNull(nbiRequest);
 
 175         System.out.println(marshal(nbiRequest));
 
 176         ResponseEntity res = nssiManagerService.allocateNssi(nbiRequest);
 
 178         assertNotNull(res.getBody());
 
 179         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
 
 180         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
 
 181         assertEquals(allRes.getNssiId(), "NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
 
 183         System.out.println(res);
 
 188     private NssmfAdapterNBIRequest createAllocateNssi() {
 
 189         CnSliceProfile sP = new CnSliceProfile();
 
 190         List<String> sns = new LinkedList<>();
 
 191         sns.add("001-100001");
 
 192         List<String> plmn = new LinkedList<>();
 
 195         PerfReqEmbb embb = new PerfReqEmbb();
 
 196         embb.setActivityFactor(50);
 
 197         List<PerfReqEmbb> embbList = new LinkedList<>();
 
 199         PerfReq perfReq = new PerfReq();
 
 200         perfReq.setPerfReqEmbbList(embbList);
 
 201         List<String> taList = new LinkedList<>();
 
 205         sP.setSnssaiList(sns);
 
 206         sP.setSliceProfileId("ab9af40f13f721b5f13539d87484098");
 
 207         sP.setPLMNIdList(plmn);
 
 208         sP.setPerfReq(perfReq);
 
 209         sP.setMaxNumberOfUEs(200);
 
 210         sP.setCoverageAreaTAList(taList);
 
 212         sP.setResourceSharingLevel(NON_SHARED);
 
 213         NsiInfo nsiInfo = new NsiInfo();
 
 214         nsiInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
 
 215         nsiInfo.setNsiName("eMBB-001");
 
 216         AllocateCnNssi cnNssi = new AllocateCnNssi();
 
 217         cnNssi.setNssiId("NSST-C-001-HDBNJ-NSSMF-01-A-ZX");
 
 218         cnNssi.setNssiName("eMBB-001");
 
 219         cnNssi.setScriptName("CN1");
 
 220         cnNssi.setSliceProfile(sP);
 
 221         cnNssi.setNsiInfo(nsiInfo);
 
 223         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 224         nbiRequest.setAllocateCnNssi(cnNssi);
 
 229     public void deAllocateNssi() throws Exception {
 
 230         DeAllocateNssi deAllocateNssi = new DeAllocateNssi();
 
 231         deAllocateNssi.setTerminateNssiOption(0);
 
 232         List<String> snssai = new LinkedList<>();
 
 233         snssai.add("001-100001");
 
 234         deAllocateNssi.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
 
 235         deAllocateNssi.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
 
 236         deAllocateNssi.setScriptName("CN1");
 
 237         deAllocateNssi.setSnssaiList(snssai);
 
 239         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 240         nbiRequest.setDeAllocateNssi(deAllocateNssi);
 
 242         NssmfInfo nssmf = new NssmfInfo();
 
 243         nssmf.setUserName("nssmf-user");
 
 244         nssmf.setPassword("nssmf-pass");
 
 245         nssmf.setPort("8080");
 
 246         nssmf.setIpAddress("127.0.0.1");
 
 248         NssiResponse nssiRes = new NssiResponse();
 
 249         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
 
 251         TokenResponse token = new TokenResponse();
 
 252         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
 
 253         token.setExpires(1800);
 
 255         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
 
 256         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
 
 258         createCommonMock(202, nssmf);
 
 259         ResponseEntity res = nssiManagerService.deAllocateNssi(nbiRequest, "ab9af40f13f721b5f13539d87484098");
 
 261         assertNotNull(res.getBody());
 
 262         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
 
 263         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
 
 265         assertNotNull(res.getBody());
 
 269     public void activateNssi() throws Exception {
 
 270         NssmfInfo nssmf = new NssmfInfo();
 
 271         nssmf.setUserName("nssmf-user");
 
 272         nssmf.setPassword("nssmf-pass");
 
 273         nssmf.setPort("8080");
 
 274         nssmf.setIpAddress("127.0.0.1");
 
 276         NssiResponse nssiRes = new NssiResponse();
 
 277         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
 
 279         TokenResponse token = new TokenResponse();
 
 280         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
 
 281         token.setExpires(1800);
 
 283         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
 
 284         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
 
 286         ActDeActNssi act = new ActDeActNssi();
 
 287         act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
 
 288         act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
 
 290         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 291         nbiRequest.setActDeActNssi(act);
 
 293         createCommonMock(200, nssmf);
 
 294         ResponseEntity res = nssiManagerService.activateNssi(nbiRequest, "001-100001");
 
 296         assertNotNull(res.getBody());
 
 297         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
 
 298         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
 
 302     public void deActivateNssi() throws Exception {
 
 303         NssmfInfo nssmf = new NssmfInfo();
 
 304         nssmf.setUserName("nssmf-user");
 
 305         nssmf.setPassword("nssmf-pass");
 
 306         nssmf.setPort("8080");
 
 307         nssmf.setIpAddress("127.0.0.1");
 
 309         NssiResponse nssiRes = new NssiResponse();
 
 310         nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
 
 312         TokenResponse token = new TokenResponse();
 
 313         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
 
 314         token.setExpires(1800);
 
 316         postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
 
 317         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
 
 319         ActDeActNssi act = new ActDeActNssi();
 
 320         act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
 
 321         act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
 
 323         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 324         nbiRequest.setActDeActNssi(act);
 
 326         createCommonMock(200, nssmf);
 
 327         ResponseEntity res = nssiManagerService.deActivateNssi(nbiRequest, "001-100001");
 
 329         assertNotNull(res.getBody());
 
 330         NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
 
 331         assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
 
 335     public void queryJobStatus() throws Exception {
 
 336         NssmfInfo nssmf = new NssmfInfo();
 
 337         nssmf.setUserName("nssmf-user");
 
 338         nssmf.setPassword("nssmf-pass");
 
 339         nssmf.setPort("8080");
 
 340         nssmf.setIpAddress("127.0.0.1");
 
 342         JobStatusResponse jobStatusResponse = new JobStatusResponse();
 
 343         ResponseDescriptor descriptor = new ResponseDescriptor();
 
 344         descriptor.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
 
 345         descriptor.setProgress(20);
 
 346         descriptor.setStatusDescription("Initiating VNF Instance");
 
 347         descriptor.setStatus("processing");
 
 348         jobStatusResponse.setResponseDescriptor(descriptor);
 
 350         TokenResponse token = new TokenResponse();
 
 351         token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
 
 352         token.setExpires(1800);
 
 354         postStream = new ByteArrayInputStream(marshal(jobStatusResponse).getBytes(UTF_8));
 
 355         tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
 
 357         ResourceOperationStatus operationStatus = new ResourceOperationStatus();
 
 358         operationStatus.setOperationId("4b45d919816ccaa2b762df5120f72067");
 
 359         operationStatus.setResourceTemplateUUID("8ee5926d-720b-4bb2-86f9-d20e921c143b");
 
 360         operationStatus.setServiceId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
 
 362         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 363         nbiRequest.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
 
 364         List<ResourceOperationStatus> optional = new ArrayList<>();
 
 365         optional.add(operationStatus);
 
 367         doAnswer(invocation -> optional).when(repository).findByServiceIdAndOperationId(any(), any());
 
 369         createCommonMock(200, nssmf);
 
 371         ResponseEntity res = nssiManagerService.queryJobStatus(nbiRequest, "4b45d919816ccaa2b762df5120f72067");
 
 373         assertNotNull(res.getBody());
 
 374         JobStatusResponse allRes = unMarshal(res.getBody().toString(), JobStatusResponse.class);
 
 375         assertEquals(allRes.getResponseDescriptor().getProgress(), 20);
 
 376         assertEquals(allRes.getResponseDescriptor().getStatus(), "processing");
 
 377         assertEquals(allRes.getResponseDescriptor().getResponseId(), "7512eb3feb5249eca5ddd742fedddd39");
 
 379         System.out.println(res);
 
 384     public void queryNSSISelectionCapability() throws Exception {
 
 386         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 387         ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
 
 389         assertNotNull(res.getBody());
 
 390         Map allRes = unMarshal(res.getBody().toString(), Map.class);
 
 391         assertEquals(allRes.get("selection"), "NSMF");
 
 393         System.out.println(res);
 
 395         nbiRequest.getEsrInfo().setVendor(NssmfAdapterConsts.ONAP_INTERNAL_TAG);
 
 396         res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
 
 398         assertNotNull(res.getBody());
 
 399         allRes = unMarshal(res.getBody().toString(), Map.class);
 
 400         assertEquals(allRes.get("selection"), "NSSMF");
 
 402         System.out.println(res);
 
 404         nbiRequest.getEsrInfo().setNetworkType(NetworkType.ACCESS);
 
 405         res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
 
 407         assertNotNull(res.getBody());
 
 408         allRes = unMarshal(res.getBody().toString(), Map.class);
 
 409         assertEquals(allRes.get("selection"), "NSSMF");
 
 411         System.out.println(res);
 
 414     private NssmfAdapterNBIRequest createNbiRequest() {
 
 415         NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest();
 
 416         EsrInfo esrInfo = new EsrInfo();
 
 417         esrInfo.setVendor("huawei");
 
 418         esrInfo.setNetworkType(CORE);
 
 419         ServiceInfo serviceInfo = new ServiceInfo();
 
 420         serviceInfo.setServiceUuid("8ee5926d-720b-4bb2-86f9-d20e921c143b");
 
 421         serviceInfo.setServiceInvariantUuid("e75698d9-925a-4cdd-a6c0-edacbe6a0b51");
 
 422         serviceInfo.setGlobalSubscriberId("5GCustomer");
 
 423         serviceInfo.setServiceType("5G");
 
 424         serviceInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
 
 425         nbiRequest.setEsrInfo(esrInfo);
 
 426         nbiRequest.setServiceInfo(serviceInfo);
 
 431     public void querySubnetCapability() {
 
 432         NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
 
 434         QuerySubnetCapability subnetCapabilityQuery = new QuerySubnetCapability();
 
 435         List<String> subnetTypes = Arrays.asList("TN-FH", "TN-MH", "TN-BH");
 
 436         subnetCapabilityQuery.setSubnetTypes(subnetTypes);
 
 437         nbiRequest.setSubnetCapabilityQuery(subnetCapabilityQuery);
 
 438         ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
 
 440         assertNotNull(res.getBody());