Unit Test for UnimgrMapper class
[unimgr.git] / impl / src / test / java / org / opendaylight / unimgr / impl / UnimgrMapperTest.java
1 /*
2  * Copyright (c) 2016 Inocybe Technologies 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 package org.opendaylight.unimgr.impl;
9
10 import static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertTrue;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbBridgeRef;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.OvsdbNodeAugmentation;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.rev150105.ovsdb.node.attributes.ManagedNodeEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.unimgr.rev151012.UniAugmentation;
32 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.LinkId;
33 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NetworkTopology;
34 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
35 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.TpId;
36 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.Topology;
37 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.TopologyKey;
38 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Link;
39 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
40 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPoint;
42 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.node.TerminationPointKey;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44 import org.powermock.api.mockito.PowerMockito;
45 import org.powermock.core.classloader.annotations.PrepareForTest;
46 import org.powermock.modules.junit4.PowerMockRunner;
47
48 @PrepareForTest({UnimgrUtils.class})
49 @RunWith(PowerMockRunner.class)
50 public class UnimgrMapperTest {
51
52     private static final String BRIDGE_NAME = "br-0";
53     private static final String NODE_ID = "uni://10.0.0.1";
54     private static final String OVSDB_TOPOLOGY_KEY = "ovsdb:1";
55     private static final String EVC_TOPOLOGY_KEY = "unimgr:evc";
56     private static final String UNI_TOPOLOGY_KEY = "unimgr:uni";
57     private static final String PORT_NAME = "port 0";
58     private static final String IPV4_ADDRESS = "10.0.0.1";
59
60     @Before
61     public void setUp() throws Exception {
62         PowerMockito.whenNew(TopologyKey.class).withAnyArguments().thenReturn(mock(TopologyKey.class));
63         PowerMockito.whenNew(NodeKey.class).withAnyArguments().thenReturn(mock(NodeKey.class));
64     }
65
66     @Test
67     public void testCreateOvsdbBridgeNodeIid() throws Exception {
68         Node ovsdbNode = mock(Node.class);
69         NodeId nodeId = mock(NodeId.class);
70         when(ovsdbNode.getNodeId()).thenReturn(nodeId);
71         when(nodeId.getValue()).thenReturn(NODE_ID);
72         String bridgeNodeName = NODE_ID
73                 + UnimgrConstants.DEFAULT_BRIDGE_NODE_ID_SUFFIX
74                 + BRIDGE_NAME;
75         PowerMockito.whenNew(NodeId.class).withArguments(bridgeNodeName).thenReturn(nodeId);
76
77         InstanceIdentifier<Node> iid = UnimgrMapper.createOvsdbBridgeNodeIid(ovsdbNode, BRIDGE_NAME);
78         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
79         assertTrue(iid.firstKeyOf(Node.class).getNodeId().getValue().equalsIgnoreCase(bridgeNodeName));
80     }
81
82     @Test
83     public void testGetEvcLinkIid() {
84         LinkId id = mock(LinkId.class);
85         InstanceIdentifier<Link> iid = UnimgrMapper.getEvcLinkIid(id);
86         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(EVC_TOPOLOGY_KEY));
87     }
88
89     @Test
90     public void testGetEvcTopologyIid() {
91         InstanceIdentifier<Topology> iid = UnimgrMapper.getEvcTopologyIid();
92         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(EVC_TOPOLOGY_KEY));
93     }
94
95     @Test
96     public void testGetEvcTopologyNodeIid() throws Exception {
97         InstanceIdentifier<Node> iid = UnimgrMapper.getEvcTopologyNodeIid();
98         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(EVC_TOPOLOGY_KEY));
99     }
100
101     @SuppressWarnings("unchecked")
102     @Test
103     public void testGetOvsdbBridgeNodeIid() {
104         Node ovsdbNode = mock(Node.class);
105         OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class, Mockito.RETURNS_DEEP_STUBS);
106         when(ovsdbNode.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation);
107         List<ManagedNodeEntry> managedNodes = new ArrayList<>();
108         ManagedNodeEntry managedNodeEntry = mock(ManagedNodeEntry.class);
109         managedNodes.add(managedNodeEntry);
110         when(ovsdbNodeAugmentation.getManagedNodeEntry()).thenReturn(managedNodes);
111         OvsdbBridgeRef ovsdbBridgeRef = mock(OvsdbBridgeRef.class);
112         when(managedNodeEntry.getBridgeRef()).thenReturn(ovsdbBridgeRef);
113
114         @SuppressWarnings("rawtypes")
115         InstanceIdentifier nodePath = InstanceIdentifier
116                 .create(NetworkTopology.class)
117                 .child(Topology.class,
118                         new TopologyKey(UnimgrConstants.OVSDB_TOPOLOGY_ID))
119                 .child(Node.class,
120                         new NodeKey(new NodeId(NODE_ID)));
121         when(ovsdbBridgeRef.getValue()).thenReturn(nodePath);
122
123         InstanceIdentifier<Node> iid = UnimgrMapper.getOvsdbBridgeNodeIid(ovsdbNode);
124         assertNotNull(iid);
125         verify(ovsdbNodeAugmentation).getManagedNodeEntry();
126     }
127
128     /*
129      *  This test for 2 functions with the
130      *  same name that take different parameters.
131      */
132     @Test
133     public void testGetOvsdbNodeIid() throws Exception {
134         IpAddress ipAddress = mock(IpAddress.class);
135         Ipv4Address ipV4Address = mock(Ipv4Address.class);
136         when(ipAddress.getIpv4Address()).thenReturn(ipV4Address);
137         when(ipV4Address.getValue()).thenReturn(IPV4_ADDRESS);
138         String nodeIdUri = UnimgrConstants.OVSDB_PREFIX
139                 + IPV4_ADDRESS
140                 + ":"
141                 + UnimgrConstants.OVSDB_PORT;
142         NodeId nodeId = mock(NodeId.class);
143         PowerMockito.whenNew(NodeId.class).withArguments(nodeIdUri).thenReturn(nodeId);
144
145         //test method 1
146         InstanceIdentifier<Node> iid = UnimgrMapper.getOvsdbNodeIid(ipAddress);
147         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
148         assertTrue(iid.firstKeyOf(Node.class).getNodeId().getValue().equalsIgnoreCase(nodeIdUri));
149
150         //test method 2
151         iid = UnimgrMapper.getOvsdbNodeIid(nodeId);
152         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
153         assertTrue(iid.firstKeyOf(Node.class).getNodeId().equals(nodeId));
154     }
155
156     @Test
157     public void testGetOvsdbTopologyIid() throws Exception {
158         InstanceIdentifier<Topology> iid = UnimgrMapper.getOvsdbTopologyIid();
159         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
160     }
161
162     /*
163      *  This test for 2 functions with the
164      *  same name that take different parameters.
165      */
166     @Test
167     public void testGetTerminationPointIid() throws Exception {
168         Node bridgeNode = mock(Node.class);
169         TpId tpId = mock(TpId.class);
170
171         PowerMockito.whenNew(TerminationPointKey.class).withAnyArguments().thenReturn(mock(TerminationPointKey.class));
172         when(bridgeNode.getKey()).thenReturn(mock(NodeKey.class));
173         PowerMockito.whenNew(TpId.class).withAnyArguments().thenReturn(mock(TpId.class));
174
175         // test method 1
176         InstanceIdentifier<TerminationPoint> iid = UnimgrMapper.getTerminationPointIid(bridgeNode, PORT_NAME);
177         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
178         assertTrue(iid.firstKeyOf(TerminationPoint.class).getTpId().getValue().contains(PORT_NAME));
179
180         // test method 2
181         iid = UnimgrMapper.getTerminationPointIid(bridgeNode, tpId);
182         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(OVSDB_TOPOLOGY_KEY));
183         assertTrue(iid.firstKeyOf(TerminationPoint.class).getTpId().equals(tpId));
184     }
185
186     /*
187      *  This test for 2 functions with the
188      *  same name that take different parameters.
189      */
190     @Test
191     public void testGetUniIid() throws Exception {
192         DataBroker dataBroker = mock(DataBroker.class);
193         IpAddress ip = mock(IpAddress.class);
194
195         List<Node> uniNodes = new ArrayList<>();
196         Node node = mock(Node.class);
197         uniNodes.add(node);
198
199         PowerMockito.mockStatic(UnimgrUtils.class);
200         PowerMockito.when(UnimgrUtils.getUniNodes(any(DataBroker.class), any(LogicalDatastoreType.class))).thenReturn(uniNodes);
201
202         UniAugmentation uniAugmentation = mock(UniAugmentation.class);
203         when(node.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation);
204         when(uniAugmentation.getIpAddress()).thenReturn(ip);
205         when(node.getKey()).thenReturn(mock(NodeKey.class));
206
207         // test method 1
208         InstanceIdentifier<Node> iid = UnimgrMapper.getUniIid(dataBroker, ip, LogicalDatastoreType.CONFIGURATION);
209         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
210
211         // test method 2
212         iid = UnimgrMapper.getUniIid(dataBroker, ip);
213         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
214     }
215
216     @Test
217     public void testGetUniTopologyIid() {
218         InstanceIdentifier<Topology> iid = UnimgrMapper.getUniTopologyIid();
219         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
220     }
221
222     @Test
223     public void testGetUniTopologyNodeIid() {
224         InstanceIdentifier<Node> iid = UnimgrMapper.getUniTopologyNodeIid();
225         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
226     }
227
228     @Test
229     public void testGetUniNodeIid() throws Exception {
230         NodeId nodeId = mock(NodeId.class);
231         PowerMockito.whenNew(NodeId.class).withAnyArguments().thenReturn(nodeId);
232
233         InstanceIdentifier<Node> iid = UnimgrMapper.getUniNodeIid(nodeId);
234         assertTrue(iid.firstKeyOf(Topology.class).getTopologyId().getValue().contains(UNI_TOPOLOGY_KEY));
235         assertTrue(iid.firstKeyOf(Node.class).getNodeId().equals(nodeId));
236     }
237 }