public void testGetQueryWorksCorrectly() throws Exception {
    SecurityType securityType =
        (SecurityType) ManagerFactory.getPermissionTypeManager().getSchemeType("reporter");

    // Setup permissions so that a query is created
    GenericValue project = UtilsForTests.getTestEntity("Project", EasyMap.build("name", "Project"));
    PermissionSchemeManager permissionSchemeManager = ManagerFactory.getPermissionSchemeManager();
    GenericValue scheme = permissionSchemeManager.createScheme("Scheme", "scheme");
    permissionSchemeManager.addSchemeToProject(project, scheme);

    SchemeEntity schemeEntity =
        new SchemeEntity(securityType.getType(), null, new Long(Permissions.BROWSE));
    permissionSchemeManager.createSchemeEntity(scheme, schemeEntity);

    Query query = securityType.getQuery(u, project, null);
    assertEquals(
        "(+"
            + DocumentConstants.PROJECT_ID
            + ":"
            + project.getLong("id")
            + " +"
            + DocumentConstants.ISSUE_AUTHOR
            + ":owen)",
        query.toString(""));
  }
  public void testDoUpgrade() throws Exception {
    for (int i = 1; i <= PROJECTS_WITH_PERMS; i++) {
      if (i > Permissions.MAX_PERMISSION) break;
      projectPerms = EntityUtils.createValue("Project", EasyMap.build("name", "Project" + i));
      EntityUtils.createValue(
          "Permission",
          EasyMap.build(
              "project", projectPerms.getLong("id"), "type", new Long(10 + i), "group", "test"));
    }

    for (int i = 1; i <= PROJECTS_WITHOUT_PERMS; i++) {
      projectNoPerms = EntityUtils.createValue("Project", EasyMap.build("name", "No Perms" + i));
    }

    task.doUpgrade(false);

    // check that the default permission scheme was added
    assertNotNull(psm.getDefaultScheme());

    // All projects with no permissions should be added to default scheme
    assertEquals(psm.getProjects(psm.getDefaultScheme()).size(), PROJECTS_WITHOUT_PERMS);

    // There should be a scheme for all . The default and the one for project with permissions
    assertEquals(psm.getSchemes().size(), PROJECTS_WITH_PERMS + 1);
  }
  public void testMergeDuplicateSchemes() throws Exception {

    for (int i = 1; i <= PROJECTS_WITH_PERMS; i++) {
      // if (i > Permissions.MAX_PERMISSION) break;
      projectPerms = EntityUtils.createValue("Project", EasyMap.build("name", "Test Project" + i));
      EntityUtils.createValue(
          "Permission",
          EasyMap.build(
              "project", projectPerms.getLong("id"), "type", new Long(10), "group", "test"));
    }

    task.createProjectSchemes(psm, psm.createDefaultScheme());

    task.mergeDuplicateSchemes(psm);

    // There should be a scheme for projects with permissions and the default
    assertEquals(psm.getSchemes().size(), 2);

    List schemes = psm.getSchemes();
    for (int i = 0; i < schemes.size(); i++) {
      GenericValue scheme = (GenericValue) schemes.get(i);

      List projects = psm.getProjects(scheme);
      for (int j = 0; j < projects.size(); j++) {
        GenericValue project = (GenericValue) projects.get(j);
      }
    }
  }
  public void testIsDuplicate() throws Exception {

    GenericValue schemeA = psm.createScheme("testA", "test");
    GenericValue schemeB = psm.createScheme("testB", "test");
    GenericValue schemeC = psm.createScheme("testC", "test");

    SchemeEntity schemeEntity = new SchemeEntity(GroupDropdown.DESC, "test", new Long(2));
    SchemeEntity schemeEntity2 = new SchemeEntity(GroupDropdown.DESC, "test", new Long(3));

    psm.createSchemeEntity(schemeA, schemeEntity);
    psm.createSchemeEntity(schemeB, schemeEntity);
    psm.createSchemeEntity(schemeC, schemeEntity2);

    assertTrue(task.isDuplicate(psm, schemeA, schemeB));
    assertTrue(task.isDuplicate(psm, schemeB, schemeA));
    assertTrue(!task.isDuplicate(psm, schemeA, schemeC));
    assertTrue(!task.isDuplicate(psm, schemeB, schemeC));
  }
  public void testaddOldGlobalPermissionsToScheme() throws Exception {
    GenericValue scheme = psm.createScheme("test", "test");

    EntityUtils.createValue(
        "Permission", EasyMap.build("project", null, "type", new Long(99), "group", "test"));

    task.addOldGlobalPermissionsToScheme(psm, 99, scheme);

    assertNotNull(psm.getScheme("test"));

    List perms = psm.getEntities(scheme, new Long(99), "test");

    assertTrue(perms.size() > 0);

    assertTrue(perms.size() == 1);

    GenericValue p = (GenericValue) perms.get(0);
    assertEquals(p.getString("parameter"), "test");
  }
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    final FieldVisibilityManager visibilityBean = EasyMock.createMock(FieldVisibilityManager.class);
    EasyMock.expect(
            visibilityBean.isFieldHidden(
                (String) EasyMock.anyObject(), (Issue) EasyMock.anyObject()))
        .andReturn(false)
        .anyTimes();
    EasyMock.replay(visibilityBean);
    ManagerFactory.addService(FieldVisibilityManager.class, visibilityBean);

    originalFactory =
        ComponentManager.getComponentInstanceOfType(FieldClausePermissionChecker.Factory.class);
    ManagerFactory.addService(
        FieldClausePermissionChecker.Factory.class, new MockFieldClausePermissionFactory());
    bob = createMockUser("bob");

    final Group group = addUserToNewGroup(bob, "group");
    carl = createMockUser("carl");

    final GenericValue project =
        UtilsForTests.getTestEntity("Project", EasyMap.build("key", "TST"));

    final PermissionSchemeManager permissionSchemeManager =
        ManagerFactory.getPermissionSchemeManager();
    final GenericValue defaultScheme = permissionSchemeManager.createDefaultScheme();
    final SchemeEntity schemeEntity =
        new SchemeEntity(GroupDropdown.DESC, null, Permissions.BROWSE);
    permissionSchemeManager.createSchemeEntity(defaultScheme, schemeEntity);

    permissionSchemeManager.addSchemeToProject(project, defaultScheme);

    UtilsForTests.getTestEntity("Resolution", EasyMap.build("id", resolutionId));
    // Create two Issues with same comment but different level
    final GenericValue issue1 =
        UtilsForTests.getTestEntity(
            "Issue",
            EasyMap.build(
                "project",
                project.getLong("id"),
                "key",
                "TST-1",
                "resolution",
                resolutionId,
                "summary",
                SUMMARY_BODY));
    UtilsForTests.getTestEntity(
        "Action",
        EasyMap.build(
            "issue",
            issue1.getLong("id"),
            "type",
            ActionConstants.TYPE_COMMENT,
            "body",
            COMMENT_BODY));

    final GenericValue issue2 =
        UtilsForTests.getTestEntity(
            "Issue",
            EasyMap.build(
                "project",
                project.getLong("id"),
                "key",
                "TST-2",
                "resolution",
                resolutionId,
                "summary",
                SUMMARY_BODY));
    UtilsForTests.getTestEntity(
        "Action",
        EasyMap.build(
            "issue",
            issue2.getLong("id"),
            "type",
            ActionConstants.TYPE_COMMENT,
            "body",
            COMMENT_BODY,
            "level",
            group.getName()));

    final GenericValue issue3 =
        UtilsForTests.getTestEntity(
            "Issue",
            EasyMap.build(
                "project",
                project.getLong("id"),
                "key",
                "TST-3",
                "resolution",
                resolutionId,
                "summary",
                ANOTHER_SUMMARY_BODY));
    UtilsForTests.getTestEntity(
        "Action",
        EasyMap.build(
            "issue",
            issue3.getLong("id"),
            "type",
            ActionConstants.TYPE_COMMENT,
            "body",
            ANOTHER_COMMENT_BODY,
            "level",
            group.getName()));

    ManagerFactory.getIndexManager().reIndexAll();
  }
  protected void setUp() throws Exception {
    super.setUp();

    user = createMockUser("logged-in-user");
    secondaryUser = createMockUser("testuser");
    g = createMockGroup("jira-user");
    addUserToGroup(user, g);
    addUserToGroup(secondaryUser, g);
    JiraTestUtil.loginUser(user);
    ComponentAccessor.getApplicationProperties()
        .setOption(APKeys.JIRA_OPTION_ALLOWUNASSIGNED, true);
    ManagerFactory.getGlobalPermissionManager().addPermission(Permissions.ADMINISTER, null);

    SchemeManager permManager = ManagerFactory.getPermissionSchemeManager();
    GenericValue scheme = permManager.createDefaultScheme();
    PermissionManager pm = ManagerFactory.getPermissionManager();
    pm.addPermission(Permissions.CREATE_ISSUE, scheme, "jira-user", "group");
    pm.addPermission(Permissions.BROWSE, scheme, "jira-user", "group");
    pm.addPermission(Permissions.RESOLVE_ISSUE, scheme, "jira-user", "group");
    pm.addPermission(Permissions.ASSIGN_ISSUE, scheme, "jira-user", "group");
    pm.addPermission(Permissions.CLOSE_ISSUE, scheme, "jira-user", "group");
    pm.addPermission(Permissions.WORK_ISSUE, scheme, "jira-user", "group");
    pm.addPermission(Permissions.ASSIGNABLE_USER, scheme, "jira-user", "group");

    // Build IssueConstants
    UpgradeTask_Build11 upgradeTask_build11 =
        new UpgradeTask_Build11(ComponentAccessor.getConstantsManager());
    upgradeTask_build11.doUpgrade(false);

    // Build the default statuses
    UpgradeTask_Build27 upgradeTask_build27 = new UpgradeTask_Build27();
    upgradeTask_build27.doUpgrade(false);

    // Create field screens
    UpgradeTask upgradeTask = (UpgradeTask) JiraUtils.loadComponent(UpgradeTask_Build83.class);
    upgradeTask.doUpgrade(false);

    // Build default IssueTypeScheme
    UpgradeTask upgradeTask101 = (UpgradeTask) JiraUtils.loadComponent(UpgradeTask_Build101.class);
    upgradeTask101.doUpgrade(false);

    JiraAuthenticationContext authenticationContext =
        ComponentAccessor.getJiraAuthenticationContext();
    // Log in user
    authenticationContext.setLoggedInUser(user);
    GenericValue project =
        UtilsForTests.getTestEntity(
            "Project",
            EasyMap.build(
                "key", "ABC", "name", "A Project", "lead", user.getName(), "counter", new Long(1)));
    ComponentAccessor.getIssueTypeScreenSchemeManager().associateWithDefaultScheme(project);
    ManagerFactory.getProjectManager().refresh();

    final PermissionSchemeManager permissionSchemeManager =
        ManagerFactory.getPermissionSchemeManager();
    permissionSchemeManager.addSchemeToProject(project, scheme);

    // Create a REAL issue (with valid transitions and everything) with key ABC-2
    final MutableIssue issue1 = ComponentAccessor.getIssueFactory().getIssue();
    issue1.setProject(project);
    issue1.setAssignee(user);
    GenericValue issueTypeGV = ComponentAccessor.getConstantsManager().getIssueType("1");
    issue1.setIssueType(issueTypeGV);
    ComponentAccessor.getIssueManager().createIssue(user, issue1);

    // Create a second REAL issue with key ABC-3
    final MutableIssue issue2 = ComponentAccessor.getIssueFactory().getIssue();
    issue2.setProject(project);
    issue2.setAssignee(secondaryUser);
    issue2.setIssueType(issueTypeGV);
    ComponentAccessor.getIssueManager().createIssue(secondaryUser, issue2);

    // Create a SearchRequest to return the two issues
    sr =
        UtilsForTests.getTestEntity(
            "SearchRequest",
            EasyMap.build(
                "id",
                new Long(10241),
                "name",
                "test",
                "author",
                "logged-in-user",
                "user",
                "logged-in-user",
                "group",
                "jira-user",
                "project",
                project.get("id"),
                "request",
                "project = " + project.get("id")));
  }