1
package org.hisp.dhis.importexport.importobject;
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.Collection;
32
import org.hisp.dhis.DhisConvenienceTest;
33
import org.hisp.dhis.importexport.GroupMemberAssociation;
34
import org.hisp.dhis.dataelement.DataElement;
35
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
36
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
37
import org.hisp.dhis.dataelement.DataElementGroup;
38
import org.hisp.dhis.dataelement.DataElementService;
39
import org.hisp.dhis.dataset.DataSet;
40
import org.hisp.dhis.importexport.GroupMemberType;
41
import org.hisp.dhis.importexport.ImportObject;
42
import org.hisp.dhis.importexport.ImportObjectService;
43
import org.hisp.dhis.importexport.ImportObjectStatus;
44
import org.hisp.dhis.indicator.Indicator;
45
import org.hisp.dhis.indicator.IndicatorGroup;
46
import org.hisp.dhis.indicator.IndicatorService;
47
import org.hisp.dhis.indicator.IndicatorType;
48
import org.hisp.dhis.period.MonthlyPeriodType;
50
import static org.hisp.dhis.expression.Expression.SEPARATOR;
53
* @author Lars Helge Overland
56
public class ImportObjectServiceTest
57
extends DhisConvenienceTest
59
private ImportObjectService importObjectService;
61
private DataElement dataElementA;
62
private DataElement dataElementB;
63
private DataElement dataElementC;
65
private DataElementCategoryCombo categoryComboA;
67
private DataElementGroup dataElementGroupA;
68
private DataElementGroup dataElementGroupB;
69
private DataElementGroup dataElementGroupC;
71
private GroupMemberAssociation associationA;
72
private GroupMemberAssociation associationB;
73
private GroupMemberAssociation associationC;
74
private GroupMemberAssociation associationD;
75
private GroupMemberAssociation associationE;
76
private GroupMemberAssociation associationF;
78
private DataSet dataSetA;
80
private GroupMemberAssociation associationG;
81
private GroupMemberAssociation associationH;
82
private GroupMemberAssociation associationI;
84
private IndicatorType indicatorTypeA;
86
private Indicator indicatorA;
87
private Indicator indicatorB;
88
private Indicator indicatorC;
90
private IndicatorGroup indicatorGroupA;
91
private IndicatorGroup indicatorGroupB;
103
// -------------------------------------------------------------------------
105
// -------------------------------------------------------------------------
107
public void setUpTest()
109
importObjectService = (ImportObjectService) getBean( ImportObjectService.ID );
111
categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
113
dataElementService = (DataElementService) getBean( DataElementService.ID );
115
indicatorService = (IndicatorService) getBean( IndicatorService.ID );
117
categoryComboA = new DataElementCategoryCombo( "CategoryComboA" );
119
categoryComboA.setId( 'A' );
121
dataElementA = createDataElement( 'A', categoryComboA );
122
dataElementB = createDataElement( 'B', categoryComboA );
123
dataElementC = createDataElement( 'C', categoryComboA );
125
dataElementA.setId( 'A' );
126
dataElementB.setId( 'B' );
127
dataElementC.setId( 'C' );
129
dataElementGroupA = createDataElementGroup( 'A' );
130
dataElementGroupB = createDataElementGroup( 'B' );
131
dataElementGroupC = createDataElementGroup( 'C' );
133
dataElementGroupA.setId( 'A' );
134
dataElementGroupB.setId( 'B' );
135
dataElementGroupB.setId( 'C' );
137
associationA = new GroupMemberAssociation( 'A', 'A' );
138
associationB = new GroupMemberAssociation( 'A', 'B' );
139
associationC = new GroupMemberAssociation( 'A', 'C' );
140
associationD = new GroupMemberAssociation( 'B', 'A' );
141
associationE = new GroupMemberAssociation( 'B', 'B' );
142
associationF = new GroupMemberAssociation( 'B', 'C' );
144
dataSetA = createDataSet( 'A', new MonthlyPeriodType() );
146
dataSetA.setId( 'A' );
148
associationG = new GroupMemberAssociation( 'A', 'A' );
149
associationH = new GroupMemberAssociation( 'A', 'B' );
150
associationI = new GroupMemberAssociation( 'A', 'C' );
152
indicatorTypeA = createIndicatorType( 'A' );
154
indicatorTypeA.setId( 'A' );
156
indicatorA = createIndicator( 'A', indicatorTypeA );
157
indicatorB = createIndicator( 'B', indicatorTypeA );
158
indicatorC = createIndicator( 'C', indicatorTypeA );
160
indicatorA.setId( 'A' );
161
indicatorB.setId( 'B' );
162
indicatorC.setId( 'C' );
164
indicatorGroupA = createIndicatorGroup( 'A' );
165
indicatorGroupB = createIndicatorGroup( 'B' );
167
indicatorGroupA.setId( 'A' );
168
indicatorGroupB.setId( 'B' );
170
idA = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, dataElementA );
171
idB = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, dataElementB );
172
idC = importObjectService.addImportObject( ImportObjectStatus.UPDATE, GroupMemberType.NONE, dataElementC );
173
idD = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, dataElementGroupA );
174
idE = importObjectService.addImportObject( ImportObjectStatus.UPDATE, GroupMemberType.NONE, dataElementGroupB );
175
idF = importObjectService.addImportObject( ImportObjectStatus.UPDATE, GroupMemberType.NONE, dataElementGroupC );
176
idG = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationA );
177
idH = importObjectService.addImportObject( ImportObjectStatus.MATCH, GroupMemberType.DATAELEMENTGROUP, associationB );
178
idI = importObjectService.addImportObject( ImportObjectStatus.MATCH, GroupMemberType.DATAELEMENTGROUP, associationC );
181
// -------------------------------------------------------------------------
182
// Supportive methods
183
// -------------------------------------------------------------------------
185
private void assertEquals( ImportObject importObject, ImportObjectStatus status, Class<?> clazz, GroupMemberType type, Object object, Object compareObject )
187
assertEquals( importObject.getStatus(), status );
188
assertEquals( importObject.getClassName(), clazz.getName() );
189
assertEquals( importObject.getGroupMemberType(), type );
190
assertEquals( importObject.getObject(), object );
191
assertEquals( importObject.getCompareObject(), compareObject );
194
private void assertNulls( Integer... identifiers )
196
for ( Integer id : identifiers )
198
assertNull( String.valueOf( id ), importObjectService.getImportObject( id ) );
202
private void assertNotNulls( Integer... identifiers )
204
for ( Integer id : identifiers )
206
assertNotNull( String.valueOf( id ), importObjectService.getImportObject( id ) );
210
// -------------------------------------------------------------------------
212
// -------------------------------------------------------------------------
214
public void testAddGetImportObject()
216
assertEquals( importObjectService.getImportObject( idA ), ImportObjectStatus.NEW, DataElement.class, GroupMemberType.NONE, dataElementA, null );
217
assertEquals( importObjectService.getImportObject( idC ), ImportObjectStatus.UPDATE, DataElement.class, GroupMemberType.NONE, dataElementC, null );
218
assertEquals( importObjectService.getImportObject( idH ), ImportObjectStatus.MATCH, GroupMemberAssociation.class, GroupMemberType.DATAELEMENTGROUP, associationB, null );
221
public void testAddGetImportObjectWithCompareObject()
223
int importObjectIdA = importObjectService.addImportObject( ImportObjectStatus.NEW, dataElementA, dataElementB );
224
int importObjectIdB = importObjectService.addImportObject( ImportObjectStatus.UPDATE, dataElementB, dataElementC );
225
int importObjectIdC = importObjectService.addImportObject( ImportObjectStatus.MATCH, dataElementC, dataElementA );
227
assertEquals( importObjectService.getImportObject( importObjectIdA ), ImportObjectStatus.NEW, DataElement.class, GroupMemberType.NONE, dataElementA, dataElementB );
228
assertEquals( importObjectService.getImportObject( importObjectIdB ), ImportObjectStatus.UPDATE, DataElement.class, GroupMemberType.NONE, dataElementB, dataElementC );
229
assertEquals( importObjectService.getImportObject( importObjectIdC ), ImportObjectStatus.MATCH, DataElement.class, GroupMemberType.NONE, dataElementC, dataElementA );
232
public void testGetImportObjectsByClass()
234
Collection<ImportObject> importObjects = importObjectService.getImportObjects( DataElement.class );
236
assertEquals( importObjects.size(), 3 );
238
assertTrue( importObjects.contains( importObjectService.getImportObject( idA ) ) );
239
assertTrue( importObjects.contains( importObjectService.getImportObject( idB ) ) );
240
assertTrue( importObjects.contains( importObjectService.getImportObject( idC ) ) );
242
importObjects = importObjectService.getImportObjects( GroupMemberAssociation.class );
244
assertEquals( importObjects.size(), 3 );
246
assertTrue( importObjects.contains( importObjectService.getImportObject( idG ) ) );
247
assertTrue( importObjects.contains( importObjectService.getImportObject( idH ) ) );
248
assertTrue( importObjects.contains( importObjectService.getImportObject( idI ) ) );
251
public void testGetImportObjectsByStatusClass()
253
Collection<ImportObject> importObjects = importObjectService.getImportObjects( ImportObjectStatus.NEW, DataElement.class );
255
assertEquals( importObjects.size(), 2 );
257
assertTrue( importObjects.contains( importObjectService.getImportObject( idA ) ) );
258
assertTrue( importObjects.contains( importObjectService.getImportObject( idB ) ) );
260
importObjects = importObjectService.getImportObjects( ImportObjectStatus.MATCH, GroupMemberAssociation.class );
262
assertEquals( importObjects.size(), 2 );
264
assertTrue( importObjects.contains( importObjectService.getImportObject( idH ) ) );
265
assertTrue( importObjects.contains( importObjectService.getImportObject( idI ) ) );
268
public void testGetImportObjectsByGroupMemberType()
270
Collection<ImportObject> importObjects = importObjectService.getImportObjects( GroupMemberType.NONE );
272
assertEquals( importObjects.size(), 6 );
274
assertTrue( importObjects.contains( importObjectService.getImportObject( idA ) ) );
275
assertTrue( importObjects.contains( importObjectService.getImportObject( idB ) ) );
276
assertTrue( importObjects.contains( importObjectService.getImportObject( idC ) ) );
277
assertTrue( importObjects.contains( importObjectService.getImportObject( idD ) ) );
278
assertTrue( importObjects.contains( importObjectService.getImportObject( idE ) ) );
279
assertTrue( importObjects.contains( importObjectService.getImportObject( idF ) ) );
281
importObjects = importObjectService.getImportObjects( GroupMemberType.DATAELEMENTGROUP );
283
assertEquals( importObjects.size(), 3 );
285
assertTrue( importObjects.contains( importObjectService.getImportObject( idG ) ) );
286
assertTrue( importObjects.contains( importObjectService.getImportObject( idH ) ) );
287
assertTrue( importObjects.contains( importObjectService.getImportObject( idI ) ) );
290
public void testDeleteImportObject()
292
assertNotNulls( idA, idB, idC );
294
importObjectService.deleteImportObject( idA );
297
assertNotNulls( idB, idC );
299
importObjectService.deleteImportObject( idB );
301
assertNulls( idA, idB );
302
assertNotNulls( idC );
304
importObjectService.deleteImportObject( idC );
306
assertNulls( idA, idB, idC );
309
public void testDeleteImportObjectsByClass()
311
assertNotNulls( idA, idB, idC, idD, idE, idF, idG, idH, idI );
313
importObjectService.deleteImportObjects( DataElement.class );
315
assertNotNulls( idD, idE, idF, idG, idH, idI );
316
assertNulls( idA, idB, idC );
318
importObjectService.deleteImportObjects( DataElementGroup.class );
320
assertNotNulls( idG, idH, idI );
321
assertNulls( idA, idB, idC, idD, idE, idF );
324
public void testCascadeDeleteImportObject()
326
int idJ = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationD );
327
int idK = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationE );
328
int idL = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationF );
330
int idM = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, dataSetA );
332
int idN = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATASET, associationG );
333
int idO = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATASET, associationH );
334
int idP = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATASET, associationI );
336
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 3 );
337
assertEquals( importObjectService.getImportObjects( DataElementGroup.class ).size(), 3 );
338
assertEquals( importObjectService.getImportObjects( DataSet.class ).size(), 1 );
339
assertEquals( importObjectService.getImportObjects( GroupMemberAssociation.class ).size(), 9 );
341
assertNotNulls( idJ, idK, idL, idM, idN, idO, idP );
343
importObjectService.cascadeDeleteImportObject( idA );
345
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 2 );
346
assertEquals( importObjectService.getImportObjects( DataElementGroup.class ).size(), 3 );
347
assertEquals( importObjectService.getImportObjects( DataSet.class ).size(), 1 );
348
assertEquals( importObjectService.getImportObjects( GroupMemberAssociation.class ).size(), 6 );
350
assertNulls( idA, idG, idJ, idN );
352
importObjectService.cascadeDeleteImportObject( idD );
354
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 2 );
355
assertEquals( importObjectService.getImportObjects( DataElementGroup.class ).size(), 2 );
356
assertEquals( importObjectService.getImportObjects( DataSet.class ).size(), 1 );
357
assertEquals( importObjectService.getImportObjects( GroupMemberAssociation.class ).size(), 4 );
359
assertNulls( idD, idH, idI );
361
importObjectService.cascadeDeleteImportObject( idM );
363
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 2 );
364
assertEquals( importObjectService.getImportObjects( DataElementGroup.class ).size(), 2 );
365
assertEquals( importObjectService.getImportObjects( DataSet.class ).size(), 0 );
366
assertEquals( importObjectService.getImportObjects( GroupMemberAssociation.class ).size(), 2 );
368
assertNulls( idM, idO, idP );
371
public void testCascadeDeleteIndicatorsContainingDataElement()
373
String suffix = SEPARATOR + categoryComboA.getId();
375
indicatorA.setNumerator( "[" + Integer.valueOf( 'A' ) + suffix + "]+[" + Integer.valueOf( 'B' ) + suffix + "]-[" + Integer.valueOf( 'C' ) + suffix + "]" );
376
indicatorA.setDenominator( "[" + Integer.valueOf( 'A' ) + suffix + "]" );
378
indicatorB.setNumerator( "[" + Integer.valueOf( 'B' ) + suffix + "]+[" + Integer.valueOf( 'C' ) + suffix + "]" );
379
indicatorB.setDenominator( "[" + Integer.valueOf( 'B' ) + suffix + "]" );
381
indicatorC.setNumerator( "[" + Integer.valueOf( 'A' ) + suffix + "]" );
382
indicatorC.setDenominator( "[" + Integer.valueOf( 'A' ) + suffix + "]" );
384
importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, indicatorA );
385
importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, indicatorB );
386
importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, indicatorC );
388
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 3 );
389
assertEquals( importObjectService.getImportObjects( Indicator.class ).size(), 3 );
391
importObjectService.cascadeDeleteImportObject( idA );
393
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 2 );
394
assertEquals( importObjectService.getImportObjects( Indicator.class ).size(), 1 );
396
importObjectService.cascadeDeleteImportObject( idC );
398
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 1 );
399
assertEquals( importObjectService.getImportObjects( Indicator.class ).size(), 0 );
402
public void testCascadeDeleteImportObjects()
404
int idJ = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationD );
405
int idK = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationE );
406
int idL = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.DATAELEMENTGROUP, associationF );
408
int idM = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, dataSetA );
410
int idN = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, indicatorA );
411
int idO = importObjectService.addImportObject( ImportObjectStatus.NEW, GroupMemberType.NONE, indicatorB );
413
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 3 );
414
assertEquals( importObjectService.getImportObjects( DataElementGroup.class ).size(), 3 );
415
assertEquals( importObjectService.getImportObjects( Indicator.class ).size(), 2 );
416
assertEquals( importObjectService.getImportObjects( DataSet.class ).size(), 1 );
417
assertEquals( importObjectService.getImportObjects( GroupMemberAssociation.class ).size(), 6 );
419
assertNotNulls( idJ, idK, idL, idM, idN, idO );
421
importObjectService.cascadeDeleteImportObjects( DataElement.class );
423
assertEquals( importObjectService.getImportObjects( DataElement.class ).size(), 0 );
424
assertEquals( importObjectService.getImportObjects( DataElementGroup.class ).size(), 3 );
425
assertEquals( importObjectService.getImportObjects( Indicator.class ).size(), 0 );
426
assertEquals( importObjectService.getImportObjects( DataSet.class ).size(), 0 );
427
assertEquals( importObjectService.getImportObjects( GroupMemberAssociation.class ).size(), 0 );
429
assertNulls( idA, idB, idC, idG, idH, idI, idJ, idK, idL, idN, idO );
432
// -------------------------------------------------------------------------
434
// -------------------------------------------------------------------------
436
public void testMatchObject()
438
indicatorService.addIndicatorType( indicatorTypeA );
440
categoryComboService.addDataElementCategoryCombo( categoryComboA );
442
int existingObjectIdA = dataElementService.addDataElement( dataElementA );
443
int existingObjectIdB = indicatorService.addIndicator( indicatorA );
445
int importObjectIdA = importObjectService.addImportObject( ImportObjectStatus.NEW, dataElementB, null );
446
int importObjectIdB = importObjectService.addImportObject( ImportObjectStatus.UPDATE, indicatorB, indicatorA );
448
importObjectService.matchObject( importObjectIdA, existingObjectIdA );
449
importObjectService.matchObject( importObjectIdB, existingObjectIdB );
451
ImportObject importObjectA = importObjectService.getImportObject( importObjectIdA );
452
ImportObject importObjectB = importObjectService.getImportObject( importObjectIdB );
454
DataElement existingObjectA = (DataElement) importObjectA.getObject();
455
Indicator existingObjectB = (Indicator) importObjectB.getObject();
457
assertEquals( existingObjectA.getId(), 'B' );
458
assertEquals( existingObjectA.getName(), "DataElementA" );
459
assertEquals( importObjectA.getStatus(), ImportObjectStatus.MATCH );
461
assertEquals( existingObjectB.getId(), 'B' );
462
assertEquals( existingObjectB.getName(), "IndicatorA" );
463
assertEquals( importObjectB.getStatus(), ImportObjectStatus.MATCH );