d53dc556722246a1411f3c0dd74c49524b5195d4
[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 testGetPm() {
67         when(this.mappingUtils.getOpenRoadmVersion(anyString()))
68             .thenReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1);
69         when(this.deviceTransactionManager.getDataFromDevice(anyString(), any(), any(), anyLong(), any()))
70             .thenReturn(Optional.of(OlmPowerServiceRpcImplUtil.getCurrentPmList121()));
71
72         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
73         GetPmOutput result = this.olmPowerService.getPm(input);
74         assertEquals(
75             org.opendaylight.yang.gen.v1.http.org.transportpce.common.types.rev220926.PmGranularity._15min,
76             result.getGranularity());
77         assertEquals(
78             PmNamesEnum.OpticalPowerInput.toString(),
79             result.getMeasurements().stream().findFirst().orElseThrow().getPmparameterName());
80         assertEquals(
81             String.valueOf(3.0),
82             result.getMeasurements().stream().findFirst().orElseThrow().getPmparameterValue());
83         assertEquals(
84             "ots-deg1",
85             result.getResourceIdentifier().getResourceName());
86     }
87
88     @Test
89     void testServicePowerSetupSuccess() {
90         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
91         when(this.powerMgmt.setPower(any())).thenReturn(true);
92         ServicePowerSetupOutput result = this.olmPowerService.servicePowerSetup(input);
93         assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), result);
94         assertEquals("Success", result.getResult());
95     }
96
97     @Test
98     void testServicePowerSetupFailed() {
99         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
100         when(this.powerMgmt.setPower(any())).thenReturn(false);
101         ServicePowerSetupOutput output = this.olmPowerService.servicePowerSetup(input);
102         assertEquals("Failed", output.getResult());
103     }
104
105     @Test
106     void testServicePowerTurnDownSuccess() {
107         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
108         when(this.powerMgmt.powerTurnDown(any())).thenReturn(true);
109         ServicePowerTurndownOutput output = this.olmPowerService.servicePowerTurndown(input);
110         assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
111         assertEquals("Success", output.getResult());
112     }
113
114     @Test
115     void testServicePowerTurnDownFailed() {
116         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
117         when(this.powerMgmt.powerTurnDown(any())).thenReturn(false);
118         ServicePowerTurndownOutput output = this.olmPowerService.servicePowerTurndown(input);
119         assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
120         assertEquals("Failed", output.getResult());
121     }
122
123     @Test
124     void testServicePowerReset() {
125         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
126         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
127         assertEquals(null, output);
128     }
129 }