fix ServicehandlerImpl Junit warnings
[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.service.delete.input.ServiceDeleteReqInfoBuilder;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45
46 public class ServicehandlerImplTest extends AbstractTest  {
47
48     @Mock
49     private PathComputationService pathComputationService;
50
51     @Mock
52     private RendererServiceOperations rendererServiceOperations;
53
54     @Mock
55     private NotificationPublishService notificationPublishService;
56
57     @Mock
58     private PceListenerImpl pceListenerImpl;
59
60     @Mock
61     private RendererListenerImpl rendererListenerImpl;
62
63     private ListeningExecutorService executorService;
64     private CountDownLatch endSignal;
65     private static final int NUM_THREADS = 5;
66     private boolean callbackRan;
67
68     @Before
69     public void setUp() {
70         executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
71         endSignal = new CountDownLatch(1);
72         callbackRan = false;
73         MockitoAnnotations.initMocks(this);
74     }
75
76     @Test
77     public void createServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
78         ServicehandlerImpl servicehandlerImpl =
79             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
80                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
81         ListenableFuture<RpcResult<ServiceCreateOutput>> result =
82             servicehandlerImpl.serviceCreate(new ServiceCreateInputBuilder().build());
83         result.addListener(new Runnable() {
84             @Override
85             public void run() {
86                 callbackRan = true;
87                 endSignal.countDown();
88             }
89         }, executorService);
90
91         endSignal.await();
92
93         RpcResult<ServiceCreateOutput> rpcResult = result.get();
94         Assert.assertEquals(
95             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
96     }
97
98     @Test
99     public void createServiceShouldBeSuccessfulWhenPreformPCESuccessful()
100         throws ExecutionException, InterruptedException {
101         ServiceCreateInput input = ServiceDataUtils.buildServiceCreateInput();
102         Mockito.when(pathComputationService.pathComputationRequest(any())).thenReturn(Futures.immediateFuture(any()));
103         ServicehandlerImpl servicehandlerImpl =
104             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
105                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
106         ListenableFuture<RpcResult<ServiceCreateOutput>> result =  servicehandlerImpl.serviceCreate(input);
107         result.addListener(new Runnable() {
108             @Override
109             public void run() {
110                 callbackRan = true;
111                 endSignal.countDown();
112             }
113         }, executorService);
114
115         endSignal.await();
116
117         RpcResult<ServiceCreateOutput> rpcResult = result.get();
118         Assert.assertEquals(
119             ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
120     }
121
122     @Test
123     public void deleteServiceShouldBeFailedWithEmptyInput() throws ExecutionException, InterruptedException {
124         ServicehandlerImpl servicehandlerImpl =
125             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
126                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
127         ListenableFuture<RpcResult<ServiceDeleteOutput>> result =
128              servicehandlerImpl.serviceDelete(new ServiceDeleteInputBuilder()
129                 .setServiceDeleteReqInfo(new ServiceDeleteReqInfoBuilder().setServiceName("").build()).build());
130         result.addListener(new Runnable() {
131             @Override
132             public void run() {
133                 callbackRan = true;
134                 endSignal.countDown();
135             }
136         }, executorService);
137
138         endSignal.await();
139
140         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
141         Assert.assertEquals(
142             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
143     }
144
145     @Test
146     public void deleteServiceShouldBeFailedWithNonExistService() throws ExecutionException, InterruptedException {
147         ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
148         ServicehandlerImpl servicehandlerImpl =
149             new ServicehandlerImpl(getNewDataBroker(), pathComputationService, rendererServiceOperations,
150                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
151         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
152         result.addListener(new Runnable() {
153             @Override
154             public void run() {
155                 callbackRan = true;
156                 endSignal.countDown();
157             }
158         }, executorService);
159
160         endSignal.await();
161
162         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
163         Assert.assertEquals(
164             ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
165     }
166
167     @Test
168     public void deleteServiceShouldBeSuccessForExistingService() throws ExecutionException, InterruptedException {
169         DataBroker dataBroker = getNewDataBroker();
170         Mockito.when(rendererServiceOperations.serviceDelete(any())).thenReturn(Futures.immediateFuture(any()));
171         ServicehandlerImpl servicehandlerImpl =
172             new ServicehandlerImpl(dataBroker, pathComputationService, rendererServiceOperations,
173                 notificationPublishService, pceListenerImpl, rendererListenerImpl, null);
174         ServiceDataStoreOperationsImpl serviceDataStoreOperations = new ServiceDataStoreOperationsImpl(dataBroker);
175         ServiceCreateInput createInput = ServiceDataUtils.buildServiceCreateInput();
176         serviceDataStoreOperations.createService(createInput);
177         ServiceDeleteInput input = ServiceDataUtils.buildServiceDeleteInput();
178         ListenableFuture<RpcResult<ServiceDeleteOutput>> result = servicehandlerImpl.serviceDelete(input);
179         result.addListener(new Runnable() {
180             @Override
181             public void run() {
182                 callbackRan = true;
183                 endSignal.countDown();
184             }
185         }, executorService);
186
187         endSignal.await();
188
189         RpcResult<ServiceDeleteOutput> rpcResult = result.get();
190         Assert.assertEquals(
191             ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
192     }
193 }