@Test
 public void testGetProjectByLeadWhereNoProjectsExistForLead()
     throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
   final Collection<Project> projectsWithoutLead =
       testedObject.getProjectsLeadBy(userMockFactory.getComponentLead());
   assertEquals(0, projectsWithoutLead.size());
 }
  @Test
  public void testGetProjectByLead()
      throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
    final GenericEntity projectWithDefaultAssigneeLead =
        projectMockFactory.getProjectWithDefaultAssigneeLead();

    // Now test that we can retrieve the project with lead
    final Collection<Project> projectsWithLead =
        testedObject.getProjectsLeadBy(userMockFactory.getProjectLead());
    assertEquals(1, projectsWithLead.size());
    assertEquals("projectWithAssigneeLead", projectWithDefaultAssigneeLead.getString("name"));
  }
  @Test
  public void testDefaultAssigneeWithNoUnassigned()
      throws DefaultAssigneeException, OperationNotPermittedException, InvalidUserException,
          InvalidCredentialException {
    final User projectLead = userMockFactory.getProjectLead();
    final GenericValue projectWithDefaultAssigneeLead =
        projectMockFactory.getProjectWithDefaultAssigneeLead();

    // Should be false as project lead cannot be assigned issues.
    _testNoDefaultAssignee(projectWithDefaultAssigneeLead, null);

    when(permissionManager.hasPermission(
            Permissions.ASSIGNABLE_USER, projectWithDefaultAssigneeLead, projectLead))
        .thenReturn(Boolean.TRUE);

    // Should be true as project lead can be assigned issues.
    _testDefaultAssignee(projectWithDefaultAssigneeLead, null, projectLead);
  }
  @Test
  public void testDefaultAssigneeWithUnassigned()
      throws DefaultAssigneeException, GenericEntityException, OperationNotPermittedException,
          InvalidUserException, InvalidCredentialException {
    final User projectLead = userMockFactory.getProjectLead();
    final GenericValue projectWithDefaultAssigneeLead =
        projectMockFactory.getProjectWithDefaultAssigneeLead();
    final GenericValue projectWithDefaultUnassigned =
        projectMockFactory.getProjectWithDefaultUnassigned();

    // Should be false as unassigned is turned off and project lead cannot be assigned issues.
    _testNoDefaultAssignee(projectWithDefaultUnassigned, null);

    when(permissionManager.hasPermission(
            Permissions.ASSIGNABLE_USER, projectWithDefaultAssigneeLead, projectLead))
        .thenReturn(Boolean.TRUE);
    when(permissionManager.hasPermission(
            Permissions.ASSIGNABLE_USER, projectWithDefaultUnassigned, projectLead))
        .thenReturn(Boolean.TRUE);

    // Should be false as unassigned is turned off and the lead is null so it fails
    _testNoDefaultAssignee(projectWithDefaultUnassigned, null);

    projectWithDefaultUnassigned.set("lead", projectLead.getName());
    projectWithDefaultUnassigned.store();

    // Should be true as unassigned is turned off and project lead can be assigned issues,
    // so it defaults to project lead.
    assertTrue(testedObject.isDefaultAssignee(projectWithDefaultUnassigned, null));

    final User defaultAssignee =
        testedObject.getDefaultAssignee(projectWithDefaultUnassigned, null);
    assertEquals(projectLead, defaultAssignee);

    // Turn on unassigned
    ComponentAccessor.getApplicationProperties()
        .setOption(APKeys.JIRA_OPTION_ALLOWUNASSIGNED, true);

    // Reset permissions

    // Should be true as unassigned is turned on
    _testDefaultAssignee(projectWithDefaultUnassigned, null, null);
  }
  @Test
  public void testDefaultAssigneeProjectLead()
      throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
    final User projectLead = userMockFactory.getProjectLead();
    final GenericValue projectWithDefaultAssigneeLead =
        projectMockFactory.getProjectWithDefaultAssigneeLead();
    final GenericValue componentWithProjectLeadAssignee =
        componentMockFactory.getComponentWithProjectLeadAssignee();

    // Should return false as project lead is unassignable
    _testNoDefaultAssignee(projectWithDefaultAssigneeLead, componentWithProjectLeadAssignee);

    when(permissionManager.hasPermission(
            Permissions.ASSIGNABLE_USER, projectWithDefaultAssigneeLead, projectLead))
        .thenReturn(Boolean.TRUE);

    // Should return true as project lead is assignable
    _testDefaultAssignee(
        projectWithDefaultAssigneeLead, componentWithProjectLeadAssignee, projectLead);
  }
  @Test
  public void testDefaultAssigneeComponentLead()
      throws OperationNotPermittedException, InvalidUserException, InvalidCredentialException {
    final User componentLead = userMockFactory.getComponentLead();
    final GenericValue projectWithDefaultUnassigned =
        projectMockFactory.getProjectWithDefaultUnassigned();
    final GenericValue componentWithComponentAssignee =
        componentMockFactory.getComponentWithComponentAssignee();

    // Should return false as components lead is unassignable, unassigned is turned off and project
    // lead is unassignable
    _testNoDefaultAssignee(projectWithDefaultUnassigned, componentWithComponentAssignee);

    when(permissionManager.hasPermission(
            Permissions.ASSIGNABLE_USER,
            projectFactory.getProject(projectWithDefaultUnassigned),
            ApplicationUsers.from(componentLead)))
        .thenReturn(Boolean.TRUE);

    // Should return true as component lead is assignable
    _testDefaultAssignee(
        projectWithDefaultUnassigned, componentWithComponentAssignee, componentLead);
  }