Search in sources :

Example 1 with DataMayNotBeApprovedException

use of org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException in project dhis2-core by dhis2.

the class DefaultDataApprovalService method approveData.

// -------------------------------------------------------------------------
// Data approval logic
// -------------------------------------------------------------------------
@Override
public void approveData(List<DataApproval> dataApprovalList) {
    log.debug("approveData ( " + dataApprovalList.size() + " items )");
    Map<String, DataApprovalStatus> statusMap = getStatusMap(dataApprovalList);
    List<DataApproval> checkedList = new ArrayList<>();
    for (DataApproval da : dataApprovalList) {
        DataApprovalStatus status = statusMap.get(daKey(da));
        if (status == null) {
            log.info("approveData: data may not be approved, status = null " + " " + da);
            throw new DataMayNotBeApprovedException();
        } else if (status.getState().isApproved()) {
            if (da.getDataApprovalLevel() != null && status.getActionLevel() != null && da.getDataApprovalLevel().getLevel() >= status.getActionLevel().getLevel()) {
                // Already approved at or above the level requested
                continue;
            }
            DataApprovalLevel nextLevel = nextHigherLevel(status.getActionLevel(), da.getWorkflow());
            if (da.getDataApprovalLevel() == null) {
                da.setDataApprovalLevel(nextLevel);
            } else if (da.getDataApprovalLevel().getLevel() != nextLevel.getLevel()) {
                log.info("approveData: data approved under workflow " + da.getWorkflow().getName() + " at level " + status.getActionLevel().getLevel() + " is ready for level " + nextLevel.getLevel() + ", not level " + da.getDataApprovalLevel().getLevel());
                throw new DataMayNotBeApprovedException();
            }
        } else if (da.getDataApprovalLevel() == null) {
            da.setDataApprovalLevel(status.getActionLevel());
        } else if (da.getDataApprovalLevel().getLevel() != status.getActionLevel().getLevel()) {
            log.info("approveData: unapproved data in workflow " + da.getWorkflow().getName() + " must first be approved at level " + status.getActionLevel().getLevel());
            throw new DataMayNotBeApprovedException();
        }
        if (!status.getPermissions().isMayApprove()) {
            log.info("approveData: data may not be approved, state " + status.getState().name() + " " + da);
            throw new DataMayNotBeApprovedException();
        }
        if (da.getOrganisationUnit().getLevel() != da.getDataApprovalLevel().getOrgUnitLevel()) {
            log.info("approveData: org unit " + da.getOrganisationUnit().getUid() + " '" + da.getOrganisationUnit().getName() + "' has wrong org unit level " + da.getOrganisationUnit().getLevel() + " for approval level " + da.getDataApprovalLevel().getLevel());
            throw new DataMayNotBeApprovedException();
        }
        checkedList.add(da);
    }
    for (DataApproval da : checkedList) {
        log.debug("-> approving " + da);
        audit(da, APPROVE);
        dataApprovalStore.addDataApproval(da);
    }
    log.info("Approvals saved: " + checkedList.size());
}
Also used : ArrayList(java.util.ArrayList) DataMayNotBeApprovedException(org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException)

Example 2 with DataMayNotBeApprovedException

use of org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testMayApproveSameLevel.

