Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore:Replace organisation units with no groups integrity check with SQL #15801

Merged
merged 9 commits into from
Nov 30, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public enum DataIntegrityCheckType {
// OrganisationUnits
ORG_UNITS_WITH_CYCLIC_REFERENCES,
ORG_UNITS_BEING_ORPHANED,
ORG_UNITS_WITHOUT_GROUPS,
ORG_UNITS_VIOLATING_EXCLUSIVE_GROUP_SETS,
ORG_UNIT_GROUPS_WITHOUT_GROUP_SETS,

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,9 @@ public FlattenedDataIntegrityReport(Map<String, DataIntegrityDetails> detailsByN
this.orphanedOrganisationUnits =
listOfDisplayNameOrUid(
detailsByName.get(DataIntegrityCheckType.ORG_UNITS_BEING_ORPHANED.getName()));
// Replaced with SQL based equivalent
this.organisationUnitsWithoutGroups =
listOfDisplayNameOrUid(
detailsByName.get(DataIntegrityCheckType.ORG_UNITS_WITHOUT_GROUPS.getName()));
listOfDisplayNameOrUid(detailsByName.get("orgunits_no_groups"));
this.organisationUnitGroupsWithoutGroupSets =
listOfDisplayNameOrUid(
detailsByName.get(DataIntegrityCheckType.ORG_UNIT_GROUPS_WITHOUT_GROUP_SETS.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -444,10 +444,6 @@ List<DataIntegrityIssue> getOrphanedOrganisationUnits() {
return toSimpleIssueList(organisationUnitService.getOrphanedOrganisationUnits().stream());
}

List<DataIntegrityIssue> getOrganisationUnitsWithoutGroups() {
return toSimpleIssueList(organisationUnitService.getOrganisationUnitsWithoutGroups().stream());
}

List<DataIntegrityIssue> getOrganisationUnitsViolatingExclusiveGroupSets() {
return toIssueList(
organisationUnitService.getOrganisationUnitsViolatingExclusiveGroupSets().stream(),
Expand Down Expand Up @@ -609,10 +605,6 @@ public void initIntegrityChecks() {
DataIntegrityCheckType.ORG_UNITS_BEING_ORPHANED,
OrganisationUnit.class,
this::getOrphanedOrganisationUnits);
registerNonDatabaseIntegrityCheck(
DataIntegrityCheckType.ORG_UNITS_WITHOUT_GROUPS,
OrganisationUnit.class,
this::getOrganisationUnitsWithoutGroups);
registerNonDatabaseIntegrityCheck(
DataIntegrityCheckType.ORG_UNITS_VIOLATING_EXCLUSIVE_GROUP_SETS,
OrganisationUnit.class,
Expand All @@ -621,7 +613,6 @@ public void initIntegrityChecks() {
DataIntegrityCheckType.ORG_UNIT_GROUPS_WITHOUT_GROUP_SETS,
OrganisationUnitGroup.class,
this::getOrganisationUnitGroupsWithoutGroupSets);

registerNonDatabaseIntegrityCheck(
DataIntegrityCheckType.VALIDATION_RULES_WITHOUT_GROUPS,
ValidationRule.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ checks:
- orgunits/orgunits_invalid_geometry.yaml
- orgunits/orgunits_no_geometry.yaml
- orgunits/orgunits_same_name_and_parent.yaml
- orgunits/orgunits_no_groups.yaml
- option_sets/option_sets_empty.yaml
- option_sets/unused_option_sets.yaml
- option_sets/option_sets_wrong_sort_order.yaml
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Copyright (c) 2004-2022, University of Oslo
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# Neither the name of the HISP project nor the names of its contributors may
# be used to endorse or promote products derived from this software without
# specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
---
name: orgunits_no_groups
description: Organisation units with no groups.
section: Organisation units
section_order: 13
summary_sql: >-
WITH orgunits_no_groups as (
SELECT uid,name from organisationunit
where organisationunitid NOT IN (
SELECT organisationunitid from orgunitgroupmembers))
SELECT
COUNT(*)as value,
100.0 * COUNT(*) / NULLIF( (SELECT COUNT(*) FROM organisationunit), 0) as percent
FROM orgunits_no_groups;
details_sql: >-
SELECT uid,name from organisationunit
where organisationunitid NOT IN (
SELECT organisationunitid from orgunitgroupmembers)
ORDER BY name;
details_id_type: organisationUnits
severity: WARNING
introduction: |
All organisation units should usually belong to at least one organisation unit group.
When organisation units do not belong to any groups, they become more difficult to identify
in analysis apps like the data visualizer.
recommendation: |
Create useful organisation unit groups to help users filter certain classes of organisation
units. These groups may or may not be used in organisation unit group sets
Original file line number Diff line number Diff line change
Expand Up @@ -428,13 +428,6 @@ void testGetOrphanedOrganisationUnits() {
verifyNoMoreInteractions(organisationUnitService);
}

@Test
void testGetOrganisationUnitsWithoutGroups() {
subject.getOrganisationUnitsWithoutGroups();
verify(organisationUnitService).getOrganisationUnitsWithoutGroups();
verifyNoMoreInteractions(organisationUnitService);
}

@Test
void testGetProgramRulesWithNoExpression() {
programRuleB.setCondition(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void testReadDataIntegrityYaml() {

List<DataIntegrityCheck> checks = new ArrayList<>();
readYaml(checks, "data-integrity-checks.yaml", "data-integrity-checks", CLASS_PATH);
assertEquals(63, checks.size());
assertEquals(64, checks.size());

// Names should be unique
List<String> allNames = checks.stream().map(DataIntegrityCheck::getName).toList();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.webapi.controller.dataintegrity;

import static org.hisp.dhis.web.WebClientUtils.assertStatus;

import org.hisp.dhis.web.HttpStatus;
import org.junit.jupiter.api.Test;

/**
* Checks for organisation units with no groups. {@see
* dhis-2/dhis-services/dhis-service-administration/src/main/resources/data-integrity-checks/orgunits/orgunits_no_groups.yaml}
*
* @author Jason P. Pickering
*/
class DataIntegrityOrganisationUnitsNoGroupsControllerTest
extends AbstractDataIntegrityIntegrationTest {
private String orgunitA;

private String orgunitB;

private static final String check = "orgunits_no_groups";

private static final String detailsIdType = "organisationUnits";

@Test
void testOrgunitsNoGroups() {

orgunitA =
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnits",
"{ 'name': 'Fish District', 'shortName': 'Fish District', 'openingDate' : '2022-01-01'}"));

orgunitB =
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnits",
"{ 'name': 'Pizza District', 'shortName': 'Pizza District', 'openingDate' : '2022-01-01'}"));

// Create an orgunit group
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnitGroups",
"{'name': 'Type A', 'shortName': 'Type A', 'organisationUnits' : [{'id' : '"
+ orgunitA
+ "'}]}"));
assertHasDataIntegrityIssues(
detailsIdType, check, 50, orgunitB, "Pizza District", "Type", true);
}

@Test
void testOrgunitsInGroups() {

orgunitA =
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnits",
"{ 'name': 'Fish District', 'shortName': 'Fish District', 'openingDate' : '2022-01-01'}"));

orgunitB =
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnits",
"{ 'name': 'Pizza District', 'shortName': 'Pizza District', 'openingDate' : '2022-01-01'}"));

// Create an orgunit group
assertStatus(
HttpStatus.CREATED,
POST(
"/organisationUnitGroups",
"{'name': 'Type A', 'shortName': 'Type A', 'organisationUnits' : [{'id' : '"
+ orgunitA
+ "'}, {'id' : '"
+ orgunitB
+ "'}]}"));
assertHasNoDataIntegrityIssues(detailsIdType, check, true);
}

@Test
void testOrgUnitsNoGroupsDivideByZero() {
assertHasNoDataIntegrityIssues(detailsIdType, check, false);
}
}
Loading