Skip to content

Commit

Permalink
Add missing files
Browse files Browse the repository at this point in the history
  • Loading branch information
jason-p-pickering committed Nov 27, 2024
1 parent ab040db commit ef31be0
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,7 @@ public enum ErrorCode {
E1120("Update cannot be applied as it would make existing data values inaccessible"),
E1121("Data element `{0}` value type cannot be changed as it has associated data values"),

E1122("Category option combo {0} already exists for category combo {1}"),
E1123("Category option combo {0} must be associated with a category combo"),
E1124("Category option combo {0} cannot be associated with the default category combo"),

E1122("Category option combo {0} cannot be associated with the default category combo"),
E1125("Category option combo {0} contains options not associated with category combo {1}"),
/* Org unit merge */
E1500("At least two source orgs unit must be specified"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@
*/
package org.hisp.dhis.dxf2.metadata.objectbundle.hooks;

import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import org.hisp.dhis.category.CategoryCombo;
import org.hisp.dhis.category.CategoryOption;
import org.hisp.dhis.category.CategoryOptionCombo;
import org.hisp.dhis.category.CategoryService;
import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle;
Expand All @@ -47,70 +43,6 @@ public class CategoryOptionComboObjectBundleHook
extends AbstractObjectBundleHook<CategoryOptionCombo> {
private final CategoryService categoryService;

static boolean haveEqualCatComboCatOptionReferenceIds(
CategoryOptionCombo one, CategoryOptionCombo other) {
if (one == null || other == null) {
return false;
}

if (one.getCategoryCombo() == null || other.getCategoryCombo() == null) {
return false;
}

if (one.getCategoryOptions() == null || other.getCategoryOptions() == null) {
return false;
}

if (!one.getCategoryCombo().getUid().equals(other.getCategoryCombo().getUid())) {
return false;
}

Set<String> oneCategoryOptionUids =
one.getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.toSet());

Set<String> otherCategoryOptionUids =
other.getCategoryOptions().stream().map(CategoryOption::getUid).collect(Collectors.toSet());

return oneCategoryOptionUids.equals(otherCategoryOptionUids);
}

private void checkDuplicateCategoryOptionCombos(
CategoryOptionCombo categoryOptionCombo,
ObjectBundle bundle,
Consumer<ErrorReport> addReports) {

if (bundle.isPersisted(categoryOptionCombo)) {
return; // Only check for duplicates if the object is not persisted
}

List<CategoryOptionCombo> categoryOptionCombos =
categoryService.getAllCategoryOptionCombos().stream()
.filter(
coc ->
coc.getCategoryCombo()
.getUid()
.equals(categoryOptionCombo.getCategoryCombo().getUid()))
.toList();

// This could be an update or re-import of the same object.
if (categoryOptionCombos.stream()
.anyMatch(coc -> coc.getUid().equals(categoryOptionCombo.getUid()))) {
return;
}
// Check to see if the COC already exists in the list of COCs by comparing reference ids
for (CategoryOptionCombo existingCategoryOptionCombo : categoryOptionCombos) {
if (haveEqualCatComboCatOptionReferenceIds(categoryOptionCombo, existingCategoryOptionCombo)
&& !categoryOptionCombo.getUid().equals(existingCategoryOptionCombo.getUid())) {
addReports.accept(
new ErrorReport(
CategoryOptionCombo.class,
ErrorCode.E1122,
categoryOptionCombo.getName(),
existingCategoryOptionCombo.getName()));
}
}
}

private void checkNonStandardDefaultCatOptionCombo(
CategoryOptionCombo categoryOptionCombo, Consumer<ErrorReport> addReports) {

Expand All @@ -125,7 +57,7 @@ private void checkNonStandardDefaultCatOptionCombo(
if (!categoryOptionCombo.getUid().equals(defaultCatOptionCombo.getUid())) {
addReports.accept(
new ErrorReport(
CategoryOptionCombo.class, ErrorCode.E1124, categoryOptionCombo.getName()));
CategoryOptionCombo.class, ErrorCode.E1122, categoryOptionCombo.getName()));
}
}

Expand All @@ -136,6 +68,5 @@ public void validate(
Consumer<ErrorReport> addReports) {

checkNonStandardDefaultCatOptionCombo(categoryOptionCombo, addReports);
checkDuplicateCategoryOptionCombos(categoryOptionCombo, bundle, addReports);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,35 +121,6 @@ void catOptionCombosExcludingDefaultTest() {
catOptionComboNames.contains("default"), "default catOptionCombo is not in payload");
}

@Test
@DisplayName("Duplicate CategoryOptionCombos should not be allowed")
void catOptionCombosDuplicatedTest() {

JsonObject response =
GET("/categoryOptionCombos?filter=id:eq:CocUid0001&fields=id,categoryCombo[id],categoryOptions[id]")
.content();
JsonList<JsonCategoryOptionCombo> catOptionCombos =
response.getList("categoryOptionCombos", JsonCategoryOptionCombo.class);
String catOptionComboAOptions = catOptionCombos.get(0).getCategoryOptions().get(0).getId();
String catOptionComboACatComboId = catOptionCombos.get(0).getCategoryCombo().getId();

JsonErrorReport error =
POST(
"/categoryOptionCombos/",
"""
{ "name": "A_1",
"categoryOptions" : [{"id" : "%s"}],
"categoryCombo" : {"id" : "%s"} }
"""
.formatted(catOptionComboAOptions, catOptionComboACatComboId))
.content(HttpStatus.CONFLICT)
.find(JsonErrorReport.class, report -> report.getErrorCode() == ErrorCode.E1122);
assertNotNull(error);
assertEquals(
"Category option combo A_1 already exists for category combo CatOptCombo A",
error.getMessage());
}

@Test
@DisplayName("Duplicate default category option combos should not be allowed")
void catOptionCombosDuplicatedDefaultTest() {
Expand All @@ -176,7 +147,7 @@ void catOptionCombosDuplicatedDefaultTest() {
response.find(JsonErrorReport.class, report -> report.getErrorCode() == ErrorCode.E1122);
assertNotNull(error);
assertEquals(
"Category option combo Not default already exists for category combo default",
"Category option combo Not default cannot be associated with the default category combo",
error.getMessage());
}
}

0 comments on commit ef31be0

Please sign in to comment.