Modified two test files
[nemo.git] / nemo-impl / src / test / java / org / opendaylight / nemo / user / vnspacemanager / structurestyle / updateintent / UpdateFlowTest.java
index 599f516b4b666b7ffd49102d8e9727969a938cd2..193546203352db949896fe81e9b570f82523fbf2 100644 (file)
-/*\r
- * Copyright (c) 2015 Huawei, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-package org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent;\r
-import org.opendaylight.nemo.user.vnspacemanager.structurestyle.updateintent.UpdateFlow;\r
-\r
-import static org.mockito.Mockito.mock;\r
-import static org.mockito.Mockito.*;\r
-import org.junit.Assert;\r
-import org.junit.Before;\r
-import org.junit.Test;\r
-\r
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;\r
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;\r
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;\r
-import org.opendaylight.nemo.user.tenantmanager.TenantManage;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserId;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.Users;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.Objects;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.Flow;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.FlowBuilder;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.user.intent.objects.FlowKey;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;\r
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.UserKey;\r
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;\r
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;\r
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;\r
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;\r
-import com.google.common.util.concurrent.CheckedFuture;\r
-import com.google.common.util.concurrent.FutureCallback;\r
-import com.google.common.util.concurrent.Futures;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
-\r
-public class UpdateFlowTest {\r
-\r
-    private UpdateFlow updateFlow;\r
-\r
-    private DataBroker dataBroker;\r
-    private TenantManage tenantManage;\r
-    private UserId userId;\r
-    private Flow flow;\r
-    \r
-    @org.junit.Before\r
-    public void setUp() throws Exception {\r
-        dataBroker = mock(DataBroker.class);\r
-        tenantManage = mock(TenantManage.class);\r
-        userId = mock(UserId.class);\r
-        flow = mock(Flow.class);\r
-        \r
-        updateFlow = new UpdateFlow(dataBroker, tenantManage);\r
-    }\r
-\r
-    @org.junit.Test\r
-    public void testFlowHandling() throws Exception {\r
-       \r
-       String errorDefinition = new String("The match item has not been defined.");\r
-       \r
-       CheckedFuture connectiondefinitionFuture = mock(CheckedFuture.class);\r
-        ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);\r
-        when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);\r
-        when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(connectiondefinitionFuture); \r
-         \r
-       Assert.assertEquals(updateFlow.FlowHandling(userId,flow),errorDefinition);\r
-       \r
-    }\r
+/*
+ * 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.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<MatchItem> matchItemList;
+    private List<MatchItem> matchItemListnull;
+    private User user;
+    private UserId userId;
+    private Objects objects;
+    private List<Flow> 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<MatchItem>(1);
+        matchItemListnull = new ArrayList<MatchItem>();
+        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<Flow>(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<MatchItemDefinition> matchItemDefinitions = new ArrayList<MatchItemDefinition>();
+        MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class);
+        matchItemDefinitions.add(matchItemDefinition);
+        when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName);
+
+        Class<UpdateFlow> class1 = UpdateFlow.class;
+        Class<GetDefinitions> 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.getUser()).thenReturn(null);
+
+        WriteTransaction writeTransaction = mock(WriteTransaction.class);
+        when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+        when(flow.getFlowId()).thenReturn(flowId);
+        when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+        CheckedFuture<Void, TransactionCommitFailedException> f;
+        f=mock(CheckedFuture.class);
+        when(writeTransaction.submit()).thenReturn(f);
+
+        Assert.assertEquals(updateFlow.FlowHandling(userId, flow), null);
+
+
+
+
+    }
+
+    @Test
+    public void testcheckInstance() throws Exception {
+        Class<UpdateFlow> class1 = UpdateFlow.class;
+        Method method = class1.getDeclaredMethod("checkInstance",new Class[]{UserId.class,Flow.class});
+        method.setAccessible(true);
+
+        when(tenantManage.getUser()).thenReturn(user);
+        when(user.getObjects()).thenReturn(objects);
+        when(user.getObjects().getFlow()).thenReturn(flowList);
+
+        when(flow2.getFlowId()).thenReturn(flowId);
+        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.");
+
+
+
+
+    }
+
+    @Test
+    public void testcheckDefinition() throws Exception {
+        Class<UpdateFlow> 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<MatchItemDefinition> matchItemDefinitions = new ArrayList<MatchItemDefinition>();
+        List<MatchItemDefinition> matchItemDefinitionsnull = new ArrayList<MatchItemDefinition>();
+        MatchItemDefinition matchItemDefinition = mock(MatchItemDefinition.class);
+        matchItemDefinitions.add(matchItemDefinition);
+        when(matchItemDefinition.getMatchItemName()).thenReturn(matchItemName);
+
+
+        Class<UpdateFlow> class1 = UpdateFlow.class;
+        Class<GetDefinitions> 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<UpdateFlow> 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