~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf/DXFImportServiceTest.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.importexport.dxf;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import java.io.InputStream;
 
31
 
 
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;
 
64
 
 
65
/**
 
66
 * <p>dxfA.zip contains 3 objects of each meta-data type.</p>
 
67
 * 
 
68
 * <p>dxfB.zip contains 5 objects of each meta-data type.</p>
 
69
 * 
 
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>
 
71
 * 
 
72
 * <p>dxfD.zip contains 2 objects of type DataElement, Period, and OrganisationUnit and 8 DataValues registered for all combinations.
 
73
 * 
 
74
 * <p>dxfE.zip contains 3 DataElements, 2 Periods, and 2 OrganisationUnits and 12 DataValues registered for all combinations.
 
75
 * 
 
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.
 
77
 * 
 
78
 * @author Lars Helge Overland
 
79
 * @version $Id$
 
80
 */
 
81
public class DXFImportServiceTest
 
82
    extends DhisConvenienceTest
 
83
{
 
84
    private final int dataASize = 3;
 
85
    private final int dataBSize = 5;
 
86
    private final int dataCSize = 5;
 
87
    
 
88
    private ImportInternalProcess importService;
 
89
    
 
90
    private InputStream inputStreamA;
 
91
    
 
92
    private InputStream inputStreamB;
 
93
    
 
94
    private InputStream inputStreamC;
 
95
    
 
96
    private InputStream inputStreamD;
 
97
    
 
98
    private InputStream inputStreamE;
 
99
    
 
100
    private InputStream inputStreamF;
 
101
    
 
102
    private ImportObjectService importObjectService;
 
103
    
 
104
    private ImportDataValueService importDataValueService;
 
105
    
 
106
    // -------------------------------------------------------------------------
 
107
    // Fixture
 
108
    // -------------------------------------------------------------------------
 
109
 
 
110
    public void setUpTest()
 
111
    {
 
112
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
 
113
 
 
114
        inputStreamA = classLoader.getResourceAsStream( "dxfA.zip" );
 
115
        
 
116
        inputStreamB = classLoader.getResourceAsStream( "dxfB.zip" );
 
117
 
 
118
        inputStreamC = classLoader.getResourceAsStream( "dxfC.zip" );
 
119
        
 
120
        inputStreamD = classLoader.getResourceAsStream( "dxfD.zip" );
 
121
 
 
122
        inputStreamE = classLoader.getResourceAsStream( "dxfE.zip" );
 
123
        
 
124
        inputStreamF = classLoader.getResourceAsStream( "dxfF.zip" );
 
125
        
 
126
        importService = (ImportInternalProcess) getBean( "internal-process-DXFImportService" );
 
127
        
 
128
        categoryOptionService = (DataElementCategoryOptionService) getBean( DataElementCategoryOptionService.ID );
 
129
        
 
130
        categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID );
 
131
        
 
132
        categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
 
133
        
 
134
        categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID );
 
135
 
 
136
        dataElementService = (DataElementService) getBean( DataElementService.ID );
 
137
        
 
138
        dataDictionaryService = (DataDictionaryService) getBean( DataDictionaryService.ID );
 
139
        
 
140
        indicatorService = (IndicatorService) getBean( IndicatorService.ID );
 
141
        
 
142
        dataSetService = (DataSetService) getBean( DataSetService.ID );
 
143
        
 
144
        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
 
145
        
 
146
        organisationUnitGroupService = (OrganisationUnitGroupService) getBean( OrganisationUnitGroupService.ID );
 
147
        
 
148
        dataValueService = (DataValueService) getBean( DataValueService.ID );
 
149
        
 
150
        validationRuleService = (ValidationRuleService) getBean( ValidationRuleService.ID );
 
151
        
 
152
        expressionService = (ExpressionService) getBean( ExpressionService.ID );
 
153
        
 
154
        importObjectService = (ImportObjectService) getBean( ImportObjectService.ID );
 
