UT for ServicehandlerImpl
[transportpce.git] / servicehandler / src / test / java / org / opendaylight / transportpce / servicehandler / impl / ServicehandlerImplTest.java
1 /*
2  * Copyright © 2019 Orange, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.transportpce.servicehandler.impl;
9
10 import com.google.common.util.concurrent.Futures;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import com.google.common.util.concurrent.ListeningExecutorService;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.MockitoAnnotations;
20 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
21 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
22 import org.opendaylight.transportpce.common.ResponseCodes;
23 import org.opendaylight.transportpce.pce.service.PathComputationService;
24 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
25 import org.opendaylight.transportpce.servicehandler.listeners.PceListenerImpl;
26 import org.opendaylight.transportpce.servicehandler.listeners.RendererListenerImpl;
27 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl;
28 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
29 import org.opendaylight.transportpce.test.AbstractTest;
30 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.*;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfoBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.Executors;
37
38 import static org.mockito.ArgumentMatchers.any;
39
40 public class ServicehandlerImplTest extends AbstractTest  {
41
42     @Mock
43     private PathComputationService pathComputationService;
44
45     @Mock
46     private RendererServiceOperations rendererServiceOperations;
47
48     @Mock
49     private NotificationPublishService notificationPublishService;
50
51     @Mock
52     private PceListenerImpl pceListenerImpl;
53
54     @Mock
55     private RendererListenerImpl rendererListenerImpl;
56
57     private ListeningExecutorService executorService;
58     private CountDownLatch endSignal;
59     private static final int NUM_THREADS = 5;
60     private boolean callbackRan;
61
62     @Before
63     public void setUp() {
64         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
65         endSignal = new CountDownLatch(1);
66         callbackRan = false;
67         MockitoAnnotations.initMocks(this);
68     }
69
70     @Test
71     public void createServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
72         ServicehandlerImpl servicehandlerImpl = new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
73                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
74         ListenableFuture<RpcResult<ServiceCreateOutput>> result =  servicehandlerImpl.serviceCreate(new ServiceCreateInputBuilder().build());
75         result.addListener(new Runnable() {
76             @Override
77             public void run() {
78                 callbackRan = true;
79                 endSignal.countDown();
80             }
81         }, executorService);
82
83         endSignal.await();
84
85         RpcResult<ServiceCreateOutput> rpcResult = result.get();
86         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
87     }
88
89     @Test
90     public void createServiceShouldBeSuccessfulWhenPreformPCESuccessful() throws ExecutionException, InterruptedException {
91         ServiceCreateInput input = ServiceDataUtils.buildServiceCreateInput();
92         Mockito.when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
93         ServicehandlerImpl servicehandlerImpl = new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
94                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
95         ListenableFuture<RpcResult<ServiceCreateOutput>> result =  servicehandlerImpl.serviceCreate(input);
96         result.addListener(new Runnable() {
97             @Override
98             public void run() {
99                 callbackRan = true;
100                 endSignal.countDown();
101             }
102         }, executorService);
103
104         endSignal.await();
105
106         RpcResult<ServiceCreateOutput> rpcResult = result.get();
107         Assert.assertEquals(ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
108     }
109
110     @Test
111     public void deleteServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
112         ServicehandlerImpl servicehandlerImpl = new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
113                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
114         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(new ServiceDeleteInputBuilder()
115                 .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder().setServiceName("").build()).build());
116         result.addListener(new Runnable() {
117             @Override
118             public void run() {
119                 callbackRan = true;
120                 endSignal.countDown();
121             }
122         }, executorService);
123
124         endSignal.await();
125
126         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
127         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
128     }
129
130     @Test
131     public void deleteServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
132         ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
133         ServicehandlerImpl servicehandlerImpl = new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
134                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
135         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
136         result.addListener(new Runnable() {
137             @Override
138             public void run() {
139                 callbackRan = true;
140                 endSignal.countDown();
141             }
142         }, executorService);
143
144         endSignal.await();
145
146         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
147         Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
148     }
149
150     @Test
151     public void deleteServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
152         DataBroker dataBroker = getNewDataBroker();
153         Mockito.when(rendererServiceOperations.serviceDelete(any())).thenReturn(Futures.immediateFuture(any()));
154         ServicehandlerImpl servicehandlerImpl = new ServicehandlerImpl(dataBroker, pathComputationService, rendererServiceOperations,
155                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
156         ServiceDataStoreOperationsImpl serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(dataBroker);
157         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
158         serviceDataStoreOperations.createService(createInput);
159         ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
160         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
161         result.addListener(new Runnable() {
162             @Override
163             public void run() {
164                 callbackRan = true;
165                 endSignal.countDown();
166             }
167         }, executorService);
168
169         endSignal.await();
170
171         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
172         Assert.assertEquals(ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
173     }
174 }