|
@@ -1,8 +1,17 @@
|
|
package cn.iselab.mooctest.site.service.impl;
|
|
package cn.iselab.mooctest.site.service.impl;
|
|
|
|
|
|
import cn.iselab.mooctest.site.Application;
|
|
import cn.iselab.mooctest.site.Application;
|
|
|
|
+import cn.iselab.mooctest.site.dao.Group2WorkerDao;
|
|
import cn.iselab.mooctest.site.dao.GroupDao;
|
|
import cn.iselab.mooctest.site.dao.GroupDao;
|
|
-import cn.iselab.mooctest.site.models.Group;
|
|
|
|
|
|
+import cn.iselab.mooctest.site.dao.ManagerPropertyDao;
|
|
|
|
+import cn.iselab.mooctest.site.dao.Task2GroupDao;
|
|
|
|
+import cn.iselab.mooctest.site.data.UserDTOForMT;
|
|
|
|
+import cn.iselab.mooctest.site.models.*;
|
|
|
|
+import cn.iselab.mooctest.site.models.instancePermission.GroupPermission;
|
|
|
|
+import cn.iselab.mooctest.site.service.AssignedTaskService;
|
|
|
|
+import cn.iselab.mooctest.site.service.UserService;
|
|
|
|
+import cn.iselab.mooctest.site.service.instancePermission.GroupPermissionService;
|
|
|
|
+import cn.iselab.mooctest.site.web.exception.IllegalOperationException;
|
|
import org.junit.Assert;
|
|
import org.junit.Assert;
|
|
import org.junit.Before;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.Test;
|
|
@@ -12,11 +21,13 @@ import org.mockito.Mock;
|
|
import org.mockito.MockitoAnnotations;
|
|
import org.mockito.MockitoAnnotations;
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
import org.mockito.runners.MockitoJUnitRunner;
|
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
|
import org.springframework.boot.test.SpringApplicationConfiguration;
|
|
|
|
+import org.springframework.data.domain.*;
|
|
import org.springframework.test.context.web.WebAppConfiguration;
|
|
import org.springframework.test.context.web.WebAppConfiguration;
|
|
-
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
-
|
|
|
|
|
|
+import static org.mockito.Matchers.anyLong;
|
|
|
|
+import static org.mockito.Mockito.doNothing;
|
|
|
|
+import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.when;
|
|
import static org.mockito.Mockito.when;
|
|
/**
|
|
/**
|
|
* Created by major on 2017/6/19.
|
|
* Created by major on 2017/6/19.
|
|
@@ -31,44 +42,286 @@ public class GroupServiceTest {
|
|
|
|
|
|
@Mock
|
|
@Mock
|
|
private GroupDao groupDao;
|
|
private GroupDao groupDao;
|
|
|
|
+ @Mock
|
|
|
|
+ private Group2WorkerDao group2WorkerDao;
|
|
|
|
+ @Mock
|
|
|
|
+ private GroupPermissionService groupPermissionService;
|
|
|
|
+ @Mock
|
|
|
|
+ private UserService userService;
|
|
|
|
+ @Mock
|
|
|
|
+ private AssignedTaskService assignedTaskService;
|
|
|
|
+ @Mock
|
|
|
|
+ private Task2GroupDao task2GroupDao;
|
|
|
|
+ @Mock
|
|
|
|
+ private ManagerPropertyDao managerPropertyDao;
|
|
|
|
+
|
|
|
|
+ private List<Group> groupList = new ArrayList<>();
|
|
|
|
+ private Page<Group> groupPage;
|
|
|
|
+ private List<User> userList = new ArrayList<>();
|
|
|
|
+ private Group group;
|
|
|
|
+ private Group2Worker group2Worker;
|
|
|
|
+ private GroupPermission groupPermission;
|
|
|
|
+ private Sort sort;
|
|
|
|
+ private Pageable pageable;
|
|
|
|
|
|
@Before
|
|
@Before
|
|
public void setUp() {
|
|
public void setUp() {
|
|
MockitoAnnotations.initMocks(this);
|
|
MockitoAnnotations.initMocks(this);
|
|
|
|
+ group = new Group();
|
|
|
|
+ group.setId(1L);
|
|
|
|
+ group.setOwnerId(7L);
|
|
|
|
+ groupList.add(group);
|
|
|
|
+ groupPage = new PageImpl<>(groupList);
|
|
|
|
+ group2Worker = new Group2Worker();
|
|
|
|
+ groupPermission = new GroupPermission();
|
|
|
|
+
|
|
|
|
+ User user1 = new User();
|
|
|
|
+ user1.setId(7L);
|
|
|
|
+ userList.add(user1);
|
|
|
|
+
|
|
|
|
+ sort = new Sort(Sort.Direction.DESC, "id");
|
|
|
|
+ pageable = new PageRequest(0,10);
|
|
|
|
+
|
|
|
|
+ when(group2WorkerDao.save(group2Worker)).thenReturn(group2Worker);
|
|
|
|
+ when(groupPermissionService.joinGroup(anyLong(),anyLong())).thenReturn(groupPermission);
|
|
|
|
+ when(group2WorkerDao.findByParticipantIdAndGroupId(1796L,1L)).thenReturn(group2Worker);
|
|
|
|
+ when(group2WorkerDao.findByParticipantIdAndGroupId(3333L,1L)).thenReturn(null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnAllJoinedGroups_when_givenParticipantId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.findGroupListByParticipant(anyLong())).thenReturn(groupList);
|
|
|
|
+ //action
|
|
|
|
+ List<Group> result = groupService.getGroupsByParticipantId(123L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(result.get(0).getId(),groupList.get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnGroup_when_givenGroupId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.findOne(123L)).thenReturn(group);
|
|
|
|
+ //action
|
|
|
|
+ Group result = groupService.getGroup(123L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(group.getId(),result.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnOwnerGroupList_when_givenOwnerId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.findByOwnerIdAndIsActive(123L,true,sort)).thenReturn(groupList);
|
|
|
|
+ //action
|
|
|
|
+ List<Group> result = groupService.getGroupsByOwnerId(123L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(groupList.get(0).getId(),result.get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnPageGroupList_when_givenOwnerId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.findByOwnerId(123L,pageable)).thenReturn(groupPage);
|
|
|
|
+ //action
|
|
|
|
+ Page<Group> result = groupService.getPageableGroupsByOwnerId(123L,pageable);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(groupList.get(0).getId(),result.getContent().get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnGroupList_whenGivenExamIdAndParticipantId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.findByExamIdAndParticipantId(1L, 7L)).thenReturn(groupList);
|
|
|
|
+ //action
|
|
|
|
+ List<Group> result = groupService.getGroupsByExamIdAndParticipantId(1L,7L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(groupList.get(0).getId(),result.get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnUserList_when_givenGroupList(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.getUserByGroupId(123L)).thenReturn(userList);
|
|
|
|
+ //action
|
|
|
|
+ List<User> result = groupService.getUserByGroupId(123L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(userList.get(0).getId(),result.get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnGroup_when_joinGroup(){
|
|
|
|
+ //arrange
|
|
|
|
+ //action
|
|
|
|
+ Group result1 = groupService.joinGroup(1796L,group);
|
|
|
|
+ Group result2 = groupService.joinGroup(3333L,group);
|
|
|
|
+ try{
|
|
|
|
+ groupService.joinGroup(7L,group);
|
|
|
|
+ }catch(Exception e){
|
|
|
|
+ Assert.assertEquals(IllegalOperationException.class,e.getClass());
|
|
|
|
+ }
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(group.getId(),result1.getId());
|
|
|
|
+ Assert.assertEquals(group.getId(),result2.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnWorkerCount_when_givenGroupId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(group2WorkerDao.countByGroupId(123L)).thenReturn(12);
|
|
|
|
+ //action
|
|
|
|
+ int result = groupService.getWorkerCount(123L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(12,result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnWorkersCount_when_givenGroupIds(){
|
|
|
|
+ //arrange
|
|
|
|
+ List<Long> groupIds = new ArrayList<>();
|
|
|
|
+ groupIds.add(123L);
|
|
|
|
+ when(group2WorkerDao.countByGroupId(123L)).thenReturn(12);
|
|
|
|
+ //action
|
|
|
|
+ List<Integer> result = groupService.getWorkerCounts(groupIds);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(12,result.get(0),0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnUser_whenAddUserIntoGroup(){
|
|
|
|
+ //arrange
|
|
|
|
+ UserDTOForMT userVO = new UserDTOForMT();
|
|
|
|
+ userVO.setId(123L);
|
|
|
|
+ when(userService.findByUserId(3333L)).thenReturn(userVO);
|
|
|
|
+ //action
|
|
|
|
+ User result = groupService.addUserIntoGroup(3333L,1L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(123L,result.getId(),0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnFalse_whenCheckUserExists(){
|
|
|
|
+ //arrange
|
|
|
|
+ //action
|
|
|
|
+ Boolean result = groupService.checkUserExist(1796L,1L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(true,result);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnUser_when_deleteUserFromGroup(){
|
|
|
|
+ //arrange
|
|
|
|
+ UserDTOForMT userDTOForMT = new UserDTOForMT();
|
|
|
|
+ userDTOForMT.setId(123L);
|
|
|
|
+ List<AssignedTask> assignedTasks = new ArrayList<>();
|
|
|
|
+ when(userService.findByUserId(1796L)).thenReturn(userDTOForMT);
|
|
|
|
+ doNothing().when(group2WorkerDao).delete(group2Worker);
|
|
|
|
+ when(assignedTaskService.getUserAssignedTasks(1796L,1L)).thenReturn(assignedTasks);
|
|
|
|
+ when(assignedTaskService.saveOrUpdateAssignedTask(assignedTasks)).thenReturn(assignedTasks);
|
|
|
|
+ //action
|
|
|
|
+ User result = groupService.deleteUserFromGroup(1796L,1L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(123L,result.getId(),0);
|
|
|
|
+ verify(group2WorkerDao).delete(group2Worker);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnGroup_when_saveGroup(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.save(group)).thenReturn(group);
|
|
|
|
+ //action
|
|
|
|
+ Group result = groupService.save(group);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(group.getId(),result.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnGroupIds_when_givenTaskId(){
|
|
|
|
+ //arrange
|
|
|
|
+ List<Task2Group> task2Groups = new ArrayList<>();
|
|
|
|
+ Task2Group task2Group = new Task2Group();
|
|
|
|
+ task2Group.setGroupId(123L);
|
|
|
|
+ task2Groups.add(task2Group);
|
|
|
|
+ when(task2GroupDao.findByTaskId(111L)).thenReturn(task2Groups);
|
|
|
|
+ //action
|
|
|
|
+ List<Long> result = groupService.getGroupIdsByTaskId(111L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(task2Group.getGroupId(),result.get(0),0.0);
|
|
}
|
|
}
|
|
|
|
|
|
@Test
|
|
@Test
|
|
- public void should_returnAllMergedGroups_when_givenExamIdAndParticipantId() {
|
|
|
|
|
|
+ public void should_returnGroup_when_userRegister(){
|
|
//arrange
|
|
//arrange
|
|
- long examId = 193;
|
|
|
|
- long participantId = 1602;
|
|
|
|
|
|
+ //action
|
|
|
|
+ Group result = groupService.joinGroupForRegister(3333L,group);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(group.getId(),result.getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnGroupList_when_givenExamId(){
|
|
|
|
+ //arrange
|
|
|
|
+ when(groupDao.findByTaskId(111L)).thenReturn(groupList);
|
|
|
|
+ //action
|
|
|
|
+ List<Group> result = groupService.getByExamId(111L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(groupList.get(0).getId(),result.get(0).getId());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnUser_when_addUserIntoGroupForOauth2(){
|
|
|
|
+ //arrange
|
|
|
|
+ UserDTOForMT userDTOForMT = new UserDTOForMT();
|
|
|
|
+ userDTOForMT.setId(123L);
|
|
|
|
+ when(userService.findByUserId(3333L)).thenReturn(userDTOForMT);
|
|
|
|
+ //action
|
|
|
|
+ User result = groupService.addUserIntoGroupForOauth2(3333L,1L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(userDTOForMT.getId(),result.getId());
|
|
|
|
+ }
|
|
|
|
|
|
- List<Group> expect = new ArrayList<>();
|
|
|
|
- Group group = new Group();
|
|
|
|
- group.setId(12L);
|
|
|
|
- group.setName("测试班");
|
|
|
|
- group.setManagerId(17L);
|
|
|
|
- group.setAllowJoin(true);
|
|
|
|
- group.setInformation("La La Land");
|
|
|
|
- expect.add(group);
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnTrue_when_givenUserId(){
|
|
|
|
+ //arrange
|
|
|
|
+ ManagerProperty managerProperty = new ManagerProperty();
|
|
|
|
+ managerProperty.setGroupSize(50);
|
|
|
|
+ when(groupDao.findByOwnerIdAndIsActive(7L,true)).thenReturn(groupList);
|
|
|
|
+ when(group2WorkerDao.countByGroupId(1L)).thenReturn(40);
|
|
|
|
+ when(managerPropertyDao.findByUserId(7L)).thenReturn(managerProperty);
|
|
|
|
+ //action
|
|
|
|
+ Boolean result = groupService.checkManagerGroupSize(7L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(true,result);
|
|
|
|
+ }
|
|
|
|
|
|
- Group group2 = new Group();
|
|
|
|
- group.setId(12L);
|
|
|
|
- group.setName("测试班");
|
|
|
|
- group.setManagerId(17L);
|
|
|
|
- group.setAllowJoin(true);
|
|
|
|
- group.setInformation("La La Land");
|
|
|
|
- expect.add(group2);
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnFalse_when_givenUserId(){
|
|
|
|
+ //arrange
|
|
|
|
+ ManagerProperty managerProperty = new ManagerProperty();
|
|
|
|
+ managerProperty.setGroupSize(50);
|
|
|
|
+ when(groupDao.findByOwnerIdAndIsActive(7L,true)).thenReturn(groupList);
|
|
|
|
+ when(group2WorkerDao.countByGroupId(1L)).thenReturn(400);
|
|
|
|
+ when(managerPropertyDao.findByUserId(7L)).thenReturn(managerProperty);
|
|
|
|
+ //action
|
|
|
|
+ Boolean result = groupService.checkManagerGroupSize(7L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(false,result);
|
|
|
|
+ }
|
|
|
|
|
|
- when(groupDao.findByExamIdAndParticipantId(examId, participantId)).thenReturn(expect);
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnFasle_when_givenUserIdAndGroupId(){
|
|
|
|
+ //arrange
|
|
//action
|
|
//action
|
|
- List<Group> result = groupService.getGroupsByExamIdAndParticipantId(examId, participantId);
|
|
|
|
|
|
+ Boolean result = groupService.isUserInGroup(3333L,1L);
|
|
//assert
|
|
//assert
|
|
- Group expectGroup = expect.get(0);
|
|
|
|
- Group resultGroup = result.get(0);
|
|
|
|
- Assert.assertEquals(expectGroup.getId(), resultGroup.getId());
|
|
|
|
- Assert.assertEquals(expectGroup.getName(), resultGroup.getName());
|
|
|
|
- Assert.assertEquals(expectGroup.getInformation(), resultGroup.getInformation());
|
|
|
|
|
|
+ Assert.assertEquals(false,result);
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ @Test
|
|
|
|
+ public void should_returnTrue_when_givenUserIdAndGroupId(){
|
|
|
|
+ //arrange
|
|
|
|
+ //action
|
|
|
|
+ Boolean result = groupService.isUserInGroup(1796L,1L);
|
|
|
|
+ //assert
|
|
|
|
+ Assert.assertEquals(true,result);
|
|
}
|
|
}
|
|
}
|
|
}
|