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 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;
34 import java.util.concurrent.CountDownLatch;
35 import java.util.concurrent.ExecutionException;
36 import java.util.concurrent.Executors;
38 import static org.mockito.ArgumentMatchers.any;
40 public class ServicehandlerImplTest extends AbstractTest {
43 private PathComputationService pathComputationService;
46 private RendererServiceOperations rendererServiceOperations;
49 private NotificationPublishService notificationPublishService;
52 private PceListenerImpl pceListenerImpl;
55 private RendererListenerImpl rendererListenerImpl;
57 private ListeningExecutorService executorService;
58 private CountDownLatch endSignal;
59 private static final int NUM_THREADS = 5;
60 private boolean callbackRan;
64 executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(NUM_THREADS));
65 endSignal = new CountDownLatch(1);
67 MockitoAnnotations.initMocks(this);
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() {
79 endSignal.countDown();
85 RpcResult<ServiceCreateOutput> rpcResult = result.get();
86 Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
100 endSignal.countDown();
106 RpcResult<ServiceCreateOutput> rpcResult = result.get();
107 Assert.assertEquals(ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
120 endSignal.countDown();
126 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
127 Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
140 endSignal.countDown();
146 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
147 Assert.assertEquals(ResponseCodes.RESPONSE_FAILED, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());
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() {
165 endSignal.countDown();
171 RpcResult<ServiceDeleteOutput> rpcResult = result.get();
172 Assert.assertEquals(ResponseCodes.RESPONSE_OK, rpcResult.getResult().getConfigurationResponseCommon().getResponseCode());