1
package org.hisp.dhis.importexport.dxf;
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.io.InputStream;
32
import org.hisp.dhis.DhisConvenienceTest;
33
import org.hisp.dhis.datadictionary.DataDictionaryService;
34
import org.hisp.dhis.dataelement.DataElement;
35
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
36
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
37
import org.hisp.dhis.dataelement.DataElementCategoryOptionService;
38
import org.hisp.dhis.dataelement.DataElementCategoryService;
39
import org.hisp.dhis.dataelement.DataElementGroup;
40
import org.hisp.dhis.dataelement.DataElementService;
41
import org.hisp.dhis.dataset.DataSet;
42
import org.hisp.dhis.dataset.DataSetService;
43
import org.hisp.dhis.datavalue.DataValueService;
44
import org.hisp.dhis.expression.ExpressionService;
45
import org.hisp.dhis.importexport.GroupMemberType;
46
import org.hisp.dhis.importexport.ImportDataValueService;
47
import org.hisp.dhis.importexport.ImportInternalProcess;
48
import org.hisp.dhis.importexport.ImportObjectService;
49
import org.hisp.dhis.importexport.ImportObjectStatus;
50
import org.hisp.dhis.importexport.ImportParams;
51
import org.hisp.dhis.importexport.ImportStrategy;
52
import org.hisp.dhis.importexport.util.ImportExportUtils;
53
import org.hisp.dhis.indicator.Indicator;
54
import org.hisp.dhis.indicator.IndicatorGroup;
55
import org.hisp.dhis.indicator.IndicatorService;
56
import org.hisp.dhis.indicator.IndicatorType;
57
import org.hisp.dhis.organisationunit.OrganisationUnit;
58
import org.hisp.dhis.organisationunit.OrganisationUnitGroup;
59
import org.hisp.dhis.organisationunit.OrganisationUnitGroupService;
60
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
61
import org.hisp.dhis.organisationunit.OrganisationUnitService;
62
import org.hisp.dhis.validation.ValidationRule;
63
import org.hisp.dhis.validation.ValidationRuleService;
66
* <p>dxfA.zip contains 3 objects of each meta-data type.</p>
68
* <p>dxfB.zip contains 5 objects of each meta-data type.</p>
70
* <p>dxfC.zip contains 5 objects of each meta-data type with variations in properties in 3 of them, except for one-property objects.</p>
72
* <p>dxfD.zip contains 2 objects of type DataElement, Period, and OrganisationUnit and 8 DataValues registered for all combinations.
74
* <p>dxfE.zip contains 3 DataElements, 2 Periods, and 2 OrganisationUnits and 12 DataValues registered for all combinations.
76
* <p>dxfF.zip contains 3 DataElements with variations in properties in 2 of them, 2 Periods, and 2 OrganisationUnits and 12 DataValues registered for all combinations.
78
* @author Lars Helge Overland
81
public class DXFImportServiceTest
82
extends DhisConvenienceTest
84
private final int dataASize = 3;
85
private final int dataBSize = 5;
86
private final int dataCSize = 5;
88
private ImportInternalProcess importService;
90
private InputStream inputStreamA;
92
private InputStream inputStreamB;
94
private InputStream inputStreamC;
96
private InputStream inputStreamD;
98
private InputStream inputStreamE;
100
private InputStream inputStreamF;
102
private ImportObjectService importObjectService;
104
private ImportDataValueService importDataValueService;
106
// -------------------------------------------------------------------------
108
// -------------------------------------------------------------------------
110
public void setUpTest()
112
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
114
inputStreamA = classLoader.getResourceAsStream( "dxfA.zip" );
116
inputStreamB = classLoader.getResourceAsStream( "dxfB.zip" );
118
inputStreamC = classLoader.getResourceAsStream( "dxfC.zip" );
120
inputStreamD = classLoader.getResourceAsStream( "dxfD.zip" );
122
inputStreamE = classLoader.getResourceAsStream( "dxfE.zip" );
124
inputStreamF = classLoader.getResourceAsStream( "dxfF.zip" );
126
importService = (ImportInternalProcess) getBean( "internal-process-DXFImportService" );
128
categoryOptionService = (DataElementCategoryOptionService) getBean( DataElementCategoryOptionService.ID );
130
categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID );
132
categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
134
categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID );
136
dataElementService = (DataElementService) getBean( DataElementService.ID );
138
dataDictionaryService = (DataDictionaryService) getBean( DataDictionaryService.ID );
140
indicatorService = (IndicatorService) getBean( IndicatorService.ID );
142
dataSetService = (DataSetService) getBean( DataSetService.ID );
144
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
146
organisationUnitGroupService = (OrganisationUnitGroupService) getBean( OrganisationUnitGroupService.ID );
148
dataValueService = (DataValueService) getBean( DataValueService.ID );
150
validationRuleService = (ValidationRuleService) getBean( ValidationRuleService.ID );
152
expressionService = (ExpressionService) getBean( ExpressionService.ID );
154
importObjectService = (ImportObjectService) getBean( ImportObjectService.ID );
156
importDataValueService = (ImportDataValueService) getBean( ImportDataValueService.ID );
159
public void tearDownTest()
162
inputStreamA.close();
164
inputStreamB.close();
166
inputStreamC.close();
168
inputStreamD.close();
170
inputStreamE.close();
172
inputStreamF.close();
175
// -------------------------------------------------------------------------
177
// -------------------------------------------------------------------------
179
// TODO Improve test on duplicate GroupMemberAssociations
181
public void testImportMetaData()
183
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, false );
185
importService.importData( importParams, inputStreamA );
187
assertObjects( dataASize );
190
public void testImportMetaDataWithPreview()
192
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, false, false );
194
importService.importData( importParams, inputStreamA );
196
assertImportObjects( ImportObjectStatus.NEW, dataASize );
198
assertGroupMembers( dataASize );
201
public void testImportMetaDataWithPreviewAndDuplicates()
203
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, false );
205
importService.importData( importParams, inputStreamA );
207
assertObjects( dataASize );
209
importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, false, false );
211
importService.importData( importParams, inputStreamB );
213
assertImportObjects( ImportObjectStatus.NEW, dataBSize - dataASize );
215
assertImportObjects( ImportObjectStatus.UPDATE, 0 );
217
assertGroupMembers( dataBSize );
220
public void testImportMetaDataWithPreviewAndUpdates()
222
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, false );
224
importService.importData( importParams, inputStreamA );
226
assertObjects( dataASize );
228
importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, false, false );
230
importService.importData( importParams, inputStreamC );
232
assertUpdateableImportObjects( ImportObjectStatus.UPDATE, dataASize );
234
assertImportObjects( ImportObjectStatus.NEW, dataCSize - dataASize );
236
assertGroupMembers( dataCSize );
239
public void testImportDataValues()
241
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, true, false );
243
importService.importData( importParams, inputStreamD );
245
assertEquals( dataElementService.getAllDataElements().size(), 2 );
247
assertEquals( organisationUnitService.getAllOrganisationUnits().size(), 2 );
249
assertEquals( dataValueService.getAllDataValues().size(), 8 );
252
public void testImportDataValuesWithPreview()
254
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, true, false );
256
importService.importData( importParams, inputStreamD );
258
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class ).size(), 2 );
260
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, OrganisationUnit.class ).size(), 2 );
262
assertEquals( importDataValueService.getImportDataValues( ImportObjectStatus.NEW ).size(), 8 );
265
public void testImportDataValuesWithPreviewAndDuplicates()
267
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, true, false );
269
importService.importData( importParams, inputStreamD );
271
assertEquals( dataElementService.getAllDataElements().size(), 2 );
273
assertEquals( organisationUnitService.getAllOrganisationUnits().size(), 2 );
275
assertEquals( dataValueService.getAllDataValues().size(), 8 );
277
importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, true, false );
279
importService.importData( importParams, inputStreamE );
281
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class ).size(), 1 );
283
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, OrganisationUnit.class ).size(), 0 );
285
assertEquals( importDataValueService.getImportDataValues( ImportObjectStatus.NEW ).size(), 12 );
288
public void testImportDataValuesWithPreviewAndUpdates()
290
ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, true, false );
292
importService.importData( importParams, inputStreamD );
294
assertEquals( dataElementService.getAllDataElements().size(), 2 );
296
assertEquals( organisationUnitService.getAllOrganisationUnits().size(), 2 );
298
assertEquals( dataValueService.getAllDataValues().size(), 8 );
300
importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, true, false );
302
importService.importData( importParams, inputStreamF );
304
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class ).size(), 1 );
306
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, OrganisationUnit.class ).size(), 0 );
308
assertEquals( importObjectService.getImportObjects( ImportObjectStatus.UPDATE, DataElement.class ).size(), 2 );
310
assertEquals( importDataValueService.getImportDataValues( ImportObjectStatus.NEW ).size(), 12 );
313
// -------------------------------------------------------------------------
314
// Supportive methods
315
// -------------------------------------------------------------------------
317
private void assertObjects( int expectedSize )
319
assertEquals( categoryOptionService.getAllDataElementCategoryOptions().size(), 5 ); // Including default
321
assertEquals( categoryService.getAllDataElementCategories().size(), 3 ); // Including default
323
assertEquals( categoryComboService.getAllDataElementCategoryCombos().size(), 3 ); // Including default
325
assertEquals( categoryOptionComboService.getAllDataElementCategoryOptionCombos().size(), 5 ); // Including default
327
assertEquals( dataElementService.getAllDataElements().size(), expectedSize );
329
assertEquals( dataElementService.getAllDataElementGroups().size(), expectedSize );
331
for ( DataElementGroup group : dataElementService.getAllDataElementGroups() )
333
assertEquals( group.getMembers().size(), expectedSize );
336
assertEquals( indicatorService.getAllIndicatorTypes().size(), expectedSize );
338
assertEquals( indicatorService.getAllIndicators().size(), expectedSize );
340
assertEquals( indicatorService.getAllIndicatorGroups().size(), expectedSize );
342
for ( IndicatorGroup group : indicatorService.getAllIndicatorGroups() )
344
assertEquals( group.getMembers().size(), expectedSize );
347
assertEquals( dataSetService.getAllDataSets().size(), expectedSize );
349
for ( DataSet dataSet : dataSetService.getAllDataSets() )
351
assertEquals( dataSet.getDataElements().size(), expectedSize );
354
assertEquals( organisationUnitService.getAllOrganisationUnits().size(), expectedSize );
356
assertEquals( organisationUnitGroupService.getAllOrganisationUnitGroups().size(), expectedSize );
358
for ( OrganisationUnitGroup group : organisationUnitGroupService.getAllOrganisationUnitGroups() )
360
assertEquals( group.getMembers().size(), expectedSize );
363
assertEquals( organisationUnitGroupService.getAllOrganisationUnitGroupSets().size(), expectedSize );
365
for ( OrganisationUnitGroupSet groupSet : organisationUnitGroupService.getAllOrganisationUnitGroupSets() )
367
assertEquals( groupSet.getOrganisationUnitGroups().size(), expectedSize );
370
assertEquals( validationRuleService.getAllValidationRules().size(), expectedSize );
373
private void assertImportObjects( ImportObjectStatus status, int expectedSize )
375
assertUpdateableImportObjects( status, expectedSize );
377
assertEquals( importObjectService.getImportObjects( status, DataElementGroup.class ).size(), expectedSize );
379
assertEquals( importObjectService.getImportObjects( status, IndicatorType.class ).size(), expectedSize );
381
assertEquals( importObjectService.getImportObjects( status, IndicatorGroup.class ).size(), expectedSize );
383
assertEquals( importObjectService.getImportObjects( status, DataSet.class ).size(), expectedSize );
385
assertEquals( importObjectService.getImportObjects( status, OrganisationUnitGroup.class ).size(), expectedSize );
387
assertEquals( importObjectService.getImportObjects( status, OrganisationUnitGroupSet.class ).size(), expectedSize );
389
assertEquals( importObjectService.getImportObjects( status, ValidationRule.class ).size(), expectedSize );
392
private void assertUpdateableImportObjects( ImportObjectStatus status, int expectedSize )
394
assertEquals( importObjectService.getImportObjects( status, DataElement.class ).size(), expectedSize );
396
assertEquals( importObjectService.getImportObjects( status, Indicator.class ).size(), expectedSize );
398
assertEquals( importObjectService.getImportObjects( status, OrganisationUnit.class ).size(), expectedSize );
401
private void assertGroupMembers( int expectedSize )
403
assertEquals( importObjectService.getImportObjects( GroupMemberType.DATAELEMENTGROUP ).size(), expectedSize * expectedSize );
405
assertEquals( importObjectService.getImportObjects( GroupMemberType.INDICATORGROUP ).size(), expectedSize * expectedSize );
407
assertEquals( importObjectService.getImportObjects( GroupMemberType.DATASET ).size(), expectedSize * expectedSize );
409
assertEquals( importObjectService.getImportObjects( GroupMemberType.ORGANISATIONUNITGROUP ).size(), expectedSize * expectedSize );
411
assertEquals( importObjectService.getImportObjects( GroupMemberType.ORGANISATIONUNITGROUPSET ).size(), expectedSize * expectedSize );