|
@@ -1,7 +1,12 @@
|
|
|
package com.mooctest.crowd.domain.repository;
|
|
|
|
|
|
import com.mooctest.crowd.domain.dao.*;
|
|
|
-import com.mooctest.crowd.domain.domainobject.*;
|
|
|
+import com.mooctest.crowd.domain.domainobject.EvaluationAgency;
|
|
|
+import com.mooctest.crowd.domain.domainobject.Permission;
|
|
|
+import com.mooctest.crowd.domain.domainobject.Role;
|
|
|
+import com.mooctest.crowd.domain.domainobject.User;
|
|
|
+import com.mooctest.crowd.domain.exception.PermissionNotFoundException;
|
|
|
+import com.mooctest.crowd.domain.exception.RoleNotFoundException;
|
|
|
import com.mooctest.crowd.domain.exception.UserNotExistException;
|
|
|
import com.mooctest.crowd.domain.model.*;
|
|
|
import org.junit.Assert;
|
|
@@ -13,6 +18,7 @@ import org.mockito.Mock;
|
|
|
import org.mockito.MockitoAnnotations;
|
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
import java.util.LinkedList;
|
|
|
import java.util.List;
|
|
|
import java.util.Optional;
|
|
@@ -41,6 +47,10 @@ public class UserRepoTest {
|
|
|
private RegionalManagerDao regionalManagerDao;
|
|
|
@Mock
|
|
|
private RegionalDao regionalDao;
|
|
|
+ @Mock
|
|
|
+ private EvaluationAgencyAbilityDao evaluationAgencyAbilityDao;
|
|
|
+ @Mock
|
|
|
+ private EvaluationAgencyResourceDao evaluationAgencyResourceDao;
|
|
|
|
|
|
private UserPO expectUserPO;
|
|
|
private RolePO expectRolePO;
|
|
@@ -48,43 +58,101 @@ public class UserRepoTest {
|
|
|
private List<UserToRolePO> expectUserToRolePOList;
|
|
|
private List<RoleToPermissionPO> expectRoleToPermissionPOList;
|
|
|
private UserToRolePO expectUserToRolePO;
|
|
|
+ private RoleToPermissionPO expectRoleToPermissionPO;
|
|
|
+ private RegionalManagerPO expectRegionalPOManagerPO;
|
|
|
+ private EvaluationAgencyPO expectEvaluationAgencyPO;
|
|
|
+ private RegionalPO expectRegionalPO;
|
|
|
+ private EvaluationAgencyAbilityPO evaluationAgencyAbilityPO;
|
|
|
+ private EvaluationAgencyResourcePO evaluationAgencyResourcePO;
|
|
|
+ private List<RegionalManagerPO> expectRegionalPOManagerList = new ArrayList<>();
|
|
|
+ private List<EvaluationAgencyPO> evaluationAgencyPOList = new ArrayList<>();
|
|
|
+ private List<EvaluationAgencyAbilityPO> evaluationAgencyAbilityPOList = new ArrayList<>();
|
|
|
+ private List<EvaluationAgencyResourcePO> evaluationAgencyResourcePOList = new ArrayList<>();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
@Before
|
|
|
public void before() {
|
|
|
MockitoAnnotations.initMocks(this);
|
|
|
|
|
|
expectUserPO = new UserPO();
|
|
|
+ expectUserPO.setId(1L);
|
|
|
+ expectUserPO.setName("郭超");
|
|
|
+ expectUserPO.setEmail("1021172957@qq.com");
|
|
|
+ expectUserPO.setPassword("123456");
|
|
|
+ expectUserPO.setMobile("13657094936");
|
|
|
+
|
|
|
expectRolePO = new RolePO();
|
|
|
+ expectRolePO.setId(1L);
|
|
|
+ expectRolePO.setName("generalUser");
|
|
|
+
|
|
|
expectUserToRolePO = new UserToRolePO();
|
|
|
- RoleToPermissionPO expectRoleToPermissionPO = new RoleToPermissionPO();
|
|
|
+ expectUserToRolePO.setId(1L);
|
|
|
+ expectUserToRolePO.setRoleId(1L);
|
|
|
+ expectUserToRolePO.setUserId(1L);
|
|
|
+
|
|
|
expectPermission = new PermissionPO();
|
|
|
+ expectPermission.setId(1L);
|
|
|
+ expectPermission.setName("查看项目");
|
|
|
+
|
|
|
+ expectRoleToPermissionPO = new RoleToPermissionPO();
|
|
|
+ expectRoleToPermissionPO.setId(1L);
|
|
|
+ expectRoleToPermissionPO.setPermissionId(1L);
|
|
|
+
|
|
|
expectUserToRolePOList = new LinkedList<>();
|
|
|
+ expectUserToRolePOList.add(expectUserToRolePO);
|
|
|
+
|
|
|
expectRoleToPermissionPOList = new LinkedList<>();
|
|
|
expectRoleToPermissionPOList.add(expectRoleToPermissionPO);
|
|
|
- expectUserToRolePOList.add(expectUserToRolePO);
|
|
|
+
|
|
|
+ expectEvaluationAgencyPO = new EvaluationAgencyPO();
|
|
|
+ expectEvaluationAgencyPO.setId(1L);
|
|
|
+ expectEvaluationAgencyPO.setIsAuthentication(-1);
|
|
|
+ expectEvaluationAgencyPO.setBankAccount("123445666");
|
|
|
+ expectEvaluationAgencyPO.setAgencyPhoto("http://mooctest.png");
|
|
|
+ expectEvaluationAgencyPO.setUserId(1L);
|
|
|
+ evaluationAgencyPOList.add(expectEvaluationAgencyPO);
|
|
|
+
|
|
|
+ expectRegionalPO = new RegionalPO();
|
|
|
+ expectRegionalPO.setId(1L);
|
|
|
+
|
|
|
+ expectRegionalPOManagerPO = new RegionalManagerPO();
|
|
|
+ expectRegionalPOManagerPO.setId(1L);
|
|
|
+ expectRegionalPOManagerPO.setUserId(1L);
|
|
|
+ expectRegionalPOManagerPO.setRegionalId(1L);
|
|
|
+ expectRegionalPOManagerList.add(expectRegionalPOManagerPO);
|
|
|
+
|
|
|
+ evaluationAgencyAbilityPO = new EvaluationAgencyAbilityPO();
|
|
|
+ evaluationAgencyAbilityPO.setId(1L);
|
|
|
+ evaluationAgencyAbilityPO.setEvaluationAgencyId(1L);
|
|
|
+ evaluationAgencyAbilityPO.setAbilityName("web测试");
|
|
|
+ evaluationAgencyAbilityPOList.add(evaluationAgencyAbilityPO);
|
|
|
+
|
|
|
+ evaluationAgencyResourcePO = new EvaluationAgencyResourcePO();
|
|
|
+ evaluationAgencyResourcePO.setId(1L);
|
|
|
+ evaluationAgencyResourcePO.setEvaluationAgencyId(1L);
|
|
|
+ evaluationAgencyResourcePO.setCount(10);
|
|
|
+ evaluationAgencyResourcePOList.add(evaluationAgencyResourcePO);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void should_return_user_optional_when_get_by_id_success() {
|
|
|
//arrange
|
|
|
- EvaluationAgencyPO expectEvaluationAgency = new EvaluationAgencyPO();
|
|
|
- List<RegionalManagerPO> expectRegionalManagerList = new LinkedList<>();
|
|
|
- RegionalManagerPO regionalManagerPO = new RegionalManagerPO();
|
|
|
- expectRegionalManagerList.add(regionalManagerPO);
|
|
|
- RegionalPO expectRegional = new RegionalPO();
|
|
|
-
|
|
|
when(userDao.findById(anyLong())).thenReturn(Optional.of(expectUserPO));
|
|
|
when(userToRoleDao.findByUserId(anyLong())).thenReturn(expectUserToRolePOList);
|
|
|
when(roleDao.findById(anyLong())).thenReturn(Optional.of(expectRolePO));
|
|
|
when(roleToPermissionDao.findAllByRoleId(anyLong())).thenReturn(expectRoleToPermissionPOList);
|
|
|
when(permissionDao.findById(anyLong())).thenReturn(Optional.of(expectPermission));
|
|
|
- when(evaluationAgencyDao.findByUserId(anyLong())).thenReturn(expectEvaluationAgency);
|
|
|
- when(regionalManagerDao.findByUserId(anyLong())).thenReturn(expectRegionalManagerList);
|
|
|
- when(regionalDao.findById(anyLong())).thenReturn(Optional.of(expectRegional));
|
|
|
+ when(evaluationAgencyDao.findByUserId(anyLong())).thenReturn(expectEvaluationAgencyPO);
|
|
|
+ when(evaluationAgencyAbilityDao.findByEvaluationAgencyId(anyLong())).thenReturn(evaluationAgencyAbilityPOList);
|
|
|
+ when(evaluationAgencyResourceDao.findByEvaluationAgencyId(anyLong())).thenReturn(evaluationAgencyResourcePOList);
|
|
|
+ when(regionalManagerDao.findByUserId(anyLong())).thenReturn(expectRegionalPOManagerList);
|
|
|
+ when(regionalDao.findById(anyLong())).thenReturn(Optional.of(expectRegionalPO));
|
|
|
//action
|
|
|
- User user = userRepo.getByID(0L);
|
|
|
+ User user = userRepo.getByID(1L);
|
|
|
//assert
|
|
|
- Assert.assertNotNull(user);
|
|
|
+ Assert.assertEquals(user.getId(),expectUserPO.getId());
|
|
|
}
|
|
|
|
|
|
@Test(expected = UserNotExistException.class)
|
|
@@ -92,22 +160,38 @@ public class UserRepoTest {
|
|
|
//arrange
|
|
|
when(userDao.findById(anyLong())).thenReturn(Optional.empty());
|
|
|
//action
|
|
|
- userRepo.getByID(0L);
|
|
|
- //assert
|
|
|
+ userRepo.getByID(1L);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected = RoleNotFoundException.class)
|
|
|
+ public void should_throw_when_get_by_id_role_not_exist(){
|
|
|
+ //arrange
|
|
|
+ //arrange
|
|
|
+ when(userDao.findById(anyLong())).thenReturn(Optional.of(expectUserPO));
|
|
|
+ when(userToRoleDao.findByUserId(anyLong())).thenReturn(expectUserToRolePOList);
|
|
|
+ when(roleToPermissionDao.findAllByRoleId(anyLong())).thenReturn(expectRoleToPermissionPOList);
|
|
|
+ when(permissionDao.findById(anyLong())).thenReturn(Optional.of(expectPermission));
|
|
|
+ when(evaluationAgencyDao.findByUserId(anyLong())).thenReturn(expectEvaluationAgencyPO);
|
|
|
+ when(evaluationAgencyAbilityDao.findByEvaluationAgencyId(anyLong())).thenReturn(evaluationAgencyAbilityPOList);
|
|
|
+ when(regionalManagerDao.findByUserId(anyLong())).thenReturn(expectRegionalPOManagerList);
|
|
|
+ when(regionalDao.findById(anyLong())).thenReturn(Optional.of(expectRegionalPO));
|
|
|
+ when(roleDao.findById(anyLong())).thenReturn(Optional.empty());
|
|
|
+ //action
|
|
|
+ userRepo.getByID(1L);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void should_return_user_optional_when_get_by_mobile_number_success() {
|
|
|
//arrange
|
|
|
- when(userDao.findByMobile(anyString())).thenReturn(expectUserPO);
|
|
|
+ when(userDao.findByMobile("13657094936")).thenReturn(expectUserPO);
|
|
|
when(userToRoleDao.findByUserId(anyLong())).thenReturn(expectUserToRolePOList);
|
|
|
when(roleDao.findById(anyLong())).thenReturn(Optional.of(expectRolePO));
|
|
|
when(roleToPermissionDao.findAllByRoleId(anyLong())).thenReturn(expectRoleToPermissionPOList);
|
|
|
when(permissionDao.findById(anyLong())).thenReturn(Optional.of(expectPermission));
|
|
|
//action
|
|
|
- User user = userRepo.getByMobileNum("123");
|
|
|
+ User user = userRepo.getByMobileNum(expectUserPO.getMobile());
|
|
|
//assert
|
|
|
- Assert.assertNotNull(user);
|
|
|
+ Assert.assertEquals(user.getId(),expectUserPO.getId());
|
|
|
}
|
|
|
|
|
|
@Test(expected = UserNotExistException.class)
|
|
@@ -116,7 +200,6 @@ public class UserRepoTest {
|
|
|
when(userDao.findByMobile(anyString())).thenReturn(null);
|
|
|
//action
|
|
|
userRepo.getByMobileNum("123");
|
|
|
- //assert
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -130,10 +213,29 @@ public class UserRepoTest {
|
|
|
when(userDao.save(any(UserPO.class))).thenReturn(expectUserPO);
|
|
|
when(roleDao.findByName(anyString())).thenReturn(expectRolePO);
|
|
|
when(userToRoleDao.save(any(UserToRolePO.class))).thenReturn(expectUserToRolePO);
|
|
|
+ when(roleToPermissionDao.findAllByRoleId(anyLong())).thenReturn(expectRoleToPermissionPOList);
|
|
|
+ when(permissionDao.findById(anyLong())).thenReturn(Optional.of(expectPermission));
|
|
|
//action
|
|
|
User resultUser = userRepo.saveUserAndRole(user);
|
|
|
//assert
|
|
|
- Assert.assertNotNull(resultUser);
|
|
|
+ Assert.assertEquals(resultUser.getId(), expectUserPO.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test(expected = PermissionNotFoundException.class)
|
|
|
+ public void should_return_expert_when_save_user_and_role_permission_not_found() {
|
|
|
+ //arrange
|
|
|
+ Role expectRole = new Role();
|
|
|
+ List<Role> roleList = new LinkedList<>();
|
|
|
+ roleList.add(expectRole);
|
|
|
+ User user = new User();
|
|
|
+ user.setRoleList(roleList);
|
|
|
+ when(userDao.save(any(UserPO.class))).thenReturn(expectUserPO);
|
|
|
+ when(roleDao.findByName(anyString())).thenReturn(expectRolePO);
|
|
|
+ when(userToRoleDao.save(any(UserToRolePO.class))).thenReturn(expectUserToRolePO);
|
|
|
+ when(roleToPermissionDao.findAllByRoleId(1L)).thenReturn(expectRoleToPermissionPOList);
|
|
|
+ when(permissionDao.findById(anyLong())).thenReturn(Optional.empty());
|
|
|
+ //action
|
|
|
+ userRepo.saveUserAndRole(user);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -144,6 +246,7 @@ public class UserRepoTest {
|
|
|
permissionList.add(permission);
|
|
|
List<Role> roleList = new LinkedList<>();
|
|
|
Role role = new Role();
|
|
|
+ role.setId(1L);
|
|
|
role.setPermissionList(permissionList);
|
|
|
roleList.add(role);
|
|
|
EvaluationAgency evaluationAgency = new EvaluationAgency();
|
|
@@ -158,7 +261,31 @@ public class UserRepoTest {
|
|
|
//action
|
|
|
User resultUser = userRepo.saveUser(user);
|
|
|
//assert
|
|
|
- Assert.assertNotNull(resultUser);
|
|
|
+ Assert.assertEquals(resultUser.getId(),expectUserPO.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_return_user_when_save_user_role_is_null() {
|
|
|
+ //arrange
|
|
|
+ List<Permission> permissionList = new LinkedList<>();
|
|
|
+ Permission permission = new Permission();
|
|
|
+ permissionList.add(permission);
|
|
|
+ List<Role> roleList = new LinkedList<>();
|
|
|
+ Role role = new Role();
|
|
|
+ role.setPermissionList(permissionList);
|
|
|
+ roleList.add(role);
|
|
|
+ EvaluationAgency evaluationAgency = new EvaluationAgency();
|
|
|
+ User user = new User();
|
|
|
+ user.setEvaluationAgency(evaluationAgency);
|
|
|
+ user.setRoleList(null);
|
|
|
+ expectUserPO.setId(0L);
|
|
|
+
|
|
|
+ when(userDao.save(any(UserPO.class))).thenReturn(expectUserPO);
|
|
|
+ when(userDao.findById(any(Long.class))).thenReturn(Optional.of(expectUserPO));
|
|
|
+ //action
|
|
|
+ User resultUser = userRepo.saveUser(user);
|
|
|
+ //assert
|
|
|
+ Assert.assertEquals(resultUser.getId(),expectUserPO.getId());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
@@ -208,5 +335,25 @@ public class UserRepoTest {
|
|
|
Assert.assertNull(null);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void should_return_user_list_when_get_apply_agency() {
|
|
|
+ //arrange
|
|
|
+ when(evaluationAgencyDao.findByIsAuthentication(-1)).thenReturn(evaluationAgencyPOList);
|
|
|
+ when(userDao.findById(any(Long.class))).thenReturn(Optional.of(expectUserPO));
|
|
|
+ //action
|
|
|
+ List<User> applyAgencyList = userRepo.getApplyEvaluationAgencyByIsAuthenticated();
|
|
|
+ //assert
|
|
|
+ Assert.assertNotNull(applyAgencyList);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void should_return_role_when_get_role(){
|
|
|
+ //arrange
|
|
|
+ when(roleDao.findByName(anyString())).thenReturn(expectRolePO);
|
|
|
+ //action
|
|
|
+ Role role = userRepo.getRole(expectRolePO.getName());
|
|
|
+ Assert.assertEquals(role.getName(),expectRolePO.getName());
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
}
|