--- /dev/null
+/*
+ * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.nemo.renderer.cli;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.path.instance.PhysicalLink;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNetwork;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.PhysicalLinks;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.PhysicalPaths;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.paths.PhysicalPath;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.paths.PhysicalPathBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network.rev151010.virtual.networks.VirtualNetwork;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.UserIntentVnMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.user.intent.vn.mapping.IntentVnMappingResult;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.user.intent.vn.mapping.intent.vn.mapping.result.VirtualResource;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.vn.pn.mapping.results.UserVnPnMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.vn.pn.mapping.results.user.vn.pn.mapping.VnPnMappingResult;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalResourceEntityId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalLinkId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalNodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalPortId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.VirtualResourceEntityId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.IntentId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Connection;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.instance.EndNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.VirtualResourceInstance.VirtualResourceType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalLinkId;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.lang.reflect.Modifier;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+/**
+ * Created by zhangmeng on 2015/12/7.
+ */
+public class CliBuilderTest extends TestCase {
+ private CliBuilder cliBuilder;
+ private DataBroker dataBroker;
+
+ @Before
+ public void setUp() throws Exception {
+ dataBroker = mock(DataBroker.class);
+ cliBuilder = new CliBuilder(dataBroker);
+ }
+
+ @Test
+ public void testInit() throws Exception {
+ List<PhysicalNode> physicalNodes = new ArrayList<PhysicalNode>();
+ cliBuilder.init(physicalNodes);
+ }
+
+ @Test//
+ public void testIsFullMeshTopology() throws Exception {
+// Object configElementOnePE ;
+// Object configElementAnother;
+// Class classes = cliBuilder.getClass().getDeclaringClass();
+//
+// int i = classes.getModifiers();
+// String s = Modifier.toString(i);
+// configElementOnePE = classes.getConstructor().newInstance();
+// configElementAnother = classes.getConstructor().newInstance();
+//
+//
+// PhysicalNodeId physicalNodeId = mock(PhysicalNodeId.class);
+// PhysicalNodeId physicalNodeId1 = mock(PhysicalNodeId.class);
+// List<List> peersList = new ArrayList<List>();
+// List peers = new ArrayList();
+//
+// peers.add(configElementOnePE);
+// peers.add(configElementAnother);
+// peersList.add(peers);
+//
+// when(configElementOnePE.getPhysicalNodeId()).thenReturn(physicalNodeId);
+// when(configElementAnother.getPhysicalNodeId()).thenReturn(physicalNodeId1);
+//
+// cliBuilder.isFullMeshTopology(peersList);
+
+// Class classes[] = CliBuilder.class.getClass().getDeclaredClasses();
+// Assert.assertTrue(classes.length == 1);
+// Class c = classes[0];
+
+
+ }
+
+ @Test
+ public void testUpdateCliExecutionSequence() throws Exception {
+ User user = mock(User.class);
+ VirtualNetwork virtualNetwork = mock(VirtualNetwork.class);
+ UserIntentVnMapping userIntentVnMapping = mock(UserIntentVnMapping.class);
+ UserVnPnMapping userVnPnMapping = mock(UserVnPnMapping.class);
+ PhysicalNetwork physicalNetwork = mock(PhysicalNetwork.class);
+ Objects objects = mock(Objects.class);
+ Connection connection = mock(Connection.class);
+ EndNode endnode = mock(EndNode.class);
+ EndNode endnode1 = mock(EndNode.class);
+ NodeId nodeId = mock(NodeId.class);
+ NodeId nodeId1 = mock(NodeId.class);
+ Node node = mock(Node.class);
+ NodeType nodeType = mock(NodeType.class);
+ IntentVnMappingResult intentVnMappingResult = mock(IntentVnMappingResult.class);
+ IntentId intentId = mock(IntentId.class);
+ VirtualResource virtualResource = mock(VirtualResource.class);
+ VnPnMappingResult vnPnMappingResult = mock(VnPnMappingResult.class);
+ VirtualResourceType virtualResourceType = VirtualResource.VirtualResourceType.Vport;
+ VirtualResourceEntityId virtualResourceEntityId = mock(VirtualResourceEntityId.class);
+ PhysicalResourceEntityId physicalResourceEntityId = mock(PhysicalResourceEntityId.class);
+ PhysicalResourceEntityId physicalResourceEntityId_parent = mock(PhysicalResourceEntityId.class);
+ PhysicalPaths physicalPaths = mock(PhysicalPaths.class);
+ PhysicalPath physicalPath = mock(PhysicalPath.class);
+ PhysicalLink physicalLink = mock(PhysicalLink.class);
+ PhysicalLinkId physicalLinkId = mock(PhysicalLinkId.class);
+ PhysicalLinkId physicalLinkId1 = mock(PhysicalLinkId.class);
+ PhysicalLinks physicalLinksInNetwork = mock(PhysicalLinks.class);
+ PhysicalNodeId physicalNodeId_src = mock(PhysicalNodeId.class);
+ PhysicalNodeId physicalNodeId_dst = mock(PhysicalNodeId.class);
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.path.instance.PhysicalLink physicalLink_special =
+ mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.path.instance.PhysicalLink.class);
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.path.instance.PhysicalLink physicalLink_special1 =
+ mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.path.instance.PhysicalLink.class);
+ org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.network.physical.links
+ .PhysicalLink physicalLink_getSrcNodeIdforLinkInPath =
+ mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.network.physical.links
+ .PhysicalLink.class);
+ List<PhysicalLink> physicalLinkList = new ArrayList<PhysicalLink>();
+ List<Connection> connectionList = new ArrayList<Connection>();
+ List<EndNode> endNodeList = new ArrayList<EndNode>();
+ List<Node> nodeList = new ArrayList<Node>();
+ List<IntentVnMappingResult> intentVnMappingResults = new ArrayList<IntentVnMappingResult>();
+ List<VirtualResource> virtualResources = new ArrayList<VirtualResource>();
+ List<VnPnMappingResult> vnPnMappingResults = new ArrayList<VnPnMappingResult>();
+ List<PhysicalPath> physicalPathList = new ArrayList<PhysicalPath>();
+ List< org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.path.instance.PhysicalLink> physicalLinks =
+ new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.path.instance.PhysicalLink>();
+ List<org.opendaylight.yang.gen.v1.urn.opendaylight.params
+ .xml.ns.yang.generic.physical.network.rev151010
+ .physical.network.physical.links.PhysicalLink>
+ physicalLinksInNetworkList =
+ new ArrayList<org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink>();
+
+
+ intentVnMappingResults.add(intentVnMappingResult);
+ connectionList.add(connection);
+ endNodeList.add(endnode);
+ nodeList.add(node);
+ virtualResources.add(virtualResource);
+ vnPnMappingResults.add(vnPnMappingResult);
+ physicalPathList.add(physicalPath);
+ physicalLinkList.add(physicalLink);
+ physicalLinks.add(physicalLink_special);
+ physicalLinks.add(physicalLink_special1);
+ physicalLinksInNetworkList.add(physicalLink_getSrcNodeIdforLinkInPath);
+// endNodeList.add(endnode1);
+
+ when(user.getObjects())
+ .thenReturn(null)//test null
+ .thenReturn(objects)//get into circle
+ .thenReturn(objects)//List<Connection> connectionList = user.getObjects().getConnection();
+ .thenReturn(objects);//used in method "isConnectTwoExtGroup"
+ //test null
+ cliBuilder.updateCliExecutionSequence(user, virtualNetwork, userIntentVnMapping, userVnPnMapping, physicalNetwork);
+ verify(user).getObjects();
+
+ //test not null
+ when(objects.getConnection()).thenReturn(connectionList);
+ when(connection.getEndNode())
+ .thenReturn(endNodeList)
+ .thenReturn(endNodeList);//used in method "isConnectTwoExtGroup"
+ ////get into method "isConnectTwoExtGroup"
+ when(endnode.getNodeId()).thenReturn(nodeId);
+ when(endnode1.getNodeId()).thenReturn(nodeId1);
+ when(objects.getNode()).thenReturn(nodeList);
+ when(node.getNodeId()).thenReturn(nodeId);//if(node.getNodeId().equals(nodeId))
+ when(node.getNodeType()).thenReturn(nodeType);
+ when(nodeType.getValue())
+ .thenReturn(new String("null"))
+ .thenReturn(new String("ext-group"));//test not null
+
+ endNodeList.add(endnode1);
+ Assert.assertTrue(endNodeList.size() == 2);
+ Assert.assertTrue(endNodeList.get(1) == endnode1 && endNodeList.get(0) == endnode);
+
+ //test null
+ cliBuilder.updateCliExecutionSequence(user, virtualNetwork, userIntentVnMapping, userVnPnMapping, physicalNetwork);
+ verify(nodeType).getValue();
+
+ ////get into method "getNodeVnPnMappingResult". args(nodeId,userIntentVnMapping,userVnPnMapping)
+ when(userIntentVnMapping.getIntentVnMappingResult()).thenReturn(intentVnMappingResults);
+ when(intentVnMappingResult.getIntentId()).thenReturn(intentId);
+ when(nodeId.getValue()).thenReturn(new String("test"));
+ when(intentId.getValue()).thenReturn(new String("test"));
+ when(intentVnMappingResult.getVirtualResource()).thenReturn(virtualResources);
+ when(virtualResource.getVirtualResourceType()).thenReturn(virtualResourceType);
+ when(userVnPnMapping.getVnPnMappingResult()).thenReturn(vnPnMappingResults);
+ when(vnPnMappingResult.getVirtualResourceEntityId()).thenReturn(virtualResourceEntityId);
+ when(virtualResource.getVirtualResourceEntityId()).thenReturn(virtualResourceEntityId);
+ when(virtualResourceEntityId.getValue()).thenReturn(new String("test"));
+ //get out of method "getNodeVnPnMappingResult" .... and vnPnMappingResult is returned
+ when(vnPnMappingResult.getPhysicalResourceEntityId()).thenReturn(physicalResourceEntityId);
+ when(physicalResourceEntityId.getValue()).thenReturn(new String("test"));
+ when(vnPnMappingResult.getParentPhysicalResourceEntityId()).thenReturn(physicalResourceEntityId_parent);
+ when(physicalResourceEntityId_parent.getValue()).thenReturn(new String("test"));
+ /////get into method "getNodeVnPnMappingResult" again. args(nodeId,userIntentVnMapping,userVnPnMapping)
+ when(nodeId1.getValue()).thenReturn(new String("test"));
+ /*
+ get out of method "getNodeVnPnMappingResult" .... and vnPnMappingResult is returned
+ no more when and return is needed!
+ */
+ ////get into method "getPhysicalPathforConnection" args (onePhysicalNodeId, otherPhysicalNodeId, physicalNetwork)
+ when(physicalNetwork.getPhysicalPaths()).thenReturn(physicalPaths);
+ when(physicalPaths.getPhysicalPath())
+ .thenReturn(null)
+ .thenReturn(physicalPathList);
+
+ cliBuilder.updateCliExecutionSequence(user, virtualNetwork, userIntentVnMapping, userVnPnMapping, physicalNetwork);
+
+ //////get into method "sortPhysicalLinksOfPhysicalPath" args(physicalPath)
+ when(physicalPath.getPhysicalLink())
+ .thenReturn(physicalLinkList)//if ( physicalPath.getPhysicalLink().isEmpty()|| 1 == physicalPath.getPhysicalLink().size() )
+ .thenReturn(physicalLinkList)
+ .thenReturn(physicalLinks);//back to method "getPhysicalPathforConnection"
+ Assert.assertTrue(physicalLinkList.size() == 1);
+ ////get out of method "sortPhysicalLinksOfPhysicalPath" args(physicalPath)
+ when(physicalLink_special.getLinkId()).thenReturn(physicalLinkId);
+ when(physicalLink_special1.getLinkId()).thenReturn(physicalLinkId1);
+ //////get into method "getSrcNodeIdforLinkInPath"
+ PhysicalLinkId physicalLinkId_getSrcNodeIdforLinkInPath = mock(PhysicalLinkId.class);
+ when(physicalNetwork.getPhysicalLinks()).thenReturn(physicalLinksInNetwork);
+ when(physicalLinksInNetwork.getPhysicalLink()).thenReturn(physicalLinksInNetworkList);
+ when(physicalLink_getSrcNodeIdforLinkInPath.getLinkId()).thenReturn(physicalLinkId_getSrcNodeIdforLinkInPath);
+ when(physicalLinkId_getSrcNodeIdforLinkInPath.getValue()).thenReturn(new String("test"));
+ when(physicalLinkId.getValue()).thenReturn(new String("test"));
+ when(physicalLink_getSrcNodeIdforLinkInPath.getSrcNodeId()).thenReturn(physicalNodeId_src);
+ ////get out of method "getSrcNodeIdforLinkInPath"
+ ////// get into method "getDestNodeIdforLinkInPath"
+ when(physicalLinkId1.getValue()).thenReturn(new String("test"));
+ when(physicalLink_getSrcNodeIdforLinkInPath.getDestNodeId()).thenReturn(physicalNodeId_dst);
+ //// return into method "getPhysicalPathforConnection"
+ when(physicalNodeId_src.getValue()).thenReturn(new String("test1"));
+ when(physicalNodeId_dst.getValue()).thenReturn(new String("test"));
+
+ cliBuilder.updateCliExecutionSequence(user, virtualNetwork, userIntentVnMapping, userVnPnMapping, physicalNetwork);
+
+ }
+
+ @Test
+ public void testClose() throws Exception {
+
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.nemo.renderer.cli;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import com.google.common.util.concurrent.CheckedFuture;
+
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker.DataChangeScope;
+import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNetwork;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.PhysicalNodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network.rev151010.VirtualNetworks;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network.rev151010.virtual.networks.VirtualNetwork;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.virtual.network.rev151010.virtual.networks.VirtualNetworkKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.IntentVnMappingResults;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.VnPnMappingResults;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.UserIntentVnMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.intent.vn.mapping.results.UserIntentVnMappingKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.intent.mapping.result.rev151010.vn.pn.mapping.results.UserVnPnMapping;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.VirtualNetworkId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;
+import com.google.common.util.concurrent.CheckedFuture;
+import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Map;
+import java.util.Set;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+/**
+ * Created by zhangmeng on 2015/12/8.
+ */
+public class CliTriggerTest extends TestCase {
+ private CliTrigger cliTrigger;
+ private DataBroker dataBroker;
+ @Before
+ public void setUp() throws Exception {
+ dataBroker = mock(DataBroker.class);
+ cliTrigger = new CliTrigger(dataBroker);
+ }
+
+ @Test
+ public void testClose() throws Exception {
+ cliTrigger.close();
+ }
+
+ @Test
+ public void testRegisterUserVnPnMappingListener() throws Exception{
+ Class<CliTrigger> class1 = CliTrigger.class;
+ Method method = class1.getDeclaredMethod("registerUserVnPnMappingListener");
+ method.setAccessible(true);
+
+ method.invoke(cliTrigger);
+ }
+
+ @Test
+ public void testGetUser() throws Exception{
+ Class<CliTrigger> class1 = CliTrigger.class;
+ Method method = class1.getDeclaredMethod("getUser", new Class[]{UserId.class});
+ method.setAccessible(true);
+
+ Optional<User> optional = mock(Optional.class);
+ UserId userId = mock(UserId.class);
+ User result;
+ ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+ when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture);
+ when(checkedFuture.get()).thenReturn(optional);
+ when(optional.isPresent())
+ .thenReturn(false)
+ .thenReturn(true);
+ when(optional.get()).thenReturn(mock(User.class));
+
+ result = (User)method.invoke(cliTrigger, userId);
+ Assert.assertTrue(result == null);
+ result = (User)method.invoke(cliTrigger,userId);
+ Assert.assertTrue(result != null);
+ verify(optional).get();
+ verify(optional,times(2)).isPresent();
+ }
+
+ @Test
+ public void testGetVirtualNetwork() throws Exception{
+ Class<CliTrigger> class1 = CliTrigger.class;
+ Method method = class1.getDeclaredMethod("getVirtualNetwork", new Class[]{UserId.class});
+ method.setAccessible(true);
+
+ UserId userId = mock(UserId.class);
+ VirtualNetwork result;
+ ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+ Optional<VirtualNetwork> optional = mock(Optional.class);
+
+ when(userId.getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222"));
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+ when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture);
+ when(checkedFuture.get()).thenReturn(optional);
+ when(optional.isPresent())
+ .thenReturn(false)
+ .thenReturn(true);
+ when(optional.get()).thenReturn(mock(VirtualNetwork.class));
+
+ result = (VirtualNetwork) method.invoke(cliTrigger,userId);
+ Assert.assertTrue(result == null);
+ result = (VirtualNetwork) method.invoke(cliTrigger,userId);
+ Assert.assertTrue(result != null);
+ verify(optional).get();
+ verify(optional,times(2)).isPresent();
+
+ }
+
+ @Test
+ public void testGetUserIntentVnMapping() throws Exception{
+ Class<CliTrigger> class1 = CliTrigger.class;
+ Method method = class1.getDeclaredMethod("getUserIntentVnMapping", new Class[]{UserId.class});
+ method.setAccessible(true);
+
+ UserId userId = mock(UserId.class);
+ UserIntentVnMapping result;
+ ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+ Optional<UserIntentVnMapping> optional = mock(Optional.class);
+
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+ when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture);
+ when(checkedFuture.get()).thenReturn(optional);
+ when(optional.isPresent())
+ .thenReturn(false)
+ .thenReturn(true);
+ when(optional.get()).thenReturn(mock(UserIntentVnMapping.class));
+
+ result = (UserIntentVnMapping) method.invoke(cliTrigger,userId);
+ Assert.assertTrue(result == null);
+// new UserId("00001111-0000-0000-0000-000011112222");
+ result = (UserIntentVnMapping) method.invoke(cliTrigger,userId);
+ Assert.assertTrue(result != null);
+ verify(optional).get();
+ verify(optional,times(2)).isPresent();
+ }
+
+ @Test
+ public void testGetPhysicalNetwork() throws Exception{
+ Class<CliTrigger> class1 = CliTrigger.class;
+ Method method = class1.getDeclaredMethod("getPhysicalNetwork");
+ method.setAccessible(true);
+
+ ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+ Optional<PhysicalNetwork> optional = mock(Optional.class);
+ PhysicalNetwork result;
+
+ when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+ when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(checkedFuture);
+ when(checkedFuture.get()).thenReturn(optional);
+ when(optional.isPresent())
+ .thenReturn(false)
+ .thenReturn(true);
+ when(optional.get()).thenReturn(mock(PhysicalNetwork.class));
+
+ result = (PhysicalNetwork) method.invoke(cliTrigger);
+ Assert.assertTrue(result == null);
+ result = (PhysicalNetwork) method.invoke(cliTrigger);
+ Assert.assertTrue(result != null);
+ verify(optional).get();
+ verify(optional, times(2)).isPresent();
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.nemo.renderer.cli;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.jboss.netty.bootstrap.ClientBootstrap;
+import org.jboss.netty.channel.ChannelFactory;
+import org.jboss.netty.channel.ChannelPipeline;
+import org.jboss.netty.channel.ChannelPipelineFactory;
+import org.jboss.netty.channel.Channels;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
+import org.jboss.netty.handler.codec.string.StringDecoder;
+import org.jboss.netty.handler.codec.string.StringEncoder;
+import org.jboss.netty.channel.*;
+
+import java.lang.reflect.Method;
+import java.net.InetSocketAddress;
+import java.util.concurrent.Executors;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import sun.java2d.pipe.AAShapePipe;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+/**
+ * Created by zhangmeng on 2015/12/8.
+ */
+public class TelnetUtilsTest extends TestCase {
+ private TelnetUtils telnetUtils;
+ @Before
+ public void setUp() throws Exception {
+ telnetUtils = new TelnetUtils();
+ }
+
+ @Test
+ public void testClose() throws Exception {
+ telnetUtils.close();
+ Assert.assertTrue(telnetUtils != null);
+ }
+
+ @Test
+ public void testIsClearOver() throws Exception {
+ Assert.assertTrue(TelnetUtils.isClearOver());
+ }
+
+ @Test
+ public void testSetClearOver() throws Exception {
+ Boolean clearOver = false;
+ TelnetUtils.setClearOver(clearOver);
+ Assert.assertTrue(!TelnetUtils.isClearOver());
+ }
+
+ @Test
+ public void testSet_GetInstanceOfClearCliTemplate() throws Exception {
+// Assert.assertTrue(TelnetUtils.getInstanceOfClearCliTemplate() == null);//test null
+ String instanceOfClearCliTemplate = new String("test");
+ TelnetUtils.setInstanceOfClearCliTemplate(instanceOfClearCliTemplate);
+ Assert.assertTrue(TelnetUtils.getInstanceOfClearCliTemplate().equals("test"));
+ }
+
+ @Test
+ public void testSet_GetInstanceOfNewCliTemplate() throws Exception {
+// Assert.assertEquals(TelnetUtils.getInstanceOfNewCliTemplate(),null);//test null
+ String instanceOfNewCliTemplate = new String("test");
+ TelnetUtils.setInstanceOfNewCliTemplate(instanceOfNewCliTemplate);
+ Assert.assertTrue(TelnetUtils.getInstanceOfNewCliTemplate().equals("test"));
+ }
+
+ @Test
+ public void testSet_GetCurrentDeviceName() throws Exception {
+// Assert.assertTrue(TelnetUtils.getCurrentDeviceName() == null); //test null
+ String currentDeviceName = new String("test");
+ TelnetUtils.setCurrentDeviceName(currentDeviceName);
+ Assert.assertTrue(TelnetUtils.getCurrentDeviceName().equals("test"));
+
+ }
+ @Test
+ public void testSet_GetConfigOver() throws Exception {
+ Assert.assertTrue(!TelnetUtils.isConfigOver()); //test null
+ Boolean configOver = true;
+ TelnetUtils.setConfigOver(configOver);
+ Assert.assertTrue(TelnetUtils.isConfigOver());
+ }
+
+ @Test
+ public void testConfigL3vpnOnDeviceByCli() throws Exception {
+ String deviceName = new String("deviceNamePE1");
+ String clearOldConfigString = new String("clearOldConfigString");
+ String cliExecutionSequences = new String("cliExecutionSequences");
+
+ telnetUtils.configL3vpnOnDeviceByCli(deviceName,clearOldConfigString,cliExecutionSequences);
+
+ Assert.assertTrue(!TelnetUtils.isConfigOver());
+ Assert.assertEquals(TelnetUtils.getInstanceOfClearCliTemplate(), "clearOldConfigString");
+ Assert.assertEquals(TelnetUtils.getInstanceOfNewCliTemplate(),"cliExecutionSequences");
+ Assert.assertEquals(TelnetUtils.getCurrentDeviceName(),"deviceNamePE1");
+ }
+
+ @Test
+ public void testStartConnectAndConfig() throws Exception {
+ Class<TelnetUtils> class1 = TelnetUtils.class;
+ Method method = class1.getDeclaredMethod("startConnectAndConfig",new Class[]{SimpleChannelUpstreamHandler.class});
+ Method method1 = class1.getDeclaredMethod("sendNewConfig");
+ method.setAccessible(true);
+ method1.setAccessible(true);
+
+ SimpleChannelUpstreamHandler simpleChannelUpstreamHandler = mock(SimpleChannelUpstreamHandler.class);
+ Assert.assertEquals(TelnetUtils.getCurrentDeviceName(),"deviceNamePE1");
+ method.invoke(telnetUtils,simpleChannelUpstreamHandler);
+ method1.invoke(telnetUtils);
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.nemo.renderer.cli.physicalnetwork;
+
+import com.google.common.util.concurrent.CheckedFuture;
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey;
+
+import java.lang.reflect.Method;
+
+import static org.mockito.Mockito.*;
+
+/**
+ * Created by zhangmeng on 2015/11/30.
+ */
+public class DataBrokerAdapterTest extends TestCase {
+ private DataBrokerAdapter dataBrokerAdapter;
+ private DataBroker dataBroker;
+ @Override
+ @Before
+ public void setUp() throws Exception {
+ dataBroker = mock(DataBroker.class);
+ dataBrokerAdapter = new DataBrokerAdapter(dataBroker);
+ }
+
+ @Test
+ public void testGetPhysicalHostIdentifier() throws Exception {
+ PhysicalHostKey physicalHostKey = mock(PhysicalHostKey.class);
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("getPhysicalHostIdentifier", new Class[]{PhysicalHostKey.class});
+
+ method.setAccessible(true);
+ Object object =
+ method.invoke(dataBrokerAdapter, new Object[]{physicalHostKey = mock(PhysicalHostKey.class)});
+ Assert.assertTrue(object != null);
+ }
+
+ @Test
+ public void testGetPhysicalNodeIdentifier() throws Exception {
+ PhysicalNodeKey physicalNodeKey = mock(PhysicalNodeKey.class);
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("getPhysicalNodeIdentifier",new Class[]{PhysicalNodeKey.class});
+
+ method.setAccessible(true);
+ Object object = method.invoke(dataBrokerAdapter,physicalNodeKey);
+ Assert.assertTrue(object != null);
+
+ }
+
+ @Test
+ public void testGetPhysicalLinkIdentifier() throws Exception {
+ PhysicalLinkKey physicalLinkKey = mock(PhysicalLinkKey.class);
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("getPhysicalLinkIdentifier",new Class[]{PhysicalLinkKey.class});
+
+ method.setAccessible(true);
+ Object object = method.invoke(dataBrokerAdapter,physicalLinkKey);
+ Assert.assertTrue(object != null);
+ }
+
+ @Test
+ public void testAddPhysicalHost() throws Exception {
+ final PhysicalHost physicalHost = mock(PhysicalHost.class);
+ WriteTransaction writeTransaction = mock(WriteTransaction.class);
+ PhysicalHostKey physicalHostKey = mock(PhysicalHostKey.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("addPhysicalHost",new Class[]{PhysicalHost.class});
+ method.setAccessible(true);
+
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+ when(physicalHost.getKey()).thenReturn(physicalHostKey);
+ when(writeTransaction.submit()).thenReturn(checkedFuture);
+
+ method.invoke(dataBrokerAdapter, physicalHost);
+
+ verify(dataBroker).newWriteOnlyTransaction();
+ verify(physicalHost).getKey();
+ verify(writeTransaction).submit();
+ }
+ @Test
+ public void testAddPhysicalNode() throws Exception {
+ final PhysicalNode physicalNode = mock(PhysicalNode.class);
+ WriteTransaction writeTransaction = mock(WriteTransaction.class);
+ PhysicalNodeKey physicalNodeKey = mock(PhysicalNodeKey.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("addPhysicalNode",new Class[]{PhysicalNode.class});
+ method.setAccessible(true);
+
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+ when(physicalNode.getKey()).thenReturn(physicalNodeKey);
+ when(writeTransaction.submit()).thenReturn(checkedFuture);
+
+// boolean b = false;
+ Assert.assertNotNull(method.invoke(dataBrokerAdapter, physicalNode));
+
+ verify(dataBroker).newWriteOnlyTransaction();
+ verify(physicalNode).getKey();
+ verify(writeTransaction).submit();
+
+ }
+ @Test
+ public void testRemovePhysicalNode() throws Exception {
+ final PhysicalNodeKey nodeKey = mock(PhysicalNodeKey.class);
+ WriteTransaction writeTransaction = mock(WriteTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("removePhysicalNode",new Class[]{PhysicalNodeKey.class});
+ method.setAccessible(true);
+
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+ when(writeTransaction.submit()).thenReturn(checkedFuture);
+
+ Assert.assertNotNull(method.invoke(dataBrokerAdapter, nodeKey));
+
+ verify(dataBroker).newWriteOnlyTransaction();
+ verify(writeTransaction).submit();
+
+ }
+ @Test
+ public void testAddPhysicalLink() throws Exception {
+ final PhysicalLink physicalLink = mock(PhysicalLink.class);
+ WriteTransaction writeTransaction = mock(WriteTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+ PhysicalLinkKey physicalLinkKey = mock(PhysicalLinkKey.class);
+
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("addPhysicalLink",new Class[]{PhysicalLink.class});
+ method.setAccessible(true);
+
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+ when(physicalLink.getKey()).thenReturn(physicalLinkKey);
+ when(writeTransaction.submit()).thenReturn(checkedFuture);
+
+ method.invoke(dataBrokerAdapter, physicalLink);
+
+ verify(dataBroker).newWriteOnlyTransaction();
+ verify(physicalLink).getKey();
+ verify(writeTransaction).submit();
+ }
+
+ @Test
+ public void testRemovePhysicalLink() throws Exception {
+ final PhysicalLinkKey physicalLinkKey = mock(PhysicalLinkKey.class);
+ WriteTransaction writeTransaction = mock(WriteTransaction.class);
+ CheckedFuture checkedFuture = mock(CheckedFuture.class);
+
+ Class<DataBrokerAdapter> class1 = DataBrokerAdapter.class;
+ Method method = class1.getDeclaredMethod("removePhysicalLink", new Class[]{PhysicalLinkKey.class});
+ method.setAccessible(true);
+
+ when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+ when(writeTransaction.submit()).thenReturn(checkedFuture);
+
+ method.invoke(dataBrokerAdapter, physicalLinkKey);
+
+ verify(dataBroker).newWriteOnlyTransaction();
+ verify(writeTransaction).submit();
+
+ }
+
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.nemo.renderer.cli.physicalnetwork;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.HashBasedTable;
+import com.google.common.collect.Table;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.attribute.instance.AttributeValueBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.host.instance.IpAddressesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHost;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.hosts.PhysicalHostKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLink;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.links.PhysicalLinkBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.network.physical.nodes.PhysicalNodeKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPort;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.node.instance.PhysicalPortBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.Attribute;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.physical.port.instance.AttributeKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.xml.crypto.Data;
+import java.io.IOException;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+
+/**
+ * Created by zhangmeng on 2015/12/8.
+ */
+public class PhysicalResourceLoaderTest extends TestCase {
+ private DataBroker dataBroker;
+ private PhysicalResourceLoader physicalResourceLoader;
+ @Before
+ public void setUp() throws Exception {
+ dataBroker = mock(DataBroker.class);
+ physicalResourceLoader = new PhysicalResourceLoader(dataBroker);
+ }
+
+ @Test
+ public void testGetPhysicalNode() throws Exception {
+ PhysicalNodeId physicalNodeId = mock(PhysicalNodeId.class);
+ PhysicalNode physicalNode ;
+
+ Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("getPhysicalNode",new Class[]{PhysicalNodeId.class});
+ method.setAccessible(true);
+
+ physicalNode = (PhysicalNode) method.invoke(physicalResourceLoader,physicalNodeId);
+
+ Assert.assertTrue( physicalNode == null);
+ }
+
+ @Test
+ public void testGetPhysicalPort() throws Exception {
+ PhysicalPortId physicalPortId = mock(PhysicalPortId.class);
+ PhysicalPort physicalPort;
+
+ Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("getPhysicalPort",new Class[]{PhysicalPortId.class});
+ method.setAccessible(true);
+
+ physicalPort = (PhysicalPort) method.invoke(physicalResourceLoader,physicalPortId);
+
+ Assert.assertTrue(physicalPort == null);
+ }
+
+ @Test
+ public void testGetPhysicalLink() throws Exception {
+ PhysicalLinkId physicalLinkId = mock(PhysicalLinkId.class);
+ PhysicalLink physicalLink;
+
+ Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("getPhysicalLink",new Class[]{PhysicalLinkId.class});
+ method.setAccessible(true);
+
+ physicalLink = (PhysicalLink) method.invoke(physicalResourceLoader,physicalLinkId);
+
+ Assert.assertTrue(physicalLink == null);
+ }
+
+ @Test
+ public void testClose() throws Exception {
+ physicalResourceLoader.close();
+ }
+
+ @Test
+ public void testBuildHosts()throws Exception{
+ Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("buildHosts", new Class[]{JsonNode.class});
+ method.setAccessible(true);
+
+ JsonNode hostsNode = mock(JsonNode.class);
+ JsonNode hosts = mock(JsonNode.class);
+ JsonNode host = mock(JsonNode.class);
+ JsonNode host_temp = mock(JsonNode.class);
+ JsonNode host_temp1 = mock(JsonNode.class);
+ JsonNode ipaddrs = mock(JsonNode.class);
+ JsonNode ipaddr = mock(JsonNode.class);
+ List<PhysicalHost> list;
+
+ when(hostsNode.path(any(String.class))).thenReturn(hosts);
+ when(hosts.size()).thenReturn(1);
+ when(hosts.get(any(Integer.class))).thenReturn(host);
+ //get into method "buildhost"
+ when(host.get(any(String.class))).thenReturn(host_temp);
+ when(host_temp.asText())
+ .thenReturn(new String("00001111-0000-0000-0000-000011112222")) //HOST_ID
+ .thenReturn(new String("hostName")) //HOST_NAME
+ .thenReturn(new String("00:11:22:33:44:55")) //MAC_ADDRESS
+ .thenReturn(new String("nodeId")) //NODE_ID
+ .thenReturn(new String("connetionId"));//PhysicalPortId
+ when(host.path(any(String.class))).thenReturn(ipaddrs);
+ when(ipaddrs.size()).thenReturn(1);
+ when(ipaddrs.get(any(Integer.class))).thenReturn(ipaddr);
+ when(ipaddr.get(any(String.class))).thenReturn(host_temp1);
+ when(host_temp1.asText()).thenReturn(new String("192.168.1.1"));//ipv4_address
+
+
+ list = (List<PhysicalHost>) method.invoke(physicalResourceLoader,hostsNode);
+ Assert.assertTrue(list.size() == 1);
+ }
+
+ @Test
+ public void testBuildPortAttributes()throws Exception{
+ Class<PhysicalResourceLoader>class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("buildPortAttributes",new Class[]{JsonNode.class});
+ method.setAccessible(true);
+
+ List<Attribute> attributeList;
+ JsonNode attributes = mock(JsonNode.class);
+ JsonNode portAttribute = mock(JsonNode.class);
+ JsonNode jsonNode_temp = mock(JsonNode.class);
+
+ when(attributes.size()).thenReturn(1);
+ when(attributes.get(any(Integer.class))).thenReturn(portAttribute);
+ //get into method "buildPortAttribute"
+ when(portAttribute.path(any(String.class))).thenReturn(jsonNode_temp);
+ when(jsonNode_temp.asText())
+ .thenReturn(new String(""))//branch null
+ .thenReturn(new String("zm"));
+ attributeList = (List<Attribute>)method.invoke(physicalResourceLoader,attributes);
+ Assert.assertTrue(attributeList.size() == 0);
+ //new AttributeName("zm");
+ attributeList = (List<Attribute>)method.invoke(physicalResourceLoader,attributes);
+ Assert.assertTrue(attributeList.size() == 1);
+ verify(portAttribute,times(3)).path(any(String.class));
+ verify(jsonNode_temp,times(3)).asText();
+ }
+
+ @Test
+ public void testBuildLinks()throws Exception{
+ List<PhysicalLink> list;
+ Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("buildLinks", new Class[]{JsonNode.class});
+ method.setAccessible(true);
+
+ JsonNode linksRoot = mock(JsonNode.class);
+ JsonNode links = mock(JsonNode.class);
+ JsonNode link = mock(JsonNode.class);
+ JsonNode link_temp_buildlink = mock(JsonNode.class);
+
+ when(linksRoot.path(any(String.class))).thenReturn(links);
+ when(links.size()).thenReturn(1);
+ when(links.get(any(Integer.class))).thenReturn(link);
+ //get into method "build link" args(link)
+ when(link.get(any(String.class))).thenReturn(link_temp_buildlink);
+ when(link_temp_buildlink.asText())
+ .thenReturn(new String("1"))//new PhysicalLinkId(strLinkId)
+ .thenReturn(new String("2"))//new PhysicalNodeId
+ .thenReturn(new String("3"))//new PhysicalPortId
+ .thenReturn(new String("4"))//new PhysicalNodeId
+ .thenReturn(new String("5"))//new PhysicalPortId
+ .thenReturn(new String(""));//linkNode.get("link-bandwidth").asText().equals("")
+ when(link_temp_buildlink.asLong()).thenReturn((long) 1);
+
+ list = (List<PhysicalLink>)method.invoke(physicalResourceLoader,linksRoot);
+ Assert.assertTrue(list.size() == 1);
+ verify(link_temp_buildlink,times(6)).asText();
+ }
+
+ @Test
+ public void testBuildNodes()throws Exception{
+ Class<PhysicalResourceLoader> class1 = PhysicalResourceLoader.class;
+ Method method = class1.getDeclaredMethod("buildNodes", new Class[]{JsonNode.class});
+ method.setAccessible(true);
+
+ List<PhysicalNode> result = new ArrayList<PhysicalNode>();
+ JsonNode nodesRoot = mock(JsonNode.class);
+ JsonNode nodes = mock(JsonNode.class);
+ JsonNode node = mock(JsonNode.class);
+ JsonNode node_temp_buildnode = mock(JsonNode.class);
+ JsonNode ports = mock(JsonNode.class);
+ JsonNode attributes = mock(JsonNode.class);
+ JsonNode port = mock(JsonNode.class);
+ JsonNode port_temp_buildport = mock(JsonNode.class);
+ JsonNode portAttributes = mock(JsonNode.class);
+ JsonNode portAttribute = mock(JsonNode.class);
+ JsonNode port_temp_buildPortAttribute = mock(JsonNode.class);
+ JsonNode portAttribute_father = mock(JsonNode.class);
+ JsonNode attribute_temp_father = mock(JsonNode.class);
+
+ when(nodesRoot.path(any(String.class))).thenReturn(nodes);
+ when(nodes.size()).thenReturn(1);
+ when(nodes.get(any(Integer.class))).thenReturn(node);
+ //get into method "build Node"
+ when(node.get(any(String.class))).thenReturn(node_temp_buildnode);
+ when(node_temp_buildnode.asText())
+ .thenReturn(new String(""))//test null
+ .thenReturn(new String("test"))//node id
+ .thenReturn(new String("switch"));// node type
+ result = (List<PhysicalNode>) method.invoke(physicalResourceLoader,nodesRoot);
+ Assert.assertTrue(result.size() == 0);
+ verify(node_temp_buildnode).asText();
+ when(node.path(any(String.class)))
+ .thenReturn(ports)//get into method "build ports"
+ .thenReturn(attributes); //get into method "build attributes"
+ ////get into method"build ports"
+ when(ports.size()).thenReturn(1);
+ when(ports.get(any(Integer.class))).thenReturn(port);
+ //////get into method "build port"
+ when(port.get(any(String.class))).thenReturn(port_temp_buildport);
+ when(port_temp_buildport.asText())
+ .thenReturn(new String("test"))//port id
+ .thenReturn(new String("external"))//port type
+ .thenReturn(new String("00:11:22:33:44:55"))//if(!(port.get("port-mac-address").asText().equals(""))) get in
+ .thenReturn(new String("00:11:22:33:44:55"))//mac address
+ .thenReturn(new String(""));// if(port.get("bandwidth").asText().equals("")) get in
+ when(port.path(any(String.class))).thenReturn(portAttributes);
+ ////////get into method "buildPortAttributes" args(portAttributes)
+ when(portAttributes.size()).thenReturn(1);
+ when(portAttributes.get(any(Integer.class))).thenReturn(portAttribute);
+ ////////// get into method "buildPortAttribute" args(portAttribute)
+ when(portAttribute.path(any(String.class))).thenReturn(port_temp_buildPortAttribute);
+ when(port_temp_buildPortAttribute.asText())
+ .thenReturn(new String("test"))//ATTRIBUTE_NAME
+ .thenReturn(new String("test"));//ATTRIBUTE_VALUE
+ ////return to method "buildnode" and get into method "..............buildNodeAttributes"
+ when(attributes.size()).thenReturn(1);
+ when(attributes.get(any(Integer.class))).thenReturn(portAttribute_father);
+ //////get into method "..............buildNodeAttribute"
+ when(portAttribute_father.path(any(String.class))).thenReturn(attribute_temp_father);
+ when(attribute_temp_father.asText())
+ .thenReturn(new String("test"))//ATTRIBUTE_NAME
+ .thenReturn(new String("test"));//ATTRIBUTE_VALUE
+ result = (List<PhysicalNode>) method.invoke(physicalResourceLoader,nodesRoot); //return empty list
+ Assert.assertTrue(result.size() == 1);
+ verify(port_temp_buildport,times(5)).asText();
+ verify(attribute_temp_father,times(2)).asText();
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.nemo.renderer.cli.physicalnetwork;
+
+import org.junit.Assert;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalNodeInstance;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.generic.physical.network.rev151010.PhysicalPortInstance;
+
+import static org.mockito.Mockito.*;
+
+/**
+ * Created by zhangmeng on 2015/11/30.
+ */
+public class UtilsTest extends Utils {
+
+ @Test
+ public void testGetNodeType() throws Exception {
+ String strType = "switch";
+ Assert.assertTrue(Utils.getNodeType(strType) == PhysicalNodeInstance.NodeType.Switch);
+ strType = "router";
+ Assert.assertTrue(Utils.getNodeType(strType) == PhysicalNodeInstance.NodeType.Router);
+ strType = "firewall";
+ Assert.assertTrue(Utils.getNodeType(strType) == PhysicalNodeInstance.NodeType.Firewall);
+ strType = "loadbalancer";
+ Assert.assertTrue(Utils.getNodeType(strType) == PhysicalNodeInstance.NodeType.Loadbalancer);
+ strType = "test";
+ Assert.assertTrue(Utils.getNodeType(strType) == null);
+ }
+
+ @Test
+ public void testGetPortType() throws Exception {
+ String strType = "external";
+ Assert.assertTrue(Utils.getPortType(strType) == PhysicalPortInstance.PortType.External);
+ strType = "test";
+ Assert.assertTrue(Utils.getPortType(strType) == PhysicalPortInstance.PortType.Internal);
+ }
+
+ @Test
+ public void testReadFile() throws Exception {
+ String Path = new String(".");
+ Assert.assertNotNull(Utils.readFile(Path));
+ Path = "./test";
+ Assert.assertNotNull(Utils.readFile(Path));
+ Assert.assertTrue(Utils.readFile(Path) == "");
+ }
+}
\ No newline at end of file