/*
-
-* 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.tenantmanager;
-
-import static org.mockito.Mockito.doNothing;
-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 java.util.LinkedList;
-
import junit.framework.TestCase;
-
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+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 static org.junit.Assert.*;
+import static org.mockito.Mockito.*;
/**
- * Created by zhangmeng on 2015/11/20.
+ * Created by zhangmeng on 2015/12/15.
*/
public class RegisterUserTest extends TestCase {
- private RegisterUser registerUser;
private TenantManage tenantManage;
- private RegisterUserInput input;
+ private RegisterUser registerUser;
+ @Override
@Before
public void setUp() throws Exception {
tenantManage = mock(TenantManage.class);
- registerUser = new RegisterUser(tenantManage);
- input = mock(RegisterUserInput.class);
+ registerUser = new RegisterUser(tenantManage);
}
@Test
public void testRegisterUser() throws Exception {
+ RegisterUserInput input = mock(RegisterUserInput.class);
+ UserRoleName userRoleName = mock(UserRoleName.class);
+ UserRole userRole = mock(UserRole.class);
+ UserId userId = mock(UserId.class);
+ User user = mock(User.class);
+ Map<UserRoleName, UserRole> userRoles = new HashMap<UserRoleName, UserRole>();
+ Map<UserId, User> users = new HashMap<UserId, User>();
+
+ when(tenantManage.getUsers()).thenReturn(users);
+ when(tenantManage.getUserRoles()).thenReturn(userRoles);
+
+ //test if
+ Assert.assertTrue(userRoles.isEmpty());
+ Assert.assertTrue(registerUser.registerUser(input).equals("There are no roles be defined."));
+
+ //test else
+ userRoles.put(userRoleName, userRole);
+ when(input.getUserRole())
+ .thenReturn(new UserRoleName("admin"))
+ .thenReturn(userRoleName);
+ Assert.assertTrue(registerUser.registerUser(input).equals("The role is not defined in the data store."));
+ verify(input).getUserRole();
+ Assert.assertTrue(!userRoles.containsKey(new UserRoleName("admin")));
+
+ users.put(userId,user);
+ when(input.getUserId())
+ .thenReturn(userId)
+ .thenReturn(new UserId("00001111-0000-0000-0000-000011112222"));
+
+ Assert.assertTrue(registerUser.registerUser(input).equals("The user has been registered."));
+
+ doNothing().when(tenantManage).addUser(any(RegisterUserInput.class));
+ Assert.assertTrue(registerUser.registerUser(input) == null);
+ verify(tenantManage).addUser(any(RegisterUserInput.class));
+
- //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