2 * Copyright (C) 2018 CMCC, Inc. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.usecaseui.server.service.lcm.impl;
18 import static org.mockito.ArgumentMatchers.eq;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.when;
21 import static org.onap.usecaseui.server.util.CallStub.failedCall;
22 import static org.onap.usecaseui.server.util.CallStub.successfulCall;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.List;
28 import jakarta.servlet.ReadListener;
29 import jakarta.servlet.ServletInputStream;
30 import jakarta.servlet.http.HttpServletRequest;
32 import okhttp3.MediaType;
34 import okio.BufferedSource;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 import org.junit.Assert;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.Mockito;
41 import org.onap.usecaseui.server.bean.sotn.Pinterface;
42 import org.onap.usecaseui.server.bean.sotn.PinterfaceRsp;
43 import org.onap.usecaseui.server.service.lcm.domain.aai.AAIService;
44 import org.onap.usecaseui.server.service.lcm.domain.aai.exceptions.AAIException;
45 import org.onap.usecaseui.server.service.lcm.domain.so.exceptions.SOException;
46 import org.onap.usecaseui.server.service.sotn.impl.SOTNServiceImpl;
48 import okhttp3.ResponseBody;
50 public class SOTNServiceImplTest {
52 SOTNServiceImpl dsts = null;
53 AAIService aaiService = null;
57 public void before() throws Exception {
58 aaiService= mock(AAIService.class);
59 dsts = new SOTNServiceImpl(aaiService);
60 result= new ResponseBody() {
63 public MediaType contentType() {
64 return MediaType.parse("application/json; charset=utf-8");
68 public long contentLength() {
74 public BufferedSource source() {
81 private HttpServletRequest mockRequest() throws IOException {
82 HttpServletRequest request = mock(HttpServletRequest.class);
83 when(request.getContentLength()).thenReturn(0);
84 ServletInputStream inStream = new ServletInputStream() {
86 public boolean isFinished() {
91 public boolean isReady() {
96 public void setReadListener(ReadListener readListener) {
101 public int read() throws IOException {
105 when(request.getInputStream()).thenReturn(inStream);
110 public void itCanGetNetWorkResources(){
111 when(aaiService.listNetWorkResources()).thenReturn(successfulCall(result));
112 dsts.getNetWorkResources();
116 public void getNetWorkResourcesWithThrowsEexception(){
117 when(aaiService.listNetWorkResources()).thenReturn(failedCall("aai is not exist!"));
118 dsts.getNetWorkResources();
121 @SuppressWarnings("unchecked")
123 public void itCanGetPinterfaceByPnfName(){
124 String pnfName="test";
125 PinterfaceRsp rsp = new PinterfaceRsp();
126 List pinterfaces = new ArrayList();
127 Pinterface pinterface = new Pinterface("interfaceName");
128 pinterfaces.add(pinterface);
129 rsp.setPinterfaces(pinterfaces);
130 when(aaiService.getPinterfaceByPnfName(pnfName)).thenReturn(successfulCall(rsp));
131 dsts.getPinterfaceByPnfName(pnfName);
134 @Test(expected = AAIException.class)
135 public void getPinterfaceByPnfNameWithThrowsEexception(){
136 String pnfName="test";
137 when(aaiService.getPinterfaceByPnfName(pnfName)).thenReturn(failedCall("aai is not exist!"));
138 dsts.getPinterfaceByPnfName(pnfName);
142 public void itCanGetLogicalLinks(){
143 when(aaiService.getLogicalLinks()).thenReturn(successfulCall(result));
144 dsts.getLogicalLinks();
148 public void getLogicalLinksWithThrowsEexception(){
149 when(aaiService.getLogicalLinks()).thenReturn(failedCall("aai is not exist!"));
150 dsts.getLogicalLinks();
154 public void itCanGetSpecificLogicalLink(){
155 String linkName="linkName";
156 when(aaiService.getSpecificLogicalLink(linkName)).thenReturn(successfulCall(result));
157 dsts.getSpecificLogicalLink(linkName);
161 public void getSpecificLogicalLinkWithThrowsEexception(){
162 String linkName="linkName";
163 when(aaiService.getSpecificLogicalLink(linkName)).thenReturn(failedCall("aai is not exist!"));
164 dsts.getSpecificLogicalLink(linkName);
168 public void itCanGetHostUrl(){
169 String linkName="linkName";
170 when(aaiService.getHostUrl(linkName)).thenReturn(successfulCall(result));
171 dsts.getHostUrl(linkName);
175 public void getHostUrlWithThrowsEexception(){
176 String linkName="linkName";
177 when(aaiService.getHostUrl(linkName)).thenReturn(failedCall("aai is not exist!"));
178 dsts.getHostUrl(linkName);
182 public void itCanGetExtAaiId(){
183 String linkName="linkName";
184 when(aaiService.getExtAaiId(linkName)).thenReturn(successfulCall(result));
185 dsts.getExtAaiId(linkName);
189 public void getExtAaiIdWithThrowsEexception(){
190 String linkName="linkName";
191 when(aaiService.getExtAaiId(linkName)).thenReturn(failedCall("aai is not exist!"));
192 dsts.getExtAaiId(linkName);
196 public void itCanCreateHostUrl() throws IOException{
197 String linkName="linkName";
198 HttpServletRequest request = mockRequest();
199 when(aaiService.createHostUrl(Mockito.any(),eq(linkName))).thenReturn(successfulCall(result));
200 Assert.assertSame("{\"status\":\"SUCCESS\"}",dsts.createHostUrl(request,linkName));
204 public void createHostUrlWithThrowsEexception() throws IOException{
205 String linkName="linkName";
206 HttpServletRequest request = mockRequest();
207 when(aaiService.createHostUrl(Mockito.any(),eq(linkName))).thenReturn(failedCall("aai is not exist!"));
208 Assert.assertSame("{\"status\":\"FAILED\"}",dsts.createHostUrl(request,linkName));
212 public void itCanCreateTopoNetwork() throws IOException{
213 String linkName="linkName";
214 HttpServletRequest request = mockRequest();
215 when(aaiService.createTopoNetwork(Mockito.any(),eq(linkName))).thenReturn(successfulCall(result));
216 Assert.assertSame("{\"status\":\"SUCCESS\"}",dsts.createTopoNetwork(request,linkName));
220 public void createTopoNetworkWithThrowsEexception() throws IOException{
221 String linkName="linkName";
222 HttpServletRequest request = mockRequest();
223 when(aaiService.createTopoNetwork(Mockito.any(),eq(linkName))).thenReturn(failedCall("aai is not exist!"));
224 Assert.assertSame("{\"status\":\"FAILED\"}",dsts.createTopoNetwork(request,linkName));
228 public void itCanCreateTerminationPoint() throws IOException{
229 String linkName="linkName";
231 HttpServletRequest request = mockRequest();
232 when(aaiService.createTerminationPoint(Mockito.any(),eq(linkName),eq(linkName))).thenReturn(successfulCall(result));
233 Assert.assertSame("{\"status\":\"FAILED\"}",dsts.createTerminationPoint(request,linkName,tpid));
237 public void createTerminationPointWithThrowsEexception() throws IOException{
238 String linkName="linkName";
240 HttpServletRequest request = mockRequest();
241 when(aaiService.createTerminationPoint(Mockito.any(),eq(linkName),eq(linkName))).thenReturn(failedCall("aai is not exist!"));
242 Assert.assertSame("{\"status\":\"FAILED\"}",dsts.createTerminationPoint(request,linkName,tpid));
246 public void itCanCreateLink() throws IOException{
247 String linkName="linkName";
248 HttpServletRequest request = mockRequest();
249 when(aaiService.createLink(Mockito.any(),eq(linkName))).thenReturn(successfulCall(result));
250 dsts.createLink(request,linkName);
254 public void createLinkWithThrowsEexception() throws IOException{
255 String linkName="linkName";
256 HttpServletRequest request = mockRequest();
257 when(aaiService.createLink(Mockito.any(),eq(linkName))).thenReturn(failedCall("aai is not exist!"));
258 dsts.createLink(request,linkName);
262 public void itCanCreatePnf() throws IOException{
263 String linkName="linkName";
264 HttpServletRequest request = mockRequest();
265 when(aaiService.createPnf(Mockito.any(),eq(linkName))).thenReturn(successfulCall(result));
266 dsts.createPnf(request,linkName);
270 public void createPnfWithThrowsEexception() throws IOException{
271 String linkName="linkName";
272 HttpServletRequest request = mockRequest();
273 when(aaiService.createPnf(Mockito.any(),eq(linkName))).thenReturn(failedCall("aai is not exist!"));
274 dsts.createPnf(request,linkName);
278 public void itCanDeleteLink() throws IOException{
279 String linkName="linkName";
280 String resourceVersion="resourceVersion";
281 when(aaiService.deleteLink(linkName,resourceVersion)).thenReturn(successfulCall(result));
282 dsts.deleteLink(linkName,resourceVersion);
286 public void deleteLinkWithThrowsEexception() throws IOException{
287 String linkName="linkName";
288 String resourceVersion="resourceVersion";
289 when(aaiService.deleteLink(linkName,resourceVersion)).thenReturn(failedCall("aai is not exist!"));
290 dsts.deleteLink(linkName,resourceVersion);
294 public void itCanGetServiceInstances() throws IOException{
295 String linkName="linkName";
296 String resourceVersion="resourceVersion";
297 when(aaiService.getServiceInstances(linkName,resourceVersion)).thenReturn(successfulCall(result));
298 dsts.getServiceInstances(linkName,resourceVersion);
302 public void getServiceInstancesWithThrowsEexception() throws IOException{
303 String linkName="linkName";
304 String resourceVersion="resourceVersion";
305 when(aaiService.getServiceInstances(linkName,resourceVersion)).thenReturn(failedCall("aai is not exist!"));
306 dsts.getServiceInstances(linkName,resourceVersion);
310 public void itCanGerviceInstanceInfo() throws IOException{
311 String linkName="linkName";
312 String resourceVersion="resourceVersion";
313 String serviceId="serviceId";
314 when(aaiService.serviceInstaneInfo(linkName,resourceVersion,serviceId)).thenReturn(successfulCall(result));
315 dsts.serviceInstanceInfo(linkName,resourceVersion,serviceId);
318 @Test(expected = SOException.class)
319 public void serviceInstanceInfoWithThrowsEexception() throws IOException{
320 String linkName="linkName";
321 String resourceVersion="resourceVersion";
322 String serviceId="serviceId";
323 when(aaiService.serviceInstaneInfo(linkName,resourceVersion,serviceId)).thenReturn(failedCall("aai is not exist!"));
324 dsts.serviceInstanceInfo(linkName,resourceVersion,serviceId);
328 public void itCanGetPnfInfo() throws IOException{
329 String linkName="linkName";
330 when(aaiService.getPnfInfo(linkName)).thenReturn(successfulCall(result));
331 dsts.getPnfInfo(linkName);
335 public void getPnfInfoWithThrowsEexception() throws IOException{
336 String linkName="linkName";
337 when(aaiService.getPnfInfo(linkName)).thenReturn(failedCall("aai is not exist!"));
338 dsts.getPnfInfo(linkName);
342 public void itCangetAllottedResources() throws IOException{
343 String linkName="linkName";
344 String resourceVersion="resourceVersion";
345 String serviceId="serviceId";
346 when(aaiService.getAllottedResources(linkName,resourceVersion,serviceId)).thenReturn(successfulCall(result));
347 dsts.getAllottedResources(linkName,resourceVersion,serviceId);
351 public void getAllottedResourcesWithThrowsEexception() throws IOException{
352 String linkName="linkName";
353 String resourceVersion="resourceVersion";
354 String serviceId="serviceId";
355 when(aaiService.getAllottedResources(linkName,resourceVersion,serviceId)).thenReturn(failedCall("aai is not exist!"));
356 dsts.getAllottedResources(linkName,resourceVersion,serviceId);
360 public void itCangetConnectivityInfo() throws IOException{
361 String linkName="linkName";
362 when(aaiService.getConnectivityInfo(linkName)).thenReturn(successfulCall(result));
363 dsts.getConnectivityInfo(linkName);
367 public void getConnectivityInfoWithThrowsEexception() throws IOException{
368 String linkName="linkName";
369 when(aaiService.getConnectivityInfo(linkName)).thenReturn(failedCall("aai is not exist!"));
370 dsts.getConnectivityInfo(linkName);
374 public void itCangetPinterfaceByVpnId() throws IOException{
375 String linkName="linkName";
376 when(aaiService.getPinterfaceByVpnId(linkName)).thenReturn(successfulCall(result));
377 dsts.getPinterfaceByVpnId(linkName);
381 public void getPinterfaceByVpnIdWithThrowsEexception() throws IOException{
382 String linkName="linkName";
383 when(aaiService.getPinterfaceByVpnId(linkName)).thenReturn(failedCall("aai is not exist!"));
384 dsts.getPinterfaceByVpnId(linkName);
388 public void itCandeleteExtNetwork() throws IOException{
389 ResponseBody result=null;
390 String linkName="linkName";
391 String resourceVersion="resourceVersion";
392 when(aaiService.deleteExtNetwork(linkName,resourceVersion)).thenReturn(successfulCall(result));
393 dsts.deleteExtNetwork(linkName,resourceVersion);
397 public void deleteExtNetworkWithThrowsEexception() throws IOException{
398 String linkName="linkName";
399 String resourceVersion="resourceVersion";
400 when(aaiService.deleteExtNetwork(linkName,resourceVersion)).thenReturn(failedCall("aai is not exist!"));
401 dsts.deleteExtNetwork(linkName,resourceVersion);