155
        
 
156
        importDataValueService = (ImportDataValueService) getBean( ImportDataValueService.ID );
 
157
    }
 
158
    
 
159
    public void tearDownTest()
 
160
        throws Exception
 
161
    {
 
162
        inputStreamA.close();
 
163
        
 
164
        inputStreamB.close();
 
165
        
 
166
        inputStreamC.close();
 
167
        
 
168
        inputStreamD.close();
 
169
        
 
170
        inputStreamE.close();
 
171
        
 
172
        inputStreamF.close();
 
173
    }
 
174
    
 
175
    // -------------------------------------------------------------------------
 
176
    // Tests
 
177
    // -------------------------------------------------------------------------
 
178
    
 
179
    // TODO Improve test on duplicate GroupMemberAssociations
 
180
    
 
181
    public void testImportMetaData()
 
182
    {        
 
183
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, false );
 
184
        
 
185
        importService.importData( importParams, inputStreamA );
 
186
        
 
187
        assertObjects( dataASize );
 
188
    }
 
189
    
 
190
    public void testImportMetaDataWithPreview()
 
191
    {
 
192
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, false, false );
 
193
        
 
194
        importService.importData( importParams, inputStreamA );
 
195
        
 
196
        assertImportObjects( ImportObjectStatus.NEW, dataASize );
 
197
        
 
198
        assertGroupMembers( dataASize );
 
199
    }
 
200
    
 
201
    public void testImportMetaDataWithPreviewAndDuplicates()
 
202
    {
 
203
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, false );
 
204
        
 
205
        importService.importData( importParams, inputStreamA );
 
206
        
 
207
        assertObjects( dataASize );
 
208
        
 
209
        importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, false, false );
 
210
        
 
211
        importService.importData( importParams, inputStreamB );
 
212
        
 
213
        assertImportObjects( ImportObjectStatus.NEW, dataBSize - dataASize );
 
214
        
 
215
        assertImportObjects( ImportObjectStatus.UPDATE, 0 );
 
216
        
 
217
        assertGroupMembers( dataBSize );
 
218
    }
 
219
    
 
220
    public void testImportMetaDataWithPreviewAndUpdates()
 
221
    {
 
222
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, false, false );
 
223
        
 
224
        importService.importData( importParams, inputStreamA );
 
225
        
 
226
        assertObjects( dataASize );
 
227
 
 
228
        importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, false, false );
 
229
        
 
230
        importService.importData( importParams, inputStreamC );
 
231
        
 
232
        assertUpdateableImportObjects( ImportObjectStatus.UPDATE, dataASize );
 
233
        
 
234
        assertImportObjects( ImportObjectStatus.NEW, dataCSize - dataASize );
 
235
        
 
236
        assertGroupMembers( dataCSize );
 
237
    }
 
238
    
 
239
    public void testImportDataValues()
 
240
    {
 
241
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, true, false );
 
242
        
 
243
        importService.importData( importParams, inputStreamD );
 
244
        
 
245
        assertEquals( dataElementService.getAllDataElements().size(), 2 );
 
246
        
 
247
        assertEquals( organisationUnitService.getAllOrganisationUnits().size(), 2 );
 
248
        
 
249
        assertEquals( dataValueService.getAllDataValues().size(), 8 );
 
250
    }
 
251
    
 
252
    public void testImportDataValuesWithPreview()
 
253
    {
 
254
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, true, false );
 
255
        
 
256
        importService.importData( importParams, inputStreamD );
 
257
        
 
258
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class ).size(), 2 );
 
259
        
 
260
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, OrganisationUnit.class ).size(), 2 );
 
261
        
 
262
        assertEquals( importDataValueService.getImportDataValues( ImportObjectStatus.NEW ).size(), 8 );
 
263
    }
 
264
 
 
265
    public void testImportDataValuesWithPreviewAndDuplicates()
 
