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