Merge "Migrate to Al and reactivate some Junit tests" into stable/aluminium
[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     @Test
199     public void testCalculateSpanlossBase3() {
200         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
201         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
202             .child(Network.class, overlayTopologyKey)
203             .augmentation(Network1.class)
204             .build();
205         Network1 network = TransactionUtils.getNetwork();
206         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
207         try {
208             Thread.sleep(1000);
209         } catch (InterruptedException e) {
210             LOG.error("Write transaction failed !",e);
211         }
212         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
213         //TODO
214         Mockito.when(this.olmPowerServiceMock.calculateSpanlossBase(Mockito.any()))
215                 .thenReturn(new CalculateSpanlossBaseOutputBuilder().setResult("Failed").build());
216         CalculateSpanlossBaseOutput output = this.olmPowerServiceMock.calculateSpanlossBase(input);
217         Assert.assertEquals("Failed", output.getResult());
218
219     }
220
221
222     @Test
223     public void testCalculateSpanlossBase4() {
224         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
225         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
226             .child(Network.class, overlayTopologyKey)
227             .augmentation(Network1.class)
228             .build();
229         Network1 network = TransactionUtils.getEmptyNetwork();
230         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
231         try {
232             Thread.sleep(1000);
233         } catch (InterruptedException e) {
234             LOG.error("Write transaction failed !",e);
235         }
236         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
237         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
238         Assert.assertEquals("Failed", output.getResult());
239
240     }
241
242     @Test
243     public void testCalculateSpanlossBase5() {
244         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
245         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
246             .child(Network.class, overlayTopologyKey)
247             .augmentation(Network1.class)
248             .build();
249         Network1 network = TransactionUtils.getNullNetwork();
250         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
251         try {
252             Thread.sleep(1000);
253         } catch (InterruptedException e) {
254             LOG.error("Write transaction failed !",e);
255         }
256         CalculateSpanlossBaseInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossBaseInput2();
257         CalculateSpanlossBaseOutput output = this.olmPowerService.calculateSpanlossBase(input);
258         Assert.assertEquals("Failed", output.getResult());
259
260     }
261
262     @Test
263     public void testCalculateSpanlossCurrent1() {
264         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
265         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
266         Assert.assertEquals(null, output);
267     }
268
269     @Test
270     public void testCalculateSpanlossCurrent2() {
271         NetworkKey overlayTopologyKey = new NetworkKey(new NetworkId(NetworkUtils.OVERLAY_NETWORK_ID));
272         InstanceIdentifier<Network1> networkIID = InstanceIdentifier.builder(Networks.class)
273             .child(Network.class, overlayTopologyKey)
274             .augmentation(Network1.class)
275             .build();
276         Network1 network = TransactionUtils.getNetwork();
277         TransactionUtils.writeTransaction(this.getDataBroker(), networkIID, network);
278         try {
279             Thread.sleep(1000);
280         } catch (InterruptedException e) {
281             LOG.error("Write transaction failed !",e);
282         }
283         CalculateSpanlossCurrentInput input = OlmPowerServiceRpcImplUtil.getCalculateSpanlossCurrentInput();
284         CalculateSpanlossCurrentOutput output = this.olmPowerService.calculateSpanlossCurrent(input);
285         Assert.assertEquals(null, output);
286     }
287
288     @Test
289     public void testServicePowerReset() {
290         ServicePowerResetInput input = OlmPowerServiceRpcImplUtil.getServicePowerResetInput();
291         ServicePowerResetOutput output = this.olmPowerService.servicePowerReset(input);
292         Assert.assertEquals(null, output);
293     }
294
295     @Test
296     public void testServicePowerTurndownSuccessResult() {
297         ServicePowerTurndownInput servicePowerTurndownInput = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
298         ServicePowerTurndownOutput servicePowerTurndownOutput =
299                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
300         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerTurndownOutput.getResult());
301     }
302
303     @Test
304     public void testServicePowerTurndownFailResult() {
305         ServicePowerTurndownInput servicePowerTurndownInput =
306                 OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
307         ServicePowerTurndownOutput servicePowerTurndownOutput =
308                 this.olmPowerService.servicePowerTurndown(servicePowerTurndownInput);
309         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerTurndownOutput.getResult());
310     }
311
312     @Test
313     public void testServicePowerSetupSuccessResult() {
314         ServicePowerSetupInput servicePowerSetupInput =
315                 OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
316         ServicePowerSetupOutput servicePowerSetupOutput =
317                 this.olmPowerService.servicePowerSetup(servicePowerSetupInput);
318         Assert.assertEquals(ResponseCodes.SUCCESS_RESULT, servicePowerSetupOutput.getResult());
319     }
320
321     @Test
322     public void testServicePowerSetupFailResult() {
323         ServicePowerSetupInput servicePowerSetupInput = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
324         Mockito.when(powerMgmtMock.setPower(servicePowerSetupInput)).thenReturn(Boolean.FALSE);
325         OlmPowerService olmPowerServiceWithMock = new OlmPowerServiceImpl(dataBroker, powerMgmtMock,
326                 this.deviceTransactionManager, this.portMapping, this.mappingUtils, this.openRoadmInterfaces);
327         ServicePowerSetupOutput servicePowerSetupOutput =
328                 olmPowerServiceWithMock.servicePowerSetup(servicePowerSetupInput);
329         Assert.assertEquals(ResponseCodes.FAILED_RESULT, servicePowerSetupOutput.getResult());
330     }
331 }