|
32 | 32 | import static org.hisp.dhis.analytics.DataQueryParams.VALUE_ID; |
33 | 33 | import static org.hisp.dhis.common.DimensionConstants.PERIOD_DIM_ID; |
34 | 34 | import static org.hisp.dhis.common.DimensionType.PERIOD; |
| 35 | +import static org.hisp.dhis.test.TestBase.createDataElement; |
| 36 | +import static org.hisp.dhis.test.TestBase.createLegend; |
| 37 | +import static org.hisp.dhis.test.TestBase.createLegendSet; |
35 | 38 | import static org.hisp.dhis.test.TestBase.createOrganisationUnit; |
36 | 39 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 40 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
37 | 41 |
|
38 | 42 | import java.lang.reflect.Method; |
| 43 | +import java.util.ArrayList; |
| 44 | +import java.util.HashMap; |
39 | 45 | import java.util.LinkedHashMap; |
40 | 46 | import java.util.List; |
41 | 47 | import java.util.Map; |
42 | 48 | import org.hisp.dhis.analytics.AnalyticsMetaDataKey; |
| 49 | +import org.hisp.dhis.analytics.EventAnalyticsDimensionalItem; |
43 | 50 | import org.hisp.dhis.analytics.common.ColumnHeader; |
44 | 51 | import org.hisp.dhis.analytics.event.EventQueryParams; |
45 | 52 | import org.hisp.dhis.common.BaseDimensionalObject; |
46 | 53 | import org.hisp.dhis.common.DimensionalItemObject; |
47 | 54 | import org.hisp.dhis.common.Grid; |
48 | 55 | import org.hisp.dhis.common.GridHeader; |
| 56 | +import org.hisp.dhis.common.ValueType; |
| 57 | +import org.hisp.dhis.common.ValueTypedDimensionalItemObject; |
| 58 | +import org.hisp.dhis.dataelement.DataElement; |
| 59 | +import org.hisp.dhis.legend.Legend; |
| 60 | +import org.hisp.dhis.legend.LegendSet; |
49 | 61 | import org.hisp.dhis.period.PeriodDimension; |
50 | 62 | import org.hisp.dhis.program.Program; |
51 | 63 | import org.hisp.dhis.system.grid.ListGrid; |
@@ -123,6 +135,39 @@ void shouldRemoveRawPeMetadataWhenNoDefaultAggregatePeriodIsVisible() throws Exc |
123 | 135 | assertEquals(List.of("scheduleddate"), dimensions.keySet().stream().toList()); |
124 | 136 | } |
125 | 137 |
|
| 138 | + @Test |
| 139 | + void shouldFallBackToAllLegendsWhenDimensionsMetadataOmitsLegendSetDataElement() |
| 140 | + throws Exception { |
| 141 | + Legend legendA = createLegend('A', 0.0, 2.0); |
| 142 | + Legend legendB = createLegend('B', 2.0, 4.0); |
| 143 | + LegendSet legendSet = createLegendSet('A', legendA, legendB); |
| 144 | + |
| 145 | + DataElement dataElement = createDataElement('A'); |
| 146 | + dataElement.setValueType(ValueType.NUMBER); |
| 147 | + dataElement.setLegendSets(List.of(legendSet)); |
| 148 | + |
| 149 | + Grid grid = new ListGrid(); |
| 150 | + grid.getMetaData().put(AnalyticsMetaDataKey.DIMENSIONS.getKey(), new HashMap<String, Object>()); |
| 151 | + |
| 152 | + List<EventAnalyticsDimensionalItem> dimensionalItems = new ArrayList<>(); |
| 153 | + |
| 154 | + invokeAddEventReportDimensionalItems(dataElement, dimensionalItems, grid, dataElement.getUid()); |
| 155 | + |
| 156 | + assertFalse(dimensionalItems.isEmpty()); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void shouldNotNpeWhenItemsMetadataOmitsRowDimension() throws Exception { |
| 161 | + String missingDimensionUid = "deUidAAAAA"; |
| 162 | + |
| 163 | + Grid grid = new ListGrid(); |
| 164 | + grid.getMetaData().put(AnalyticsMetaDataKey.ITEMS.getKey(), new HashMap<String, Object>()); |
| 165 | + |
| 166 | + EventQueryParams params = new EventQueryParams.Builder().build(); |
| 167 | + |
| 168 | + invokeGenerateOutputGrid(grid, params, List.of(), List.of(), List.of(missingDimensionUid)); |
| 169 | + } |
| 170 | + |
126 | 171 | @Test |
127 | 172 | void shouldKeepValueAndEnrollmentOuHeadersUnchanged() throws Exception { |
128 | 173 | EventQueryParams params = |
@@ -157,6 +202,44 @@ private void invokePrivate( |
157 | 202 | method.invoke(service, arg0, arg1); |
158 | 203 | } |
159 | 204 |
|
| 205 | + private void invokeAddEventReportDimensionalItems( |
| 206 | + ValueTypedDimensionalItemObject item, |
| 207 | + List<EventAnalyticsDimensionalItem> dimensionalItems, |
| 208 | + Grid grid, |
| 209 | + String dimension) |
| 210 | + throws Exception { |
| 211 | + Method method = |
| 212 | + EventAggregateService.class.getDeclaredMethod( |
| 213 | + "addEventReportDimensionalItems", |
| 214 | + ValueTypedDimensionalItemObject.class, |
| 215 | + List.class, |
| 216 | + Grid.class, |
| 217 | + String.class); |
| 218 | + method.setAccessible(true); |
| 219 | + method.invoke(service, item, dimensionalItems, grid, dimension); |
| 220 | + } |
| 221 | + |
| 222 | + private Grid invokeGenerateOutputGrid( |
| 223 | + Grid grid, |
| 224 | + EventQueryParams params, |
| 225 | + List<Map<String, EventAnalyticsDimensionalItem>> rowPermutations, |
| 226 | + List<Map<String, EventAnalyticsDimensionalItem>> columnPermutations, |
| 227 | + List<String> rowDimensions) |
| 228 | + throws Exception { |
| 229 | + |
| 230 | + Method method = |
| 231 | + EventAggregateService.class.getDeclaredMethod( |
| 232 | + "generateOutputGrid", |
| 233 | + Grid.class, |
| 234 | + EventQueryParams.class, |
| 235 | + List.class, |
| 236 | + List.class, |
| 237 | + List.class); |
| 238 | + method.setAccessible(true); |
| 239 | + return (Grid) |
| 240 | + method.invoke(service, grid, params, rowPermutations, columnPermutations, rowDimensions); |
| 241 | + } |
| 242 | + |
160 | 243 | private EventQueryParams singleDateFieldParams(String dateField, Program program) { |
161 | 244 | PeriodDimension period = PeriodDimension.of("2021").setDateField(dateField); |
162 | 245 | BaseDimensionalObject periodDimension = |
|
0 commit comments