From d856837f50143131b0d5d827336bed1c9559e30a Mon Sep 17 00:00:00 2001 From: saomenmen Date: Sat, 26 Dec 2015 17:10:29 +0800 Subject: [PATCH] Modified and added partical test files Change-Id: I65129cb6fbfe080debb6a6224d9f7b4590d57ccd Signed-off-by: saomenmen --- .../updateintent/GetDefinitionsTest.java | 26 + .../updateintent/UpdateConnectionTest.java | 377 ++++++++++++++ .../updateintent/UpdateFlowTest.java | 292 +++++++++++ .../updateintent/UpdateIntentTest.java | 53 +- .../updateintent/UpdateNodeTest.java | 473 ++++++++++++++++++ 5 files changed, 1220 insertions(+), 1 deletion(-) create mode 100644 nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateConnectionTest.java create mode 100644 nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateFlowTest.java create mode 100644 nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateNodeTest.java diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitionsTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitionsTest.java index 4b065ab..820cce8 100644 --- a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitionsTest.java +++ b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/GetDefinitionsTest.java @@ -16,6 +16,8 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.engine.common.rev151010.PhysicalHostName; +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.nemo.common.rev151010.*; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions; import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.MatchItemDefinitions; @@ -226,4 +228,28 @@ public class GetDefinitionsTest { } + + @Test + public void testGetPhysicalHost() throws Exception { + Map map1 = new HashMap(); + PhysicalHostName physicalHostName = mock(PhysicalHostName.class); + PhysicalHost physicalHost = mock(PhysicalHost.class); + map1.put(physicalHostName,physicalHost); + + List physicalHostList = new ArrayList(); + physicalHostList.add(physicalHost); + + Class class_1 = GetDefinitions.class; + Field field = class_1.getDeclaredField("physicalHostList"); + field.setAccessible(true); + + field.set(getDefinitions, physicalHostList); + CheckedFuture physicalHostsFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(physicalHostsFuture); + when(physicalHost.getHostName()).thenReturn(physicalHostName); + + Assert.assertEquals(getDefinitions.getPhysicalHost(), map1); + } } \ No newline at end of file diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateConnectionTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateConnectionTest.java new file mode 100644 index 0000000..aadf75a --- /dev/null +++ b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateConnectionTest.java @@ -0,0 +1,377 @@ +/* + * 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.user.vnspacemanager.structurestyle.updateintent; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.nemo.user.tenantmanager.TenantManage; +import com.google.common.base.Optional; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionId; + +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.nemo.user.tenantmanager.TenantManage; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionType; +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.user.intent.Objects; +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.ConnectionBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.ConnectionKey; +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.intent.rev151010.users.UserKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.ConnectionDefinitions; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.connection.definitions.ConnectionDefinition; +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.nemo.object.rev151010.connection.instance.Property; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.PropertyName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.ConnectionName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.NodeId; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import java.util.LinkedList; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import java.util.*; +import java.util.List; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; + +import com.google.common.util.concurrent.CheckedFuture; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.doNothing; + +/** + * Created by ldzd11 on 2015/12/21. + */ +public class UpdateConnectionTest { + + UpdateConnection updateConnection; + + private DataBroker dataBroker; + private TenantManage tenantManage; + private List propertyList; + private PropertyName propertyName; + private PropertyName propertyName2; + private Property property; + private Connection connection; + private Connection connection2; + private NodeId nodeId; + private NodeId nodeId2; + + + private List connectionList; + private ConnectionType connectionType; + private ConnectionType connectionType2; + private ConnectionName connectionName; + private ConnectionName connectionName2; + private User user; + private UserId userId; + private ConnectionId connectionId; + private Objects objects; + private EndNode endNode; + private List endNodeList; + private EndNode endNode2; + private List endNodeList2; + private Node node; + private Node node2; + + + + private PropertyDefinition propertyDefinition; + private List propertyDefinitionList; + @Before + public void setUp() throws Exception { + dataBroker = mock(DataBroker.class); + tenantManage = mock(TenantManage.class); + + property = mock(Property.class); + propertyList = new ArrayList(1); + propertyList.add(property); + + propertyName = mock(PropertyName.class); + propertyName2 = mock(PropertyName.class); + + connectionName = mock(ConnectionName.class); + connectionName2 = mock(ConnectionName.class); + + propertyDefinition = mock(PropertyDefinition.class); + propertyDefinitionList = new ArrayList(1); + propertyDefinitionList.add(propertyDefinition); + + connectionType = mock(ConnectionType.class); + connectionType2 = mock(ConnectionType.class); + connection = mock(Connection.class); + connection2 = mock(Connection.class); + connectionList = new ArrayList(1); + connectionList.add(connection); + objects = mock(Objects.class); + + user = mock(User.class); + userId = mock(UserId.class); + nodeId = mock(NodeId.class); + nodeId2 = mock(NodeId.class); + + endNode = mock(EndNode.class); + endNodeList = new ArrayList(); + endNodeList.add(endNode); + endNode2 = mock(EndNode.class); + endNodeList2 = new ArrayList(); + endNodeList2.add(endNode2); + node = mock(Node.class); + node2 = mock(Node.class); + + + connectionId = mock(ConnectionId.class); + updateConnection = new UpdateConnection(dataBroker,tenantManage); + + } + + @Test + public void testConnectionHandling() throws Exception { + + CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture); + + List connectionDefinitionList = new ArrayList(); + List connectionDefinitionListnull = new ArrayList(); + ConnectionDefinition connectionDefinition = mock(ConnectionDefinition.class); + connectionDefinitionList.add(connectionDefinition); + when(connectionDefinition.getConnectionType()).thenReturn(connectionType); + + + Class class1 = UpdateConnection.class; + Class class2 = GetDefinitions.class; + Field field = class1.getDeclaredField("getDefinitions"); + field.setAccessible(true); + Field field1 = class2.getDeclaredField("connectionDefinitionsList"); + field1.setAccessible(true); + + field1.set(field.get(updateConnection), connectionDefinitionList); + when(connection.getConnectionType()).thenReturn(connectionType); + when(connection.getProperty()).thenReturn(null); + when(connectionDefinition.getPropertyDefinition()).thenReturn(null); + + when(tenantManage.getConnection(userId)).thenReturn(null); + when(tenantManage.getConnectionDataStore(userId)).thenReturn(null); + when(connection.getEndNode()).thenReturn(endNodeList); + Map map2 = new HashMap(); + map2.put(nodeId, node2); + when(tenantManage.getNode(userId)).thenReturn(map2); + when(endNode.getNodeId()).thenReturn(nodeId); + when(tenantManage.getNodeDataStore(userId)).thenReturn(map2); + when(connection.getConnectionId()).thenReturn(connectionId); + doNothing().when(tenantManage).setConnection(any(UserId.class),any(ConnectionId.class),any(Connection.class)); + Assert.assertEquals(updateConnection.ConnectionHandling(userId,connection),null); + + + + /* when(tenantManage.getUser()).thenReturn(null); + when(connection.getEndNode()).thenReturn(null); + WriteTransaction writeTransaction = mock(WriteTransaction.class); + when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); + ConnectionId connectionId = mock(ConnectionId.class); + when(connection.getConnectionId()).thenReturn(connectionId); + + when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction); + CheckedFuture f; + f=mock(CheckedFuture.class); + when(writeTransaction.submit()).thenReturn(f); + Assert.assertEquals(updateConnection.ConnectionHandling(userId,connection),null); + */ + + + + + + + + + } + + @Test + public void testcheckInstance() throws Exception { + Class class1 = UpdateConnection.class; + Method method = class1.getDeclaredMethod("checkInstance", new Class[]{UserId.class, Connection.class}); + method.setAccessible(true); + + Map map1 = new HashMap(); + map1.put(connectionId, connection2); + + when(tenantManage.getConnection(userId)).thenReturn(map1); + when(connection.getConnectionId()).thenReturn(connectionId); + when(connection2.getConnectionName()).thenReturn(connectionName2); + when(connection.getConnectionName()).thenReturn(connectionName); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The connection name should not be changed."); + + when(connection2.getConnectionName()).thenReturn(connectionName2); + when(connection.getConnectionName()).thenReturn(connectionName2); + when(connection2.getConnectionType()).thenReturn(connectionType2); + when(connection.getConnectionType()).thenReturn(connectionType); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The connection type should not be changed."); + + when(connection2.getConnectionName()).thenReturn(connectionName2); + when(connection.getConnectionName()).thenReturn(connectionName2); + when(connection2.getConnectionType()).thenReturn(connectionType2); + when(connection.getConnectionType()).thenReturn(connectionType2); + when(connection2.getEndNode()).thenReturn(endNodeList2); + when(connection.getEndNode()).thenReturn(endNodeList); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The connection end node should not be changed."); + + when(tenantManage.getConnection(userId)).thenReturn(null); + when(tenantManage.getConnectionDataStore(userId)).thenReturn(map1); + when(connection.getConnectionId()).thenReturn(connectionId); + when(connection2.getConnectionName()).thenReturn(connectionName2); + when(connection.getConnectionName()).thenReturn(connectionName); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The connection name should not be changed."); + + when(connection2.getConnectionName()).thenReturn(connectionName2); + when(connection.getConnectionName()).thenReturn(connectionName2); + when(connection2.getConnectionType()).thenReturn(connectionType2); + when(connection.getConnectionType()).thenReturn(connectionType); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The connection type should not be changed."); + + when(connection2.getConnectionName()).thenReturn(connectionName2); + when(connection.getConnectionName()).thenReturn(connectionName2); + when(connection2.getConnectionType()).thenReturn(connectionType2); + when(connection.getConnectionType()).thenReturn(connectionType2); + when(connection2.getEndNode()).thenReturn(endNodeList2); + when(connection.getEndNode()).thenReturn(endNodeList); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The connection end node should not be changed."); + + when(tenantManage.getConnection(userId)).thenReturn(null); + when(tenantManage.getConnectionDataStore(userId)).thenReturn(null); + when(connection.getEndNode()).thenReturn(endNodeList); + + Map map2 = new HashMap(); + map2.put(nodeId, node2); + when(tenantManage.getNode(userId)).thenReturn(map2); + when(endNode.getNodeId()).thenReturn(nodeId); + when(tenantManage.getNodeDataStore(userId)).thenReturn(map2); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), null); + + when(tenantManage.getConnection(userId)).thenReturn(null); + when(tenantManage.getConnectionDataStore(userId)).thenReturn(null); + when(connection.getEndNode()).thenReturn(endNodeList); + when(tenantManage.getNode(userId)).thenReturn(null); + when(tenantManage.getNodeDataStore(userId)).thenReturn(null); + when(endNode.getNodeId()).thenReturn(nodeId); + when(endNode.getNodeId().getValue()).thenReturn(new String("nodeId")); + Assert.assertEquals(method.invoke(updateConnection, userId, connection), "The endnode "+ endNode.getNodeId().getValue() +" is not exist;"); + } + + @Test + public void testcheckDefinition() throws Exception { + Class class3 = UpdateConnection.class; + Method method = class3.getDeclaredMethod("checkDefinition",new Class[]{Connection.class}); + method.setAccessible(true); + + CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture); + + List connectionDefinitionList = new ArrayList(); + List connectionDefinitionListnull = new ArrayList(); + ConnectionDefinition connectionDefinition = mock(ConnectionDefinition.class); + connectionDefinitionList.add(connectionDefinition); + when(connectionDefinition.getConnectionType()).thenReturn(connectionType); + + + Class class1 = UpdateConnection.class; + Class class2 = GetDefinitions.class; + Field field = class1.getDeclaredField("getDefinitions"); + field.setAccessible(true); + Field field1 = class2.getDeclaredField("connectionDefinitionsList"); + field1.setAccessible(true); + + + // when(connection.getConnectionType()).thenReturn(connectionType); + // when(connection.getProperty()).thenReturn(propertyList); + // when(connectionDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionList); + // Assert.assertEquals(method.invoke(updateConnection, connection), "There are no properties for this type of connection."); + + field1.set(field.get(updateConnection), connectionDefinitionList); + when(connection.getConnectionType()).thenReturn(connectionType); + when(connection.getProperty()).thenReturn(propertyList); + when(connectionDefinition.getPropertyDefinition()).thenReturn(null); + // when(connection.getConnectionType()).thenReturn(connectionType2); + Assert.assertEquals(method.invoke(updateConnection, connection), "There are no properties for this type of connection."); + + field1.set(field.get(updateConnection), connectionDefinitionList); + when(connection.getConnectionType()).thenReturn(connectionType); + when(connection.getProperty()).thenReturn(propertyList); + when(connectionDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionList); + when(property.getPropertyName()).thenReturn(propertyName); + when(propertyDefinition.getPropertyName()).thenReturn(propertyName2); + when(property.getPropertyName().toString()).thenReturn(new String("propertyname")); + Assert.assertEquals(method.invoke(updateConnection, connection), "This type of propertypropertyname has not been defined."); + + when(connection.getConnectionType()).thenReturn(connectionType2); + Assert.assertEquals(method.invoke(updateConnection, connection),"This type of connection has not been defined."); + + field1.set(field.get(updateConnection), connectionDefinitionListnull); + Assert.assertEquals(method.invoke(updateConnection, connection), "This type of connection has not been defined."); + + } + + @Test + public void testcheckProperty() throws Exception { + Class class1 = UpdateConnection.class; + Method method = class1.getDeclaredMethod("checkProperty",new Class[]{List.class,List.class}); + method.setAccessible(true); + when(property.getPropertyName()).thenReturn(propertyName); + when(propertyDefinition.getPropertyName()).thenReturn(propertyName2); + when(property.getPropertyName().toString()).thenReturn(new String("propertyname")); + Assert.assertEquals(method.invoke(updateConnection, propertyList, propertyDefinitionList), "This type of propertypropertyname has not been defined."); + + + + + } +} \ No newline at end of file diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateFlowTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateFlowTest.java new file mode 100644 index 0000000..778e256 --- /dev/null +++ b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateFlowTest.java @@ -0,0 +1,292 @@ +/* + * 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.user.vnspacemanager.structurestyle.updateintent; + + + +import static org.mockito.Mockito.doNothing; + +import org.junit.Before; +import org.junit.Test; +import org.opendaylight.nemo.user.tenantmanager.TenantManage; + +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.nemo.user.tenantmanager.TenantManage; +import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.MatchItemName; +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.user.intent.Objects; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.FlowBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.FlowKey; +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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.MatchItemDefinitions; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.flow.instance.MatchItem; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.definitions.MatchItemDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.match.item.instance.MatchItemValue; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.FlowId; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.FlowName; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.LinkedList; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import java.util.*; +import java.util.List; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import com.google.common.util.concurrent.CheckedFuture; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; + + +import static org.junit.Assert.*; + +/** + * Created by ldzd11 on 2015/12/22. + */ +public class UpdateFlowTest { + + private UpdateFlow updateFlow; + private DataBroker dataBroker; + private TenantManage tenantManage; + private MatchItemName matchItemName; + private MatchItemName matchItemName2; + private MatchItem matchItem; + private List matchItemList; + private List matchItemListnull; + private User user; + private UserId userId; + private Objects objects; + private List flowList; + private FlowId flowId; + private FlowName flowName; + private FlowName flowName2; + + + + private MatchItemValue matchItemValue; + private Flow flow; + private Flow flow2; + + @Before + public void setUp() throws Exception { + dataBroker = mock(DataBroker.class); + tenantManage = mock(TenantManage.class); + matchItemName = mock(MatchItemName.class); + matchItemName2= mock(MatchItemName.class); + matchItem = mock(MatchItem.class); + matchItemList = new ArrayList(1); + matchItemListnull = new ArrayList(); + userId = mock(UserId.class); + user = mock(User.class); + matchItemList.add(matchItem); + flow = mock(Flow.class); + flow2 = mock(Flow.class); + flowId = mock(FlowId.class); + flowName = mock(FlowName.class); + flowName2 = mock(FlowName.class); + + matchItemValue = mock(MatchItemValue.class); + objects = mock(Objects.class); + flowList = new ArrayList(1); + flowList.add(flow2); + + updateFlow = new UpdateFlow(dataBroker,tenantManage); + + } + + + + + @Test + public void testFlowHandling() throws Exception { + + //into checkdefinition for error = null + CheckedFuture matchitemdefinitionFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(matchitemdefinitionFuture); + + List matchItemDefinitions = new ArrayList(); + MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class); + matchItemDefinitions.add(matchItemDefinition); + when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName); + + Class class1 = UpdateFlow.class; + Class class2 = GetDefinitions.class; + Field field = class1.getDeclaredField("getDefinitions"); + field.setAccessible(true); + Field field1 = class2.getDeclaredField("matchItemDefinitionList"); + field1.setAccessible(true); + + field1.set(field.get(updateFlow), matchItemDefinitions); + when(flow.getMatchItem()).thenReturn(null); + //into checkinstance for error = null + when(tenantManage.getFlow(userId)).thenReturn(null); + when(tenantManage.getFlowDataStore(userId)).thenReturn(null); + doNothing().when(tenantManage).setFlow(any(UserId.class),any(FlowId.class),any(Flow.class)); + + + Assert.assertEquals(updateFlow.FlowHandling(userId, flow), null); + + + + + } + + @Test + public void testcheckInstance() throws Exception { + Class class1 = UpdateFlow.class; + Method method = class1.getDeclaredMethod("checkInstance",new Class[]{UserId.class, Flow.class}); + method.setAccessible(true); + Map map1 = new HashMap(); + map1.put(flowId,flow2); + when(tenantManage.getFlow(userId)).thenReturn(map1); + when(flow.getFlowId()).thenReturn(flowId); + when(flow2.getFlowName()).thenReturn(flowName2); + when(flow.getFlowName()).thenReturn(flowName); + Assert.assertEquals(method.invoke(updateFlow, userId, flow), "The flow name should not be changed."); + + when(tenantManage.getFlow(userId)).thenReturn(null); + when(tenantManage.getFlowDataStore(userId)).thenReturn(map1); + when(flow.getFlowId()).thenReturn(flowId); + when(flow2.getFlowName()).thenReturn(flowName2); + when(flow.getFlowName()).thenReturn(flowName); + Assert.assertEquals(method.invoke(updateFlow, userId, flow), "The flow name should not be changed."); + + when(tenantManage.getFlow(userId)).thenReturn(null); + when(tenantManage.getFlowDataStore(userId)).thenReturn(null); + Assert.assertEquals(method.invoke(updateFlow, userId, flow), null); + } + + @Test + public void testcheckDefinition() throws Exception { + Class class3 = UpdateFlow.class; + Method method = class3.getDeclaredMethod("checkDefinition",new Class[]{Flow.class}); + method.setAccessible(true); + + CheckedFuture matchitemdefinitionFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(matchitemdefinitionFuture); + + + List matchItemDefinitions = new ArrayList(); + List matchItemDefinitionsnull = new ArrayList(); + MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class); + matchItemDefinitions.add(matchItemDefinition); + when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName); + + + Class class1 = UpdateFlow.class; + Class class2 = GetDefinitions.class; + Field field = class1.getDeclaredField("getDefinitions"); + field.setAccessible(true); + Field field1 = class2.getDeclaredField("matchItemDefinitionList"); + field1.setAccessible(true); + + //the else + field1.set(field.get(updateFlow), matchItemDefinitions); + when(flow.getMatchItem()).thenReturn(matchItemList); + when(matchItem.getMatchItemName()).thenReturn(matchItemName2); + Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined."); + + //the upper + field1.set(field.get(updateFlow), matchItemDefinitionsnull); + Assert.assertEquals(method.invoke(updateFlow, flow), "The match item has not been defined."); + + //the if errorInfo == null into checkPredefine + field1.set(field.get(updateFlow), matchItemDefinitions); + when(flow.getMatchItem()).thenReturn(matchItemListnull); + //into checkPredefine + Assert.assertEquals(method.invoke(updateFlow, flow), null); + + + + + } + + @Test + public void testcheckPredefine() throws Exception { + Class class1 = UpdateFlow.class; + Method method = class1.getDeclaredMethod("checkPredefine",new Class[]{List.class}); + method.setAccessible(true); + + when(matchItem.getMatchItemName()).thenReturn(matchItemName); + when(matchItem.getMatchItemName().getValue()).thenReturn(new String("src-ip")); + when(matchItem.getMatchItemValue()).thenReturn(matchItemValue); + + //stringValues.contains("/") + when(matchItem.getMatchItemValue().getStringValue()).thenReturn(new String("110/")); + //into checkIpPrefix(stringvalues) legalValue=false + Assert.assertEquals(method.invoke(updateFlow, matchItemList),"The " + NEMOConstants.ip_address + " is not legal."); + + when(matchItem.getMatchItemValue().getStringValue()).thenReturn(new String("110\\.")); + //into checkip address + Assert.assertEquals(method.invoke(updateFlow, matchItemList),"The " + NEMOConstants.ip_address + " is not legal."); + + + when(matchItem.getMatchItemName().getValue()).thenReturn(new String("src-mac")); + when(matchItem.getMatchItemValue()).thenReturn(matchItemValue); + when(matchItem.getMatchItemValue().getStringValue()).thenReturn(new String("110:")); + //into valuecheck.checkMac + Assert.assertEquals(method.invoke(updateFlow, matchItemList), "The " + NEMOConstants.mac_address + " is not legal."); + + + + + + + + + + + + } +} \ No newline at end of file diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateIntentTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateIntentTest.java index 9448d70..d2ec4df 100644 --- a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateIntentTest.java +++ b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateIntentTest.java @@ -32,6 +32,10 @@ import org.junit.Before; import org.junit.Test; import java.util.*; import java.util.List; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.template.definitions.TemplateDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.template.instances.TemplateInstance; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.TemplateInstances; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.TemplateDefinitions; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; @@ -77,6 +81,13 @@ public class UpdateIntentTest { private List operationList; private Results results; private StructureStyleNemoUpdateInput structureStyleNemoUpdateInput; + private TemplateDefinition templateDefinition; + private List templateDefinitionList; + private TemplateInstance templateInstance; + private List templateInstanceList; + private TemplateDefinitions templateDefinitions; + private TemplateInstances templateInstances; + @Before public void setUp() throws Exception { @@ -103,6 +114,16 @@ public class UpdateIntentTest { flowList = new ArrayList(1); flowList.add(flow); + templateDefinition = mock(TemplateDefinition.class); + templateDefinitionList = new ArrayList(); + templateDefinitionList.add(templateDefinition); + templateDefinitions = mock(TemplateDefinitions.class); + + templateInstance = mock(TemplateInstance.class); + templateInstanceList = new ArrayList(); + templateInstanceList.add(templateInstance); + templateInstances = mock(TemplateInstances.class); + updateIntent = new UpdateIntent(dataBroker,tenantManage); @@ -123,7 +144,7 @@ public class UpdateIntentTest { field_1.setAccessible(true); UpdateNode updateNode_1 = mock(UpdateNode.class); - field_1.set(updateIntent,updateNode_1); + field_1.set(updateIntent, updateNode_1); when(updateNode_1.NodeHandling(userId,node)).thenReturn(new String("node success")); Assert.assertEquals(updateIntent.updateIntent(aaa, structureStyleNemoUpdateInput), "node success"); @@ -177,6 +198,36 @@ public class UpdateIntentTest { when(updateResult.ResultHandling(userId, results)).thenReturn(new String("result success")); Assert.assertEquals(updateIntent.updateIntent(aaa, structureStyleNemoUpdateInput), "result success"); + //getTemplateDefinitions + when(structureStyleNemoUpdateInput.getObjects()).thenReturn(null); + when(structureStyleNemoUpdateInput.getOperations()).thenReturn(null); + when(structureStyleNemoUpdateInput.getResults()).thenReturn(null); + when(structureStyleNemoUpdateInput.getTemplateDefinitions()).thenReturn(templateDefinitions); + when(structureStyleNemoUpdateInput.getTemplateDefinitions().getTemplateDefinition()).thenReturn(templateDefinitionList); + + Field field_6 = class_1.getDeclaredField("updateTemplateDefinition"); + field_6.setAccessible(true); + UpdateTemplateDefinition updateTemplateDefinition = mock(UpdateTemplateDefinition.class); + field_6.set(updateIntent, updateTemplateDefinition); + when(updateTemplateDefinition.checkTemplateDefinition(userId, templateDefinition)).thenReturn(new String("templateDefinition success")); + Assert.assertEquals(updateIntent.updateIntent(aaa, structureStyleNemoUpdateInput), "templateDefinition success"); + + + //getTemplateInstances + when(structureStyleNemoUpdateInput.getObjects()).thenReturn(null); + when(structureStyleNemoUpdateInput.getOperations()).thenReturn(null); + when(structureStyleNemoUpdateInput.getResults()).thenReturn(null); + when(structureStyleNemoUpdateInput.getTemplateDefinitions()).thenReturn(null); + when(structureStyleNemoUpdateInput.getTemplateInstances()).thenReturn(templateInstances); + when(structureStyleNemoUpdateInput.getTemplateInstances().getTemplateInstance()).thenReturn(templateInstanceList); + + Field field_7 = class_1.getDeclaredField("updateTemplateInstance"); + field_7.setAccessible(true); + UpdateTemplateInstance updateTemplateInstance = mock(UpdateTemplateInstance.class); + field_7.set(updateIntent, updateTemplateInstance); + when(updateTemplateInstance.checkTemplateInstance(userId, templateInstance)).thenReturn(new String("templateInstance success")); + Assert.assertEquals(updateIntent.updateIntent(aaa, structureStyleNemoUpdateInput),"templateInstance success"); + diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateNodeTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateNodeTest.java new file mode 100644 index 0000000..cc162f5 --- /dev/null +++ b/nemo-impl/src/test/java/org/opendaylight/nemo/user/vnspacemanager/structurestyle/updateintent/UpdateNodeTest.java @@ -0,0 +1,473 @@ +/* + * 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.user.vnspacemanager.structurestyle.updateintent; + +import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.UpdateNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.template.definitions.TemplateDefinition; + +import org.opendaylight.nemo.user.tenantmanager.TenantManage; +import com.google.common.base.Optional; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.FutureCallback; +import com.google.common.util.concurrent.Futures; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; +import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.SubNode; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition; +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.nemo.intent.rev151010.user.intent.objects.Node; +import org.opendaylight.nemo.user.tenantmanager.TenantManage; +import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants; +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.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.user.intent.Objects; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeKey; +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 org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.NodeDefinitions; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.StringValue; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.definitions.NodeDefinition; +import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.PropertyName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; +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.common.rev151010.NodeName; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.NodeKey; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.opendaylight.controller.md.sal.binding.api.DataBroker; +import org.opendaylight.nemo.user.tenantmanager.TenantManage; +import org.opendaylight.nemo.user.vnspacemanager.languagestyle.NEMOConstants; +import org.opendaylight.nemo.user.vnspacemanager.languagestyle.updateintentlang.UpdateTemplateInstanceLang; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.*; +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.user.intent.template.instances.TemplateInstanceBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.template.instances.TemplateInstanceKey; +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.node.definitions.NodeDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.node.instance.Property; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.definitions.PropertyDefinition; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.PropertyValues; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.StringValue; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.instance.grouping.TemplateParameter; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.instance.grouping.TemplateParameterBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.instance.grouping.TemplateParameterKey; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.instance.grouping.template.parameter.ParameterValuesBuilder; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.template.rev151201.template.instance.grouping.template.parameter.parameter.values.*; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.template.instances.TemplateInstance; + + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserRoleName; +import java.util.LinkedList; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import java.util.*; +import java.util.List; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.google.common.util.concurrent.CheckedFuture; +import static org.mockito.Mockito.doNothing; + +/** + * Created by ldzd11 on 2015/12/18. + */ +public class UpdateNodeTest { + + private UpdateNode updateNode; + private Node node; + private Node node2; + private NodeName nodeName; + private NodeName nodeName2; + private List nodeList; + private DataBroker dataBroker; + private TenantManage tenantManage; + private Property property; + private List propertyList; + private PropertyName propertyName; + private PropertyName propertyName2; + private StringValue stringValue; + private List stringValues; + private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.RangeValue rangeValue; + private PropertyValues propertyValues; + private ValueCheck valueCheck; + private PropertyDefinition propertyDefinition; + private NodeType nodeType; + private NodeType nodeType2; + private List propertyDefinitions; + private UserId userId; + private WriteTransaction writeTransaction; + private User user; + private NodeId nodeId; + private NodeKey nodeKey; + private Objects objects; + private SubNode subNode; + private UserRoleName userRoleName; + private org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.IntValue intValue; + private List intValueList; + private List subNodeList; + @org.junit.Before + public void setUp() throws Exception { + dataBroker = mock(DataBroker.class); + tenantManage = mock(TenantManage.class); + propertyValues = mock(PropertyValues.class); + valueCheck = mock(ValueCheck.class); + nodeId = mock(NodeId.class); + nodeName = mock(NodeName.class); + nodeName2 = mock(NodeName.class); + + propertyName = mock(PropertyName.class); + propertyName2 = mock(PropertyName.class); + + property = mock(Property.class); + propertyList = new ArrayList(1); + propertyList.add(property); + + stringValue = mock(StringValue.class); + stringValues = new ArrayList(1); + stringValues.add(stringValue); + nodeType = mock(NodeType.class); + + propertyDefinition= mock(PropertyDefinition.class); + propertyDefinitions = new ArrayList(1); + propertyDefinitions.add(propertyDefinition); + + node = mock(Node.class); + node2 = mock(Node.class); + userId = mock(UserId.class); + user = mock(User.class); + objects = mock(Objects.class); + nodeKey = mock(NodeKey.class); + + nodeList = new ArrayList(1); + nodeList.add(node2); + writeTransaction = mock(WriteTransaction.class); + + subNode = mock(SubNode.class); + subNodeList = new ArrayList(); + subNodeList.add(subNode); + + intValue = mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.IntValue.class); + intValueList = new ArrayList(); + intValueList.add(intValue); + + rangeValue = mock(org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.object.rev151010.property.instance.property.values.RangeValue.class); + + + + updateNode = new UpdateNode(dataBroker,tenantManage); + nodeType2 = mock(NodeType.class); + userRoleName = mock(UserRoleName.class); + + + + } + + @org.junit.Test + public void testNodeHandling() throws Exception { + + Class class1 = UpdateNode.class; + TemplateName templateName = new TemplateName("aaa"); + TemplateDefinition templateDefinition = mock(TemplateDefinition.class); + Map map1 = new HashMap(); + map1.put(templateName, templateDefinition); + // userTemplateDefinitionMap.put(userId, map1); + //field1.set(field.get(updateNode), userTemplateDefinitionMap); + + when(tenantManage.getTempalteDefinition(userId)).thenReturn(map1); + when(node.getNodeType()).thenReturn(nodeType); + when(node.getNodeType().getValue()).thenReturn(new String("aaa")); + + when(tenantManage.getDefinitionDataStore(userId)).thenReturn(map1) +; when(node.getNodeType()).thenReturn(nodeType); + when(node.getNodeType().getValue()).thenReturn(new String("aaa")); + + /////////////////////////////////////////////////////////////// + when(tenantManage.getTempalteDefinition(userId)).thenReturn(null); + when(tenantManage.getDefinitionDataStore(userId)).thenReturn(null); + Map map2 = new HashMap(); + UserId userId1 = mock(UserId.class); + User user1 = mock(User.class); + map2.put(userId1, user1); + when(tenantManage.getUsers()).thenReturn(map2); + when(user1.getUserRole()).thenReturn(userRoleName); + when(user1.getUserRole().getValue()).thenReturn(NEMOConstants.admin); + when(user1.getUserId()).thenReturn(userId1); + when(tenantManage.getDefinitionDataStore(user1.getUserId())).thenReturn(map1); + when(tenantManage.getDefinitionDataStore(user1.getUserId())).thenReturn(map1); + ///////////////////////////////////////////////////////////////////////////////// + + + //first nodeModel = true 79 if + when(node.getSubNode()).thenReturn(subNodeList); + Assert.assertEquals(updateNode.NodeHandling(userId, node), "Subnodes should not be included in template instance."); + //else 83 + when(node.getSubNode()).thenReturn(null); + when(node.getNodeId()).thenReturn(nodeId); + when(node.getNodeId().getValue()).thenReturn(new String("00001111-0000-0000-0000-000011112222")); + when(node.getNodeName()).thenReturn(nodeName); + when(node.getNodeName().getValue()).thenReturn(new String("nodename")); + when(node.getProperty()).thenReturn(propertyList); + when(property.getPropertyName()).thenReturn(propertyName); + when(property.getPropertyName().getValue()).thenReturn(new String("propertyname")); + when(property.getPropertyValues()).thenReturn(propertyValues); + //intValue + when(property.getPropertyValues().getIntValue()).thenReturn(intValueList); + long a = 1; + long b = 2; + when(intValue.getOrder()).thenReturn(a); + when(intValue.getValue()).thenReturn(a); + //stringValue + when(property.getPropertyValues().getStringValue()).thenReturn(stringValues); + when(stringValue.getOrder()).thenReturn(a); + when(stringValue.getValue()).thenReturn(new String("stringvalue")); + //RangeValue + when(property.getPropertyValues().getRangeValue()).thenReturn(rangeValue); + when(property.getPropertyValues().getRangeValue().getMin()).thenReturn(a); + when(property.getPropertyValues().getRangeValue().getMax()).thenReturn(b); + + UpdateTemplateInstance updateTemplateInstance = mock(UpdateTemplateInstance.class); + Field field1 = class1.getDeclaredField("updateTemplateInstance"); + field1.setAccessible(true); + field1.set(updateNode, updateTemplateInstance); + when(updateTemplateInstance.checkTemplateInstance(any(UserId.class), any(TemplateInstance.class))).thenReturn(new String("success")); + Assert.assertEquals(updateNode.NodeHandling(userId, node), "success"); + + + when(tenantManage.getTempalteDefinition(userId)).thenReturn(null); + when(tenantManage.getDefinitionDataStore(userId)).thenReturn(null); + Map mapnull = new HashMap(); + when(tenantManage.getUsers()).thenReturn(mapnull); + + //into checkDefinition + CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture); + + //Class class1 = UpdateNode.class; + Class class2 = GetDefinitions.class; + Field field = class1.getDeclaredField("getDefinitions"); + field.setAccessible(true); + Field field2 = class2.getDeclaredField("nodeDefinitionList"); + field2.setAccessible(true); + + + List nodeDefinitionList = new ArrayList(); + NodeDefinition nodeDefinition = mock(NodeDefinition.class); + when(nodeDefinition.getNodeType()).thenReturn(nodeType); + nodeDefinitionList.add(nodeDefinition); + + field2.set(field.get(updateNode), nodeDefinitionList); + // System.out.print("2"); + // Assert.assertTrue((List) field1.get(field.get(updateNode)) == nodeDefinitionList); + + when(node.getNodeType()).thenReturn(nodeType); + when(node.getProperty()).thenReturn(null); + when(nodeDefinition.getPropertyDefinition()).thenReturn(null); + + //into checkinstance + when(tenantManage.getNode(userId)).thenReturn(null); + when(tenantManage.getNodeDataStore(userId)).thenReturn(null); + //into else tenantManage.setNode + when(node.getNodeId()).thenReturn(nodeId); + doNothing().when(tenantManage).setNode(any(UserId.class), any(NodeId.class), any(Node.class)); + Assert.assertEquals(updateNode.NodeHandling(userId, node), null); + } + @Test + public void testcheckInstance() throws Exception { + Class class1 = UpdateNode.class; + Method method = class1.getDeclaredMethod("checkInstance", new Class[]{UserId.class, Node.class}); + method.setAccessible(true); + + Class class2 = TenantManage.class; + Field field = class1.getDeclaredField("tenantManage"); + field.setAccessible(true); + Field field1 = class2.getDeclaredField("userNodeMap"); + field1.setAccessible(true); + + Map> userNodeMap = new HashMap>(); + Map> userNodeMap2 = new HashMap>(); + + Map map1 = new HashMap(); + map1.put(nodeId, node2); + userNodeMap.put(userId, map1); + field1.set(field.get(updateNode), userNodeMap); + + when(tenantManage.getNode(userId)).thenReturn(map1); + when(node.getNodeId()).thenReturn(nodeId); + when(node2.getNodeName()).thenReturn(nodeName); + when(node.getNodeName()).thenReturn(nodeName2); + Assert.assertEquals(method.invoke(updateNode, userId, node), "The node name should not be changed."); + + when(node2.getNodeName()).thenReturn(nodeName); + when(node.getNodeName()).thenReturn(nodeName); + when(node2.getNodeType()).thenReturn(nodeType2); + when(node.getNodeType()).thenReturn(nodeType); + Assert.assertEquals(method.invoke(updateNode, userId, node), "The node type should not be changed."); + + field1.set(field.get(updateNode), userNodeMap2); + Field field2 = class2.getDeclaredField("user"); + field2.setAccessible(true); + field2.set(field.get(updateNode), user); + when(user.getObjects()).thenReturn(objects); + when(user.getObjects().getNode()).thenReturn(nodeList); + when(node2.getNodeId()).thenReturn(nodeId);//map.put(nodeId,node2) + //when(tenantManage.getNodeDataStore(userId)).thenReturn(map1); + when(node.getNodeId()).thenReturn(nodeId); + when(node2.getNodeName()).thenReturn(nodeName2); + when(node.getNodeName()).thenReturn(nodeName); + Assert.assertEquals(method.invoke(updateNode, userId, node), "The node name should not be changed."); + + when(node2.getNodeName()).thenReturn(nodeName); + when(node.getNodeName()).thenReturn(nodeName); + when(node2.getNodeType()).thenReturn(nodeType2); + when(node.getNodeType()).thenReturn(nodeType); + Assert.assertEquals(method.invoke(updateNode, userId, node), "The node type should not be changed."); + + } + +@Test + public void testcheckDefinition() throws Exception { + + Class class3 = UpdateNode.class; + Method method = class3.getDeclaredMethod("checkDefinition", new Class[]{Node.class}); + method.setAccessible(true); + + CheckedFuture nodedefinitionFuture = mock(CheckedFuture.class); + ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class); + when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction); + when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(nodedefinitionFuture); + + List nodeDefinitionList = new ArrayList(); + List nodeDefinitionListnull = new ArrayList(); + NodeDefinition nodeDefinition = mock(NodeDefinition.class); + nodeDefinitionList.add(nodeDefinition); + when(nodeDefinition.getNodeType()).thenReturn(nodeType); + + Class class1 = UpdateNode.class; + Class class2 = GetDefinitions.class; + Field field = class1.getDeclaredField("getDefinitions"); + field.setAccessible(true); + Field field1 = class2.getDeclaredField("nodeDefinitionList"); + field1.setAccessible(true); + + + System.out.println(field1.get(field.get(updateNode))); + field1.set(field.get(updateNode), nodeDefinitionList); + System.out.println(field1.get(field.get(updateNode))); + Assert.assertTrue((List) field1.get(field.get(updateNode)) == nodeDefinitionList); + + when(node.getNodeType()).thenReturn(nodeType); + when(node.getProperty()).thenReturn(propertyList); + when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitions); + + when(property.getPropertyName()).thenReturn(propertyName); + when(propertyDefinition.getPropertyName()).thenReturn(propertyName2); + + when(property.getPropertyName().getValue()).thenReturn(new String("propertyname")); + Assert.assertEquals(method.invoke(updateNode, node), "The property propertyname has not been defined."); + + field1.set(field.get(updateNode), nodeDefinitionList); + when(node.getNodeType()).thenReturn(nodeType); + List propertyListnull = new ArrayList(); + List propertyDefinitionListnull = new ArrayList(); + + when(node.getProperty()).thenReturn(propertyListnull); + when(nodeDefinition.getPropertyDefinition()).thenReturn(propertyDefinitionListnull); + Assert.assertEquals(method.invoke(updateNode, node), null); + + field1.set(field.get(updateNode), nodeDefinitionListnull); + Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined."); + + field1.set(field.get(updateNode), nodeDefinitionList); + when(node.getNodeType()).thenReturn(nodeType2); + Assert.assertEquals(method.invoke(updateNode, node), "This type of Node has not been defined."); + + +/* + (String)method.invoke(updateNode,node)*/ + } + + @Test + public void testcheckProperty() throws Exception { + Class class1 = UpdateNode.class; + Method method = class1.getDeclaredMethod("checkProperty", new Class[]{List.class, List.class}); + method.setAccessible(true); + when(property.getPropertyName()).thenReturn(propertyName); + when(propertyDefinition.getPropertyName()).thenReturn(propertyName2); + when(property.getPropertyName().getValue()).thenReturn(new String("propertyname")); + Assert.assertEquals(method.invoke(updateNode, propertyList,propertyDefinitions), "The property propertyname has not been defined."); + + } + + + @Test + public void testcheckPredefine() throws Exception { + Class class1 = UpdateNode.class; + Method method = class1.getDeclaredMethod("checkPredefine", new Class[]{List.class}); + method.setAccessible(true); + + when(property.getPropertyName()).thenReturn(propertyName); + when(property.getPropertyName().getValue()).thenReturn(new String("mac-address")); + when(property.getPropertyValues()).thenReturn(propertyValues); + when(property.getPropertyValues().getStringValue()).thenReturn(stringValues); + when(stringValue.getValue()).thenReturn(new String("test")); + when(valueCheck.checkMac(stringValue.getValue())).thenReturn(false); + Assert.assertEquals(method.invoke(updateNode, propertyList), "The mac-address is not legal."); + + when(property.getPropertyName().getValue()).thenReturn(new String("ip-address")); + when(property.getPropertyValues().getStringValue()).thenReturn(stringValues); + when(stringValue.getValue()).thenReturn(new String("/")); + when(valueCheck.checkIpPrefix(stringValue.getValue())).thenReturn(false); + Assert.assertEquals(method.invoke(updateNode, propertyList), "The ip-address is not legal."); + + when(property.getPropertyName().getValue()).thenReturn(new String("gateway-ip")); + when(property.getPropertyValues().getStringValue()).thenReturn(stringValues); + when(valueCheck.checkIpAddress(stringValue.getValue())).thenReturn(false); + Assert.assertEquals(method.invoke(updateNode, propertyList), "The gateway-ip is not legal."); + + when(property.getPropertyName().getValue()).thenReturn(new String("ac-info-network")); + when(property.getPropertyValues().getStringValue()).thenReturn(stringValues); + when(stringValue.getValue()).thenReturn(new String("layer4")); + Assert.assertEquals(method.invoke(updateNode, propertyList), "The ac-info-network is not legal."); + + when(property.getPropertyName().getValue()).thenReturn(new String("operating-mode")); + when(property.getPropertyValues().getStringValue()).thenReturn(stringValues); + when(stringValue.getValue()).thenReturn(new String("layer4")); + Assert.assertEquals(method.invoke(updateNode, propertyList), "The operating-mode is not legal."); + + } +} \ No newline at end of file -- 2.36.6