From d39afe5d88faf50e466be5ef1e217f35425ab881 Mon Sep 17 00:00:00 2001 From: melserngawy Date: Thu, 21 Jan 2016 17:10:31 -0500 Subject: [PATCH] Unit test Unimgr Utilis Change-Id: Ie359ea46888324cf8ec7f0ee63be7bb942e8be9f Signed-off-by: melserngawy --- .../unimgr/impl/UnimgrUtilsTest.java | 188 ++++++++++++++++-- 1 file changed, 171 insertions(+), 17 deletions(-) diff --git a/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrUtilsTest.java b/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrUtilsTest.java index 9c85ad3b..b5afd736 100644 --- a/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrUtilsTest.java +++ b/impl/src/test/java/org/opendaylight/unimgr/impl/UnimgrUtilsTest.java @@ -2,6 +2,7 @@ package org.opendaylight.unimgr.impl; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.mock; @@ -30,6 +31,8 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.api.support.membermodification.MemberMatcher; import org.powermock.api.support.membermodification.MemberModifier; +import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -556,51 +559,202 @@ public class UnimgrUtilsTest { @Test public void testFindUniNode() { - //TODO + DataBroker dataBroker = mock(DataBroker.class); + IpAddress ipAddress = mock(IpAddress.class); + UniAugmentation uniAugmentation = mock(UniAugmentation.class); + List uniNodes = new ArrayList(); + Node nd = mock(Node.class); + uniNodes.add(nd); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "getUniNodes", DataBroker.class)); + when(UnimgrUtils.getUniNodes(any(DataBroker.class))).thenReturn(uniNodes); + when(nd.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); + when(uniAugmentation.getIpAddress()).thenReturn(ipAddress); + Optional optNode = UnimgrUtils.findUniNode(dataBroker, ipAddress); + assertNotNull(optNode); + assertTrue(optNode.isPresent()); + uniNodes.remove(0); + optNode = UnimgrUtils.findUniNode(dataBroker, ipAddress); + assertTrue(optNode.absent() == Optional.absent()); } @Test public void testGetConnectionInfo() { - //TODO + DataBroker dataBroker = mock(DataBroker.class); + InstanceIdentifier nodeIid = PowerMockito.mock(InstanceIdentifier.class); + NodeId ovsdbNodeId = mock(NodeId.class); + Optional optNode = mock(Optional.class); + Node node = mock(Node.class); + OvsdbNodeAugmentation ovsdbNodeAugmentation = mock(OvsdbNodeAugmentation.class); + ConnectionInfo connectionInfo = mock(ConnectionInfo.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getOvsdbNodeIid", NodeId.class)); + when(UnimgrMapper.getOvsdbNodeIid(any(NodeId.class))).thenReturn(nodeIid); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "readNode", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.readNode(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(optNode); + when(optNode.isPresent()).thenReturn(true); + when(optNode.get()).thenReturn(node); + when(node.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsdbNodeAugmentation); + when(ovsdbNodeAugmentation.getConnectionInfo()).thenReturn(connectionInfo); + ConnectionInfo expectedConnInfo = UnimgrUtils.getConnectionInfo(dataBroker, ovsdbNodeId); + assertNotNull(expectedConnInfo); + assertEquals(expectedConnInfo, connectionInfo); } @Test public void testGetEvcLinks() { - //TODO + Link link = mock(Link.class); + List lnkList = new ArrayList(); + lnkList.add(link); + Topology topology = mock (Topology.class); + DataBroker dataBroker = mock(DataBroker.class); + EvcAugmentation evcAugmentation = mock(EvcAugmentation.class, Mockito.RETURNS_MOCKS); + InstanceIdentifier topologyInstanceIdentifier = mock(InstanceIdentifier.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getEvcTopologyIid")); + when(UnimgrMapper.getEvcTopologyIid()).thenReturn(topologyInstanceIdentifier); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "read", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(topology); + when(topology.getLink()).thenReturn(lnkList); + when(link.getAugmentation(EvcAugmentation.class)).thenReturn(evcAugmentation); + List expectedListLink = UnimgrUtils.getEvcLinks(dataBroker); + assertNotNull(expectedListLink); + assertEquals(expectedListLink.get(0), link); } @Test public void testGetLocalIp() { - //TODO + String ip = ""; + try { + ip = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + ip = "127.0.0.1"; + } + IpAddress ipAddress = UnimgrUtils.getLocalIp(); + assertNotNull(ipAddress); + String expectedIp = new String(ipAddress.getValue()); + assertTrue(expectedIp.equals(ip)); } @Test public void testGetOvsdbNodes() { - //TODO + Node node = mock(Node.class); + List ndList = new ArrayList(); + ndList.add(node); + Topology topology = mock (Topology.class); + DataBroker dataBroker = mock(DataBroker.class); + OvsdbNodeAugmentation ovsNdAugmentation = mock(OvsdbNodeAugmentation.class, Mockito.RETURNS_MOCKS); + InstanceIdentifier topologyInstanceIdentifier = mock(InstanceIdentifier.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getOvsdbTopologyIid")); + when(UnimgrMapper.getOvsdbTopologyIid()).thenReturn(topologyInstanceIdentifier); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "read", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(topology); + when(topology.getNode()).thenReturn(ndList); + when(node.getAugmentation(OvsdbNodeAugmentation.class)).thenReturn(ovsNdAugmentation); + List expectedListNnList = UnimgrUtils.getOvsdbNodes(dataBroker); + assertNotNull(expectedListNnList); + assertEquals(expectedListNnList.get(0), node); } - /* - * This test for 2 functions with the - * same name that take different parameters. - */ @Test public void testGetUniNodes() { - //TODO + Node node = mock(Node.class); + List ndList = new ArrayList(); + ndList.add(node); + Topology topology = mock (Topology.class); + DataBroker dataBroker = mock(DataBroker.class); + UniAugmentation uniAugmentation = mock(UniAugmentation.class); + InstanceIdentifier topologyInstanceIdentifier = mock(InstanceIdentifier.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getUniTopologyIid")); + when(UnimgrMapper.getUniTopologyIid()).thenReturn(topologyInstanceIdentifier); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "read", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(topology); + when(topology.getNode()).thenReturn(ndList); + when(node.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); + List expectedListNnList = UnimgrUtils.getUniNodes(dataBroker); + assertNotNull(expectedListNnList); + assertEquals(expectedListNnList, ndList); + } + + @Test + public void testGetUniNodes2() { + Node node = mock(Node.class); + List ndList = new ArrayList(); + ndList.add(node); + Topology topology = mock (Topology.class); + DataBroker dataBroker = mock(DataBroker.class); + UniAugmentation uniAugmentation = mock(UniAugmentation.class); + InstanceIdentifier topologyInstanceIdentifier = mock(InstanceIdentifier.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getUniTopologyIid")); + when(UnimgrMapper.getUniTopologyIid()).thenReturn(topologyInstanceIdentifier); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "read", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(topology); + when(topology.getNode()).thenReturn(ndList); + when(node.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); + List expectedListNnList = UnimgrUtils.getUniNodes(dataBroker, LogicalDatastoreType.OPERATIONAL); + assertNotNull(expectedListNnList); + assertEquals(expectedListNnList, ndList); } @Test public void testGetUnis() { - //TODO + Node node = mock(Node.class); + List ndList = new ArrayList(); + ndList.add(node); + Topology topology = mock (Topology.class); + DataBroker dataBroker = mock(DataBroker.class); + UniAugmentation uniAugmentation = mock(UniAugmentation.class); + InstanceIdentifier topologyInstanceIdentifier = mock(InstanceIdentifier.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getUniTopologyIid")); + when(UnimgrMapper.getUniTopologyIid()).thenReturn(topologyInstanceIdentifier); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "read", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(topology); + when(topology.getNode()).thenReturn(ndList); + when(node.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); + List expectedListUni = UnimgrUtils.getUnis(dataBroker, LogicalDatastoreType.CONFIGURATION); + assertNotNull(expectedListUni); + assertEquals(expectedListUni.iterator().next(), uniAugmentation); } @Test public void testGetUni() { - //TODO - } - - @Test - public void testRead() { - //TODO + Node node = mock(Node.class); + List ndList = new ArrayList(); + ndList.add(node); + Topology topology = mock (Topology.class); + DataBroker dataBroker = mock(DataBroker.class); + UniAugmentation uniAugmentation = mock(UniAugmentation.class); + IpAddress ipAddreDest = new IpAddress("10.10.0.2".toCharArray()); + InstanceIdentifier topologyInstanceIdentifier = mock(InstanceIdentifier.class); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getUniTopologyIid")); + when(UnimgrMapper.getUniTopologyIid()).thenReturn(topologyInstanceIdentifier); + PowerMockito.suppress(MemberMatcher.method(UnimgrUtils.class, "read", DataBroker.class, LogicalDatastoreType.class, InstanceIdentifier.class)); + when(UnimgrUtils.read(any(DataBroker.class), any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(topology); + when(topology.getNode()).thenReturn(ndList); + when(node.getAugmentation(UniAugmentation.class)).thenReturn(uniAugmentation); + when(uniAugmentation.getIpAddress()).thenReturn(ipAddreDest); + PowerMockito.suppress(MemberMatcher.method(UnimgrMapper.class, "getUniTopologyIid")); + when(UnimgrMapper.getUniTopologyIid()).thenReturn(topologyInstanceIdentifier); + UniAugmentation expectedUniAug = UnimgrUtils.getUni(dataBroker, LogicalDatastoreType.CONFIGURATION, ipAddreDest); + assertNotNull(expectedUniAug); + assertEquals(expectedUniAug, uniAugmentation); + } + + @Test + public void testRead() throws ReadFailedException { + DataBroker dataBroker = mock(DataBroker.class); + InstanceIdentifier nodeIid = PowerMockito.mock(InstanceIdentifier.class); + ReadOnlyTransaction transaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(transaction); + Optional optionalDataObject = mock(Optional.class); + CheckedFuture, ReadFailedException> future = mock(CheckedFuture.class); + Node nd = mock(Node.class); + when(transaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(future); + when(future.checkedGet()).thenReturn(optionalDataObject); + when(optionalDataObject.isPresent()).thenReturn(true); + when(optionalDataObject.get()).thenReturn(nd); + Node expectedNode = UnimgrUtils.read(dataBroker, LogicalDatastoreType.CONFIGURATION, nodeIid); + verify(transaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class)); + verify(transaction).close(); + assertNotNull(expectedNode); + assertEquals(expectedNode, nd); } @SuppressWarnings("unchecked") -- 2.36.6