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