Modify user test files 64/29964/1
authorsaomenmen <zhangmroy@163.com>
Fri, 20 Nov 2015 09:16:53 +0000 (17:16 +0800)
committersaomenmen <zhangmroy@163.com>
Fri, 20 Nov 2015 09:17:39 +0000 (17:17 +0800)
Change-Id: I922a117bd5496a3451a4b4fe9090997c2c7f2dc9
Signed-off-by: saomenmen <zhangmroy@163.com>
nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/AAATest.java [new file with mode: 0644]
nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/RegisterUserTest.java [new file with mode: 0644]
nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/TenantManageTest.java [new file with mode: 0644]
nemo-impl/src/test/java/org/opendaylight/nemo/user/transactionmanager/TransactionBeginTest.java
nemo-impl/src/test/java/org/opendaylight/nemo/user/transactionmanager/TransactionEndTest.java

diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/AAATest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/AAATest.java
new file mode 100644 (file)
index 0000000..7eccbfd
--- /dev/null
@@ -0,0 +1,67 @@
+/*
+
+* 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.tenantmanager;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.nemo.user.tenantmanager.AAA;
+import org.opendaylight.nemo.user.tenantmanager.TenantManage;
+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.common.rev151010.UserName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserPassword;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserRoleName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+import static org.junit.Assert.*;
+
+/**
+ * Created by zhangmeng on 2015/11/20.
+ */
+public class AAATest extends TestCase {
+    private AAA aaa;
+    private TenantManage tenantManage;
+    private UserId userId;
+    private UserName userName;
+    private UserPassword userPassword;
+    private UserRoleName userRoleName;
+
+    @Before
+    public void setUp() throws Exception {
+        tenantManage = mock(TenantManage.class);
+        aaa = new AAA(tenantManage);
+
+        userId = mock(UserId.class);
+        userName = mock(UserName.class);
+        userPassword = mock(UserPassword.class);
+        userRoleName = mock(UserRoleName.class);
+    }
+
+    @Test
+    public void testCheckUser() throws Exception {
+        doNothing().when(tenantManage).fetchUsers();
+        when(tenantManage.getUsersList()).thenReturn(null);
+        String acutal = aaa.CheckUser(userId, userName, userPassword, userRoleName);
+        String expected = "The user is not exist.";
+        verify(tenantManage).fetchUsers();
+        verify(tenantManage).getUsersList();
+        Assert.assertNotNull(aaa);
+        Assert.assertEquals(expected,acutal);
+    }
+}
\ No newline at end of file
diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/RegisterUserTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/RegisterUserTest.java
new file mode 100644 (file)
index 0000000..6fab2a3
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+
+* 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.tenantmanager;
+
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import static org.junit.Assert.*;
+import org.opendaylight.nemo.user.tenantmanager.RegisterUser;
+import org.opendaylight.nemo.user.tenantmanager.TenantManage;
+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.common.rev151010.UserRoleName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.RegisterUserInput;
+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.user.rev151010.user.roles.UserRole;
+
+import java.util.LinkedList;
+import java.util.List;
+
+import static org.mockito.Mockito.*;
+/**
+ * Created by zhangmeng on 2015/11/20.
+ */
+public class RegisterUserTest extends TestCase {
+    private RegisterUser registerUser;
+    private TenantManage tenantManage;
+    private RegisterUserInput input;
+    @Before
+    public void setUp() throws Exception {
+        tenantManage = mock(TenantManage.class);
+        registerUser = new RegisterUser(tenantManage);
+
+        input = mock(RegisterUserInput.class);
+    }
+
+    @Test
+    public void testRegisterUser() throws Exception {
+
+        //no data test
+        doNothing().when(tenantManage).fetchUserRoles();
+        when(tenantManage.getUserRoleList()).thenReturn(null);//return nothing
+        doNothing().when(tenantManage).fetchUsers();
+        when(tenantManage.getUsersList()).thenReturn(null);
+        registerUser.registerUser(input);
+
+        //data exists . and test other branch
+        when(tenantManage.getUserRoleList()).thenReturn(new LinkedList<UserRole>());//return nothing
+        when(tenantManage.getUsersList()).thenReturn(new LinkedList<User>());
+        when(input.getUserRole()).thenReturn(mock(UserRoleName.class));
+        registerUser.registerUser(input);
+
+        verify(tenantManage,times(2)).getUserRoleList();
+        verify(tenantManage,times(2)).getUsersList();
+        Assert.assertNotNull(tenantManage);
+        Assert.assertNotNull(registerUser);
+    }
+}
\ No newline at end of file
diff --git a/nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/TenantManageTest.java b/nemo-impl/src/test/java/org/opendaylight/nemo/user/tenantmanager/TenantManageTest.java
new file mode 100644 (file)
index 0000000..5c69d59
--- /dev/null
@@ -0,0 +1,146 @@
+/*
+
+* 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.tenantmanager;
+
+import junit.framework.TestCase;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import com.google.common.base.Optional;
+import com.google.common.util.concurrent.FutureCallback;
+import com.google.common.util.concurrent.Futures;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.ListenableFuture;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
+import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
+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.UserId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.RegisterUserInput;
+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.UserBuilder;
+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.user.rev151010.UserRoles;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.user.rev151010.user.roles.UserRole;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.LinkedList;
+import java.util.List;
+import static org.mockito.Mockito.*;
+/**
+ * Created by zhangmeng on 2015/11/20.
+ */
+public class TenantManageTest extends TestCase {
+    private TenantManage tenantManage;
+    private DataBroker dataBroker;
+    private List<UserRole> userRoleList;
+    private List<User> usersList ;
+    private User user;
+    @Before
+    public void setUp() throws Exception {
+        userRoleList = new LinkedList<UserRole>();
+        usersList = new LinkedList<User>();
+        user = mock(User.class);
+
+        dataBroker = mock(DataBroker.class);
+        tenantManage = new TenantManage(dataBroker);
+    }
+
+    @Test
+    public void testGetUserRoleList() throws Exception {
+        Assert.assertNotNull(userRoleList);
+        userRoleList = tenantManage.getUserRoleList();
+        Assert.assertNotEquals(new LinkedList<UserRole>(),userRoleList);
+    }
+
+    @Test
+    public void testGetUsersList() throws Exception {
+        Assert.assertNotNull(usersList);
+        usersList = tenantManage.getUsersList();
+        Assert.assertNotEquals(new LinkedList<User>(),usersList);
+    }
+
+    @Test
+    public void testGetUser() throws Exception {
+        user = tenantManage.getUser();
+        Assert.assertNotEquals(mock(User.class),user);
+    }
+
+    @Test
+    public void testFetchUserRoles() throws Exception {
+        //ListenableFuture<Optional<UserRoles>> userRolesFuture = mock(ListenableFuture.class);
+        CheckedFuture userRolesFuture = mock(CheckedFuture.class);
+        ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+//      when(dataBroker.newReadOnlyTransaction().read(any(LogicalDatastoreType.class),
+//      any(InstanceIdentifier.class))).thenReturn(userRolesFuture);
+        when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+        when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(userRolesFuture);
+        tenantManage.fetchUserRoles();
+        verify(dataBroker).newReadOnlyTransaction();
+        verify(readOnlyTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
+        Assert.assertNotNull(tenantManage);
+    }
+
+    @Test
+    public void testFetchUsers() throws Exception {
+        CheckedFuture usersFuture = mock(CheckedFuture.class);
+        ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+
+        when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+        when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(usersFuture);
+        tenantManage.fetchUsers();
+        verify(dataBroker).newReadOnlyTransaction();
+        verify(readOnlyTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
+        Assert.assertNotNull(tenantManage);
+    }
+
+    @Test
+    public void testFetchVNSpace() throws Exception {
+        CheckedFuture usersFuture = mock(CheckedFuture.class);
+        ReadOnlyTransaction readOnlyTransaction = mock(ReadOnlyTransaction.class);
+
+        when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
+        when(readOnlyTransaction.read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class))).thenReturn(usersFuture);
+        tenantManage.fetchVNSpace(mock(UserId.class));
+        verify(dataBroker).newReadOnlyTransaction();
+        verify(readOnlyTransaction).read(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
+        Assert.assertNotNull(tenantManage);
+    }
+
+    @Test // As a parameter registerUserInput has no data , so the test case just test a small part
+    public void testAddUser() throws Exception {
+        RegisterUserInput registerUserInput = mock(RegisterUserInput.class);
+        WriteTransaction writeTransaction = mock(WriteTransaction.class);
+
+        when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
+        when(registerUserInput.getUserId()).thenReturn(null);
+        tenantManage.addUser(registerUserInput);
+
+        CheckedFuture f = mock(CheckedFuture.class);
+        when(registerUserInput.getUserId()).thenReturn(mock(UserId.class));
+        when(writeTransaction.submit()).thenReturn(f);
+        tenantManage.addUser(registerUserInput);
+
+        verify(dataBroker,times(2)).newWriteOnlyTransaction();
+        verify(registerUserInput,times(4)).getUserId();
+        Assert.assertNotNull(tenantManage);
+    }
+}
\ No newline at end of file
index 249b80b5c84801aee35f1fc4bdac154b4ad9a98e..ae9ef3ed09973d3655cdc9bd0965306c28ef7c61 100644 (file)
@@ -1,36 +1,51 @@
 /*
- * 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
- */
+
+* 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.transactionmanager;
 
+import junit.framework.TestCase;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-
-import static org.mockito.Mockito.mock;
-
 import org.opendaylight.nemo.user.tenantmanager.AAA;
+import org.opendaylight.nemo.user.transactionmanager.TransactionBegin;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.BeginTransactionInput;
+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.common.rev151010.UserName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserPassword;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserRoleName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
+
 /**
- * Created by wangjunfei on 2015/11/9.
+ * Created by zhangmeng on 2015/11/20.
  */
-public class TransactionBeginTest {
-    private AAA aaa;
-    private BeginTransactionInput input;
-    private TransactionBegin begin;
+public class TransactionBeginTest extends TestCase {
+    private TransactionBegin transactionBegin;
     @Before
     public void setUp() throws Exception {
-        aaa = mock(AAA.class);
-        input = mock(BeginTransactionInput.class);
-        begin = mock(TransactionBegin.class);
+        transactionBegin = new TransactionBegin();
     }
 
     @Test
     public void testTransactionbegin() throws Exception {
-        Assert.assertNull(begin.transactionbegin(aaa,input));
+        AAA aaa = mock(AAA.class);
+        BeginTransactionInput input = mock(BeginTransactionInput.class);
+        when(aaa.CheckUser(any(UserId.class),any(UserName.class),any(UserPassword.class),any(UserRoleName.class)))
+                .thenReturn(new String("test"));
+        String flag = transactionBegin.transactionbegin(aaa,input);
+        verify(aaa).CheckUser(any(UserId.class),any(UserName.class),any(UserPassword.class),any(UserRoleName.class));
+        Assert.assertEquals("test",flag);
     }
 }
\ No newline at end of file
index d9448bfe7f10318703d93672703bea2789a2d57d..1e10dc8ada803586d123af09bf6e1b027418029d 100644 (file)
@@ -1,39 +1,51 @@
 /*
- * 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.transactionmanager;
 
-import org.opendaylight.nemo.user.transactionmanager.TransactionEnd;
+* 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,
 
-import static org.mockito.Mockito.mock;
+* and is available at http://www.eclipse.org/legal/epl-v10.html
 
+*/
+package org.opendaylight.nemo.user.transactionmanager;
+
+import junit.framework.TestCase;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
-
 import org.opendaylight.nemo.user.tenantmanager.AAA;
+import org.opendaylight.nemo.user.transactionmanager.TransactionEnd;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.EndTransactionInput;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.BeginTransactionInput;
+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.common.rev151010.UserName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserPassword;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.common.rev151010.UserRoleName;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.nemo.intent.rev151010.users.User;
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
 /**
- * Created by wangjunfei on 2015/11/9.
+ * Created by zhangmeng on 2015/11/20.
  */
-public class TransactionEndTest {
-    private AAA aaa;
-    private EndTransactionInput input;
-    private TransactionEnd end;
-
-    @org.junit.Before
+public class TransactionEndTest extends TestCase {
+    private TransactionEnd transactionEnd;
+    @Before
     public void setUp() throws Exception {
-        aaa = mock(AAA.class);
-        input = mock(EndTransactionInput.class);
-        end = mock(TransactionEnd.class);
+        transactionEnd = new TransactionEnd();
     }
 
-    @org.junit.Test
+    @Test
     public void testTransactionend() throws Exception {
-        Assert.assertNull(end.transactionend(aaa, input));
+        AAA aaa = mock(AAA.class);
+        EndTransactionInput input = mock(EndTransactionInput.class);
+        when(aaa.CheckUser(any(UserId.class),any(UserName.class),any(UserPassword.class),any(UserRoleName.class)))
+                .thenReturn(new String("test"));
+        String flag = transactionEnd.transactionend(aaa,input);
+        verify(aaa).CheckUser(any(UserId.class),any(UserName.class),any(UserPassword.class),any(UserRoleName.class));
+        Assert.assertEquals("test", flag);
     }
 }
\ No newline at end of file