GetUserTaskCommand extends TaskCommand<Task> { @Override public Task execute(Context cntxt) { TaskContext context = (TaskContext) cntxt; Task task = context.getTaskQueryService().getTaskInstanceById(taskId); if (task == null) { throw new IllegalStateException("Unable to find task with id " + taskId); } if (!checkUserPermissions(userId, task, context)) { throw new PermissionDeniedException("User " + userId + " isn't allowed to see the task " + taskId); } return task; } GetUserTaskCommand();  GetUserTaskCommand(String userId,
                              long taskId); @Override Task execute(Context cntxt);  }
@Test(expected = IllegalStateException.class) public void testGetUnexistentClass() { when(taskQueryService.getTaskInstanceById(TASK_ID)).thenReturn(null); command.execute(taskContext); fail(); }
@Test public void testUserOnPotentialOwnersGroups() { List<String> userGroups = new ArrayList<>(); userGroups.add("user"); when(userGroupCallback.getGroupsForUser(USER_ID)).thenReturn(userGroups); List<OrganizationalEntity> potentialOwners = new ArrayList<>(); potentialOwners.add(new GroupImpl("user")); when(peopleAssignments.getPotentialOwners()).thenReturn(potentialOwners); Task commandTask = command.execute(taskContext); assertEquals(task, commandTask); }
@Test public void testUserOnBusinessAdministratorGroups() { List<String> userGroups = new ArrayList<>(); userGroups.add("admin"); when(userGroupCallback.getGroupsForUser(USER_ID)).thenReturn(userGroups); List<OrganizationalEntity> admins = new ArrayList<>(); admins.add(new GroupImpl("admin")); when(peopleAssignments.getBusinessAdministrators()).thenReturn(admins); Task commandTask = command.execute(taskContext); assertEquals(task, commandTask); }
@Test(expected = PermissionDeniedException.class) public void testUserWithoutPermission() { when(userGroupCallback.getGroupsForUser(USER_ID)).thenReturn(new ArrayList<String>()); when(peopleAssignments.getBusinessAdministrators()).thenReturn(new ArrayList<OrganizationalEntity>()); command.execute(taskContext); fail(); }