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.Collection;
31
import java.util.Date;
32
import java.util.HashSet;
33
import java.util.Iterator;
35
import org.hisp.dhis.DhisConvenienceTest;
36
import org.hisp.dhis.dataelement.DataElement;
37
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
38
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
39
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
40
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
41
import org.hisp.dhis.dataelement.DataElementService;
42
import org.hisp.dhis.dataset.DataSet;
43
import org.hisp.dhis.dataset.DataSetService;
44
import org.hisp.dhis.datavalue.DataValueService;
45
import org.hisp.dhis.indicator.IndicatorService;
46
import org.hisp.dhis.organisationunit.OrganisationUnit;
47
import org.hisp.dhis.organisationunit.OrganisationUnitService;
48
import org.hisp.dhis.period.Period;
49
import org.hisp.dhis.period.PeriodService;
50
import org.hisp.dhis.period.PeriodType;
53
* @author Joakim Bj�rnstad
56
public class DataBrowserServiceTest
57
extends DhisConvenienceTest
59
private DataBrowserService dataBrowserService;
61
private final String T = "true";
62
private final String F = "false";
64
private DataElementCategoryCombo categoryCombo;
66
private DataElementCategoryOptionCombo categoryOptionCombo;
68
private Collection<Integer> dataElementIds;
70
private Collection<Integer> periodIds;
72
private Collection<Integer> organisationUnitIds;
74
private DataSet dataSetA;
75
private DataSet dataSetB;
77
private DataElement dataElementA;
78
private DataElement dataElementB;
80
private Period periodA;
81
private Period periodB;
82
private Period periodC;
83
private Period periodD;
85
private OrganisationUnit unitA;
86
private OrganisationUnit unitB;
87
private OrganisationUnit unitC;
88
private OrganisationUnit unitD;
89
private OrganisationUnit unitE;
90
private OrganisationUnit unitF;
91
private OrganisationUnit unitG;
92
private OrganisationUnit unitH;
93
private OrganisationUnit unitI;
95
public void setUpTest()
98
dataBrowserService = (DataBrowserService) getBean( DataBrowserService.ID );
100
categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID );
102
categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
104
dataSetService = (DataSetService) getBean( DataSetService.ID );
106
dataElementService = (DataElementService) getBean( DataElementService.ID );
108
indicatorService = (IndicatorService) getBean( IndicatorService.ID );
110
periodService = (PeriodService) getBean( PeriodService.ID );
112
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
114
dataValueService = (DataValueService) getBean( DataValueService.ID );
116
categoryCombo = categoryComboService.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
118
categoryOptionCombo = categoryOptionComboService.getDefaultDataElementCategoryOptionCombo();
120
// ---------------------------------------------------------------------
121
// Setup identifier Collections
122
// ---------------------------------------------------------------------
124
dataElementIds = new HashSet<Integer>();
125
periodIds = new HashSet<Integer>();
126
organisationUnitIds = new HashSet<Integer>();
128
// ---------------------------------------------------------------------
129
// Setup DataElements
130
// ---------------------------------------------------------------------
132
dataElementA = createDataElement( 'A', DataElement.TYPE_INT, DataElement.AGGREGATION_OPERATOR_SUM, categoryCombo );
133
dataElementB = createDataElement( 'B', DataElement.TYPE_BOOL, DataElement.AGGREGATION_OPERATOR_SUM, categoryCombo );
135
Collection<DataElement> dataElementsA = new HashSet<DataElement>();
136
dataElementsA.add( dataElementA );
137
Collection<DataElement> dataElementsB = new HashSet<DataElement>();
138
dataElementsB.add( dataElementB );
140
dataElementIds.add( dataElementService.addDataElement( dataElementA ) );
141
dataElementIds.add( dataElementService.addDataElement( dataElementB ) );
143
// ---------------------------------------------------------------------
145
// ---------------------------------------------------------------------
147
Iterator<PeriodType> periodTypeIt = periodService.getAllPeriodTypes().iterator();
148
PeriodType periodTypeA = periodTypeIt.next();
149
PeriodType periodTypeB = periodTypeIt.next();
151
Date mar01 = getDate( 2005, 3, 1 );
152
Date mar31 = getDate( 2005, 3, 31 );
153
Date apr01 = getDate( 2005, 4, 1 );
154
Date apr30 = getDate( 2005, 4, 30 );
155
Date may01 = getDate( 2005, 5, 1 );
156
Date may31 = getDate( 2005, 5, 31 );
158
periodA = createPeriod( periodTypeA, mar01, mar31 );
159
periodB = createPeriod( periodTypeA, apr01, apr30 );
160
periodC = createPeriod( periodTypeB, may01, may31 );
161
periodD = createPeriod( periodTypeB, mar01, may31 );
163
periodIds.add( periodService.addPeriod( periodA ) );
164
periodIds.add( periodService.addPeriod( periodB ) );
165
periodIds.add( periodService.addPeriod( periodC ) );
166
periodIds.add( periodService.addPeriod( periodD ) );
168
// ---------------------------------------------------------------------
170
// ---------------------------------------------------------------------
172
dataSetA = createDataSet( 'A', periodTypeA );
173
dataSetB = createDataSet( 'B', periodTypeB );
175
dataSetA.setDataElements( dataElementsA );
176
dataSetB.setDataElements( dataElementsB );
178
dataSetService.addDataSet( dataSetA );
179
dataSetService.addDataSet( dataSetB );
181
// ---------------------------------------------------------------------
182
// Setup OrganisationUnits
183
// ---------------------------------------------------------------------
185
unitA = createOrganisationUnit( 'A' );
186
unitB = createOrganisationUnit( 'B', unitA );
187
unitC = createOrganisationUnit( 'C', unitA );
188
unitD = createOrganisationUnit( 'D', unitB );
189
unitE = createOrganisationUnit( 'E', unitB );
190
unitF = createOrganisationUnit( 'F', unitB );
191
unitG = createOrganisationUnit( 'G', unitF );
192
unitH = createOrganisationUnit( 'H', unitF );
193
unitI = createOrganisationUnit( 'I' );
195
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitA ) );
196
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitB ) );
197
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitC ) );
198
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitD ) );
199
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitE ) );
200
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitF ) );
201
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitG ) );
202
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitH ) );
203
organisationUnitIds.add( organisationUnitService.addOrganisationUnit( unitI ) );
205
organisationUnitService.addOrganisationUnitHierarchy( new Date() ); // TODO
207
// ---------------------------------------------------------------------
209
// ---------------------------------------------------------------------
211
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitC, "90", categoryOptionCombo ) );
212
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitD, "10", categoryOptionCombo ) );
213
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitE, "35", categoryOptionCombo ) );
214
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitF, "25", categoryOptionCombo ) );
215
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitG, "20", categoryOptionCombo ) );
216
dataValueService.addDataValue( createDataValue( dataElementA, periodA, unitH, "60", categoryOptionCombo ) );
218
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitC, "70", categoryOptionCombo ) );
219
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitD, "40", categoryOptionCombo ) );
220
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitE, "65", categoryOptionCombo ) );
221
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitF, "55", categoryOptionCombo ) );
222
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitG, "20", categoryOptionCombo ) );
223
dataValueService.addDataValue( createDataValue( dataElementA, periodB, unitH, "15", categoryOptionCombo ) );
225
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitC, "95", categoryOptionCombo ) );
226
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitD, "40", categoryOptionCombo ) );
227
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitE, "45", categoryOptionCombo ) );
228
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitF, "30", categoryOptionCombo ) );
229
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitG, "50", categoryOptionCombo ) );
230
dataValueService.addDataValue( createDataValue( dataElementA, periodC, unitH, "70", categoryOptionCombo ) );
232
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitC, T, categoryOptionCombo ) );
233
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitD, T, categoryOptionCombo ) );
234
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitE, F, categoryOptionCombo ) );
235
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitF, T, categoryOptionCombo ) );
236
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitG, F, categoryOptionCombo ) );
237
dataValueService.addDataValue( createDataValue( dataElementB, periodA, unitH, T, categoryOptionCombo ) );
239
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitC, T, categoryOptionCombo ) );
240
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitD, F, categoryOptionCombo ) );
241
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitE, T, categoryOptionCombo ) );
242
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitF, T, categoryOptionCombo ) );
243
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitG, F, categoryOptionCombo ) );
244
dataValueService.addDataValue( createDataValue( dataElementB, periodB, unitH, T, categoryOptionCombo ) );
246
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitC, F, categoryOptionCombo ) );
247
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitD, T, categoryOptionCombo ) );
248
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitE, F, categoryOptionCombo ) );
249
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitF, T, categoryOptionCombo ) );
250
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitG, T, categoryOptionCombo ) );
251
dataValueService.addDataValue( createDataValue( dataElementB, periodC, unitH, T, categoryOptionCombo ) );
255
public void testGetAllCountDataSetsByPeriodType()
257
DataBrowserTable table = dataBrowserService.getAllCountDataSetsByPeriodType( periodA.getPeriodType() );
258
assertNotNull( "DataBrowserTable not supposed to be null", table );
259
assertEquals( "No. of queries", 1, table.getQueryCount() );
260
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
262
assertEquals( "Metarows", 2, table.getRows().size() );
263
assertEquals( dataSetA.getName(), table.getRows().get( 0 ).getName() );
264
assertEquals( dataSetA.getId(), table.getRows().get( 0 ).getId().intValue() );
265
assertEquals( dataSetB.getName(), table.getRows().get( 1 ).getName() );
266
assertEquals( dataSetB.getId(), table.getRows().get( 1 ).getId().intValue() );
268
assertEquals( "Metacolumns", 2, table.getColumns().size() );
270
assertEquals( "Row count entries", 2, table.getCounts().size() );
271
assertEquals( "DataValues in dataSetA", 12, table.getRowBasedOnRowName( dataSetA.getName() ).get( 0 )
273
assertEquals( "DataValues in dataSetB", 12, table.getRowBasedOnRowName( dataSetB.getName() ).get( 0 )
278
public void testGetAllCountDataElementsByPeriodType()
280
DataBrowserTable table = dataBrowserService.getAllCountDataElementsByPeriodType( dataSetA.getId(), periodA
282
assertNotNull( "DataBrowserTable not supposed to be null", table );
283
assertEquals( "No. of queries", 3, table.getQueryCount() );
284
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
286
assertEquals( "Metarows", 1, table.getRows().size() );
287
assertEquals( dataElementA.getName(), table.getRows().get( 0 ).getName() );
288
assertEquals( dataElementA.getId(), table.getRows().get( 0 ).getId().intValue() );
290
assertEquals( "Metacolumns", 3, table.getColumns().size() );
292
assertEquals( "Row count entries", 1, table.getCounts().size() );
293
assertEquals( "DataValues in periodA", 6, table.getRowBasedOnRowName( dataElementA.getName() ).get( 0 )
295
assertEquals( "DataValues in periodB", 6, table.getRowBasedOnRowName( dataElementA.getName() ).get( 1 )
299
public void testGetAllCountOrgUnitsByPeriodType()
301
DataBrowserTable table = dataBrowserService.getAllCountOrgUnitsByPeriodType( unitB.getId(), periodA
303
assertNotNull( "DataBrowserTable not supposed to be null", table );
304
assertNotSame( "No. of queries more than 0", 0, table.getQueryCount() );
305
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
307
assertEquals( "Metarows", 3, table.getRows().size() );
308
assertEquals( unitD.getName(), table.getRows().get( 0 ).getName() );
309
assertEquals( unitD.getId(), table.getRows().get( 0 ).getId().intValue() );
310
assertEquals( unitF.getName(), table.getRows().get( 2 ).getName() );
311
assertEquals( unitF.getId(), table.getRows().get( 2 ).getId().intValue() );
313
assertEquals( "Metacolumns", 1 + 2, table.getColumns().size() );
314
assertEquals( periodA.getStartDate().toString(), table.getColumns().get( 1 ).getName() );
315
assertEquals( periodB.getStartDate().toString(), table.getColumns().get( 2 ).getName() );
317
assertEquals( "Row count entries", 3, table.getCounts().size() );
318
assertEquals( "DataValues in unitF for periodB", 2, table.getRowBasedOnRowName( unitF.getName() ).get( 1 )
323
public void testGetCountDataSetsInPeriod()
326
DataBrowserTable table = dataBrowserService.getCountDataSetsInPeriod( "2005-02-28", "2005-05-01", periodA
328
assertNotNull( "DataBrowserTable not supposed to be null", table );
329
assertEquals( "No. of queries", 1, table.getQueryCount() );
330
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
332
assertEquals( "Metarows", 2, table.getRows().size() );
333
assertEquals( dataSetA.getName(), table.getRows().get( 0 ).getName() );
334
assertEquals( dataSetA.getId(), table.getRows().get( 0 ).getId().intValue() );
335
assertEquals( dataSetB.getName(), table.getRows().get( 1 ).getName() );
336
assertEquals( dataSetB.getId(), table.getRows().get( 1 ).getId().intValue() );
338
assertEquals( "Metacolumns", 2, table.getColumns().size() );
340
assertEquals( "Row count entries", 2, table.getCounts().size() );
341
assertEquals( "DataValues in dataSetA", 12, table.getRowBasedOnRowName( dataSetA.getName() ).get( 0 )
343
assertEquals( "DataValues in dataSetB", 12, table.getRowBasedOnRowName( dataSetB.getName() ).get( 0 )
347
public void testGetCountDataElementsInPeriod()
349
DataBrowserTable table = dataBrowserService.getCountDataElementsInPeriod( dataSetA.getId(), "2005-02-28",
350
"2005-05-01", periodA.getPeriodType() );
351
assertNotNull( "DataBrowserTable not supposed to be null", table );
352
assertEquals( "No. of queries", 3, table.getQueryCount() );
353
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
355
assertEquals( "Metarows", 1, table.getRows().size() );
356
assertEquals( dataElementA.getName(), table.getRows().get( 0 ).getName() );
357
assertEquals( dataElementA.getId(), table.getRows().get( 0 ).getId().intValue() );
359
assertEquals( "Metacolumns", 1 + 2, table.getColumns().size() );
361
assertEquals( "Row count entries", 1, table.getCounts().size() );
362
assertEquals( "DataValues in periodA", 6, table.getRowBasedOnRowName( dataElementA.getName() ).get( 0 )
364
assertEquals( "DataValues in periodB", 6, table.getRowBasedOnRowName( dataElementA.getName() ).get( 1 )
368
public void testGetCountOrgUnitsInPeriod()
370
DataBrowserTable table = dataBrowserService.getCountOrgUnitsInPeriod( unitB.getId(), "2005-02-28",
371
"2005-05-01", periodA.getPeriodType() );
373
assertNotNull( "DataBrowserTable not supposed to be null", table );
374
assertEquals( "No. of queries", 3, table.getQueryCount() );
375
assertNotSame( "Querytime more than 0", 0, table.getQueryTime() );
377
assertEquals( "Metarows", 3, table.getRows().size() );
378
assertEquals( unitD.getName(), table.getRows().get( 0 ).getName() );
379
assertEquals( unitD.getId(), table.getRows().get( 0 ).getId().intValue() );
380
assertEquals( unitF.getName(), table.getRows().get( 2 ).getName() );
381
assertEquals( unitF.getId(), table.getRows().get( 2 ).getId().intValue() );
383
assertEquals( "Metacolumns", 1 + 2, table.getColumns().size() );
384
assertEquals( periodA.getStartDate().toString(), table.getColumns().get( 1 ).getName() );
385
assertEquals( periodB.getStartDate().toString(), table.getColumns().get( 2 ).getName() );
387
assertEquals( "Row count entries", 3, table.getCounts().size() );
388
assertEquals( "DataValues in unitF for periodA", 2, table.getRowBasedOnRowName( unitF.getName() ).get( 0 )