Skip to content
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
9 changes: 9 additions & 0 deletions src/main/java/org/hisp/dhis/model/OrgUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,13 @@ public OrgUnit(String id, String name, String shortName, OrgUnit parent, Date op
public DimensionItemType getDimensionItemType() {
return DimensionItemType.ORGANISATION_UNIT;
}

/**
* Indicates whether this org unit has a parent.
*
* @return true if this org unit has a parent, false otherwise.
*/
public boolean hasParent() {
return parent != null;
}
}
24 changes: 20 additions & 4 deletions src/main/java/org/hisp/dhis/util/UidUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,29 @@ public static List<String> generateUids(int n) {
}

/**
* Tests whether the given code is a valid UID.
* Tests whether the given input is a valid UID.
*
* @param code the code to validate.
* @param input the input to validate.
* @return true if the code is valid.
*/
public static boolean isValidUid(String code) {
return code != null && UID_PATTERN.matcher(code).matches();
public static boolean isValidUid(String input) {
return input != null && UID_PATTERN.matcher(input).matches();
}

/**
* Tests whether the given input is a valid UID. Throws an {@link IllegalArgumentException} if
* not.
*
* @param input the input to validate.
* @return the input UID if valid.
* @throws IllegalArgumentException if input is not a valid UID.
*/
public static String requireUid(String input) {
if (!isValidUid(input)) {
String message = String.format("Input must be a valid UID: '%s'", input);
throw new IllegalArgumentException(message);
}
return input;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/test/java/org/hisp/dhis/BaseDhis2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ void testWithMetadataImportParams() throws Exception {
URI expected =
new URI(
"""
https://server.org/api/metadata\
?importStrategy=CREATE_AND_UPDATE\
&atomicMode=ALL\
&skipSharing=false\
&async=false""");
https://server.org/api/metadata\
?importStrategy=CREATE_AND_UPDATE\
&atomicMode=ALL\
&skipSharing=false\
&async=false""");

assertEquals(expected, dhis2.withMetadataImportParams(uriBuilder));
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/hisp/dhis/util/UidUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.stream.IntStream;
Expand Down Expand Up @@ -64,6 +65,15 @@ void testUidIsValid() {
assertFalse(UidUtils.isValidUid("1T1hdS_WjfD"));
}

@Test
void testRequireUid() {
assertEquals("mq4jAnN6fg3", UidUtils.requireUid("mq4jAnN6fg3"));
assertEquals("iz9HDQXFDrQ", UidUtils.requireUid("iz9HDQXFDrQ"));

assertThrows(IllegalArgumentException.class, () -> UidUtils.requireUid("1T1hdSWjfDC"));
assertThrows(IllegalArgumentException.class, () -> UidUtils.requireUid("QX4LpiTZmUHg"));
}

@Test
void testToUid() {
assertToUid("PpZ!m3thN#sm8QVcOdwTcil4");
Expand Down
Loading