Skip to content

Commit

Permalink
feat: Add defaults to optionSetItem [DHIS2-18643] (#19536)
Browse files Browse the repository at this point in the history
* feat: Add defaults to optionSetItem [DHIS2-18643]

* test: For absent aggregation [DHIS2-18643]
  • Loading branch information
maikelarabori authored Dec 19, 2024
1 parent 11ce320 commit 0c43892
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
*/
package org.hisp.dhis.common;

import static org.hisp.dhis.analytics.Aggregation.AGGREGATED;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand All @@ -40,6 +42,7 @@
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import org.hisp.dhis.analytics.Aggregation;
import org.hisp.dhis.dataelement.DataElement;
import org.hisp.dhis.dataelement.DataElementOperand;
import org.hisp.dhis.expressiondimensionitem.ExpressionDimensionItem;
Expand Down Expand Up @@ -113,16 +116,31 @@ public class DataDimensionItem {
@AllArgsConstructor
public static class Attributes implements Serializable {
/** The option item for this dimension item. * */
private OptionSetItem optionItem;
private OptionSetItem optionSetItem;

@JsonProperty
@JacksonXmlProperty(namespace = DxfNamespaces.DXF_2_0)
public OptionSetItem getOptionSetItem() {
return optionItem;
return optionSetItem;
}

/**
* This method ensure that existing persisted items will return default values, case the current
* {@link OptionSetItem} is null or does not have an {@link Aggregation} defined.
*
* @return the correct version of an {@link OptionSetItem}.
*/
public OptionSetItem getOptionSetItemOrDefault() {
if (optionSetItem != null) {
return new OptionSetItem(
optionSetItem.getOptions(), optionSetItem.getAggregationOrDefault());
}

return new OptionSetItem(Set.of(), AGGREGATED);
}

public void setOptionSetItem(OptionSetItem optionItem) {
this.optionItem = optionItem;
public void setOptionSetItem(OptionSetItem optionSetItem) {
this.optionSetItem = optionSetItem;
}
}

Expand Down Expand Up @@ -232,14 +250,17 @@ public DimensionalItemObject getDimensionalItemObject() {
}

/**
* Simply loads the internal attributes into the given item object.
* Simply loads the internal attributes into the given item object. Some objects, when null, will
* be loaded with their respective defaults.
*
* @param itemObject the {@link BaseDimensionalItemObject}.
*/
private void loadAttributes(BaseDimensionalItemObject itemObject) {
if (attributes != null) {
itemObject.setOptionSetItem(attributes.getOptionSetItem());
if (attributes == null) {
attributes = new Attributes();
}

itemObject.setOptionSetItem(attributes.getOptionSetItemOrDefault());
}

@JsonProperty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/
package org.hisp.dhis.common;

import static org.hisp.dhis.analytics.Aggregation.AGGREGATED;
import static org.hisp.dhis.common.DxfNamespaces.DXF_2_0;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down Expand Up @@ -71,4 +72,17 @@ public Aggregation getAggregation() {
public void setAggregation(Aggregation aggregation) {
this.aggregation = aggregation;
}

/**
* Returns the current {@link Aggregation} or default.
*
* @return the respective {@link Aggregation} object.
*/
public Aggregation getAggregationOrDefault() {
if (aggregation == null) {
return AGGREGATED;
}

return aggregation;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,4 +436,104 @@ void testPostOptionSetItemInDataElement() {
assertEquals(
"[\"BFhv3jQZ8Cw\"]", itemsNode.get("optionSetItem").get("options").value().toString());
}

@Test
void testPostWithoutOptionSetItemAndLoadDefaults() {
// Given
DataElement dataElement = createDataElement('A');
manager.save(dataElement);

String dataElementUid = dataElement.getUid();
String jsonBody =
"""
{
"type": "PIE",
"columns": [
{
"dimension": "dx",
"items": [
{
"id": "${dataElement}",
"name": "Data Element - OptionSet",
"dimensionItemType": "DATA_ELEMENT"
}
]
}
],
"name": "OptionSetItem - Test"
}
"""
.replace("${dataElement}", dataElementUid);

// When
String uid = assertStatus(CREATED, POST("/visualizations/", jsonBody));

// Then
String getParams = "?fields=columns[:all,items[:all,optionSetItem[options,aggregation]]";
JsonObject response = GET("/visualizations/" + uid + getParams).content();

JsonNode columnNode = response.get("columns").node().element(0);
JsonNode itemsNode = columnNode.get("items").elementOrNull(0);

assertEquals("DATA_X", columnNode.get("dimensionType").value());
assertTrue((boolean) columnNode.get("dataDimension").value());
assertEquals("DATA_ELEMENT", itemsNode.get("dimensionItemType").value());
assertEquals("SUM", itemsNode.get("aggregationType").value());
assertEquals(dataElementUid, itemsNode.get("dimensionItem").value());
assertEquals("AGGREGATED", itemsNode.get("optionSetItem").get("aggregation").value());
assertEquals("[]", itemsNode.get("optionSetItem").get("options").value().toString());
}

@Test
void testPostOptionSetItemWithNoAggregation() {
// Given
DataElement dataElement = createDataElement('A');
manager.save(dataElement);

String dataElementUid = dataElement.getUid();
String jsonBody =
"""
{
"type": "PIE",
"columns": [
{
"dimension": "dx",
"items": [
{
"id": "${dataElement}",
"name": "Data Element - OptionSet",
"dimensionItemType": "DATA_ELEMENT",
"optionSetItem": {
"options": [
"BFhv3jQZ8Cw"
]
}
}
]
}
],
"name": "OptionSetItem - Test"
}
"""
.replace("${dataElement}", dataElementUid);

// When
String uid = assertStatus(CREATED, POST("/visualizations/", jsonBody));

// Then
String getParams = "?fields=columns[:all,items[:all,optionSetItem[options,aggregation]]";
JsonObject response = GET("/visualizations/" + uid + getParams).content();

JsonNode columnNode = response.get("columns").node().element(0);
JsonNode itemsNode = columnNode.get("items").elementOrNull(0);

assertEquals("DATA_X", columnNode.get("dimensionType").value());
assertTrue((boolean) columnNode.get("dataDimension").value());
assertEquals("DATA_ELEMENT", itemsNode.get("dimensionItemType").value());
assertEquals("SUM", itemsNode.get("aggregationType").value());
assertEquals(dataElementUid, itemsNode.get("dimensionItem").value());
assertEquals("AGGREGATED", itemsNode.get("optionSetItem").get("aggregation").value());
assertEquals(
"[\"BFhv3jQZ8Cw\"]", itemsNode.get("optionSetItem").get("options").value().toString());
}
}

0 comments on commit 0c43892

Please sign in to comment.