80a81d00c5cc5532637b380a5599630ef93d5667
[transportpce.git] / renderer / src / test / java / org / opendaylight / transportpce / renderer / rpcs / DeviceRendererRPCImplTest.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.renderer.rpcs;
9
10 import static org.junit.Assert.assertFalse;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.jupiter.api.Assertions.assertEquals;
13 import static org.junit.jupiter.api.Assertions.assertNull;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.util.concurrent.ExecutionException;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.junit.jupiter.api.extension.ExtendWith;
24 import org.mockito.Mock;
25 import org.mockito.Spy;
26 import org.mockito.junit.jupiter.MockitoExtension;
27 import org.opendaylight.mdsal.binding.api.RpcProviderService;
28 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaceException;
29 import org.opendaylight.transportpce.renderer.provisiondevice.DeviceRendererService;
30 import org.opendaylight.transportpce.renderer.provisiondevice.OtnDeviceRendererService;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.Action;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.CreateOtsOms;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.CreateOtsOmsInput;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.CreateOtsOmsOutput;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollback;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackInput;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.RendererRollbackOutputBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePath;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.device.renderer.rev211004.ServicePathInput;
40 import org.opendaylight.yangtools.yang.common.RpcResult;
41
42 @ExtendWith(MockitoExtension.class)
43 public class DeviceRendererRPCImplTest {
44     @Mock
45     private DeviceRendererService deviceRendererService;
46     @Mock
47     private RpcProviderService rpcProviderService;
48     @Mock
49     private DeviceRendererService deviceRenderer;
50     @Mock
51     private OtnDeviceRendererService otnDeviceRendererService;
52     @Spy
53     private ServicePathInput servicePathInput;
54     @Mock
55     private CreateOtsOmsInput createOtsOmsInput;
56     @Mock
57     private RendererRollbackInput rendererRollbackInput;
58     private ServicePath servicePath;
59     private RendererRollback rendererRollback;
60     private CreateOtsOms createOtsOms;
61
62     @BeforeEach
63     void setup() {
64         servicePath = new ServicePathImpl(deviceRenderer);
65         rendererRollback = new RendererRollbackImpl(deviceRenderer);
66         createOtsOms = new CreateOtsOmsImpl(deviceRenderer);
67     }
68
69     @Test
70     void testRpcRegistration() {
71         new DeviceRendererRPCImpl(rpcProviderService, deviceRenderer, otnDeviceRendererService);
72         verify(rpcProviderService, times(1)).registerRpcImplementations(
73                 any(ServicePathImpl.class), any(OtnServicePathImpl.class), any(RendererRollbackImpl.class),
74                 any(CreateOtsOmsImpl.class));
75     }
76
77     @Test
78     void testServicePathCreateOption() {
79         when(servicePathInput.getOperation()).thenReturn(Action.Create);
80         servicePath.invoke(servicePathInput);
81         verify(deviceRenderer, times(1)).setupServicePath(servicePathInput, null);
82     }
83
84     @Test
85     void testServicePathDeleteOption() {
86         when(servicePathInput.getOperation()).thenReturn(Action.Delete);
87         servicePath.invoke(servicePathInput);
88         verify(deviceRenderer, times(1)).deleteServicePath(servicePathInput);
89     }
90
91     @Test
92     void testRendererRollback() {
93         when(deviceRenderer.rendererRollback(rendererRollbackInput))
94             .thenReturn(new RendererRollbackOutputBuilder().build());
95         rendererRollback.invoke(rendererRollbackInput);
96         verify(deviceRenderer, times(1)).rendererRollback(rendererRollbackInput);
97     }
98
99     @Test
100     void testCreateOtsOms() throws OpenRoadmInterfaceException {
101         when(createOtsOmsInput.getNodeId()).thenReturn("nodeId");
102         when(createOtsOmsInput.getLogicalConnectionPoint()).thenReturn("logicalConnectionPoint");
103         when(deviceRenderer.createOtsOms(createOtsOmsInput)).thenReturn(null);
104         createOtsOms.invoke(createOtsOmsInput);
105         verify(deviceRenderer, times(1)).createOtsOms(createOtsOmsInput);
106     }
107
108     @Test
109     void testCreateOtsOmsReturnException()
110             throws OpenRoadmInterfaceException, InterruptedException, ExecutionException {
111         when(createOtsOmsInput.getNodeId()).thenReturn("nodeId");
112         when(createOtsOmsInput.getLogicalConnectionPoint()).thenReturn("logicalConnectionPoint");
113         when(deviceRenderer.createOtsOms(createOtsOmsInput)).thenThrow(OpenRoadmInterfaceException.class);
114         ListenableFuture<RpcResult<CreateOtsOmsOutput>> result = createOtsOms.invoke(createOtsOmsInput);
115         assertTrue(result.isDone());
116         assertFalse(result.get().isSuccessful());
117         assertNull(result.get().getResult());
118         assertEquals("to create oms and ots interfaces", result.get().getErrors().get(0).getMessage());
119     }
120 }