266
    {
 
267
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, true, false );
 
268
        
 
269
        importService.importData( importParams, inputStreamD );
 
270
        
 
271
        assertEquals( dataElementService.getAllDataElements().size(), 2 );
 
272
        
 
273
        assertEquals( organisationUnitService.getAllOrganisationUnits().size(), 2 );
 
274
        
 
275
        assertEquals( dataValueService.getAllDataValues().size(), 8 );
 
276
        
 
277
        importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, true, false );
 
278
        
 
279
        importService.importData( importParams, inputStreamE );
 
280
        
 
281
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class ).size(), 1 );
 
282
        
 
283
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, OrganisationUnit.class ).size(), 0 );
 
284
        
 
285
        assertEquals( importDataValueService.getImportDataValues( ImportObjectStatus.NEW ).size(), 12 );        
 
286
    }
 
287
    
 
288
    public void testImportDataValuesWithPreviewAndUpdates()
 
289
    {
 
290
        ImportParams importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, false, true, false );
 
291
 
 
292
        importService.importData( importParams, inputStreamD );
 
293
 
 
294
        assertEquals( dataElementService.getAllDataElements().size(), 2 );
 
295
        
 
296
        assertEquals( organisationUnitService.getAllOrganisationUnits().size(), 2 );
 
297
        
 
298
        assertEquals( dataValueService.getAllDataValues().size(), 8 );
 
299
 
 
300
        importParams = ImportExportUtils.getImportParams( ImportStrategy.NEW_AND_UPDATES, true, true, false );
 
301
 
 
302
        importService.importData( importParams, inputStreamF );
 
303
 
 
304
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class ).size(), 1 );
 
305
        
 
306
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.NEW, OrganisationUnit.class ).size(), 0 );
 
307
        
 
308
        assertEquals( importObjectService.getImportObjects( ImportObjectStatus.UPDATE, DataElement.class ).size(), 2 );
 
309
        
 
310
        assertEquals( importDataValueService.getImportDataValues( ImportObjectStatus.NEW ).size(), 12 );        
 
311
    }
 
312
    
 
313
    // -------------------------------------------------------------------------
 
314
    // Supportive methods
 
315
    // -------------------------------------------------------------------------
 
316
    
 
317
    private void assertObjects( int expectedSize )
 
318
    {
 
319
        assertEquals( categoryOptionService.getAllDataElementCategoryOptions().size(), 5 ); // Including default
 
320
        
 
321
        assertEquals( categoryService.getAllDataElementCategories().size(), 3 ); // Including default
 
322
        
 
323
        assertEquals( categoryComboService.getAllDataElementCategoryCombos().size(), 3 ); // Including default
 
324
        
 
325
        assertEquals( categoryOptionComboService.getAllDataElementCategoryOptionCombos().size(), 5 ); // Including default
 
326
        
 
327
        assertEquals( dataElementService.getAllDataElements().size(), expectedSize );
 
328
        
 
329
        assertEquals( dataElementService.getAllDataElementGroups().size(), expectedSize );
 
330
        
 
331
        for ( DataElementGroup group : dataElementService.getAllDataElementGroups() )
 
332
        {
 
333
            assertEquals( group.getMembers().size(), expectedSize );
 
334
        }
 
335
        
 
336
        assertEquals( indicatorService.getAllIndicatorTypes().size(), expectedSize );
 
337
        
 
338
        assertEquals( indicatorService.getAllIndicators().size(), expectedSize );
 
339
        
 
340
        assertEquals( indicatorService.getAllIndicatorGroups().size(), expectedSize );
 
341
        
 
342
        for ( IndicatorGroup group : indicatorService.getAllIndicatorGroups() )
 
343
        {
 
344
            assertEquals( group.getMembers().size(), expectedSize );
 
345
        }
 
346
        
 
347
        assertEquals( dataSetService.getAllDataSets().size(), expectedSize );
 
348
        
 
349
        for ( DataSet dataSet : dataSetService.getAllDataSets() )
 
350
        {
 
351
            assertEquals( dataSet.getDataElements().size(), expectedSize );
 
352
        }
 
353
        
 
354
        assertEquals( organisationUnitService.getAllOrganisationUnits().size(), expectedSize );
 
355
        
 
356
        assertEquals( organisationUnitGroupService.getAllOrganisationUnitGroups().size(), expectedSize );
 
357
        
 
358
        for ( OrganisationUnitGroup group : organisationUnitGroupService.getAllOrganisationUnitGroups() )
 
359
        {
 
360
            assertEquals( group.getMembers().size(), expectedSize );
 
361
        }
 
362
        
 
363
        assertEquals( organisationUnitGroupService.getAllOrganisationUnitGroupSets().size(), expectedSize );
 
364
        
 
365
        for ( OrganisationUnitGroupSet groupSet : organisationUnitGroupService.getAllOrganisationUnitGroupSets() )
 
366
        {
 
367
            assertEquals( groupSet.getOrganisationUnitGroups().size(), expectedSize );
 
368
        }
 
369
        
 
370
        assertEquals( validationRuleService.getAllValidationRules().size(), expectedSize );
 
371
    }
 
