add unit tests for crossconnect package
[transportpce.git] / common / src / test / java / org / opendaylight / transportpce / common / crossconnect / CrossConnectImpl121Test.java
1 /*
2  * Copyright © 2020 Orange.  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.common.crossconnect;
10
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.math.BigDecimal;
16 import java.util.Optional;
17 import java.util.concurrent.CompletableFuture;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mockito;
22 import org.opendaylight.mdsal.binding.api.DataBroker;
23 import org.opendaylight.mdsal.binding.api.MountPoint;
24 import org.opendaylight.mdsal.binding.api.MountPointService;
25 import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.transportpce.common.Timeouts;
28 import org.opendaylight.transportpce.common.device.DeviceTransaction;
29 import org.opendaylight.transportpce.common.device.DeviceTransactionManager;
30 import org.opendaylight.transportpce.common.device.DeviceTransactionManagerImpl;
31 import org.opendaylight.yang.gen.v1.http.org.openroadm.common.types.rev161014.OpticalControlMode;
32 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPorts;
33 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.degree.ConnectionPortsBuilder;
34 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.OrgOpenroadmDevice;
35 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnections;
36 import org.opendaylight.yang.gen.v1.http.org.openroadm.device.rev170206.org.openroadm.device.container.org.openroadm.device.RoadmConnectionsKey;
37 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
38 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
39
40 public class CrossConnectImpl121Test {
41     private CrossConnectImpl121 crossConnectImpl121 = null;
42     private DeviceTransactionManager deviceTransactionManager = null;
43
44     private MountPointService mountPointServiceMock = mock(MountPointService.class);
45     private MountPoint mountPointMock = mock(MountPoint.class);
46     private DataBroker dataBrokerMock = mock(DataBroker.class);
47     private ReadWriteTransaction rwTransactionMock = mock(ReadWriteTransaction.class);
48
49     @Before
50     public void setup() {
51         deviceTransactionManager = mock(DeviceTransactionManager.class);
52         crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
53
54         //mock responses for deviceTransactionManager calls
55         InstanceIdentifier<RoadmConnections> deviceIID =
56                 InstanceIdentifier.create(OrgOpenroadmDevice.class)
57                         .child(RoadmConnections.class, new RoadmConnectionsKey("1"));
58         when(deviceTransactionManager.getDataFromDevice("deviceId",
59                 LogicalDatastoreType.OPERATIONAL, deviceIID,
60                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT))
61             .thenReturn(Optional.of(mock(RoadmConnections.class)));
62     }
63
64     @Test
65     public void getCrossConnectTest() {
66         Optional<RoadmConnections> res =
67                 crossConnectImpl121.getCrossConnect("deviceId", "1");
68         Assert.assertTrue("Optional object should have a value", res.isPresent());
69     }
70
71     @Test
72     public void postCrossConnectTest() {
73         Mockito.when(mountPointServiceMock.getMountPoint(any())).thenReturn(Optional.of(mountPointMock));
74         Mockito.when(mountPointMock.getService(any())).thenReturn(Optional.of(dataBrokerMock));
75         Mockito.when(dataBrokerMock.newReadWriteTransaction()).thenReturn(rwTransactionMock);
76         Mockito.when(rwTransactionMock.commit()).thenReturn(FluentFutures.immediateNullFluentFuture());
77         deviceTransactionManager = new DeviceTransactionManagerImpl(mountPointServiceMock, 3000);
78         crossConnectImpl121 = new CrossConnectImpl121(deviceTransactionManager);
79         Optional res = crossConnectImpl121.postCrossConnect("deviceId", 100L, "srcTp", "destTp");
80         Assert.assertEquals(res.get(), "srcTp-destTp-100");
81     }
82
83     // TODO : fix commit
84     @Test(expected = NullPointerException.class)
85     public void setPowerLevelTest() {
86         InstanceIdentifier<RoadmConnections> deviceIID =
87                 InstanceIdentifier.create(OrgOpenroadmDevice.class)
88                         .child(RoadmConnections.class, new RoadmConnectionsKey("1"));
89         when(deviceTransactionManager.getDataFromDevice("deviceId",
90                 LogicalDatastoreType.OPERATIONAL, deviceIID,
91                 Timeouts.DEVICE_READ_TIMEOUT, Timeouts.DEVICE_READ_TIMEOUT_UNIT))
92             .thenReturn(Optional.of(mock(RoadmConnections.class)));
93
94         Mockito.when(deviceTransactionManager.getDeviceTransaction("deviceId"))
95             .thenReturn(CompletableFuture.completedFuture(Optional.of(mock(DeviceTransaction.class))));
96         boolean res = crossConnectImpl121
97                 .setPowerLevel("deviceId", OpticalControlMode.Power, new BigDecimal(100), "1");
98
99         Assert.assertTrue("set Level should be true", true);
100     }
101
102     private ConnectionPorts getConnectionPorts(String c1, String p1) {
103         return new ConnectionPortsBuilder().setCircuitPackName(c1).setPortName(p1).build();
104     }
105 }