4
* Copyright (c) 2004-2007, 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.Calendar;
31
import java.util.Collection;
32
import java.util.Date;
33
import java.util.HashSet;
36
import org.hisp.dhis.datadictionary.DataDictionary;
37
import org.hisp.dhis.datadictionary.DataDictionaryService;
38
import org.hisp.dhis.datadictionary.ExtendedDataElement;
39
import org.hisp.dhis.dataelement.CalculatedDataElement;
40
import org.hisp.dhis.dataelement.DataElement;
41
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
42
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
43
import org.hisp.dhis.dataelement.DataElementCategoryOption;
44
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
45
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
46
import org.hisp.dhis.dataelement.DataElementCategoryOptionService;
47
import org.hisp.dhis.dataelement.DataElementCategoryService;
48
import org.hisp.dhis.dataelement.DataElementDimensionColumnOrderService;
49
import org.hisp.dhis.dataelement.DataElementDimensionRowOrderService;
50
import org.hisp.dhis.dataelement.DataElementGroup;
51
import org.hisp.dhis.dataelement.DataElementService;
52
import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
53
import org.hisp.dhis.dataset.DataSet;
54
import org.hisp.dhis.dataset.DataSetService;
55
import org.hisp.dhis.datavalue.DataValue;
56
import org.hisp.dhis.datavalue.DataValueService;
57
import org.hisp.dhis.expression.Expression;
58
import org.hisp.dhis.expression.ExpressionService;
59
import org.hisp.dhis.importexport.ImportDataValue;
60
import org.hisp.dhis.importexport.ImportObjectStatus;
61
import org.hisp.dhis.indicator.Indicator;
62
import org.hisp.dhis.indicator.IndicatorGroup;
63
import org.hisp.dhis.indicator.IndicatorService;
64
import org.hisp.dhis.indicator.IndicatorType;
65
import org.hisp.dhis.organisationunit.OrganisationUnit;
66
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
67
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
68
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
69
import org.hisp.dhis.organisationunit.OrganisationUnitService;
70
import org.hisp.dhis.period.Period;
71
import org.hisp.dhis.period.PeriodService;
72
import org.hisp.dhis.period.PeriodType;
73
import org.hisp.dhis.resourcetable.ResourceTableService;
74
import org.hisp.dhis.source.Source;
75
import org.hisp.dhis.source.SourceStore;
76
import org.hisp.dhis.validation.ValidationRule;
77
import org.hisp.dhis.validation.ValidationRuleGroup;
78
import org.hisp.dhis.validation.ValidationRuleService;
81
* @author Lars Helge Overland
84
public class DhisConvenienceTest
85
extends DhisSpringTest
87
private static final String BASE_UUID = "C3C2E28D-9686-4634-93FD-BE3133935EC";
89
private static Date date;
91
private static Calendar calendar;
93
// -------------------------------------------------------------------------
95
// -------------------------------------------------------------------------
97
protected DataElementService dataElementService;
99
protected DataElementCategoryService categoryService;
101
protected DataElementCategoryComboService categoryComboService;
103
protected DataElementCategoryOptionService categoryOptionService;
105
protected DataElementCategoryOptionComboService categoryOptionComboService;
107
protected DataElementDimensionColumnOrderService dimensionColumnOrderService;
109
protected DataElementDimensionRowOrderService dimensionRowOrderService;
111
protected DataDictionaryService dataDictionaryService;
113
protected IndicatorService indicatorService;
115
protected DataSetService dataSetService;
117
protected CompleteDataSetRegistrationService completeDataSetRegistrationService;
119
protected SourceStore sourceStore;
121
protected OrganisationUnitService organisationUnitService;
123
protected OrganisationUnitGroupService organisationUnitGroupService;
125
protected PeriodService periodService;
127
protected ValidationRuleService validationRuleService;
129
protected ExpressionService expressionService;
131
protected DataValueService dataValueService;
133
protected ResourceTableService resourceTableService;
137
calendar = Calendar.getInstance();
139
calendar.set( 1970, Calendar.JANUARY, 1 );
141
date = calendar.getTime();
144
// -------------------------------------------------------------------------
145
// Convenience methods
146
// -------------------------------------------------------------------------
151
* @param year the year.
152
* @param month the month.
153
* @param day the day of month.
156
protected static Date getDate( int year, int month, int day )
158
calendar.set( year, month - 1, day );
160
return calendar.getTime();
164
* Compares two collections for equality. This method does not check for the
165
* implementation type of the collection in contrast to the native equals
166
* method. This is useful for black-box testing where one will not know the
167
* implementation type of the returned collection for a method.
169
* @param actual the actual collection to check.
170
* @param reference the reference objects to check against.
171
* @return true if the collections are equal, false otherwise.
173
protected static boolean equals( Collection<?> actual, Object... reference )
175
Collection<Object> collection = new HashSet<Object>();
177
for ( Object object : reference )
179
collection.add( object );
182
if ( actual == collection )
187
if ( actual == null || collection == null )
192
if ( actual.size() != collection.size() )
197
for ( Object object : actual )
199
if ( !collection.contains( object ) )
205
for ( Object object : collection )
207
if ( !actual.contains( object ) )
216
// -------------------------------------------------------------------------
217
// Create object methods
218
// -------------------------------------------------------------------------
221
* @param uniqueCharacter A unique character to identify the object.
223
protected static DataElement createDataElement( char uniqueCharacter )
225
DataElement dataElement = new DataElement();
227
dataElement.setUuid( BASE_UUID + uniqueCharacter );
228
dataElement.setName( "DataElement" + uniqueCharacter );
229
dataElement.setAlternativeName( "AlternativeName" + uniqueCharacter );
230
dataElement.setShortName( "ShortName" + uniqueCharacter );
231
dataElement.setCode( "Code" + uniqueCharacter );
232
dataElement.setDescription( "Description" + uniqueCharacter );
233
dataElement.setActive( true );
234
dataElement.setType( DataElement.TYPE_INT );
235
dataElement.setAggregationOperator( DataElement.AGGREGATION_OPERATOR_SUM );
241
* @param uniqueCharacter A unique character to identify the object.
242
* @param categoryCombo The category combo.
244
protected static DataElement createDataElement( char uniqueCharacter, DataElementCategoryCombo categoryCombo )
246
DataElement dataElement = createDataElement( uniqueCharacter );
248
dataElement.setCategoryCombo( categoryCombo );
254
* @param uniqueCharacter A unique character to identify the object.
255
* @param type The type.
256
* @param aggregationOperator The aggregation operator.
258
protected static DataElement createDataElement( char uniqueCharacter, String type, String aggregationOperator )
260
DataElement dataElement = createDataElement( uniqueCharacter );
262
dataElement.setType( type );
263
dataElement.setAggregationOperator( aggregationOperator );
269
* @param uniqueCharacter A unique character to identify the object.
270
* @param type The type.
271
* @param aggregationOperator The aggregation operator.
272
* @param categoryCombo The category combo.
274
protected static DataElement createDataElement( char uniqueCharacter, String type, String aggregationOperator, DataElementCategoryCombo categoryCombo )
276
DataElement dataElement = createDataElement( uniqueCharacter );
278
dataElement.setType( type );
279
dataElement.setAggregationOperator( aggregationOperator );
280
dataElement.setCategoryCombo( categoryCombo );
286
* @param uniqueCharacter A unique character to identify the object.
287
* @param expression The Expression.
289
protected static CalculatedDataElement createCalculatedDataElement( char uniqueCharacter, String type, String aggregationOperator, DataElementCategoryCombo categoryCombo, Expression expression )
291
CalculatedDataElement dataElement = new CalculatedDataElement();
293
dataElement.setUuid( BASE_UUID + uniqueCharacter );
294
dataElement.setName( "DataElement" + uniqueCharacter );
295
dataElement.setAlternativeName( "AlternativeName" + uniqueCharacter );
296
dataElement.setShortName( "ShortName" + uniqueCharacter );
297
dataElement.setCode( "Code" + uniqueCharacter );
298
dataElement.setDescription( "Description" + uniqueCharacter );
299
dataElement.setActive( true );
300
dataElement.setType( type );
301
dataElement.setAggregationOperator( aggregationOperator );
302
dataElement.setCategoryCombo( categoryCombo );
303
dataElement.setSaved( false );
304
dataElement.setExpression( expression );
310
* @param categoryComboUniqueIdentifier A unique character to identify the category combo.
311
* @param categoryOptionUniqueIdentifiers Unique characters to identify the category options.
314
protected static DataElementCategoryOptionCombo createCategoryOptionCombo( char categoryComboUniqueIdentifier, char... categoryOptionUniqueIdentifiers )
316
DataElementCategoryOptionCombo categoryOptionCombo = new DataElementCategoryOptionCombo();
318
categoryOptionCombo.setCategoryCombo( new DataElementCategoryCombo( "CategoryCombo" + categoryComboUniqueIdentifier ) );
320
for ( char identifier : categoryOptionUniqueIdentifiers )
322
categoryOptionCombo.getCategoryOptions().add( new DataElementCategoryOption( "CategoryOption" + identifier, "CategoryOption" + identifier ) );
325
return categoryOptionCombo;
329
* @param uniqueCharacter A unique character to identify the object.
331
protected static DataElementGroup createDataElementGroup( char uniqueCharacter )
333
DataElementGroup group = new DataElementGroup();
335
group.setUuid( BASE_UUID + uniqueCharacter );
336
group.setName( "DataElementGroup" + uniqueCharacter );
342
* @param uniqueCharacter A unique character to identify the object.
344
protected static DataDictionary createDataDictionary( char uniqueCharacter )
346
DataDictionary dictionary = new DataDictionary();
348
dictionary.setName( "DataDictionary" + uniqueCharacter );
349
dictionary.setDescription( "Description" + uniqueCharacter );
350
dictionary.setRegion( "Region" + uniqueCharacter );
356
* @param uniqueCharacter A unique character to identify the object.
358
protected static IndicatorType createIndicatorType( char uniqueCharacter )
360
IndicatorType type = new IndicatorType();
362
type.setName( "IndicatorType" + uniqueCharacter );
363
type.setFactor( 100 );
369
* @param uniqueCharacter A unique character to identify the object.
370
* @param type The type.
372
protected static Indicator createIndicator( char uniqueCharacter, IndicatorType type )
374
Indicator indicator = new Indicator();
376
indicator.setUuid( BASE_UUID + uniqueCharacter );
377
indicator.setName( "Indicator" + uniqueCharacter );
378
indicator.setAlternativeName( "AlternativeName" + uniqueCharacter );
379
indicator.setShortName( "ShortName" + uniqueCharacter );
380
indicator.setCode( "Code" + uniqueCharacter );
381
indicator.setDescription( "Description" + uniqueCharacter );
382
indicator.setAnnualized( false );
383
indicator.setIndicatorType( type );
384
indicator.setNumerator( "Numerator" );
385
indicator.setNumeratorDescription( "NumeratorDescription" );
386
indicator.setNumeratorAggregationOperator( DataElement.AGGREGATION_OPERATOR_SUM );
387
indicator.setDenominator( "Denominator" );
388
indicator.setDenominatorDescription( "DenominatorDescription" );
389
indicator.setDenominatorAggregationOperator( DataElement.AGGREGATION_OPERATOR_SUM );
395
* @param uniqueCharacter A unique character to identify the object.
397
protected static IndicatorGroup createIndicatorGroup( char uniqueCharacter )
399
IndicatorGroup group = new IndicatorGroup();
401
group.setUuid( BASE_UUID + uniqueCharacter );
402
group.setName( "IndicatorGroup" + uniqueCharacter );
408
* @param uniqueCharacter A unique character to identify the object.
409
* @param periodType The period type.
411
protected static DataSet createDataSet( char uniqueCharacter, PeriodType periodType )
413
DataSet dataSet = new DataSet();
415
dataSet.setName( "DataSet" + uniqueCharacter );
416
dataSet.setShortName( "ShortName" + uniqueCharacter );
417
dataSet.setCode( "Code" + uniqueCharacter );
418
dataSet.setPeriodType( periodType );
424
* @param uniqueCharacter A unique character to identify the object.
426
protected static OrganisationUnit createOrganisationUnit( char uniqueCharacter )
428
OrganisationUnit unit = new OrganisationUnit();
430
unit.setUuid( BASE_UUID + uniqueCharacter );
431
unit.setName( "OrganisationUnit" + uniqueCharacter );
432
unit.setShortName( "ShortName" + uniqueCharacter );
433
unit.setCode( "Code" + uniqueCharacter );
434
unit.setOpeningDate( date );
435
unit.setClosedDate( date );
436
unit.setActive( true );
437
unit.setComment( "Comment" + uniqueCharacter );
438
unit.setGeoCode( "GeoCode" );
439
unit.setLatitude( "Latitude" );
440
unit.setLongitude( "Longitude" );
446
* @param uniqueCharacter A unique character to identify the object.
447
* @param parent The parent.
449
protected static OrganisationUnit createOrganisationUnit( char uniqueCharacter, OrganisationUnit parent )
451
OrganisationUnit unit = createOrganisationUnit( uniqueCharacter );
453
unit.setParent( parent );
459
* @param uniqueCharacter A unique character to identify the object.
461
protected static OrganisationUnitGroup createOrganisationUnitGroup( char uniqueCharacter )
463
OrganisationUnitGroup group = new OrganisationUnitGroup();
465
group.setUuid( BASE_UUID + uniqueCharacter );
466
group.setName( "OrganisationUnitGroup" + uniqueCharacter );
472
* @param uniqueCharacter A unique character to identify the object.
474
protected static OrganisationUnitGroupSet createOrganisationUnitGroupSet( char uniqueCharacter )
476
OrganisationUnitGroupSet groupSet = new OrganisationUnitGroupSet();
478
groupSet.setName( "OrganisationUnitGroupSet" + uniqueCharacter );
479
groupSet.setDescription( "Description" + uniqueCharacter );
480
groupSet.setCompulsory( true );
481
groupSet.setExclusive( true );
487
* @param uniqueCharacter A unique character to identify the object.
488
* @param startDate The start date.
489
* @param endDate The end date.
491
protected static Period createPeriod( PeriodType type, Date startDate, Date endDate )
493
Period period = new Period();
495
period.setPeriodType( type );
496
period.setStartDate( startDate );
497
period.setEndDate( endDate );
503
* @param dataElement The data element.
504
* @param period The period.
505
* @param source The source.
506
* @param value The value.
507
* @param categoryOptionCombo The data element category option combo.
509
protected static DataValue createDataValue( DataElement dataElement, Period period, Source source, String value, DataElementCategoryOptionCombo categoryOptionCombo )
511
DataValue dataValue = new DataValue();
513
dataValue.setDataElement( dataElement );
514
dataValue.setPeriod( period );
515
dataValue.setSource( source );
516
dataValue.setValue( value );
517
dataValue.setComment( "Comment" );
518
dataValue.setStoredBy( "StoredBy" );
519
dataValue.setTimestamp( date );
520
dataValue.setOptionCombo( categoryOptionCombo );
526
* @param uniqueCharacter A unique character to identify the object.
527
* @param operator The operator.
528
* @param leftSide The left side expression.
529
* @param rightSide The right side expression.
531
protected static ValidationRule createValidationRule( char uniqueCharacter, String operator, Expression leftSide, Expression rightSide )
533
ValidationRule validationRule = new ValidationRule();
535
validationRule.setName( "ValidationRule" + uniqueCharacter );
536
validationRule.setDescription( "Description" + uniqueCharacter );
537
validationRule.setType( ValidationRule.TYPE_ABSOLUTE );
538
validationRule.setOperator( operator );
539
validationRule.setLeftSide( leftSide );
540
validationRule.setRightSide( rightSide );
542
return validationRule;
546
* @param uniqueCharacter A unique character to identify the object.
549
protected static ValidationRuleGroup createValidationRuleGroup( char uniqueCharacter )
551
ValidationRuleGroup group = new ValidationRuleGroup();
553
group.setName( "ValidationRuleGroup" + uniqueCharacter );
554
group.setDescription( "Description" + uniqueCharacter );
560
* @param uniqueCharacter A unique character to identify the object.
561
* @param expressionString The expression string.
562
* @param dataElementsInExpression A collection of the data elements entering into the expression.
564
protected static Expression createExpression( char uniqueCharacter, String expressionString, Set<DataElement> dataElementsInExpression )
566
Expression expression = new Expression();
568
expression.setExpression( expressionString );
569
expression.setDescription( "Description" + uniqueCharacter );
570
expression.setDataElementsInExpression( dataElementsInExpression );
576
* @param uniqueCharacter A unique character to identify the object.
578
protected static DataElement createExtendedDataElement( char uniqueCharacter )
580
DataElement dataElement = createDataElement( uniqueCharacter );
582
ExtendedDataElement extended = createExtendedElement( uniqueCharacter );
584
dataElement.setExtended( extended );
590
* @param uniqueCharacter A unique character to identify the object.
591
* @param type The type.
593
protected static Indicator createExtendedIndicator( char uniqueCharacter, IndicatorType type )
595
Indicator indicator = createIndicator( uniqueCharacter, type );
597
ExtendedDataElement extended = createExtendedElement( uniqueCharacter );
599
indicator.setExtended( extended );
605
* @param dataElementId The data element identifier.
606
* @param categoryOptionComboId The data element category option combo identifier.
607
* @param periodId The period identifier.
608
* @param sourceId The source identifier.
609
* @param status The status.
611
protected static ImportDataValue createImportDataValue( int dataElementId, int categoryOptionComboId, int periodId, int sourceId, ImportObjectStatus status )
613
ImportDataValue importDataValue = new ImportDataValue();
615
importDataValue.setDataElementId( dataElementId );
616
importDataValue.setCategoryOptionComboId( categoryOptionComboId );
617
importDataValue.setPeriodId( periodId );
618
importDataValue.setSourceId( sourceId );
619
importDataValue.setValue( String.valueOf( 10 ) );
620
importDataValue.setStoredBy( "StoredBy" );
621
importDataValue.setTimestamp( new Date() );
622
importDataValue.setComment( "Comment" );
623
importDataValue.setStatus( status.name() );
625
return importDataValue;
628
// -------------------------------------------------------------------------
629
// Supportive methods
630
// -------------------------------------------------------------------------
632
private static ExtendedDataElement createExtendedElement( char uniqueCharacter )
634
ExtendedDataElement extended = new ExtendedDataElement();
636
extended.setMnemonic( "Mnemonic" + uniqueCharacter );
637
extended.setVersion( "Version" + uniqueCharacter );
638
extended.setContext( "Context" + uniqueCharacter );
639
extended.setSynonyms( "Synonyms" + uniqueCharacter );
640
extended.setHononyms( "Hononyms" + uniqueCharacter );
641
extended.setKeywords( "Keywords" + uniqueCharacter );
642
extended.setStatus( ExtendedDataElement.STATUS_CURRENT );
643
extended.setStatusDate( date );
644
extended.setDataElementType( ExtendedDataElement.TYPE_DATAELEMENT );
646
extended.setDataType( ExtendedDataElement.DATATYPE_ALPHABETIC );
647
extended.setRepresentationalForm( ExtendedDataElement.REPRESENTATIONAL_FORM_CODE );
648
extended.setRepresentationalLayout( "RepresentationalLayout" + uniqueCharacter );
649
extended.setMinimumSize( 0 );
650
extended.setMaximumSize( 10 );
651
extended.setDataDomain( "DataDomain" + uniqueCharacter );
652
extended.setValidationRules( "ValidationRules" + uniqueCharacter );
653
extended.setRelatedDataReferences( "RelatedDataReferences" + uniqueCharacter );
654
extended.setGuideForUse( "GuideForUse" + uniqueCharacter );
655
extended.setCollectionMethods( "CollectionMethods" + uniqueCharacter );
657
extended.setResponsibleAuthority( "ResponsibleAuthority" + uniqueCharacter );
658
extended.setUpdateRules( "UpdateRules" + uniqueCharacter );
659
extended.setAccessAuthority( "AccessAuthority" + uniqueCharacter );
660
extended.setUpdateFrequency( "UpdateFrequency" + uniqueCharacter );
661
extended.setLocation( "Location" + uniqueCharacter );
662
extended.setReportingMethods( "ReportingMethods" + uniqueCharacter );
663
extended.setVersionStatus( "VersionStatus" + uniqueCharacter );
664
extended.setPreviousVersionReferences( "PreviousVersionReferences" + uniqueCharacter );
665
extended.setSourceDocument( "SourceDocument" + uniqueCharacter );
666
extended.setSourceOrganisation( "SourceOrganisation" + uniqueCharacter );
667
extended.setComment( "Comment" + uniqueCharacter );
668
extended.setSaved( date );
669
extended.setLastUpdated( date );