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