1
package org.hisp.dhis.importexport.mapping;
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.
32
import org.hisp.dhis.DhisConvenienceTest;
33
import org.hisp.dhis.dataelement.DataElement;
34
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
35
import org.hisp.dhis.dataelement.DataElementCategoryComboService;
36
import org.hisp.dhis.dataelement.DataElementCategoryOption;
37
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
38
import org.hisp.dhis.dataelement.DataElementCategoryOptionComboService;
39
import org.hisp.dhis.dataelement.DataElementCategoryOptionService;
40
import org.hisp.dhis.dataelement.DataElementService;
41
import org.hisp.dhis.organisationunit.OrganisationUnit;
42
import org.hisp.dhis.organisationunit.OrganisationUnitService;
43
import org.hisp.dhis.period.MonthlyPeriodType;
44
import org.hisp.dhis.period.Period;
45
import org.hisp.dhis.period.PeriodService;
46
import org.hisp.dhis.period.PeriodType;
49
* @author Lars Helge Overland
52
public class ObjectMappingGeneratorTest
53
extends DhisConvenienceTest
55
private ObjectMappingGenerator objectMappingGenerator;
57
// -------------------------------------------------------------------------
59
// -------------------------------------------------------------------------
61
public void setUpTest()
63
objectMappingGenerator = (ObjectMappingGenerator) getBean( ObjectMappingGenerator.ID );
65
dataElementService = (DataElementService) getBean( DataElementService.ID );
67
categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
69
categoryOptionService = (DataElementCategoryOptionService) getBean( DataElementCategoryOptionService.ID );
71
categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID );
73
periodService = (PeriodService) getBean( PeriodService.ID );
75
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
78
public void tearDownTest()
80
NameMappingUtil.clearMapping();
83
// -------------------------------------------------------------------------
85
// -------------------------------------------------------------------------
87
public void testGetDataElementMapping()
89
DataElement dataElementA = createDataElement( 'A' );
90
DataElement dataElementB = createDataElement( 'B' );
92
dataElementA.setId( 'A' );
93
dataElementB.setId( 'B' );
95
NameMappingUtil.addDataElementMapping( dataElementA.getId(), dataElementA.getName() );
96
NameMappingUtil.addDataElementMapping( dataElementB.getId(), dataElementB.getName() );
98
int idA = dataElementService.addDataElement( dataElementA );
99
int idB = dataElementService.addDataElement( dataElementB );
101
Map<Object, Integer> mapping = objectMappingGenerator.getDataElementMapping( false );
103
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
104
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
106
mapping = objectMappingGenerator.getDataElementMapping( true );
108
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
109
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
112
public void testGetCategoryOptionComboMapping()
114
DataElementCategoryCombo categoryComboA = new DataElementCategoryCombo( "CategoryComboA" );
116
categoryComboService.addDataElementCategoryCombo( categoryComboA );
118
DataElementCategoryOption categoryOptionA = new DataElementCategoryOption( "CategoryOptionA" );
119
DataElementCategoryOption categoryOptionB = new DataElementCategoryOption( "CategoryOptionB" );
120
DataElementCategoryOption categoryOptionC = new DataElementCategoryOption( "CategoryOptionC" );
121
DataElementCategoryOption categoryOptionD = new DataElementCategoryOption( "CategoryOptionD" );
123
categoryOptionService.addDataElementCategoryOption( categoryOptionA );
124
categoryOptionService.addDataElementCategoryOption( categoryOptionB );
125
categoryOptionService.addDataElementCategoryOption( categoryOptionC );
126
categoryOptionService.addDataElementCategoryOption( categoryOptionD );
128
DataElementCategoryOptionCombo categoryOptionComboA = new DataElementCategoryOptionCombo();
129
categoryOptionComboA.setId( 'A' );
130
categoryOptionComboA.setCategoryCombo( categoryComboA );
131
categoryOptionComboA.getCategoryOptions().add( categoryOptionA );
132
categoryOptionComboA.getCategoryOptions().add( categoryOptionB );
134
DataElementCategoryOptionCombo categoryOptionComboB = new DataElementCategoryOptionCombo();
135
categoryOptionComboB.setId( 'B' );
136
categoryOptionComboB.setCategoryCombo( categoryComboA );
137
categoryOptionComboB.getCategoryOptions().add( categoryOptionC );
138
categoryOptionComboB.getCategoryOptions().add( categoryOptionD );
140
NameMappingUtil.addCategoryOptionComboMapping( categoryOptionComboA.getId(), categoryOptionComboA );
141
NameMappingUtil.addCategoryOptionComboMapping( categoryOptionComboB.getId(), categoryOptionComboB );
143
int idA = categoryOptionComboService.addDataElementCategoryOptionCombo( categoryOptionComboA );
144
int idB = categoryOptionComboService.addDataElementCategoryOptionCombo( categoryOptionComboB );
146
Map<Object, Integer> mapping = objectMappingGenerator.getCategoryOptionComboMapping( false );
148
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
149
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
151
mapping = objectMappingGenerator.getCategoryOptionComboMapping( true );
153
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
154
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
157
public void testGetPeriodMapping()
159
PeriodType periodTypeA = periodService.getPeriodTypeByName( MonthlyPeriodType.NAME );
161
Period periodA = createPeriod( periodTypeA, getDate( 1, 0, 2000 ), getDate( 31, 0, 2000 ) );
162
Period periodB = createPeriod( periodTypeA, getDate( 1, 1, 2000 ), getDate( 28, 1, 2000 ) );
164
periodA.setId( 'A' );
165
periodB.setId( 'B' );
167
NameMappingUtil.addPeriodMapping( periodA.getId(), periodA );
168
NameMappingUtil.addPeriodMapping( periodB.getId(), periodB );
170
int idA = periodService.addPeriod( periodA );
171
int idB = periodService.addPeriod( periodB );
173
Map<Object, Integer> mapping = objectMappingGenerator.getPeriodMapping( false );
175
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
176
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
178
mapping = objectMappingGenerator.getPeriodMapping( true );
180
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
181
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
184
public void testOrganisationUnitMapping()
186
OrganisationUnit organisationUnitA = createOrganisationUnit( 'A' );
187
OrganisationUnit organisationUnitB = createOrganisationUnit( 'B' );
189
organisationUnitA.setId( 'A' );
190
organisationUnitB.setId( 'B' );
192
NameMappingUtil.addOrganisationUnitMapping( organisationUnitA.getId(), organisationUnitA.getName() );
193
NameMappingUtil.addOrganisationUnitMapping( organisationUnitB.getId(), organisationUnitB.getName() );
195
int idA = organisationUnitService.addOrganisationUnit( organisationUnitA );
196
int idB = organisationUnitService.addOrganisationUnit( organisationUnitB );
198
Map<Object, Integer> mapping = objectMappingGenerator.getOrganisationUnitMapping( false );
200
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
201
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
203
mapping = objectMappingGenerator.getOrganisationUnitMapping( true );
205
assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
206
assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
209
public void testOrganisationUnitMappingWithUUID()
211
OrganisationUnit organisationUnitA = createOrganisationUnit( 'A' );
212
OrganisationUnit organisationUnitB = createOrganisationUnit( 'B' );
214
NameMappingUtil.addOrganisationUnitMapping( organisationUnitA.getUuid(), organisationUnitA.getName() );
215
NameMappingUtil.addOrganisationUnitMapping( organisationUnitB.getUuid(), organisationUnitB.getName() );
217
int idA = organisationUnitService.addOrganisationUnit( organisationUnitA );
218
int idB = organisationUnitService.addOrganisationUnit( organisationUnitB );
220
Map<Object, Integer> mapping = objectMappingGenerator.getOrganisationUnitMapping( false );
222
assertEquals( mapping.get( organisationUnitA.getUuid() ), Integer.valueOf( idA ) );
223
assertEquals( mapping.get( organisationUnitB.getUuid() ), Integer.valueOf( idB ) );
225
mapping = objectMappingGenerator.getOrganisationUnitMapping( true );
227
assertEquals( mapping.get( organisationUnitA.getUuid() ), Integer.valueOf( -1 ) );
228
assertEquals( mapping.get( organisationUnitB.getUuid() ), Integer.valueOf( -1 ) );