372
 
 
373
    private void assertImportObjects( ImportObjectStatus status, int expectedSize )
 
374
    {
 
375
        assertUpdateableImportObjects( status, expectedSize );
 
376
        
 
377
        assertEquals( importObjectService.getImportObjects( status, DataElementGroup.class ).size(), expectedSize );
 
378
        
 
379
        assertEquals( importObjectService.getImportObjects( status, IndicatorType.class ).size(), expectedSize );
 
380
 
 
381
        assertEquals( importObjectService.getImportObjects( status, IndicatorGroup.class ).size(), expectedSize );
 
382
        
 
383
        assertEquals( importObjectService.getImportObjects( status, DataSet.class ).size(), expectedSize );
 
384
                
 
385
        assertEquals( importObjectService.getImportObjects( status, OrganisationUnitGroup.class ).size(), expectedSize );
 
386
 
 
387
        assertEquals( importObjectService.getImportObjects( status, OrganisationUnitGroupSet.class ).size(), expectedSize );
 
388
 
 
389
        assertEquals( importObjectService.getImportObjects( status, ValidationRule.class  ).size(), expectedSize );        
 
390
    }
 
391
    
 
392
    private void assertUpdateableImportObjects( ImportObjectStatus status, int expectedSize )
 
393
    {
 
394
        assertEquals( importObjectService.getImportObjects( status, DataElement.class ).size(), expectedSize );
 
395
        
 
396
        assertEquals( importObjectService.getImportObjects( status, Indicator.class ).size(), expectedSize );
 
397
        
 
398
        assertEquals( importObjectService.getImportObjects( status, OrganisationUnit.class ).size(), expectedSize );
 
399
    }
 
400
    
 
401
    private void assertGroupMembers( int expectedSize )
 
402
    {
 
403
        assertEquals( importObjectService.getImportObjects( GroupMemberType.DATAELEMENTGROUP ).size(), expectedSize * expectedSize );
 
404
        
 
405
        assertEquals( importObjectService.getImportObjects( GroupMemberType.INDICATORGROUP ).size(), expectedSize * expectedSize );
 
406
        
 
407
        assertEquals( importObjectService.getImportObjects( GroupMemberType.DATASET ).size(), expectedSize * expectedSize );
 
408
        
 
409
        assertEquals( importObjectService.getImportObjects( GroupMemberType.ORGANISATIONUNITGROUP ).size(), expectedSize * expectedSize );
 
410
        
 
411
        assertEquals( importObjectService.getImportObjects( GroupMemberType.ORGANISATIONUNITGROUPSET ).size(), expectedSize * expectedSize );
 
412
    }
 
413
}