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.db.request.beans.ResourceOperationStatus;
42 import org.onap.so.db.request.data.repository.ResourceOperationStatusRepository;
43 import org.springframework.http.ResponseEntity;
44 import org.springframework.test.context.junit4.SpringRunner;
45 import java.io.ByteArrayInputStream;
46 import java.io.InputStream;
47 import java.lang.reflect.Field;
49 import static java.nio.charset.StandardCharsets.UTF_8;
50 import static org.junit.Assert.assertEquals;
51 import static org.junit.Assert.assertNotNull;
52 import static org.mockito.ArgumentMatchers.any;
53 import static org.mockito.Mockito.doAnswer;
54 import static org.mockito.Mockito.when;
55 import static org.mockito.MockitoAnnotations.initMocks;
56 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.marshal;
57 import static org.onap.so.adapters.nssmf.util.NssmfAdapterUtil.unMarshal;
58 import static org.onap.so.beans.nsmf.NetworkType.CORE;
59 import static org.onap.so.beans.nsmf.ResourceSharingLevel.NON_SHARED;
61 @RunWith(SpringRunner.class)
62 public class NssmfManagerServiceImplTest {
65 private RestUtil restUtil;
68 private NssmfManagerServiceImpl nssiManagerService;
71 private HttpResponse tokenResponse;
74 private HttpEntity tokenEntity;
77 private HttpResponse commonResponse;
80 private HttpEntity commonEntity;
83 private StatusLine statusLine;
86 private HttpClient httpClient;
88 private InputStream postStream;
90 private InputStream tokenStream;
93 private ResourceOperationStatusRepository repository;
96 public void setUp() throws Exception {
99 nssiManagerService = new NssmfManagerServiceImpl();
101 Field restUtil = nssiManagerService.getClass().getDeclaredField("restUtil");
102 restUtil.setAccessible(true);
103 restUtil.set(nssiManagerService, this.restUtil);
105 Field repository = nssiManagerService.getClass().getDeclaredField("repository");
106 repository.setAccessible(true);
107 repository.set(nssiManagerService, this.repository);
108 // nssiManagerService.setRestUtil(this.restUtil);
110 when(this.restUtil.send(any(String.class), any(HttpMethod.class), any(), any(Header.class)))
111 .thenCallRealMethod();
112 when(this.restUtil.createResponse(any(Integer.class), any(String.class))).thenCallRealMethod();
115 private void createCommonMock(int statusCode, NssmfInfo nssmf) throws Exception {
116 when(restUtil.getToken(any(NssmfInfo.class))).thenReturn("7512eb3feb5249eca5ddd742fedddd39");
117 when(restUtil.getHttpsClient()).thenReturn(httpClient);
119 when(statusLine.getStatusCode()).thenReturn(statusCode);
120 when(restUtil.getNssmfHost(any(EsrInfo.class))).thenReturn(nssmf);
122 when(tokenResponse.getEntity()).thenReturn(tokenEntity);
123 when(tokenResponse.getStatusLine()).thenReturn(statusLine);
124 when(tokenEntity.getContent()).thenReturn(tokenStream);
126 when(commonResponse.getEntity()).thenReturn(commonEntity);
127 when(commonResponse.getStatusLine()).thenReturn(statusLine);
128 when(commonEntity.getContent()).thenReturn(postStream);
130 Answer<HttpResponse> answer = invocation -> {
131 Object[] arguments = invocation.getArguments();
132 if (arguments != null && arguments.length == 1 && arguments[0] != null) {
134 HttpRequestBase base = (HttpRequestBase) arguments[0];
135 if (base.getURI().toString().endsWith("/oauth/token")) {
136 return tokenResponse;
138 return commonResponse;
141 return commonResponse;
144 doAnswer(answer).when(httpClient).execute(any(HttpRequestBase.class));
149 public void allocateNssi() throws Exception {
151 NssmfInfo nssmf = new NssmfInfo();
152 nssmf.setUserName("nssmf-user");
153 nssmf.setPassword("nssmf-pass");
154 nssmf.setPort("8080");
155 nssmf.setIpAddress("127.0.0.1");
156 nssmf.setUrl("http://127.0.0.1:8080");
158 NssiResponse nssiRes = new NssiResponse();
159 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
160 nssiRes.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
162 TokenResponse token = new TokenResponse();
163 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
164 token.setExpires(1800);
166 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
167 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
169 createCommonMock(200, nssmf);
172 NssmfAdapterNBIRequest nbiRequest = createAllocateNssi();
173 assertNotNull(nbiRequest);
174 System.out.println(marshal(nbiRequest));
175 ResponseEntity res = nssiManagerService.allocateNssi(nbiRequest);
177 assertNotNull(res.getBody());
178 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
179 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
180 assertEquals(allRes.getNssiId(), "NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
182 System.out.println(res);
187 private NssmfAdapterNBIRequest createAllocateNssi() {
188 CnSliceProfile sP = new CnSliceProfile();
189 List<String> sns = new LinkedList<>();
190 sns.add("001-100001");
191 List<String> plmn = new LinkedList<>();
194 PerfReqEmbbList embb = new PerfReqEmbbList();
195 embb.setActivityFactor(50);
196 List<PerfReqEmbbList> embbList = new LinkedList<>();
198 PerfReq perfReq = new PerfReq();
199 perfReq.setPerfReqEmbbList(embbList);
200 List<String> taList = new LinkedList<>();
204 sP.setSnssaiList(sns);
205 sP.setSliceProfileId("ab9af40f13f721b5f13539d87484098");
206 sP.setPlmnIdList(plmn);
207 sP.setPerfReq(perfReq);
208 sP.setMaxNumberofUEs(200);
209 sP.setCoverageAreaTAList(taList);
211 sP.setResourceSharingLevel(NON_SHARED);
212 NsiInfo nsiInfo = new NsiInfo();
213 nsiInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
214 nsiInfo.setNsiName("eMBB-001");
215 AllocateCnNssi cnNssi = new AllocateCnNssi();
216 cnNssi.setNssiId("NSST-C-001-HDBNJ-NSSMF-01-A-ZX");
217 cnNssi.setNssiName("eMBB-001");
218 cnNssi.setScriptName("CN1");
219 cnNssi.setSliceProfile(sP);
220 cnNssi.setNsiInfo(nsiInfo);
222 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
223 nbiRequest.setAllocateCnNssi(cnNssi);
228 public void deAllocateNssi() throws Exception {
229 DeAllocateNssi deAllocateNssi = new DeAllocateNssi();
230 deAllocateNssi.setTerminateNssiOption(0);
231 List<String> snssai = new LinkedList<>();
232 snssai.add("001-100001");
233 deAllocateNssi.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
234 deAllocateNssi.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
235 deAllocateNssi.setScriptName("CN1");
236 deAllocateNssi.setSnssaiList(snssai);
238 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
239 nbiRequest.setDeAllocateNssi(deAllocateNssi);
241 NssmfInfo nssmf = new NssmfInfo();
242 nssmf.setUserName("nssmf-user");
243 nssmf.setPassword("nssmf-pass");
244 nssmf.setPort("8080");
245 nssmf.setIpAddress("127.0.0.1");
247 NssiResponse nssiRes = new NssiResponse();
248 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
250 TokenResponse token = new TokenResponse();
251 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
252 token.setExpires(1800);
254 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
255 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
257 createCommonMock(202, nssmf);
258 ResponseEntity res = nssiManagerService.deAllocateNssi(nbiRequest, "ab9af40f13f721b5f13539d87484098");
260 assertNotNull(res.getBody());
261 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
262 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
264 assertNotNull(res.getBody());
268 public void activateNssi() throws Exception {
269 NssmfInfo nssmf = new NssmfInfo();
270 nssmf.setUserName("nssmf-user");
271 nssmf.setPassword("nssmf-pass");
272 nssmf.setPort("8080");
273 nssmf.setIpAddress("127.0.0.1");
275 NssiResponse nssiRes = new NssiResponse();
276 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
278 TokenResponse token = new TokenResponse();
279 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
280 token.setExpires(1800);
282 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
283 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
285 ActDeActNssi act = new ActDeActNssi();
286 act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
287 act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
289 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
290 nbiRequest.setActDeActNssi(act);
292 createCommonMock(200, nssmf);
293 ResponseEntity res = nssiManagerService.activateNssi(nbiRequest, "001-100001");
295 assertNotNull(res.getBody());
296 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
297 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
301 public void deActivateNssi() throws Exception {
302 NssmfInfo nssmf = new NssmfInfo();
303 nssmf.setUserName("nssmf-user");
304 nssmf.setPassword("nssmf-pass");
305 nssmf.setPort("8080");
306 nssmf.setIpAddress("127.0.0.1");
308 NssiResponse nssiRes = new NssiResponse();
309 nssiRes.setJobId("4b45d919816ccaa2b762df5120f72067");
311 TokenResponse token = new TokenResponse();
312 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
313 token.setExpires(1800);
315 postStream = new ByteArrayInputStream(marshal(nssiRes).getBytes(UTF_8));
316 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
318 ActDeActNssi act = new ActDeActNssi();
319 act.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
320 act.setNssiId("NSSI-C-001-HDBNJ-NSSMF-01-A-ZX");
322 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
323 nbiRequest.setActDeActNssi(act);
325 createCommonMock(200, nssmf);
326 ResponseEntity res = nssiManagerService.deActivateNssi(nbiRequest, "001-100001");
328 assertNotNull(res.getBody());
329 NssiResponse allRes = unMarshal(res.getBody().toString(), NssiResponse.class);
330 assertEquals(allRes.getJobId(), "4b45d919816ccaa2b762df5120f72067");
334 public void queryJobStatus() throws Exception {
335 NssmfInfo nssmf = new NssmfInfo();
336 nssmf.setUserName("nssmf-user");
337 nssmf.setPassword("nssmf-pass");
338 nssmf.setPort("8080");
339 nssmf.setIpAddress("127.0.0.1");
341 JobStatusResponse jobStatusResponse = new JobStatusResponse();
342 ResponseDescriptor descriptor = new ResponseDescriptor();
343 descriptor.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
344 descriptor.setProgress(20);
345 descriptor.setStatusDescription("Initiating VNF Instance");
346 descriptor.setStatus("processing");
347 jobStatusResponse.setResponseDescriptor(descriptor);
349 TokenResponse token = new TokenResponse();
350 token.setAccessToken("7512eb3feb5249eca5ddd742fedddd39");
351 token.setExpires(1800);
353 postStream = new ByteArrayInputStream(marshal(jobStatusResponse).getBytes(UTF_8));
354 tokenStream = new ByteArrayInputStream(marshal(token).getBytes(UTF_8));
356 ResourceOperationStatus operationStatus = new ResourceOperationStatus();
357 operationStatus.setOperationId("4b45d919816ccaa2b762df5120f72067");
358 operationStatus.setResourceTemplateUUID("8ee5926d-720b-4bb2-86f9-d20e921c143b");
359 operationStatus.setServiceId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
361 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
362 nbiRequest.setResponseId("7512eb3feb5249eca5ddd742fedddd39");
363 Optional<ResourceOperationStatus> optional = Optional.of(operationStatus);
365 doAnswer(invocation -> optional).when(repository).findOne(any());
367 createCommonMock(200, nssmf);
369 ResponseEntity res = nssiManagerService.queryJobStatus(nbiRequest, "4b45d919816ccaa2b762df5120f72067");
371 assertNotNull(res.getBody());
372 JobStatusResponse allRes = unMarshal(res.getBody().toString(), JobStatusResponse.class);
373 assertEquals(allRes.getResponseDescriptor().getProgress(), 20);
374 assertEquals(allRes.getResponseDescriptor().getStatus(), "processing");
375 assertEquals(allRes.getResponseDescriptor().getResponseId(), "7512eb3feb5249eca5ddd742fedddd39");
377 System.out.println(res);
382 public void queryNSSISelectionCapability() throws Exception {
384 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
385 ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
387 assertNotNull(res.getBody());
388 Map allRes = unMarshal(res.getBody().toString(), Map.class);
389 assertEquals(allRes.get("selection"), "NSMF");
391 System.out.println(res);
393 nbiRequest.getEsrInfo().setVendor(NssmfAdapterConsts.ONAP_INTERNAL_TAG);
394 res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
396 assertNotNull(res.getBody());
397 allRes = unMarshal(res.getBody().toString(), Map.class);
398 assertEquals(allRes.get("selection"), "NSSMF");
400 System.out.println(res);
402 nbiRequest.getEsrInfo().setNetworkType(NetworkType.ACCESS);
403 res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
405 assertNotNull(res.getBody());
406 allRes = unMarshal(res.getBody().toString(), Map.class);
407 assertEquals(allRes.get("selection"), "NSSMF");
409 System.out.println(res);
412 private NssmfAdapterNBIRequest createNbiRequest() {
413 NssmfAdapterNBIRequest nbiRequest = new NssmfAdapterNBIRequest();
414 EsrInfo esrInfo = new EsrInfo();
415 esrInfo.setVendor("huawei");
416 esrInfo.setNetworkType(CORE);
417 ServiceInfo serviceInfo = new ServiceInfo();
418 serviceInfo.setServiceUuid("8ee5926d-720b-4bb2-86f9-d20e921c143b");
419 serviceInfo.setServiceInvariantUuid("e75698d9-925a-4cdd-a6c0-edacbe6a0b51");
420 serviceInfo.setGlobalSubscriberId("5GCustomer");
421 serviceInfo.setServiceType("5G");
422 serviceInfo.setNsiId("NSI-M-001-HDBNJ-NSMF-01-A-ZX");
423 nbiRequest.setEsrInfo(esrInfo);
424 nbiRequest.setServiceInfo(serviceInfo);
429 public void querySubnetCapability() {
430 NssmfAdapterNBIRequest nbiRequest = createNbiRequest();
432 String subnetCapabilityQuery = "\"subnetTypes\": [\"TN-FH\",\"TN-MH\",\"TN-BH\"]";
433 nbiRequest.setSubnetCapabilityQuery(subnetCapabilityQuery);
434 ResponseEntity res = nssiManagerService.queryNSSISelectionCapability(nbiRequest);
436 assertNotNull(res.getBody());