1
package org.hisp.dhis.databrowser;
4
* Copyright (c) 2004-2008, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.Date;
33
import java.util.HashSet;
34
import java.util.Iterator;
35
import java.util.List;
37
import org.hisp.dhis.DhisConvenienceTest;
38
import org.hisp.dhis.dataelement.DataElement;
39
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
40
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
41
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
42
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
43
import org.hisp.dhis.dataelement.DataElementService;
44
import org.hisp.dhis.dataset.DataSet;
45
import org.hisp.dhis.dataset.DataSetService;
46
import org.hisp.dhis.datavalue.DataValueService;
47
import org.hisp.dhis.indicator.IndicatorService;
48
import org.hisp.dhis.organisationunit.OrganisationUnit;
49
import org.hisp.dhis.organisationunit.OrganisationUnitService;
50
import org.hisp.dhis.period.Period;
51
import org.hisp.dhis.period.PeriodService;
52
import org.hisp.dhis.period.PeriodType;
55
* @author Joakim Bj�rnstad
58
public class DataBrowserStoreTest
59
extends DhisConvenienceTest
61
private DataBrowserStore dataBrowserStore;
63
private final String T = "true";
65
private final String F = "false";
67
private DataElementCategoryCombo categoryCombo;
69
private DataElementCategoryOptionCombo categoryOptionCombo;
71
private Collection<Integer> dataElementIds;
73
private Collection<Integer> periodIds;
75
private Collection<Integer> organisationUnitIds;
77
private DataSet dataSetA;
78
private DataSet dataSetB;
80
private DataElement dataElementA;
81
private DataElement dataElementB;
83
private Period periodA;
84
private Period periodB;
85
private Period periodC;
86
private Period periodD;
88
private OrganisationUnit unitA;
89
private OrganisationUnit unitB;
90
private OrganisationUnit unitC;
91
private OrganisationUnit unitD;
92
private OrganisationUnit unitE;
93
private OrganisationUnit unitF;
94
private OrganisationUnit unitG;
95
private OrganisationUnit unitH;
96
private OrganisationUnit unitI;
98
public void setUpTest()
101
dataBrowserStore = (DataBrowserStore) getBean( DataBrowserStore.ID );
103
categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID );
105
categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
107
dataSetService = (DataSetService) getBean( DataSetService.ID );
109
dataElementService = (DataElementService) getBean( DataElementService.ID );
111
indicatorService = (IndicatorService) getBean( IndicatorService.ID );
113
periodService = (PeriodService) getBean( PeriodService.ID );
115
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
117
dataValueService = (DataValueService) getBean( DataValueService.ID );
119
categoryCombo = categoryComboService
120
.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
122
categoryOptionCombo = categoryOptionComboService.getDefaultDataElementCategoryOptionCombo();
124
// ---------------------------------------------------------------------
125
// Setup identifier Collections
126
// ---------------------------------------------------------------------
128
dataElementIds = new HashSet<Integer>();
129
periodIds = new HashSet<Integer>();
130
organisationUnitIds = new HashSet<Integer>();
132
// ---------------------------------------------------------------------
133
// Setup DataElements
134
// ---------------------------------------------------------------------
136
dataElementA = createDataElement( 'A', DataElement.TYPE_INT, DataElement.AGGREGATION_OPERATOR_SUM, categoryCombo );
137
dataElementB = createDataElement( 'B', DataElement.TYPE_BOOL, DataElement.AGGREGATION_OPERATOR_SUM, categoryCombo );
139
dataElementIds.add( dataElementService.addDataElement( dataElementA ) );
140
dataElementIds.add( dataElementService.addDataElement( dataElementB ) );
142
Collection<DataElement> dataElementsA = new HashSet<DataElement>();
143
dataElementsA.add( dataElementA );
144
Collection<DataElement> dataElementsB = new HashSet<DataElement>();
145
dataElementsB.add( dataElementB );
147
// ---------------------------------------------------------------------
149
// ---------------------------------------------------------------------
151
Iterator<PeriodType> periodTypeIt = periodService.getAllPeriodTypes().iterator();
152
PeriodType periodTypeA = periodTypeIt.next();
153
PeriodType periodTypeB = periodTypeIt.next();
155
Date mar01 = getDate( 2005, 3, 1 );
156
Date mar31 = getDate( 2005, 3, 31 );
157
Date apr01 = getDate( 2005, 4, 1 );
158
Date apr30 = getDate( 2005, 4, 30 );
159
Date may01 = getDate( 2005, 5, 1 );
160
Date may31 = getDate( 2005, 5, 31 );
162
periodA = createPeriod( periodTypeA, mar01, mar31 );
163
periodB = createPeriod( periodTypeA, apr01, apr30 );
164
periodC = createPeriod( periodTypeB, may01, may31 );
165
periodD = createPeriod( periodTypeB, mar01, may31 );
167
periodIds.add( periodService.addPeriod( periodA ) );
168
periodIds.add( periodService.addPeriod( periodB ) );
169
periodIds.add( periodService.addPeriod( periodC ) );
170
periodIds.add( periodService.addPeriod( periodD ) );
172
// ---------------------------------------------------------------------
174
// ---------------------------------------------------------------------
176
dataSetA = createDataSet( 'A', periodTypeA );
177
dataSetB = createDataSet( 'B', periodTypeB );
179
dataSetA.setDataElements( dataElementsA );
180
dataSetB.setDataElements( dataElementsB );
182
dataSetService.addDataSet( dataSetA );
183
dataSetService.addDataSet( dataSetB );
185
// ---------------------------------------------------------------------
186
// Setup OrganisationUnits
187
// ---------------------------------------------------------------------
189
unitA = createOrganisationUnit( 'A' );
190
unitB = createOrganisationUnit( 'B', unitA );
191
unitC = createOrganisationUnit( 'C', unitA );
192
unitD = createOrganisationUnit( 'D', unitB );
193
unitE = createOrganisationUnit( 'E', unitB );
194
unitF = createOrganisationUnit( 'F', unitB );
195
unitG = createOrganisationUnit( 'G', unitF );
196
unitH = createOrganisationUnit( 'H', unitF );
197
unitI = createOrganisationUnit( 'I' );
199
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitA ) );
200
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitB ) );
201
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitC ) );
202
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitD ) );
203
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitE ) );
204
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitF ) );
205
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitG ) );
206
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitH ) );
207
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitI ) );
209
organisationUnitService.addOrganisationUnitHierarchy( new Date() ); // TODO
211
// ---------------------------------------------------------------------
213
// ---------------------------------------------------------------------
215
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitC, "90", categoryOptionCombo ) );
216
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitD, "10", categoryOptionCombo ) );
217
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitE, "35", categoryOptionCombo ) );
218
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitF, "25", categoryOptionCombo ) );
219
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitG, "20", categoryOptionCombo ) );
220
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitH, "60", categoryOptionCombo ) );
222
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitC, "70", categoryOptionCombo ) );
223
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitD, "40", categoryOptionCombo ) );
224
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitE, "65", categoryOptionCombo ) );
225
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitF, "55", categoryOptionCombo ) );
226
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitG, "20", categoryOptionCombo ) );
227
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitH, "15", categoryOptionCombo ) );
229
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitC, "95", categoryOptionCombo ) );
230
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitD, "40", categoryOptionCombo ) );
231
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitE, "45", categoryOptionCombo ) );
232
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitF, "30", categoryOptionCombo ) );
233
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitG, "50", categoryOptionCombo ) );
234
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitH, "70", categoryOptionCombo ) );
236
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitC, T, categoryOptionCombo ) );
237
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitD, T, categoryOptionCombo ) );
238
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitE, F, categoryOptionCombo ) );
239
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitF, T, categoryOptionCombo ) );
240
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitG, F, categoryOptionCombo ) );
241
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitH, T, categoryOptionCombo ) );
243
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitC, T, categoryOptionCombo ) );
244
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitD, F, categoryOptionCombo ) );
245
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitE, T, categoryOptionCombo ) );
246
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitF, T, categoryOptionCombo ) );
247
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitG, F, categoryOptionCombo ) );
248
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitH, T, categoryOptionCombo ) );
250
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitC, F, categoryOptionCombo ) );
251
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitD, T, categoryOptionCombo ) );
252
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitE, F, categoryOptionCombo ) );
253
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitF, T, categoryOptionCombo ) );
254
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitG, T, categoryOptionCombo ) );
255
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitH, T, categoryOptionCombo ) );
258
public void testGetDataSetsInPeriod()
260
List<Integer> betweenPeriodIds = new ArrayList<Integer>();
261
betweenPeriodIds.add( periodA.getId() );
262
betweenPeriodIds.add( periodB.getId() );
263
betweenPeriodIds.add( periodC.getId() );
264
betweenPeriodIds.add( periodD.getId() );
265
DataBrowserTable table = dataBrowserStore.getDataSetsInPeriod( betweenPeriodIds );
267
assertNotNull( "DataBrowserTable not supposed to be null", table );
268
assertEquals( "No. of queries", 1, table.getQueryCount() );
269
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
271
assertEquals( "Metacolumns", 2, table.getColumns().size() );
272
assertEquals( "DataSet", table.getColumns().get( 0 ).getName() );
273
assertEquals( "Count", table.getColumns().get( 1 ).getName() );
275
assertEquals( "Metarows", 2, table.getRows().size() );
276
assertEquals( dataSetA.getName(), table.getRows().get( 0 ).getName() );
277
assertEquals( dataSetA.getId(), table.getRows().get( 0 ).getId().intValue() );
278
assertEquals( dataSetB.getName(), table.getRows().get( 1 ).getName() );
279
assertEquals( dataSetB.getId(), table.getRows().get( 1 ).getId().intValue() );
281
assertEquals( "Row count entries", 2, table.getCounts().size() );
282
assertEquals( "DataValues in dataSetA", 18, table.getRowBasedOnRowName( dataSetA.getName() ).get( 0 )
284
assertEquals( "DataValues in dataSetB", 18, table.getRowBasedOnRowName( dataSetB.getName() ).get( 0 )
288
public void testSetDataElementStructureForDataSetBetweenPeriods()
290
List<Integer> pList = new ArrayList<Integer>();
291
pList.add( periodA.getId() );
292
pList.add( periodB.getId() );
293
pList.add( periodC.getId() );
294
DataBrowserTable table = new DataBrowserTable();
295
dataBrowserStore.setDataElementStructureForDataSetBetweenPeriods( table, dataSetA.getId(), pList );
297
assertNotNull( "DataBrowserTable not supposed to be null", table );
298
assertEquals( "No. of queries", 1, table.getQueryCount() );
299
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
301
assertEquals( "Metarows", 1, table.getRows().size() );
302
assertEquals( dataElementA.getName(), table.getRows().get( 0 ).getName() );
303
assertEquals( dataElementA.getId(), table.getRows().get( 0 ).getId().intValue() );
306
public void testSetCountDataElementsInOnePeriod()
308
List<Integer> pList = new ArrayList<Integer>();
309
pList.add( periodA.getId() );
310
DataBrowserTable table = new DataBrowserTable();
311
dataBrowserStore.setDataElementStructureForDataSetBetweenPeriods( table, dataSetA.getId(), pList );
312
int results = dataBrowserStore.setCountDataElementsInOnePeriod( table, dataSetA.getId(), periodB.getId() );
313
assertEquals( "DataValue results", 1, results );
315
assertNotNull( "DataBrowserTable not supposed to be null", table );
316
assertEquals( "No. of queries", 1 + 1, table.getQueryCount() );
317
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
319
assertEquals( "Metarows", 1, table.getRows().size() );
320
assertEquals( dataElementA.getName(), table.getRows().get( 0 ).getName() );
321
assertEquals( dataElementA.getId(), table.getRows().get( 0 ).getId().intValue() );
323
assertEquals( "Row count entries", 1, table.getCounts().size() );
324
assertEquals( "DataValues in dataElementA for periodA", 6, table.getRowBasedOnRowName( dataElementA.getName() )
325
.get( 0 ).intValue() );
328
public void testSetStructureForOrgUnitBetweenPeriods()
330
List<Integer> pList = new ArrayList<Integer>();
331
pList.add( periodA.getId() );
332
pList.add( periodB.getId() );
333
pList.add( periodC.getId() );
334
pList.add( periodD.getId() );
335
DataBrowserTable table = new DataBrowserTable();
336
dataBrowserStore.setStructureForOrgUnitBetweenPeriods( table, unitB.getId(), pList );
338
assertNotNull( "DataBrowserTable not supposed to be null", table );
339
assertEquals( "No. of queries", 1, table.getQueryCount() );
340
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
342
assertEquals( "Metarows", 3, table.getRows().size() );
343
assertEquals( unitD.getName(), table.getRows().get( 0 ).getName() );
344
assertEquals( unitD.getId(), table.getRows().get( 0 ).getId().intValue() );
345
assertEquals( unitE.getName(), table.getRows().get( 1 ).getName() );
346
assertEquals( unitE.getId(), table.getRows().get( 1 ).getId().intValue() );
347
assertEquals( unitF.getName(), table.getRows().get( 2 ).getName() );
348
assertEquals( unitF.getId(), table.getRows().get( 2 ).getId().intValue() );
352
public void testSetCountOrgUnitsInOnePeriod()
354
List<Integer> pList = new ArrayList<Integer>();
355
pList.add( periodA.getId() );
356
DataBrowserTable table = new DataBrowserTable();
357
dataBrowserStore.setStructureForOrgUnitBetweenPeriods( table, unitB.getId(), pList );
358
int results = dataBrowserStore.setCountOrgUnitsInOnePeriod( table, unitB.getId(), periodA.getId() );
359
assertEquals( "DataValue results", 3, results );
361
assertNotNull( "DataBrowserTable not supposed to be null", table );
362
assertEquals( "No. of queries", 2, table.getQueryCount() );
363
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
365
assertEquals( "Metarows", 3, table.getRows().size() );
366
assertEquals( unitD.getName(), table.getRows().get( 0 ).getName() );
367
assertEquals( unitD.getId(), table.getRows().get( 0 ).getId().intValue() );
368
assertEquals( unitF.getName(), table.getRows().get( 2 ).getName() );
369
assertEquals( unitF.getId(), table.getRows().get( 2 ).getId().intValue() );
371
assertEquals( "Row count entries", 3, table.getCounts().size() );
372
assertEquals( "DataValues in unitD for periodA", 2, table.getRowBasedOnRowName( unitD.getName() ).get( 0 )
374
assertEquals( "DataValues in unitF for periodA", 2, table.getRowBasedOnRowName( unitF.getName() ).get( 0 )