From 3eb92a1c1e5ac1207f8204d91d0e4644f4882a72 Mon Sep 17 00:00:00 2001 From: David Mackessy Date: Fri, 16 Aug 2024 10:48:26 +0100 Subject: [PATCH] fix: Delete LegendSet with ref in DataSet [DHIS2-6011] --- .../dhis/dataset/DataSetDeletionHandler.java | 11 +- .../actions/metadata/MetadataActions.java | 7 ++ .../hisp/dhis/legendset/LegendSetTest.java | 109 ++++++++++++++++++ 3 files changed, 122 insertions(+), 5 deletions(-) create mode 100644 dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/legendset/LegendSetTest.java diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java index 86572e33d895..67d97065581f 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataset/DataSetDeletionHandler.java @@ -108,11 +108,12 @@ private void deleteSection(Section section) { } private void deleteLegendSet(LegendSet legendSet) { - for (DataSet dataSet : idObjectManager.getAllNoAcl(DataSet.class)) { - for (LegendSet ls : dataSet.getLegendSets()) { - if (legendSet.equals(ls)) { - dataSet.getLegendSets().remove(ls); - idObjectManager.updateNoAcl(dataSet); + for (DataSet ds : idObjectManager.getAllNoAcl(DataSet.class)) { + Iterator lsIterator = ds.getLegendSets().iterator(); + while (lsIterator.hasNext()) { + if (legendSet.equals(lsIterator.next())) { + lsIterator.remove(); + idObjectManager.updateNoAcl(ds); } } } diff --git a/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/metadata/MetadataActions.java b/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/metadata/MetadataActions.java index 177d0e6bc0c5..52f9d58cf7f0 100644 --- a/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/metadata/MetadataActions.java +++ b/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/metadata/MetadataActions.java @@ -30,6 +30,7 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.not; +import com.google.gson.Gson; import com.google.gson.JsonObject; import java.io.File; import org.hisp.dhis.actions.RestApiActions; @@ -56,6 +57,12 @@ public MetadataApiResponse importMetadata(File file, String... queryParams) { return new MetadataApiResponse(response); } + public MetadataApiResponse importMetadata(String metadata) { + JsonObject json = new Gson().fromJson(metadata, JsonObject.class); + ApiResponse response = importMetadata(json); + return new MetadataApiResponse(response); + } + public MetadataApiResponse importMetadata(JsonObject object, String... queryParams) { QueryParamsBuilder queryParamsBuilder = new QueryParamsBuilder(); queryParamsBuilder.addAll(queryParams); diff --git a/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/legendset/LegendSetTest.java b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/legendset/LegendSetTest.java new file mode 100644 index 000000000000..5205aea05545 --- /dev/null +++ b/dhis-2/dhis-test-e2e/src/test/java/org/hisp/dhis/legendset/LegendSetTest.java @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2004-2024, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.legendset; + +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasEntry; +import static org.hamcrest.Matchers.hasItem; + +import org.hisp.dhis.ApiTest; +import org.hisp.dhis.actions.RestApiActions; +import org.hisp.dhis.actions.metadata.MetadataActions; +import org.hisp.dhis.dto.ApiResponse; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +class LegendSetTest extends ApiTest { + + private MetadataActions metadataActions; + private RestApiActions legendSetActions; + private RestApiActions dataSetActions; + + @BeforeAll + public void init() { + metadataActions = new MetadataActions(); + legendSetActions = new RestApiActions("legendSets"); + dataSetActions = new RestApiActions("dataSets"); + } + + @Test + @DisplayName("Deleting a legend set which is referenced by a data set is successful") + void deleteLegendSetTest() { + metadataActions.importMetadata(dataSetWithLegendSet()).validateStatus(200); + + // confirm data set has legend set + dataSetActions + .get("DsUid000001") + .validate() + .body("legendSets", hasItem(allOf(hasEntry("id", "LegSetUid01")))); + + // delete legend set + ApiResponse response = legendSetActions.delete("LegSetUid01"); + response.validate().body("httpStatus", equalTo("OK")).body("httpStatusCode", equalTo(200)); + + // confirm data set no longer has legend set + dataSetActions.get("DsUid000001").validate().body("legendSets", empty()); + } + + private String dataSetWithLegendSet() { + return "{" + + "\"dataSets\": [" + + "{" + + "\"name\": \"ds 1\"," + + "\"id\": \"DsUid000001\"," + + "\"shortName\": \"ds 1\"," + + "\"periodType\": \"Monthly\"," + + "\"legendSets\": [" + + "{" + + "\"id\": \"LegSetUid01\"" + + "}" + + "]" + + "}" + + "]," + + "\"legendSets\": [" + + "{" + + "\"name\": \"Test legend11\"," + + "\"legends\": [" + + "{" + + "\"name\": \"45 - 60\"," + + "\"startValue\": 45.0," + + "\"endValue\": 60.0," + + "\"displayName\": \"45 - 60\"," + + "\"id\": \"LegUid00001\"" + + "}" + + "]," + + "\"displayName\": \"Test legend\"," + + "\"id\": \"LegSetUid01\"" + + "}" + + "]" + + "}"; + } +}