Do not use RpcService in TPCE olm module
[transportpce.git] / olm / src / test / java / org / opendaylight / transportpce / olm / OlmPowerServiceRpcImplTest.java
1 /*
2  * Copyright © 2018 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
9 package org.opendaylight.transportpce.olm;
10
11
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.jupiter.api.Assertions.assertEquals;
16 import static org.mockito.ArgumentMatchers.any;
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.junit.jupiter.MockitoExtension;
26 import org.opendaylight.mdsal.binding.api.RpcProviderService;
27 import org.opendaylight.transportpce.olm.service.OlmPowerService;
28 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
29 import org.opendaylight.transportpce.test.AbstractTest;
30 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseInput;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseOutputBuilder;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentInput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentInputBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentOutputBuilder;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInputBuilder;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutputBuilder;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetInput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetOutputBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
42 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutput;
43 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutputBuilder;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutputBuilder;
46 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.TransportpceOlmService;
47 import org.opendaylight.yang.gen.v1.http.org.openroadm.resource.types.rev161014.ResourceTypeEnum;
48 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.PmGranularity;
49 import org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.olm.get.pm.input.ResourceIdentifierBuilder;
50 import org.opendaylight.yangtools.yang.common.ErrorSeverity;
51 import org.opendaylight.yangtools.yang.common.ErrorTag;
52 import org.opendaylight.yangtools.yang.common.ErrorType;
53 import org.opendaylight.yangtools.yang.common.RpcResult;
54
55 @ExtendWith(MockitoExtension.class)
56 class OlmPowerServiceRpcImplTest extends AbstractTest {
57
58     @Mock
59     private OlmPowerService olmPowerService;
60     @Mock
61     private RpcProviderService rpcProviderService;
62     private TransportpceOlmService olmPowerServiceRpc;
63
64     @BeforeEach
65     public void setUp() {
66         this.olmPowerServiceRpc = new OlmPowerServiceRpcImpl(this.olmPowerService, rpcProviderService);
67     }
68
69     @Test
70     void testGetPmFailWithNodeIdNull() throws InterruptedException, ExecutionException {
71         GetPmInput input = new GetPmInputBuilder()
72             .setGranularity(PmGranularity._15min)
73             .setResourceIdentifier(new ResourceIdentifierBuilder()
74                     .setResourceName("ots-deg1").build())
75             .setResourceType(ResourceTypeEnum.Interface)
76             .build();
77         ListenableFuture<RpcResult<GetPmOutput>> output = this.olmPowerServiceRpc.getPm(input);
78         assertFalse(output.get().isSuccessful());
79         assertNull(output.get().getResult());
80         assertEquals(ErrorType.RPC, output.get().getErrors().get(0).getErrorType());
81         assertEquals("Error with input parameters", output.get().getErrors().get(0).getMessage());
82         assertEquals(ErrorSeverity.ERROR, output.get().getErrors().get(0).getSeverity());
83         assertEquals(ErrorTag.OPERATION_FAILED, output.get().getErrors().get(0).getTag());
84     }
85
86     @Test
87     void testGetPmWithSuccess() throws InterruptedException, ExecutionException {
88         GetPmInput input = new GetPmInputBuilder()
89             .setNodeId("nodeId")
90             .build();
91         when(this.olmPowerService.getPm(any())).thenReturn(new GetPmOutputBuilder().build());
92         ListenableFuture<RpcResult<GetPmOutput>> output = this.olmPowerServiceRpc.getPm(input);
93         assertTrue(output.get().isSuccessful());
94         assertEquals(new GetPmOutputBuilder().build(), output.get().getResult());
95     }
96
97     @Test
98     void testServicePowerSetup() throws InterruptedException, ExecutionException {
99         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
100         when(this.olmPowerService.servicePowerSetup(any())).thenReturn(new ServicePowerSetupOutputBuilder().build());
101         ListenableFuture<RpcResult<ServicePowerSetupOutput>> output = this.olmPowerServiceRpc.servicePowerSetup(input);
102         assertTrue(output.get().isSuccessful());
103         assertEquals(new ServicePowerSetupOutputBuilder().build(), output.get().getResult());
104     }
105
106     @Test
107     void testServicePowerTurndown() throws InterruptedException, ExecutionException {
108         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
109         when(this.olmPowerService.servicePowerTurndown(any()))
110             .thenReturn(new ServicePowerTurndownOutputBuilder().build());
111         var output = this.olmPowerServiceRpc.servicePowerTurndown(input);
112         assertTrue(output.get().isSuccessful());
113         assertEquals(new ServicePowerTurndownOutputBuilder().build(), output.get().getResult());
114     }
115
116     @Test
117     void testCalculateSpanlossBase() throws InterruptedException, ExecutionException {
118         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInputAll();
119         when(this.olmPowerService.calculateSpanlossBase(any()))
120             .thenReturn(new CalculateSpanlossBaseOutputBuilder().build());
121         var output = this.olmPowerServiceRpc.calculateSpanlossBase(input);
122         assertTrue(output.get().isSuccessful());
123         assertEquals(new CalculateSpanlossBaseOutputBuilder().build(), output.get().getResult());
124     }
125
126     @Test
127     void testCalculateSpanlossCurrent() throws InterruptedException, ExecutionException {
128         CalculateSpanlossCurrentInput input = new CalculateSpanlossCurrentInputBuilder().build();
129         when(this.olmPowerService.calculateSpanlossCurrent(any()))
130             .thenReturn(new CalculateSpanlossCurrentOutputBuilder().build());
131         var output = this.olmPowerServiceRpc.calculateSpanlossCurrent(input);
132         assertTrue(output.get().isSuccessful());
133         assertEquals(new CalculateSpanlossCurrentOutputBuilder().build(), output.get().getResult());
134     }
135
136     @Test
137     void testServicePowerReset() throws InterruptedException, ExecutionException {
138         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
139         when(this.olmPowerService.servicePowerReset(any()))
140             .thenReturn(new ServicePowerResetOutputBuilder().build());
141         var output = this.olmPowerServiceRpc.servicePowerReset(input);
142         assertTrue(output.get().isSuccessful());
143         assertEquals(new ServicePowerResetOutputBuilder().build(), output.get().getResult());
144     }
145 }