Skip to content

Commit

Permalink
fix: Add null check for program [DHIS2-18459] (#19240)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Nov 20, 2024
1 parent 8be06a2 commit f209711
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ public boolean isSingleProgramStage() {
return programStages != null && programStages.size() == 1;
}

public boolean hasMaxTeiCountToReturn() {
return maxTeiCountToReturn > 0;
}

@Override
public int increaseVersion() {
return ++version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ private String getCountQuery(TrackedEntityQueryParams params) {
* @return a count SQL query
*/
private String getCountQueryWithMaxTeiLimit(TrackedEntityQueryParams params) {
boolean hasMaxTeiCountToReturn =
params.hasProgram() && params.getProgram().hasMaxTeiCountToReturn();
return new StringBuilder()
.append(getQueryCountSelect(params))
.append(getQuerySelect(params))
Expand All @@ -415,7 +417,7 @@ private String getCountQueryWithMaxTeiLimit(TrackedEntityQueryParams params) {
.append(getQueryRelatedTables(params))
.append(getQueryGroupBy(params))
.append(
params.getProgram().getMaxTeiCountToReturn() > 0
hasMaxTeiCountToReturn
? getLimitClause(params.getProgram().getMaxTeiCountToReturn() + 1)
: "")
.append(" ) tecount")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import static org.hisp.dhis.DhisConvenienceTest.injectSecurityContext;
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;
Expand Down Expand Up @@ -75,7 +75,9 @@ class DefaultTrackedEntityServiceTest {

@Mock private TrackedEntityAttributeValueChangeLogService attributeValueAuditService;

private TrackedEntityQueryParams params;
private TrackedEntityQueryParams paramsA;

private TrackedEntityQueryParams paramsB;

private DefaultTrackedEntityService teiService;

Expand Down Expand Up @@ -103,11 +105,15 @@ void setup() {
this.user = user;
injectSecurityContext(UserDetails.fromUser(user));

params = new TrackedEntityQueryParams();
params.setOrgUnitMode(OrganisationUnitSelectionMode.ACCESSIBLE);
params.setProgram(new Program("Test program"));
params.getProgram().setMaxTeiCountToReturn(10);
params.setTrackedEntityUids(Set.of("1"));
paramsA = new TrackedEntityQueryParams();
paramsA.setOrgUnitMode(OrganisationUnitSelectionMode.ACCESSIBLE);
paramsA.setProgram(new Program("Test program"));
paramsA.getProgram().setMaxTeiCountToReturn(10);
paramsA.setTrackedEntityUids(Set.of("1"));

paramsB = new TrackedEntityQueryParams();
paramsB.setOrgUnitMode(OrganisationUnitSelectionMode.ACCESSIBLE);
paramsB.setTrackedEntityUids(Set.of("1"));
}

@Test
Expand All @@ -122,7 +128,7 @@ void exceptionThrownWhenTeiLimitReached() {
IllegalQueryException expectedException =
assertThrows(
IllegalQueryException.class,
() -> teiService.validateSearchScope(params, true),
() -> teiService.validateSearchScope(paramsA, true),
"test message");

assertEquals("maxteicountreached", expectedException.getMessage());
Expand All @@ -137,6 +143,14 @@ void noExceptionThrownWhenTeiLimitNotReached() {
String currentUsername = CurrentUserUtil.getCurrentUsername();
when(userService.getUserByUsername(currentUsername)).thenReturn(user);

teiService.validateSearchScope(params, true);
teiService.validateSearchScope(paramsA, true);
}

@Test
void noExceptionThrownWhenNoProgramSpecified() {
String currentUsername = CurrentUserUtil.getCurrentUsername();
when(userService.getUserByUsername(currentUsername)).thenReturn(user);

teiService.validateSearchScope(paramsB, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,13 @@ private QueryItem getItem(String item, Map<String, TrackedEntityAttribute> attri
at, null, at.getValueType(), at.getAggregationType(), at.getOptionSet(), at.isUnique());
}

/**
* Validates and retrieves the program optionally specified in the given criteria. Returns <code>
* null</code> if program is not specified.
*
* @param criteria the {@link TrackedEntityInstanceCriteria}.
* @return a {@link Program} or <code>null</code>.
*/
private Program validateProgram(TrackedEntityInstanceCriteria criteria) {
Function<String, Program> getProgram =
uid -> {
Expand Down

0 comments on commit f209711

Please sign in to comment.