2 * Copyright © 2019 Orange, Inc. and others. All rights reserved.
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
8 package org.opendaylight.transportpce.servicehandler.impl;
10 import static org.mockito.ArgumentMatchers.any;
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;
17 import java.util.concurrent.CountDownLatch;
18 import java.util.concurrent.ExecutionException;
19 import java.util.concurrent.Executors;
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;
46 public class ServicehandlerImplTest extends AbstractTest {
49 private PathComputationService pathComputationService;
52 private RendererServiceOperations rendererServiceOperations;
55 private NotificationPublishService notificationPublishService;
58 private PceListenerImpl pceListenerImpl;
61 private RendererListenerImpl rendererListenerImpl;
63 private ListeningExecutorService executorService;
64 private CountDownLatch endSignal;
65 private static final int NUM_THREADS = 5;
66 private boolean callbackRan;
70 executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
71 endSignal = new CountDownLatch(1);
73 MockitoAnnotations.initMocks(this);
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() {
87 endSignal.countDown();
93 RpcResult<ServiceCreateOutput> rpcResult = result.get();
95 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
111 endSignal.countDown();
117 RpcResult<ServiceCreateOutput> rpcResult = result.get();
119 ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
134 endSignal.countDown();
140 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
142 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
156 endSignal.countDown();
162 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
164 ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
183 endSignal.countDown();
189 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
191 ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());