Clean JUnit tests
[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 org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.InjectMocks;
15 import org.mockito.Mockito;
16 import org.mockito.MockitoAnnotations;
17 import org.opendaylight.mdsal.binding.api.DataBroker;
18 import org.opendaylight.mdsal.binding.api.MountPoint;
19 import org.opendaylight.mdsal.binding.api.MountPointService;
20 import org.opendaylight.transportpce.common.NetworkUtils;
21 import org.opendaylight.transportpce.common.ResponseCodes;
22 import org.opendaylight.transportpce.common.StringConstants;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
24 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
25 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
26 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
27 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl710;
28 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
29 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
30 import org.opendaylight.transportpce.common.mapping.MappingUtils;
31 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
32 import org.opendaylight.transportpce.common.mapping.PortMapping;
33 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
34 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
35 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
36 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
37 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
38 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
39 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
40 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
41 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710;
42 import org.opendaylight.transportpce.olm.power.PowerMgmt;
43 import org.opendaylight.transportpce.olm.power.PowerMgmtImpl;
44 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
45 import org.opendaylight.transportpce.olm.stub.MountPointStub;
46 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
47 import org.opendaylight.transportpce.olm.util.TransactionUtils;
48 import org.opendaylight.transportpce.test.AbstractTest;
49 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseInput;
50 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseOutput;
51 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossBaseOutputBuilder;
52 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentInput;
53 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.CalculateSpanlossCurrentOutput;
54 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmInput;
55 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutput;
56 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.GetPmOutputBuilder;
57 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetInput;
58 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerResetOutput;
59 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
60 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutput;
61 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupOutputBuilder;
62 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
63 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutput;
64 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownOutputBuilder;
65 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NetworkId;
66 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.Networks;
67 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.Network;
68 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.networks.NetworkKey;
69 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.topology.rev180226.Network1;
70 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
71 import org.powermock.api.mockito.PowerMockito;
72 import org.slf4j.Logger;
73 import org.slf4j.LoggerFactory;
74
75 public class OlmPowerServiceImplTest  extends AbstractTest {
76
77     private static final Logger LOG = LoggerFactory.getLogger(OlmPowerServiceImplTest.class);
78     private MountPoint mountPoint;
79     private MountPointService mountPointService;
80     private DeviceTransactionManager deviceTransactionManager;
81     private CrossConnect crossConnect;
82     private OpenRoadmInterfaces openRoadmInterfaces;
83     private PortMapping portMapping;
84     private PowerMgmt powerMgmt;
85     private CrossConnectImpl121 crossConnectImpl121;
86     private CrossConnectImpl221 crossConnectImpl22;
87     private CrossConnectImpl710 crossConnectImpl710;
88     private MappingUtils mappingUtils;
89     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
90     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
91     private OpenRoadmInterfacesImpl710 openRoadmInterfacesImpl710;
92     private PortMappingVersion710 portMappingVersion710;
93     private PortMappingVersion221 portMappingVersion22;
94     private PortMappingVersion121 portMappingVersion121;
95     private OlmPowerService olmPowerService;
96     private DataBroker dataBroker;
97     private PowerMgmt powerMgmtMock;
98     @InjectMocks
99     private OlmPowerService olmPowerServiceMock;
100
101
102     @Before
103     public void setUp() {
104         this.dataBroker =  getNewDataBroker();
105         this.mountPoint = new MountPointStub(this.dataBroker);
106         this.mountPointService = new MountPointServiceStub(mountPoint);
107         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
108         this.mappingUtils = Mockito.spy(new MappingUtilsImpl(dataBroker));
109         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
110                 .getOpenRoadmVersion(Mockito.anyString());
111         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
112         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
113         this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
114         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
115                 this.crossConnectImpl22, this.crossConnectImpl710);
116         this.portMappingVersion22 = new PortMappingVersion221(dataBroker, deviceTransactionManager);
117         this.portMappingVersion121 = new PortMappingVersion121(dataBroker, deviceTransactionManager);
118         this.portMappingVersion710 = new PortMappingVersion710(dataBroker, deviceTransactionManager);
119         this.portMapping = new PortMappingImpl(dataBroker, this.portMappingVersion710,
120             this.portMappingVersion22, this.portMappingVersion121);
121         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
122         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager, this.portMapping,
123             this.portMappingVersion22);
124         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager, this.portMapping,
125             this.portMappingVersion710);
126         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
127                 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22,
128             this.openRoadmInterfacesImpl710);
129         this.portMapping = Mockito.spy(this.portMapping);
130         this.powerMgmt = new PowerMgmtImpl(this.dataBroker, this.openRoadmInterfaces, this.crossConnect,
131             this.deviceTransactionManager);
132         this.olmPowerService = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmt,
133             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
134         this.powerMgmtMock = PowerMockito.mock(PowerMgmtImpl.class);
135         this.olmPowerServiceMock = new OlmPowerServiceImpl(this.dataBroker, this.powerMgmtMock,
136             this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
137         this.olmPowerServiceMock = Mockito.mock(OlmPowerServiceImpl.class);
138         MockitoAnnotations.openMocks(this);
139     }
140
141     @Test
142     public void dummyTest() {
143         OlmPowerServiceImpl olmPowerServiceImpl = (OlmPowerServiceImpl) this.olmPowerService;
144         olmPowerServiceImpl.init();
145         olmPowerServiceImpl.close();
146     }
147
148
149     @Test
150     public void testGetPm() {
151         GetPmInput input = OlmPowerServiceRpcImplUtil.getGetPmInput();
152         GetPmOutput output = this.olmPowerService.getPm(input);
153         Assert.assertEquals(new GetPmOutputBuilder().build(), output);
154         Assert.assertEquals(null, output.getResourceId());
155     }
156
157     @Test
158     public void testServicePowerSetupSuccess() {
159         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
160         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(true);
161         //TODO
162         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
163                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Success").build());
164         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
165         Assert.assertEquals(new ServicePowerSetupOutputBuilder().setResult("Success").build(), output);
166         Assert.assertEquals("Success", output.getResult());
167     }
168
169     @Test
170     public void testServicePowerSetupFailed() {
171         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
172         Mockito.when(this.powerMgmtMock.setPower(Mockito.any())).thenReturn(false);
173         //TODO
174         Mockito.when(this.olmPowerServiceMock.servicePowerSetup(Mockito.any()))
175                 .thenReturn(new ServicePowerSetupOutputBuilder().setResult("Failed").build());
176         ServicePowerSetupOutput output = this.olmPowerServiceMock.servicePowerSetup(input);
177         Assert.assertEquals("Failed", output.getResult());
178     }
179
180     @Test
181     public void testServicePowerTurnDownSuccess() {
182         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
183         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(true);
184         //TODO
185         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
186                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Success").build());
187         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
188         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Success").build(), output);
189         Assert.assertEquals("Success", output.getResult());
190     }
191
192     @Test
193     public void testServicePowerTurnDownFailed() {
194         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
195         Mockito.when(this.powerMgmtMock.powerTurnDown(Mockito.any())).thenReturn(false);
196         //TODO
197         Mockito.when(this.olmPowerServiceMock.servicePowerTurndown(Mockito.any()))
198                 .thenReturn(new ServicePowerTurndownOutputBuilder().setResult("Failed").build());
199         ServicePowerTurndownOutput output = this.olmPowerServiceMock.servicePowerTurndown(input);
200         Assert.assertEquals(new ServicePowerTurndownOutputBuilder().setResult("Failed").build(), output);
201         Assert.assertEquals("Failed", output.getResult());
202     }
203
204     @Test
205     public void testCalculateSpanlossBase3() {
206         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
207         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
208             .child(Network.class, overlayTopologyKey)
209             .augmentation(Network1.class)
210             .build();
211         Network1 network = TransactionUtils.getNetwork();
212         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
213         try {
214             Thread.sleep(1000);
215         } catch (InterruptedException e) {
216             LOG.error("Write transaction failed !",e);
217         }
218         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
219         //TODO
220         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
221                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
222         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
223         Assert.assertEquals("Failed", output.getResult());
224
225     }
226
227
228     @Test
229     public void testCalculateSpanlossBase4() {
230         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
231         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
232             .child(Network.class, overlayTopologyKey)
233             .augmentation(Network1.class)
234             .build();
235         Network1 network = TransactionUtils.getEmptyNetwork();
236         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
237         try {
238             Thread.sleep(1000);
239         } catch (InterruptedException e) {
240             LOG.error("Write transaction failed !",e);
241         }
242         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
243         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
244         Assert.assertEquals("Failed", output.getResult());
245
246     }
247
248     @Test
249     public void testCalculateSpanlossBase5() {
250         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
251         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
252             .child(Network.class, overlayTopologyKey)
253             .augmentation(Network1.class)
254             .build();
255         Network1 network = TransactionUtils.getNullNetwork();
256         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
257         try {
258             Thread.sleep(1000);
259         } catch (InterruptedException e) {
260             LOG.error("Write transaction failed !",e);
261         }
262         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
263         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
264         Assert.assertEquals("Failed", output.getResult());
265
266     }
267
268     @Test
269     public void testCalculateSpanlossCurrent1() {
270         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
271         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
272         Assert.assertEquals(null, output);
273     }
274
275     @Test
276     public void testCalculateSpanlossCurrent2() {
277         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
278         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
279             .child(Network.class, overlayTopologyKey)
280             .augmentation(Network1.class)
281             .build();
282         Network1 network = TransactionUtils.getNetwork();
283         TransactionUtils.writeTransaction(this.dataBroker, networkIID, network);
284         try {
285             Thread.sleep(1000);
286         } catch (InterruptedException e) {
287             LOG.error("Write transaction failed !",e);
288         }
289         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
290         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
291         Assert.assertEquals(null, output);
292     }
293
294     @Test
295     public void testServicePowerReset() {
296         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
297         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
298         Assert.assertEquals(null, output);
299     }
300
301     @Test
302     public void testServicePowerTurndownSuccessResult() {
303         ServicePowerTurndownInput servicePowerTurndownInput = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
304         ServicePowerTurndownOutput servicePowerTurndownOutput =
305                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
306         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerTurndownOutput.getResult());
307     }
308
309     @Test
310     public void testServicePowerTurndownFailResult() {
311         ServicePowerTurndownInput servicePowerTurndownInput =
312                 OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
313         ServicePowerTurndownOutput servicePowerTurndownOutput =
314                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
315         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerTurndownOutput.getResult());
316     }
317
318     @Test
319     public void testServicePowerSetupSuccessResult() {
320         ServicePowerSetupInput servicePowerSetupInput =
321                 OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
322         ServicePowerSetupOutput servicePowerSetupOutput =
323                 this.olmPowerService.servicePowerSetup(servicePowerSetupInput);
324         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerSetupOutput.getResult());
325     }
326
327     @Test
328     public void testServicePowerSetupFailResult() {
329         ServicePowerSetupInput servicePowerSetupInput = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
330         Mockito.when(powerMgmtMock.setPower(servicePowerSetupInput)).thenReturn(Boolean.FALSE);
331         OlmPowerService olmPowerServiceWithMock = new OlmPowerServiceImpl(dataBroker, powerMgmtMock,
332                 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
333         ServicePowerSetupOutput servicePowerSetupOutput =
334                 olmPowerServiceWithMock.servicePowerSetup(servicePowerSetupInput);
335         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerSetupOutput.getResult());
336     }
337 }