@Test
@Category(IntegrationTest.class)
public void testMayApproveSameLevel() {
    Set<OrganisationUnit> units = newHashSet(organisationUnitB);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE, AUTH_APPR_LEVEL);
    userService.addUser(currentUserService.getCurrentUser());
    setCurrentUserServiceDependencies(currentUserService);
    Date date = new Date();
    // Level 4 (organisationUnitD and organisationUnitF ready)
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 3 (organisationUnitC) and Level 4 (organisationUnitF) ready
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitD, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit C.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level4, workflow1234, periodA, organisationUnitD, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 2 (organisationUnitB) ready
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitF, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit F.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level4, workflow1234, periodA, organisationUnitF, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitE, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit E.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level3, workflow1234, periodA, organisationUnitE, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitC, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit C.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level3, workflow1234, periodA, organisationUnitC, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo));
    assertEquals("APPROVED_HERE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo));
    assertEquals("APPROVED_HERE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo));
    // Level 1 (organisationUnitA) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level2, workflow1234, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertEquals("APPROVED_HERE level=level2 approve=F unapprove=T accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level3 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo));
    assertEquals("APPROVED_ABOVE level=level4 approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo));
    // Level 1 (organisationUnitA) try to approve
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level1, workflow1234, periodA, organisationUnitA, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit A.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataMayNotBeApprovedException(org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException) Date(java.util.Date) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 3 with DataMayNotBeApprovedException

use of org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testApprovalsWithCategories.

// -------------------------------------------------------------------------
// Test with Categories
// -------------------------------------------------------------------------
@Test
@Category(IntegrationTest.class)
public void testApprovalsWithCategories() {
    setUpCategories();
    Set<OrganisationUnit> units = newHashSet(organisationUnitA);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE, DataApproval.AUTH_APPROVE_LOWER_LEVELS, DataApproval.AUTH_ACCEPT_LOWER_LEVELS, AUTH_APPR_LEVEL);
    userService.addUser(currentUserService.getCurrentUser());
    setCurrentUserServiceDependencies(currentUserService);
    Date date = new Date();
    // Category A -> Options A,B,C,D
    // Category B -> Options E,F,G,H
    //
    // Option combo A -> Options A,E -> Groups A,C
    // Option combo B -> Options A,F -> Groups A,C
    // Option combo C -> Options A,G -> Groups A,D
    // Option combo D -> Options A,H -> Groups A,D
    // Option combo E -> Options B,E -> Groups B,C
    // Option combo F -> Options B,F -> Groups B,C
    // Option combo G -> Options B,G -> Groups B,D
    // Option combo H -> Options B,H -> Groups B,D
    // Option combo I -> Options C,E -> Groups B,D
    // Option combo J -> Options C,F -> Groups B,D
    // Option combo K -> Options C,G -> Groups B,D
    List<DataApprovalLevel> levels = dataApprovalLevelService.getAllDataApprovalLevels();
    assertEquals(6, levels.size());
    assertEquals("level1", levels.get(0).getName());
    assertEquals("level2", levels.get(1).getName());
    assertEquals("level2ABCD", levels.get(2).getName());
    assertEquals("level2EFGH", levels.get(3).getName());
    assertEquals("level3", levels.get(4).getName());
    assertEquals("level4", levels.get(5).getName());
    levels = workflow12A_H.getSortedLevels();
    assertEquals(4, levels.size());
    assertEquals("level1", levels.get(0).getName());
    assertEquals("level2", levels.get(1).getName());
    assertEquals("level2ABCD", levels.get(2).getName());
    assertEquals("level2EFGH", levels.get(3).getName());
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, null));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, null));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAE));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAE));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAF));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAF));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAG));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAG));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCE));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCE));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCF));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCF));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCG));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCG));
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level1, workflow12A_H, periodA, organisationUnitA, optionComboAE, NOT_ACCEPTED, date, userA)));
        fail("organisationUnitB should not be ready for approval at level 1.");
    } catch (DataMayNotBeApprovedException ex) {
    // Can't add this approval before approving at a lower level.
    }
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level2, workflow12A_H, periodA, organisationUnitA, optionComboAE, NOT_ACCEPTED, date, userA)));
        fail("organisationUnitB should not be ready for approval at level 2.");
    } catch (DataMayNotBeApprovedException ex) {
    // Can't add this approval before approving at a lower level.
    }
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level2ABCD, workflow12A_H, periodA, organisationUnitA, optionComboAE, NOT_ACCEPTED, date, userA)));
        fail("organisationUnitB should not be ready for approval at level 2ABCD.");
    } catch (DataMayNotBeApprovedException ex) {
    // Can't add this approval before approving at a lower level.
    }
    dataApprovalService.approveData(newArrayList(new DataApproval(level2EFGH, workflow12A_H, periodA, organisationUnitB, optionComboAE, NOT_ACCEPTED, date, userA)));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, null));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, null));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAE));
    assertEquals("APPROVED_HERE level=level2EFGH approve=T unapprove=T accept=T unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAE));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAF));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAF));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAG));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAG));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCE));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCE));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCF));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCF));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCG));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCG));
    dataApprovalService.approveData(newArrayList(new DataApproval(level2EFGH, workflow12A_H, periodA, organisationUnitB, optionComboAF, ACCEPTED, date, userA)));
    dataApprovalService.approveData(newArrayList(new DataApproval(level2EFGH, workflow12A_H, periodA, organisationUnitB, optionComboCG, NOT_ACCEPTED, date, userA)));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, null));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, null));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAE));
    assertEquals("APPROVED_HERE level=level2EFGH approve=T unapprove=T accept=T unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAE));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAF));
    assertEquals("ACCEPTED_HERE level=level2EFGH approve=T unapprove=T accept=F unaccept=T read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAF));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAG));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAG));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCE));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCE));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCF));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCF));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboCG));
    assertEquals("APPROVED_HERE level=level2EFGH approve=T unapprove=T accept=T unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboCG));
    dataApprovalService.approveData(newArrayList(new DataApproval(level2ABCD, workflow12A_H, periodA, organisationUnitB, optionComboAF, ACCEPTED, date, userA)));
    assertEquals("UNAPPROVED_WAITING level=null approve=F unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAF));
    assertEquals("ACCEPTED_HERE level=level2ABCD approve=T unapprove=T accept=F unaccept=T read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAF));
    dataApprovalService.approveData(newArrayList(new DataApproval(level2, workflow12A_H, periodA, organisationUnitB, optionComboAF, ACCEPTED, date, userA)));
    assertEquals("UNAPPROVED_READY level=null approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAF));
    assertEquals("ACCEPTED_HERE level=level2 approve=T unapprove=T accept=F unaccept=T read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAF));
    dataApprovalService.approveData(newArrayList(new DataApproval(level1, workflow12A_H, periodA, organisationUnitA, optionComboAF, NOT_ACCEPTED, date, userA)));
    assertEquals("APPROVED_HERE level=level1 approve=F unapprove=T accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitA, optionComboAF));
    assertEquals("APPROVED_ABOVE level=level2 approve=T unapprove=F accept=F unaccept=F read=T", statusAndPermissions(workflow12A_H, periodA, organisationUnitB, optionComboAF));
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataMayNotBeApprovedException(org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException) Date(java.util.Date) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 4 with DataMayNotBeApprovedException

use of org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testMayApproveSameAndLowerLevels.

@Test
@Category(IntegrationTest.class)
public void testMayApproveSameAndLowerLevels() {
    Set<OrganisationUnit> units = newHashSet(organisationUnitB);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE, DataApproval.AUTH_APPROVE_LOWER_LEVELS, AUTH_APPR_LEVEL);
    userService.addUser(currentUserService.getCurrentUser());
    setCurrentUserServiceDependencies(currentUserService);
    Date date = new Date();
    // Level 4 (organisationUnitD and organisationUnitF ready)
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 3 (organisationUnitC) and Level 4 (organisationUnitF) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitD, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 2 (organisationUnitB) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitF, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitE, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitC, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 1 (organisationUnitA) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level2, workflow1234, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 1 (organisationUnitA) try to approve
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level1, workflow1234, periodA, organisationUnitA, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit A.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataMayNotBeApprovedException(org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException) Date(java.util.Date) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Example 5 with DataMayNotBeApprovedException

use of org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException in project dhis2-core by dhis2.

the class DataApprovalServiceTest method testMayApproveLowerLevels.

@Test
@Category(IntegrationTest.class)
public void testMayApproveLowerLevels() {
    Set<OrganisationUnit> units = newHashSet(organisationUnitB);
    CurrentUserService currentUserService = new MockCurrentUserService(units, null, DataApproval.AUTH_APPROVE_LOWER_LEVELS, AUTH_APPR_LEVEL);
    userService.addUser(currentUserService.getCurrentUser());
    setCurrentUserServiceDependencies(currentUserService);
    Date date = new Date();
    // Level 4 (organisationUnitD and organisationUnitF ready)
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 3 (organisationUnitC) and Level 4 (organisationUnitF) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitD, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 2 (organisationUnitB) ready
    dataApprovalService.approveData(newArrayList(new DataApproval(level4, workflow1234, periodA, organisationUnitF, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitE, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    dataApprovalService.approveData(newArrayList(new DataApproval(level3, workflow1234, periodA, organisationUnitC, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 1 (organisationUnitA) ready
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level2, workflow1234, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit B.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected error, so add the data through dataApprovalStore:
    }
    dataApprovalStore.addDataApproval(new DataApproval(level2, workflow1234, periodA, organisationUnitB, defaultOptionCombo, NOT_ACCEPTED, date, userA));
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitB, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitC, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitD, defaultOptionCombo).getPermissions().isMayApprove());
    assertFalse(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitE, defaultOptionCombo).getPermissions().isMayApprove());
    assertTrue(dataApprovalService.getDataApprovalStatusAndPermissions(workflow1234, periodA, organisationUnitF, defaultOptionCombo).getPermissions().isMayApprove());
    // Level 1 (organisationUnitA) try to approve
    try {
        dataApprovalService.approveData(newArrayList(new DataApproval(level1, workflow1234, periodA, organisationUnitA, defaultOptionCombo, NOT_ACCEPTED, date, userA)));
        fail("User should not have permission to approve org unit A.");
    } catch (DataMayNotBeApprovedException ex) {
    // Expected
    }
}
Also used : OrganisationUnit(org.hisp.dhis.organisationunit.OrganisationUnit) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) MockCurrentUserService(org.hisp.dhis.mock.MockCurrentUserService) CurrentUserService(org.hisp.dhis.user.CurrentUserService) DataMayNotBeApprovedException(org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException) Date(java.util.Date) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) Category(org.junit.experimental.categories.Category) IntegrationTest(org.hisp.dhis.IntegrationTest) Test(org.junit.Test) DhisTest(org.hisp.dhis.DhisTest)

Aggregations

DataMayNotBeApprovedException (org.hisp.dhis.dataapproval.exceptions.DataMayNotBeApprovedException)5 Date (java.util.Date)4 DhisTest (org.hisp.dhis.DhisTest)4 IntegrationTest (org.hisp.dhis.IntegrationTest)4 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)4 MockCurrentUserService (org.hisp.dhis.mock.MockCurrentUserService)4 OrganisationUnit (org.hisp.dhis.organisationunit.OrganisationUnit)4 CurrentUserService (org.hisp.dhis.user.CurrentUserService)4 Test (org.junit.Test)4 Category (org.junit.experimental.categories.Category)4 ArrayList (java.util.ArrayList)1