Add PowerMock library in OLM module
[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.MountPoint;
17 import org.opendaylight.mdsal.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(new MappingUtilsImpl(getDataBroker()));
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         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
92         boolean output = this.powerMgmt.setPower(input);
93         Assert.assertEquals(true, output);
94     }
95
96     @Test
97     public void testSetPower2() {
98         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
99         boolean output = this.powerMgmt.setPower(input);
100         Assert.assertEquals(true, output);
101     }
102
103     @Test
104     public void testSetPower3() {
105         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
106         boolean output = this.powerMgmt.setPower(input);
107         Assert.assertEquals(true, output);
108     }
109
110     @Test
111     public void testSetPower4() {
112         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
113         boolean output = this.powerMgmt.setPower(input);
114         Assert.assertEquals(true, output);
115     }
116
117     @Test
118     public void testPowerTurnDown() {
119         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput();
120         boolean output = this.powerMgmt.powerTurnDown(input);
121         Assert.assertEquals(true, output);
122     }
123
124     @Test
125     public void testPowerTurnDown2() {
126         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput2();
127         boolean output = this.powerMgmt.powerTurnDown(input);
128         Assert.assertEquals(false, output);
129     }
130
131     @Test
132     public void testPowerTurnDown3() {
133         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput3();
134         boolean output = this.powerMgmt.powerTurnDown(input);
135         Assert.assertEquals(true, output);
136     }
137
138     @Test
139     public void testPowerTurnDown4() {
140         ServicePowerTurndownInput input = OlmPowerServiceRpcImplUtil.getServicePowerTurndownInput4();
141         boolean output = this.powerMgmt.powerTurnDown(input);
142         Assert.assertEquals(false, output);
143     }
144
145     @Test
146     public void testSetPowerPresentNodes() throws InterruptedException {
147         List<NodeId> nodes = TransactionUtils.getNodeIds();
148         for (NodeId nodeId : nodes) {
149             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
150             Thread.sleep(1000);
151         }
152         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
153         boolean output = this.powerMgmt.setPower(input);
154         Assert.assertEquals(true, output);
155     }
156
157     @Test
158     public void testSetPowerPresentNodes2() throws InterruptedException {
159         List<NodeId> nodes = TransactionUtils.getNodeIds();
160         for (NodeId nodeId : nodes) {
161             TransactionUtils.writeNodeTransaction2(nodeId.getValue(), this.getDataBroker(), null);
162             Thread.sleep(500);
163         }
164         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
165         boolean output = this.powerMgmt.setPower(input);
166         Assert.assertEquals(true, output);
167     }
168
169     @Test
170     public void testSetPowerPresentNodes3() throws InterruptedException {
171         List<NodeId> nodes = TransactionUtils.getNodeIds();
172         for (NodeId nodeId : nodes) {
173             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), null);
174             Thread.sleep(500);
175         }
176         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput();
177         boolean output = this.powerMgmt.setPower(input);
178         Assert.assertEquals(true, output);
179     }
180     /*
181     @Test
182     public void testSetPowerPresentNodes31() throws InterruptedException {
183         List<NodeId> nodes = TransactionUtils.getNodeIds();
184         for (NodeId nodeId : nodes) {
185             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), "deg");
186             Thread.sleep(500);
187         }
188         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
189         boolean output = this.powerMgmt.setPower(input);
190         Assert.assertEquals(false, output);
191     }*/
192
193     @Test
194     public void testSetPowerPresentNodes312() throws InterruptedException {
195         List<NodeId> nodes = TransactionUtils.getNodeIds();
196         for (NodeId nodeId : nodes) {
197             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), "deg");
198             Thread.sleep(500);
199         }
200         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
201         boolean output = this.powerMgmt.setPower(input);
202         Assert.assertEquals(true, output);
203     }
204
205     @Test
206     public void testSetPowerPresentNodes32() throws InterruptedException {
207         List<NodeId> nodes = TransactionUtils.getNodeIds();
208         for (NodeId nodeId : nodes) {
209             TransactionUtils.writeNodeTransaction3(nodeId.getValue(), this.getDataBroker(), null);
210             Thread.sleep(500);
211         }
212         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
213         boolean output = this.powerMgmt.setPower(input);
214         Assert.assertEquals(true, output);
215     }
216
217     @Test
218     public void testSetPowerPresentNodes4() throws InterruptedException {
219         List<NodeId> nodes = TransactionUtils.getNodeIds();
220         for (NodeId nodeId : nodes) {
221             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), "network");
222             Thread.sleep(500);
223         }
224         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
225         boolean output = this.powerMgmt.setPower(input);
226         Assert.assertEquals(true, output);
227     }
228
229     /*
230     @Test
231     public void testSetPowerPresentNodes41() throws InterruptedException {
232         List<NodeId> nodes = TransactionUtils.getNodeIds();
233         for (NodeId nodeId : nodes) {
234             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
235             Thread.sleep(500);
236         }
237         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput2();
238         boolean output = this.powerMgmt.setPower(input);
239         Assert.assertEquals(false, output);
240     }*/
241
242     @Test
243     public void testSetPowerPresentNodes42() throws InterruptedException {
244         List<NodeId> nodes = TransactionUtils.getNodeIds();
245         for (NodeId nodeId : nodes) {
246             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), "deg");
247             Thread.sleep(500);
248         }
249         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
250         boolean output = this.powerMgmt.setPower(input);
251         Assert.assertEquals(true, output);
252     }
253
254     @Test
255     public void testSetPowerPresentNodes422() throws InterruptedException {
256         List<NodeId> nodes = TransactionUtils.getNodeIds();
257         for (NodeId nodeId : nodes) {
258             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), "deg");
259             Thread.sleep(500);
260         }
261         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput4();
262         boolean output = this.powerMgmt.setPower(input);
263         Assert.assertEquals(true, output);
264     }
265
266     @Test
267     public void testSetPowerPresentNodes43() throws InterruptedException {
268         List<NodeId> nodes = TransactionUtils.getNodeIds();
269         for (NodeId nodeId : nodes) {
270             TransactionUtils.writeNodeTransaction(nodeId.getValue(), this.getDataBroker(), null);
271             Thread.sleep(500);
272         }
273         ServicePowerSetupInput input = OlmPowerServiceRpcImplUtil.getServicePowerSetupInput3();
274         boolean output = this.powerMgmt.setPower(input);
275         Assert.assertEquals(true, output);
276     }
277
278 }