UT for ServicehandlerImpl update
[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 static org.mockito.ArgumentMatchers.any;
11
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.ListeningExecutorService;
15 import com.google.common.util.concurrent.MoreExecutors;
16
17 import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Executors;
20
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
29 import org.opendaylight.transportpce.common.ResponseCodes;
30 import org.opendaylight.transportpce.pce.service.PathComputationService;
31 import org.opendaylight.transportpce.renderer.provisiondevice.RendererServiceOperations;
32 import org.opendaylight.transportpce.servicehandler.listeners.PceListenerImpl;
33 import org.opendaylight.transportpce.servicehandler.listeners.RendererListenerImpl;
34 import org.opendaylight.transportpce.servicehandler.service.ServiceDataStoreOperationsImpl;
35 import org.opendaylight.transportpce.servicehandler.utils.ServiceDataUtils;
36 import org.opendaylight.transportpce.test.AbstractTest;
37 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInput;
38 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateInputBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceCreateOutput;
40 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInput;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteInputBuilder;
42 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceDeleteOutput;
43 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInput;
44 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckInputBuilder;
45 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.ServiceFeasibilityCheckOutput;
46 import org.opendaylight.yang.gen.v1.http.org.openroadm.service.rev161014.service.delete.input.ServiceDeleteReqInfoBuilder;
47 import org.opendaylight.yangtools.yang.common.RpcResult;
48
49 public class ServicehandlerImplTest extends AbstractTest  {
50
51     @Mock
52     private PathComputationService pathComputationService;
53
54     @Mock
55     private RendererServiceOperations rendererServiceOperations;
56
57     @Mock
58     private NotificationPublishService notificationPublishService;
59
60     @Mock
61     private PceListenerImpl pceListenerImpl;
62
63     @Mock
64     private RendererListenerImpl rendererListenerImpl;
65
66     private ListeningExecutorService executorService;
67     private CountDownLatch endSignal;
68     private static final int NUM_THREADS = 5;
69     private boolean callbackRan;
70
71     @Before
72     public void setUp() {
73         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
74         endSignal = new CountDownLatch(1);
75         callbackRan = false;
76         MockitoAnnotations.initMocks(this);
77     }
78
79     @Test
80     public void createServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
81         ServicehandlerImpl servicehandlerImpl =
82             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
83                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
84         ListenableFuture<RpcResult<ServiceCreateOutput>> result =
85             servicehandlerImpl.serviceCreate(new ServiceCreateInputBuilder().build());
86         result.addListener(new Runnable() {
87             @Override
88             public void run() {
89                 callbackRan = true;
90                 endSignal.countDown();
91             }
92         }, executorService);
93
94         endSignal.await();
95
96         RpcResult<ServiceCreateOutput> rpcResult = result.get();
97         Assert.assertEquals(
98             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
99     }
100
101     @Test
102     public void createServiceShouldBeSuccessfulWhenPreformPCESuccessful()
103         throws ExecutionException, InterruptedException {
104         ServiceCreateInput input = ServiceDataUtils.buildServiceCreateInput();
105         Mockito.when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
106         ServicehandlerImpl servicehandlerImpl =
107             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
108                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
109         ListenableFuture<RpcResult<ServiceCreateOutput>> result =  servicehandlerImpl.serviceCreate(input);
110         result.addListener(new Runnable() {
111             @Override
112             public void run() {
113                 callbackRan = true;
114                 endSignal.countDown();
115             }
116         }, executorService);
117
118         endSignal.await();
119
120         RpcResult<ServiceCreateOutput> rpcResult = result.get();
121         Assert.assertEquals(
122             ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
123     }
124
125     @Test
126     public void deleteServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
127         ServicehandlerImpl servicehandlerImpl =
128             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
129                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
130         ListenableFuture<RpcResult<ServiceDeleteOutput>> result =
131              servicehandlerImpl.serviceDelete(new ServiceDeleteInputBuilder()
132                 .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder().setServiceName("").build()).build());
133         result.addListener(new Runnable() {
134             @Override
135             public void run() {
136                 callbackRan = true;
137                 endSignal.countDown();
138             }
139         }, executorService);
140
141         endSignal.await();
142
143         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
144         Assert.assertEquals(
145             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
146     }
147
148     @Test
149     public void deleteServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
150         ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
151         ServicehandlerImpl servicehandlerImpl =
152             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
153                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
154         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
155         result.addListener(new Runnable() {
156             @Override
157             public void run() {
158                 callbackRan = true;
159                 endSignal.countDown();
160             }
161         }, executorService);
162
163         endSignal.await();
164
165         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
166         Assert.assertEquals(
167             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
168     }
169
170     @Test
171     public void deleteServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
172         DataBroker dataBroker = getNewDataBroker();
173         Mockito.when(rendererServiceOperations.serviceDelete(any())).thenReturn(Futures.immediateFuture(any()));
174         ServicehandlerImpl servicehandlerImpl =
175             new ServicehandlerImpl(dataBroker, pathComputationService, rendererServiceOperations,
176                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
177         ServiceDataStoreOperationsImpl serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(dataBroker);
178         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
179         serviceDataStoreOperations.createService(createInput);
180         ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
181         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
182         result.addListener(new Runnable() {
183             @Override
184             public void run() {
185                 callbackRan = true;
186                 endSignal.countDown();
187             }
188         }, executorService);
189
190         endSignal.await();
191
192         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
193         Assert.assertEquals(
194             ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
195     }
196
197
198     @Test
199     public void serviceFeasibilityCheckShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
200         ServicehandlerImpl servicehandlerImpl =
201                 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
202                         notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
203         ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> result =
204                 servicehandlerImpl.serviceFeasibilityCheck(new ServiceFeasibilityCheckInputBuilder().build());
205         result.addListener(new Runnable() {
206             @Override
207             public void run() {
208                 callbackRan = true;
209                 endSignal.countDown();
210             }
211         }, executorService);
212
213         endSignal.await();
214
215         RpcResult<ServiceFeasibilityCheckOutput> rpcResult = result.get();
216         Assert.assertEquals(
217             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
218     }
219
220     @Test
221     public void serviceFeasibilityCheckShouldBeSuccessfulWhenPreformPCESuccessful()
222             throws ExecutionException, InterruptedException {
223         ServiceFeasibilityCheckInput input = ServiceDataUtils.buildServiceFeasibilityCheckInput();
224         Mockito.when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
225         ServicehandlerImpl servicehandlerImpl =
226                 new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
227                         notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
228         ListenableFuture<RpcResult<ServiceFeasibilityCheckOutput>> result =
229             servicehandlerImpl.serviceFeasibilityCheck(input);
230         result.addListener(new Runnable() {
231             @Override
232             public void run() {
233                 callbackRan = true;
234                 endSignal.countDown();
235             }
236         }, executorService);
237
238         endSignal.await();
239
240         RpcResult<ServiceFeasibilityCheckOutput> rpcResult = result.get();
241         Assert.assertEquals(
242                 ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
243     }
244
245
246
247 }