Clean JUnit tests
[transportpce.git] / olm / src / test / java / org / opendaylight / transportpce / olm / power / PowerMgmtTest.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.power;
10
11 import java.util.List;
12 import org.junit.Assert;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.mockito.Mockito;
16 import org.opendaylight.mdsal.binding.api.DataBroker;
17 import org.opendaylight.mdsal.binding.api.MountPoint;
18 import org.opendaylight.mdsal.binding.api.MountPointService;
19 import org.opendaylight.transportpce.common.StringConstants;
20 import org.opendaylight.transportpce.common.crossconnect.CrossConnect;
21 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl;
22 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl121;
23 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl221;
24 import org.opendaylight.transportpce.common.crossconnect.CrossConnectImpl710;
25 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
26 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
27 import org.opendaylight.transportpce.common.mapping.MappingUtils;
28 import org.opendaylight.transportpce.common.mapping.MappingUtilsImpl;
29 import org.opendaylight.transportpce.common.mapping.PortMapping;
30 import org.opendaylight.transportpce.common.mapping.PortMappingImpl;
31 import org.opendaylight.transportpce.common.mapping.PortMappingVersion121;
32 import org.opendaylight.transportpce.common.mapping.PortMappingVersion221;
33 import org.opendaylight.transportpce.common.mapping.PortMappingVersion710;
34 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfaces;
35 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl;
36 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl121;
37 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl221;
38 import org.opendaylight.transportpce.common.openroadminterfaces.OpenRoadmInterfacesImpl710;
39 import org.opendaylight.transportpce.olm.stub.MountPointServiceStub;
40 import org.opendaylight.transportpce.olm.stub.MountPointStub;
41 import org.opendaylight.transportpce.olm.util.OlmPowerServiceRpcImplUtil;
42 import org.opendaylight.transportpce.olm.util.TransactionUtils;
43 import org.opendaylight.transportpce.test.AbstractTest;
44 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerSetupInput;
45 import org.opendaylight.yang.gen.v1.http.org.opendaylight.transportpce.olm.rev210618.ServicePowerTurndownInput;
46 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.network.rev180226.NodeId;
47
48 public class PowerMgmtTest extends AbstractTest {
49     private MountPoint mountPoint;
50     private MountPointService mountPointService;
51     private DeviceTransactionManager deviceTransactionManager;
52     private CrossConnect crossConnect;
53     private OpenRoadmInterfaces openRoadmInterfaces;
54     private PortMapping portMapping;
55     private PowerMgmt powerMgmt;
56     private CrossConnectImpl121 crossConnectImpl121;
57     private CrossConnectImpl221 crossConnectImpl22;
58     private CrossConnectImpl710 crossConnectImpl710;
59     private MappingUtils mappingUtils;
60     private OpenRoadmInterfacesImpl121 openRoadmInterfacesImpl121;
61     private OpenRoadmInterfacesImpl221 openRoadmInterfacesImpl22;
62     private OpenRoadmInterfacesImpl710 openRoadmInterfacesImpl710;
63     private PortMappingVersion710 portMappingVersion710;
64     private PortMappingVersion221 portMappingVersion22;
65     private PortMappingVersion121 portMappingVersion121;
66     private DataBroker dataBroker;
67
68     @Before
69     public void setUp() {
70         dataBroker = this.getNewDataBroker();
71         this.mountPoint = new MountPointStub(dataBroker);
72         this.mountPointService = new MountPointServiceStub(mountPoint);
73         this.mappingUtils = Mockito.spy(new MappingUtilsImpl(dataBroker));
74         Mockito.doReturn(StringConstants.OPENROADM_DEVICE_VERSION_1_2_1).when(mappingUtils)
75                 .getOpenRoadmVersion(Mockito.anyString());
76         this.deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointService, 3000);
77         this.crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
78         this.crossConnectImpl22 = new CrossConnectImpl221(deviceTransactionManager);
79         this.crossConnect = new CrossConnectImpl(deviceTransactionManager, this.mappingUtils, this.crossConnectImpl121,
80                 this.crossConnectImpl22, this.crossConnectImpl710);
81         this.portMappingVersion22 = new PortMappingVersion221(dataBroker, deviceTransactionManager);
82         this.portMappingVersion121 = new PortMappingVersion121(dataBroker, deviceTransactionManager);
83         this.portMapping = new PortMappingImpl(getDataBroker(), this.portMappingVersion710,
84                 this.portMappingVersion22, this.portMappingVersion121);
85         this.openRoadmInterfacesImpl121 = new OpenRoadmInterfacesImpl121(deviceTransactionManager);
86         this.openRoadmInterfacesImpl22 = new OpenRoadmInterfacesImpl221(deviceTransactionManager, this.portMapping,
87             this.portMappingVersion22);
88         this.openRoadmInterfacesImpl710 = new OpenRoadmInterfacesImpl710(deviceTransactionManager, this.portMapping,
89             this.portMappingVersion710);
90         this.openRoadmInterfaces = new OpenRoadmInterfacesImpl((this.deviceTransactionManager),
91                 this.mappingUtils,this.openRoadmInterfacesImpl121,this.openRoadmInterfacesImpl22,
92             this.openRoadmInterfacesImpl710);
93         this.openRoadmInterfaces = Mockito.spy(this.openRoadmInterfaces);
94         this.portMapping = Mockito.spy(this.portMapping);
95         this.powerMgmt = new PowerMgmtImpl(this.dataBroker, this.openRoadmInterfaces, this.crossConnect,
96                 this.deviceTransactionManager);
97     }
98
99     @Test
100     public void testSetPower() {
101         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
102         boolean output = this.powerMgmt.setPower(input);
103         Assert.assertEquals(true, output);
104     }
105
106     @Test
107     public void testSetPower2() {
108         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
109         boolean output = this.powerMgmt.setPower(input);
110         Assert.assertEquals(true, output);
111     }
112
113     @Test
114     public void testSetPower3() {
115         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
116         boolean output = this.powerMgmt.setPower(input);
117         Assert.assertEquals(true, output);
118     }
119
120     @Test
121     public void testSetPower4() {
122         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
123         boolean output = this.powerMgmt.setPower(input);
124         Assert.assertEquals(true, output);
125     }
126
127     @Test
128     public void testPowerTurnDown() {
129         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
130         boolean output = this.powerMgmt.powerTurnDown(input);
131         Assert.assertEquals(true, output);
132     }
133
134     @Test
135     public void testPowerTurnDown2() {
136         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
137         boolean output = this.powerMgmt.powerTurnDown(input);
138         Assert.assertEquals(false, output);
139     }
140
141     @Test
142     public void testPowerTurnDown3() {
143         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput3();
144         boolean output = this.powerMgmt.powerTurnDown(input);
145         Assert.assertEquals(true, output);
146     }
147
148     @Test
149     public void testPowerTurnDown4() {
150         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput4();
151         boolean output = this.powerMgmt.powerTurnDown(input);
152         Assert.assertEquals(false, output);
153     }
154
155     @Test
156     public void testSetPowerPresentNodes() throws InterruptedException {
157         List<NodeId> nodes = TransactionUtils.getNodeIds();
158         for (NodeId nodeId : nodes) {
159             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, null);
160             Thread.sleep(1000);
161         }
162         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
163         boolean output = this.powerMgmt.setPower(input);
164         Assert.assertEquals(true, output);
165     }
166
167     @Test
168     public void testSetPowerPresentNodes2() throws InterruptedException {
169         List<NodeId> nodes = TransactionUtils.getNodeIds();
170         for (NodeId nodeId : nodes) {
171             TransactionUtils.writeNodeTransaction2(nodeId.getValue(), this.dataBroker, null);
172             Thread.sleep(500);
173         }
174         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
175         boolean output = this.powerMgmt.setPower(input);
176         Assert.assertEquals(true, output);
177     }
178
179     @Test
180     public void testSetPowerPresentNodes3() throws InterruptedException {
181         List<NodeId> nodes = TransactionUtils.getNodeIds();
182         for (NodeId nodeId : nodes) {
183             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.dataBroker, null);
184             Thread.sleep(500);
185         }
186         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
187         boolean output = this.powerMgmt.setPower(input);
188         Assert.assertEquals(true, output);
189     }
190     /*
191     TODO: see if this test is always relevant
192     @Test
193     public void testSetPowerPresentNodes31() throws InterruptedException {
194         List<NodeId> nodes = TransactionUtils.getNodeIds();
195         for (NodeId nodeId : nodes) {
196             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), "deg");
197             Thread.sleep(500);
198         }
199         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
200         boolean output = this.powerMgmt.setPower(input);
201         Assert.assertEquals(false, output);
202     }*/
203
204     @Test
205     public void testSetPowerPresentNodes312() throws InterruptedException {
206         List<NodeId> nodes = TransactionUtils.getNodeIds();
207         for (NodeId nodeId : nodes) {
208             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.dataBroker, "deg");
209             Thread.sleep(500);
210         }
211         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
212         boolean output = this.powerMgmt.setPower(input);
213         Assert.assertEquals(true, output);
214     }
215
216     @Test
217     public void testSetPowerPresentNodes32() throws InterruptedException {
218         List<NodeId> nodes = TransactionUtils.getNodeIds();
219         for (NodeId nodeId : nodes) {
220             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.dataBroker, null);
221             Thread.sleep(500);
222         }
223         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
224         boolean output = this.powerMgmt.setPower(input);
225         Assert.assertEquals(true, output);
226     }
227
228     @Test
229     public void testSetPowerPresentNodes4() throws InterruptedException {
230         List<NodeId> nodes = TransactionUtils.getNodeIds();
231         for (NodeId nodeId : nodes) {
232             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, "network");
233             Thread.sleep(500);
234         }
235         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
236         boolean output = this.powerMgmt.setPower(input);
237         Assert.assertEquals(true, output);
238     }
239
240     /*
241     TODO: see if this test is always relevant
242     @Test
243     public void testSetPowerPresentNodes41() throws InterruptedException {
244         List<NodeId> nodes = TransactionUtils.getNodeIds();
245         for (NodeId nodeId : nodes) {
246             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
247             Thread.sleep(500);
248         }
249         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
250         boolean output = this.powerMgmt.setPower(input);
251         Assert.assertEquals(false, output);
252     }*/
253
254     @Test
255     public void testSetPowerPresentNodes42() throws InterruptedException {
256         List<NodeId> nodes = TransactionUtils.getNodeIds();
257         for (NodeId nodeId : nodes) {
258             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, "deg");
259             Thread.sleep(500);
260         }
261         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
262         boolean output = this.powerMgmt.setPower(input);
263         Assert.assertEquals(true, output);
264     }
265
266     @Test
267     public void testSetPowerPresentNodes422() throws InterruptedException {
268         List<NodeId> nodes = TransactionUtils.getNodeIds();
269         for (NodeId nodeId : nodes) {
270             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, "deg");
271             Thread.sleep(500);
272         }
273         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
274         boolean output = this.powerMgmt.setPower(input);
275         Assert.assertEquals(true, output);
276     }
277
278     @Test
279     public void testSetPowerPresentNodes43() throws InterruptedException {
280         List<NodeId> nodes = TransactionUtils.getNodeIds();
281         for (NodeId nodeId : nodes) {
282             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.dataBroker, null);
283             Thread.sleep(500);
284         }
285         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
286         boolean output = this.powerMgmt.setPower(input);
287         Assert.assertEquals(true, output);
288     }
289
290 }