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