Migrate olm module to JUnit5
[transportpce.git] / olm / src / test / java / org / opendaylight / transportpce / olm / service / OlmPowerServiceImplTest.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.service;
10
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.anyLong;
14 import static org.mockito.ArgumentMatchers.anyString;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import java.util.Optional;
19 import org.junit.jupiter.api.BeforeEach;
20 import org.junit.jupiter.api.Test;
21 import org.opendaylight.mdsal.binding.api.DataBroker;
22 import org.opendaylight.transportpce.common.StringConstants;
23 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
24 import org.opendaylight.transportpce.common.mapping.MappingUtils;
25 import org.opendaylight.transportpce.common.mapping.PortMapping;
26 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
27 import org.opendaylight.transportpce.olm.power.PowerMgmt;
28 import org.opendaylight.transportpce.olm.power.PowerMgmtImpl;
29 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
30 import org.opendaylight.transportpce.test.AbstractTest;
31 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
32 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
33 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetInput;
34 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetOutput;
35 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
36 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutput;
37 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutputBuilder;
38 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
39 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
40 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutputBuilder;
41 import org.opendaylight.yang.gen.v1.http.org.openroadm.pm.types.rev161014.PmNamesEnum;
42
43 public class OlmPowerServiceImplTest  extends AbstractTest {
44
45     private DeviceTransactionManager deviceTransactionManager;
46     private OpenRoadmInterfaces openRoadmInterfaces;
47     private PortMapping portMapping;
48     private PowerMgmt powerMgmt;
49     private MappingUtils mappingUtils;
50     private OlmPowerService olmPowerService;
51     private DataBroker dataBroker;
52
53     @BeforeEach
54     void setUp() {
55         this.dataBroker = mock(DataBroker.class);
56         this.powerMgmt = mock(PowerMgmtImpl.class);
57         this.deviceTransactionManager = mock(DeviceTransactionManager.class);
58         this.portMapping = mock(PortMapping.class);
59         this.mappingUtils = mock(MappingUtils.class);
60         this.openRoadmInterfaces = mock(OpenRoadmInterfaces.class);
61         this.olmPowerService = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmt, this.deviceTransactionManager,
62                 this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
63     }
64
65     @Test
66     void dummyTest() {
67         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
68         olmPowerServiceImpl.init();
69         olmPowerServiceImpl.close();
70     }
71
72     @Test
73     void testGetPm() {
74         when(this.mappingUtils.getOpenRoadmVersion(anyString()))
75             .thenReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1);
76         when(this.deviceTransactionManager.getDataFromDevice(anyString(), any(), any(), anyLong(), any()))
77             .thenReturn(Optional.of(OlmPowerServiceRpcImplUtil.getCurrentPmList121()));
78
79         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
80         GetPmOutput result = this.olmPowerService.getPm(input);
81         assertEquals(
82             org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.PmGranularity._15min,
83             result.getGranularity());
84         assertEquals(
85             PmNamesEnum.OpticalPowerInput.toString(),
86             result.getMeasurements().stream().findFirst().get().getPmparameterName());
87         assertEquals(
88             String.valueOf(3.0),
89             result.getMeasurements().stream().findFirst().get().getPmparameterValue());
90         assertEquals(
91             "ots-deg1",
92             result.getResourceIdentifier().getResourceName());
93     }
94
95     @Test
96     void testServicePowerSetupSuccess() {
97         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
98         when(this.powerMgmt.setPower(any())).thenReturn(true);
99         ServicePowerSetupOutput result = this.olmPowerService.servicePowerSetup(input);
100         assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), result);
101         assertEquals("Success", result.getResult());
102     }
103
104     @Test
105     void testServicePowerSetupFailed() {
106         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
107         when(this.powerMgmt.setPower(any())).thenReturn(false);
108         ServicePowerSetupOutput output = this.olmPowerService.servicePowerSetup(input);
109         assertEquals("Failed", output.getResult());
110     }
111
112     @Test
113     void testServicePowerTurnDownSuccess() {
114         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
115         when(this.powerMgmt.powerTurnDown(any())).thenReturn(true);
116         ServicePowerTurndownOutput output = this.olmPowerService.servicePowerTurndown(input);
117         assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
118         assertEquals("Success", output.getResult());
119     }
120
121     @Test
122     void testServicePowerTurnDownFailed() {
123         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
124         when(this.powerMgmt.powerTurnDown(any())).thenReturn(false);
125         ServicePowerTurndownOutput output = this.olmPowerService.servicePowerTurndown(input);
126         assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
127         assertEquals("Failed", output.getResult());
128     }
129
130     @Test
131     void testServicePowerReset() {
132         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
133         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
134         assertEquals(null, output);
135     }
136 }