Skip to content

Commit

Permalink
chore: Centralize creation of org units in tests [40] (#19634)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Jan 10, 2025
1 parent d58f986 commit fa4a5f0
Show file tree
Hide file tree
Showing 37 changed files with 162 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,14 @@ public void setPath(String path) {
this.path = path;
}

/**
* Note that the {@code path} is mapped with the "property access" mode. This method is for unit
* testing purposes only.
*/
public void updatePath() {
setPath(getPath());
}

/**
* Used by persistence layer. Purpose is to have a column for use in database queries. For
* application use see {@link OrganisationUnit#getLevel()} which has better performance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.is;
import static org.hisp.dhis.DhisConvenienceTest.createDataElement;
import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_DATA_X;
import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT;
import static org.hisp.dhis.period.RelativePeriodEnum.THIS_QUARTER;
Expand All @@ -59,7 +60,6 @@
import org.hisp.dhis.dataelement.DataElementGroup;
import org.hisp.dhis.indicator.Indicator;
import org.hisp.dhis.indicator.IndicatorGroup;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitLevel;
import org.hisp.dhis.period.MonthlyPeriodType;
import org.hisp.dhis.period.Period;
Expand Down Expand Up @@ -98,9 +98,7 @@ void metadataContainsOuLevelData() {
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(
new OrganisationUnit("aaa", "aaa", "OU_1", null, null, "c1"),
new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2")),
List.of(createOrganisationUnit('A'), createOrganisationUnit('B')),
new DimensionItemKeywords(
Lists.newArrayList(
buildOrgUnitLevel(2, "wjP19dkFeIk", "District", null),
Expand Down Expand Up @@ -159,9 +157,7 @@ void metadataContainsIndicatorGroupMetadata() {
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(
new OrganisationUnit("aaa", "aaa", "OU_1", null, null, "c1"),
new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2")))))
List.of(createOrganisationUnit('A'), createOrganisationUnit('B')))))
.withIgnoreLimit(true)
.withSkipData(true)
.build();
Expand Down Expand Up @@ -197,9 +193,7 @@ void metadataContainsOuGroupData() {
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(
new OrganisationUnit("aaa", "aaa", "OU_1", null, null, "c1"),
new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2")),
List.of(createOrganisationUnit('A'), createOrganisationUnit('B')),
new DimensionItemKeywords(
Lists.newArrayList(
new BaseNameableObject("tTUf91fCytl", "OU_12345", "Chiefdom"))))))
Expand Down Expand Up @@ -249,7 +243,7 @@ void metadataContainsDataElementGroupMetadata() {
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(new OrganisationUnit("aaa", "aaa", "OU_1", null, null, "c1")))))
List.of(createOrganisationUnit('A')))))
.withIgnoreLimit(true)
.withSkipData(true)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hisp.dhis.DhisConvenienceTest.createDataElement;
import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.hisp.dhis.DhisConvenienceTest.createProgram;
import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -50,7 +51,6 @@
import org.hisp.dhis.common.BaseDimensionalObject;
import org.hisp.dhis.common.DimensionType;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.YearlyPeriodType;
import org.hisp.dhis.program.Program;
Expand Down Expand Up @@ -89,7 +89,7 @@ void verifyProgramDataElementInQueryCallsEventsAnalytics() {
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2")))))
List.of(createOrganisationUnit('A')))))
.build();

initMock(params);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hisp.dhis.DhisConvenienceTest.createDataSet;
import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.ArgumentMatchers.any;
Expand Down Expand Up @@ -88,7 +89,7 @@ void verifyReportingRatesValueWhenPeriodIsFilter() {
periods.add(
new MonthlyPeriodType().createPeriod(new DateTime(2014, x, 1, 0, 0).toDate())));

OrganisationUnit ou = new OrganisationUnit("aaaa");
OrganisationUnit ou = createOrganisationUnit('A');

DataQueryParams params =
DataQueryParams.newBuilder()
Expand Down Expand Up @@ -149,7 +150,7 @@ void verifyNullValueIsZeroForReportingRate() {
List<DimensionalItemObject> periods = new ArrayList<>();
periods.add(new MonthlyPeriodType().createPeriod(new DateTime(2014, 1, 1, 0, 0).toDate()));

OrganisationUnit ou = new OrganisationUnit("aaaa");
OrganisationUnit ou = createOrganisationUnit('A');

DataQueryParams params =
DataQueryParams.newBuilder()
Expand Down Expand Up @@ -195,7 +196,7 @@ void verifyNullTargetIsNullForReportingRate() {
List<DimensionalItemObject> periods = new ArrayList<>();
periods.add(new MonthlyPeriodType().createPeriod(new DateTime(2014, 1, 1, 0, 0).toDate()));

OrganisationUnit ou = new OrganisationUnit("aaaa");
OrganisationUnit ou = createOrganisationUnit('A');

DataQueryParams params =
DataQueryParams.newBuilder()
Expand Down Expand Up @@ -240,7 +241,7 @@ void verifyReportingRatesForMonthsWithLessThen30DaysAreComputedCorrectly() {
List<DimensionalItemObject> periods = new ArrayList<>();
periods.add(PeriodType.getPeriodFromIsoString("201902"));

OrganisationUnit ou = new OrganisationUnit("aaaa");
OrganisationUnit ou = createOrganisationUnit('A');

// Create request
DataQueryParams params =
Expand Down Expand Up @@ -289,7 +290,7 @@ void verifyReportingRatesForMonthsWithMoreThen30DaysAreComputedCorrectly() {
List<DimensionalItemObject> periods = new ArrayList<>();
periods.add(PeriodType.getPeriodFromIsoString("201901"));

OrganisationUnit ou = new OrganisationUnit("aaaa");
OrganisationUnit ou = createOrganisationUnit('A');

// Create request
DataQueryParams params =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.hisp.dhis.common.DimensionalObject.PERIOD_DIM_ID;
import static org.hisp.dhis.common.IdScheme.UID;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -149,7 +150,7 @@ public void setUp() {
Mockito.lenient().when(i18nManager.getI18n()).thenReturn(i18n);
Mockito.lenient().when(i18n.getString("LAST_12_MONTHS")).thenReturn("Last 12 months");

rootOu = new OrganisationUnit("Sierra Leone");
rootOu = createOrganisationUnit("Sierra Leone");
rootOu.setUid(CodeGenerator.generateUid());
rootOu.setCode("OU_525");
}
Expand All @@ -171,7 +172,7 @@ void convertAnalyticsRequestWithOuLevelToDataQueryParam() {
.thenReturn(getOrgUnitLevel(2, "level2UID", "District", null));
when(organisationUnitService.getOrganisationUnitLevelByLevelOrUid("2")).thenReturn(2);
when(organisationUnitService.getOrganisationUnitsAtLevels(Mockito.anyList(), Mockito.anyList()))
.thenReturn(Lists.newArrayList(new OrganisationUnit(), new OrganisationUnit()));
.thenReturn(Lists.newArrayList(createOrganisationUnit('A'), createOrganisationUnit('B')));

rb.addOuFilter("LEVEL-2;ImspTQPwCqd");
rb.addDimension(concatenateUuid(DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3));
Expand Down Expand Up @@ -206,7 +207,7 @@ void convertAnalyticsRequestWithMultipleOuLevelToDataQueryParam() {
when(organisationUnitService.getOrganisationUnitLevelByLevelOrUid("3")).thenReturn(3);
when(organisationUnitService.getOrganisationUnitLevelByLevelOrUid("2")).thenReturn(2);
when(organisationUnitService.getOrganisationUnitsAtLevels(Mockito.anyList(), Mockito.anyList()))
.thenReturn(Lists.newArrayList(new OrganisationUnit(), new OrganisationUnit()));
.thenReturn(Lists.newArrayList(createOrganisationUnit('A'), createOrganisationUnit('B')));

rb.addOuFilter("LEVEL-2;LEVEL-3;ImspTQPwCqd");
rb.addDimension(concatenateUuid(DATA_ELEMENT_1, DATA_ELEMENT_2, DATA_ELEMENT_3));
Expand Down Expand Up @@ -247,9 +248,9 @@ void convertAnalyticsRequestWithIndicatorGroup() {
when(idObjectManager.getObject(IndicatorGroup.class, UID, INDICATOR_GROUP_UID))
.thenReturn(indicatorGroup);
when(idObjectManager.getObject(OrganisationUnit.class, UID, "goRUwCHPg1M"))
.thenReturn(new OrganisationUnit("aaa"));
.thenReturn(createOrganisationUnit('A'));
when(idObjectManager.getObject(OrganisationUnit.class, UID, "fdc6uOvgoji"))
.thenReturn(new OrganisationUnit("bbb"));
.thenReturn(createOrganisationUnit('B'));

rb.addOuFilter("goRUwCHPg1M;fdc6uOvgoji");
rb.addDimension("IN_GROUP-" + INDICATOR_GROUP_UID + ";cYeuwXTCPkU;Jtf34kNZhz");
Expand Down Expand Up @@ -314,8 +315,8 @@ void convertAnalyticsRequestWithOrgUnitGroupAsFilter() {

@Test
void convertAnalyticsRequestWithOrgUnitLevelAsFilter() {
OrganisationUnit level2OuA = new OrganisationUnit("Bo");
OrganisationUnit level2OuB = new OrganisationUnit("Bombali");
OrganisationUnit level2OuA = createOrganisationUnit('B');
OrganisationUnit level2OuB = createOrganisationUnit('C');

mockDimensionService();

Expand Down Expand Up @@ -357,8 +358,8 @@ void convertAnalyticsRequestWithOrgUnitLevelAsFilter() {

@Test
void convertAnalyticsRequestWithOrgUnitLevelAndOrgUnitGroupAsFilter() {
OrganisationUnit level2OuA = new OrganisationUnit("Bo");
OrganisationUnit level2OuB = new OrganisationUnit("Bombali");
OrganisationUnit level2OuA = createOrganisationUnit('B');
OrganisationUnit level2OuB = createOrganisationUnit('C');

OrganisationUnit ou1Group = new OrganisationUnit("ou1-group");
OrganisationUnit ou2Group = new OrganisationUnit("ou2-group");
Expand Down Expand Up @@ -431,9 +432,9 @@ void convertAnalyticsRequestWithDataElementGroup() {
when(idObjectManager.getObject(DataElementGroup.class, UID, DATA_ELEMENT_GROUP_UID))
.thenReturn(dataElementGroup);
when(idObjectManager.getObject(OrganisationUnit.class, UID, "goRUwCHPg1M"))
.thenReturn(new OrganisationUnit("aaa"));
.thenReturn(createOrganisationUnit('A'));
when(idObjectManager.getObject(OrganisationUnit.class, UID, "fdc6uOvgoji"))
.thenReturn(new OrganisationUnit("bbb"));
.thenReturn(createOrganisationUnit('B'));

rb.addOuFilter("goRUwCHPg1M;fdc6uOvgoji");
rb.addDimension("DE_GROUP-" + DATA_ELEMENT_GROUP_UID + ";cYeuwXTCPkU;Jtf34kNZhz");
Expand Down Expand Up @@ -480,9 +481,9 @@ void convertAnalyticsRequestWithDataElementGroupAndIndicatorGroup() {
.thenReturn(indicatorGroup);

when(idObjectManager.getObject(OrganisationUnit.class, UID, "goRUwCHPg1M"))
.thenReturn(new OrganisationUnit("aaa"));
.thenReturn(createOrganisationUnit('A'));
when(idObjectManager.getObject(OrganisationUnit.class, UID, "fdc6uOvgoji"))
.thenReturn(new OrganisationUnit("bbb"));
.thenReturn(createOrganisationUnit('B'));

rb.addOuFilter("goRUwCHPg1M;fdc6uOvgoji");
rb.addDimension(
Expand Down Expand Up @@ -623,7 +624,7 @@ private void initOrgUnitGroup(String ouGroupUID) {
when(idObjectManager.getObject(OrganisationUnit.class, UID, this.rootOu.getUid()))
.thenReturn(rootOu);
when(organisationUnitService.getOrganisationUnits(Mockito.anyList(), Mockito.anyList()))
.thenReturn(Lists.newArrayList(new OrganisationUnit(), new OrganisationUnit()));
.thenReturn(Lists.newArrayList(createOrganisationUnit('A'), createOrganisationUnit('B')));
}

private void assertOrgUnitGroup(String ouGroupUID, DimensionalObject dimension) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.hisp.dhis.DhisConvenienceTest.createDataElement;
import static org.hisp.dhis.DhisConvenienceTest.createIndicator;
import static org.hisp.dhis.DhisConvenienceTest.createIndicatorType;
import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_DATA_X;
import static org.hisp.dhis.analytics.DataQueryParams.DISPLAY_NAME_ORGUNIT;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -58,7 +59,6 @@
import org.hisp.dhis.common.DimensionalItemObject;
import org.hisp.dhis.common.ValueType;
import org.hisp.dhis.dataelement.DataElementDomain;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.period.MonthlyPeriodType;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
Expand Down Expand Up @@ -113,7 +113,7 @@ void verifyMultipleDataElementIsAggregatedWithTwoQueryGroupWhenDataTypeIsDiffere
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2")))))
List.of(createOrganisationUnit('A')))))
.withAggregationType(AnalyticsAggregationType.AVERAGE)
.build();

Expand Down Expand Up @@ -166,7 +166,7 @@ void verifySingleNonDataElementRetainAggregationTypeButNullDataType() {
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2")))))
List.of(createOrganisationUnit('A')))))
.withAggregationType(AnalyticsAggregationType.AVERAGE)
.build();

Expand Down Expand Up @@ -356,7 +356,7 @@ private DataQueryParams createDataQueryParams(BaseDimensionalObject filterDataEl
DimensionType.ORGANISATION_UNIT,
null,
DISPLAY_NAME_ORGUNIT,
List.of(new OrganisationUnit("bbb", "bbb", "OU_2", null, null, "c2"))),
List.of(createOrganisationUnit('A'))),
// DATA ELEMENT AS FILTER
filterDataElements))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@
import static org.hisp.dhis.common.QueryOperator.NEQ;
import static org.hisp.dhis.common.QueryOperator.NIEQ;
import static org.hisp.dhis.common.QueryOperator.NILIKE;
import static org.hisp.dhis.common.ValueType.*;
import static org.hisp.dhis.common.ValueType.BOOLEAN;
import static org.hisp.dhis.common.ValueType.NUMBER;
import static org.hisp.dhis.common.ValueType.TEXT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -509,7 +511,7 @@ void testGetWhereClauseWithMultipleOrgUnitDescendantsAtSameLevel() {

assertThat(
whereClause,
containsString("and ax.\"uidlevel0\" in ('ouabcdefghA','ouabcdefghB','ouabcdefghC')"));
containsString("and ax.\"uidlevel1\" in ('ouabcdefghA','ouabcdefghB','ouabcdefghC')"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.analytics.orgunit;

import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

Expand All @@ -48,9 +49,9 @@ void getActiveOrganisationUnits_returns_only_active_units() {
// Given
List<OrganisationUnit> rqOrgUnit = new ArrayList<>();

rqOrgUnit.add(new OrganisationUnit("name1"));
rqOrgUnit.add(new OrganisationUnit("name2"));
rqOrgUnit.add(new OrganisationUnit("name3"));
rqOrgUnit.add(createOrganisationUnit('A'));
rqOrgUnit.add(createOrganisationUnit('B'));
rqOrgUnit.add(createOrganisationUnit('C'));

rqOrgUnit.get(0).setUid(CodeGenerator.generateUid());
rqOrgUnit.get(1).setUid(CodeGenerator.generateUid());
Expand All @@ -75,9 +76,9 @@ void getActiveOrganisationUnits_returns_same_units() {
// Given
List<OrganisationUnit> rqOrgUnit = new ArrayList<>();

rqOrgUnit.add(new OrganisationUnit("name1"));
rqOrgUnit.add(new OrganisationUnit("name2"));
rqOrgUnit.add(new OrganisationUnit("name3"));
rqOrgUnit.add(createOrganisationUnit('A'));
rqOrgUnit.add(createOrganisationUnit('B'));
rqOrgUnit.add(createOrganisationUnit('C'));

rqOrgUnit.get(0).setUid(CodeGenerator.generateUid());
rqOrgUnit.get(1).setUid(CodeGenerator.generateUid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@
*/
package org.hisp.dhis.trackedentity;

import static org.hisp.dhis.DhisConvenienceTest.createOrganisationUnit;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.util.Set;
import org.hisp.dhis.common.IllegalQueryException;
import org.hisp.dhis.common.OrganisationUnitSelectionMode;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramService;
Expand Down Expand Up @@ -96,8 +96,8 @@ void setup() {
attributeValueAuditService);

User user = new User();
user.setOrganisationUnits(Set.of(new OrganisationUnit("A")));
user.setTeiSearchOrganisationUnits(Set.of(new OrganisationUnit("B")));
user.setOrganisationUnits(Set.of(createOrganisationUnit('A')));
user.setTeiSearchOrganisationUnits(Set.of(createOrganisationUnit('B')));
when(currentUserService.getCurrentUser()).thenReturn(user);

params = new TrackedEntityInstanceQueryParams();
Expand Down
Loading

0 comments on commit fa4a5f0

Please sign in to comment.