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

fix: program validation for related program property [DHIS2-15952] #15885

Merged
merged 2 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ public enum ErrorCode {
E6019("AttributeValue `{0}` is an invalid `{1}` ID"),
E6020("AttributeValue `{0}` is an invalid username"),
E6021("AttributeValue `{0}` is an invalid phone number"),
E6022("Object cannot reference itself by property `{0}`"),

/* File resource */
E6100("Filename not present"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,16 @@
*/
package org.hisp.dhis.program;

import lombok.Getter;

/**
* @author Chau Thu Tran
*/
@Getter
public enum ProgramType {
/** Aka tracker program */
WITH_REGISTRATION("with_registration"),
/** Aka event program */
WITHOUT_REGISTRATION("without_registration");

private final String value;
Expand All @@ -50,7 +55,11 @@ public static ProgramType fromValue(String value) {
return null;
}

public String getValue() {
return value;
public boolean isTrackerProgram() {
return this == WITH_REGISTRATION;
}

public boolean isEventProgram() {
return this == WITHOUT_REGISTRATION;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,19 @@ public void validate(Program program, ObjectBundle bundle, Consumer<ErrorReport>
if (program.getId() != 0 && getProgramInstancesCount(program) > 1) {
addReports.accept(new ErrorReport(Program.class, ErrorCode.E6000, program.getName()));
}
Program relatedProgram = program.getRelatedProgram();
if (relatedProgram != null && Objects.equals(relatedProgram.getUid(), program.getUid())) {
addReports.accept(new ErrorReport(Program.class, ErrorCode.E6022, "relatedProgram"));
}
if (relatedProgram != null && program.getProgramType().isEventProgram()) {
addReports.accept(
new ErrorReport(
Program.class,
ErrorCode.E4023,
"relatedProgram",
"programType",
ProgramType.WITHOUT_REGISTRATION.name()));
}
validateAttributeSecurity(program, bundle, addReports);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.hisp.dhis.webapi.controller;

import static org.hisp.dhis.feedback.ErrorCode.E1005;
import static org.hisp.dhis.web.WebClient.Body;
import static org.hisp.dhis.web.WebClientUtils.assertStatus;
import static org.hisp.dhis.webapi.utils.TestUtils.getMatchingGroupFromPattern;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -38,12 +39,14 @@
import java.util.Set;
import java.util.stream.Collectors;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.feedback.ErrorCode;
import org.hisp.dhis.jsontree.JsonList;
import org.hisp.dhis.trackedentity.TrackedEntityAttribute;
import org.hisp.dhis.user.User;
import org.hisp.dhis.web.HttpStatus;
import org.hisp.dhis.web.WebClient;
import org.hisp.dhis.webapi.DhisControllerConvenienceTest;
import org.hisp.dhis.webapi.json.domain.JsonErrorReport;
import org.hisp.dhis.webapi.json.domain.JsonObjectReport;
import org.hisp.dhis.webapi.json.domain.JsonProgram;
import org.hisp.dhis.webapi.json.domain.JsonProgramIndicator;
import org.hisp.dhis.webapi.json.domain.JsonProgramRuleVariable;
Expand Down Expand Up @@ -81,11 +84,30 @@ public void testSetup() throws JsonProcessingException {
POST("/trackedEntityAttributes", jsonMapper.writeValueAsString(tea2))
.content(HttpStatus.CREATED);

POST("/metadata", WebClient.Body("program/create_program.json"))
POST("/metadata", Body("program/create_program.json"))
.content(HttpStatus.OK)
.as(JsonWebMessage.class);
}

@Test
void testProgramValidation_RelatedProgram() {
// language=JSON
String json =
"""
{
"name":"test program",
"shortName": "test program",
"programType": "WITH_REGISTRATION",
"relatedProgram": {"id": "PrZMWi7rBga" }
}""";
JsonWebMessage msg =
assertWebMessage(HttpStatus.CONFLICT, PUT("/programs/" + PROGRAM_UID, json));
JsonList<JsonErrorReport> errors =
msg.getResponse().as(JsonObjectReport.class).getErrorReports();
assertEquals(1, errors.size());
assertEquals(ErrorCode.E6022, errors.get(0).getErrorCode());
}

@Test
void testCopyProgram() {
JsonWebMessage response =
Expand Down
Loading