~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/mapping/ObjectMappingGeneratorTest.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.mapping;
 
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.util.Map;
 
31
 
 
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;
 
47
 
 
48
/**
 
49
 * @author Lars Helge Overland
 
50
 * @version $Id$
 
51
 */
 
52
public class ObjectMappingGeneratorTest
 
53
    extends DhisConvenienceTest 
 
54
{
 
55
    private ObjectMappingGenerator objectMappingGenerator;
 
56
    
 
57
    // -------------------------------------------------------------------------
 
58
    // Fixture
 
59
    // -------------------------------------------------------------------------
 
60
 
 
61
    public void setUpTest()
 
62
    {
 
63
        objectMappingGenerator = (ObjectMappingGenerator) getBean( ObjectMappingGenerator.ID );
 
64
        
 
65
        dataElementService = (DataElementService) getBean( DataElementService.ID );
 
66
        
 
67
        categoryComboService = (DataElementCategoryComboService) getBean( DataElementCategoryComboService.ID );
 
68
        
 
69
        categoryOptionService = (DataElementCategoryOptionService) getBean( DataElementCategoryOptionService.ID );
 
70
        
 
71
        categoryOptionComboService = (DataElementCategoryOptionComboService) getBean( DataElementCategoryOptionComboService.ID );
 
72
        
 
73
        periodService = (PeriodService) getBean( PeriodService.ID );
 
74
        
 
75
        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
 
76
    }
 
77
    
 
78
    public void tearDownTest()
 
79
    {
 
80
        NameMappingUtil.clearMapping();
 
81
    }
 
82
 
 
83
    // -------------------------------------------------------------------------
 
84
    // Tests
 
85
    // -------------------------------------------------------------------------
 
86
 
 
87
    public void testGetDataElementMapping()
 
88
    {
 
89
        DataElement dataElementA = createDataElement( 'A' );
 
90
        DataElement dataElementB = createDataElement( 'B' );
 
91
        
 
92
        dataElementA.setId( 'A' );
 
93
        dataElementB.setId( 'B' );
 
94
        
 
95
        NameMappingUtil.addDataElementMapping( dataElementA.getId(), dataElementA.getName() );
 
96
        NameMappingUtil.addDataElementMapping( dataElementB.getId(), dataElementB.getName() );
 
97
        
 
98
        int idA = dataElementService.addDataElement( dataElementA );
 
99
        int idB = dataElementService.addDataElement( dataElementB );
 
100
        
 
101
        Map<Object, Integer> mapping = objectMappingGenerator.getDataElementMapping( false );
 
102
        
 
103
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
 
104
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
 
105
        
 
106
        mapping = objectMappingGenerator.getDataElementMapping( true );
 
107
 
 
108
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
 
109
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
 
110
    }
 
111
    
 
112
    public void testGetCategoryOptionComboMapping()
 
113
    {
 
114
        DataElementCategoryCombo categoryComboA = new DataElementCategoryCombo( "CategoryComboA" );
 
115
        
 
116
        categoryComboService.addDataElementCategoryCombo( categoryComboA );
 
117
        
 
118
        DataElementCategoryOption categoryOptionA = new DataElementCategoryOption( "CategoryOptionA" );
 
119
        DataElementCategoryOption categoryOptionB = new DataElementCategoryOption( "CategoryOptionB" );
 
120
        DataElementCategoryOption categoryOptionC = new DataElementCategoryOption( "CategoryOptionC" );
 
121
        DataElementCategoryOption categoryOptionD = new DataElementCategoryOption( "CategoryOptionD" );
 
122
        
 
123
        categoryOptionService.addDataElementCategoryOption( categoryOptionA );
 
124
        categoryOptionService.addDataElementCategoryOption( categoryOptionB );
 
125
        categoryOptionService.addDataElementCategoryOption( categoryOptionC );
 
126
        categoryOptionService.addDataElementCategoryOption( categoryOptionD );
 
127
        
 
128
        DataElementCategoryOptionCombo categoryOptionComboA = new DataElementCategoryOptionCombo();
 
129
        categoryOptionComboA.setId( 'A' );
 
130
        categoryOptionComboA.setCategoryCombo( categoryComboA );
 
131
        categoryOptionComboA.getCategoryOptions().add( categoryOptionA );
 
132
        categoryOptionComboA.getCategoryOptions().add( categoryOptionB );
 
133
 
 
134
        DataElementCategoryOptionCombo categoryOptionComboB = new DataElementCategoryOptionCombo();
 
135
        categoryOptionComboB.setId( 'B' );
 
136
        categoryOptionComboB.setCategoryCombo( categoryComboA );
 
137
        categoryOptionComboB.getCategoryOptions().add( categoryOptionC );
 
138
        categoryOptionComboB.getCategoryOptions().add( categoryOptionD );
 
139
        
 
140
        NameMappingUtil.addCategoryOptionComboMapping( categoryOptionComboA.getId(), categoryOptionComboA );
 
141
        NameMappingUtil.addCategoryOptionComboMapping( categoryOptionComboB.getId(), categoryOptionComboB );        
 
142
 
 
143
        int idA = categoryOptionComboService.addDataElementCategoryOptionCombo( categoryOptionComboA );
 
144
        int idB = categoryOptionComboService.addDataElementCategoryOptionCombo( categoryOptionComboB );
 
145
        
 
146
        Map<Object, Integer> mapping = objectMappingGenerator.getCategoryOptionComboMapping( false );
 
147
 
 
148
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
 
149
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
 
150
 
 
151
        mapping = objectMappingGenerator.getCategoryOptionComboMapping( true );
 
152
 
 
153
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
 
154
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
 
155
    }
 
156
    
 
157
    public void testGetPeriodMapping()
 
158
    {        
 
159
        PeriodType periodTypeA = periodService.getPeriodTypeByName( MonthlyPeriodType.NAME );
 
160
        
 
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 ) );
 
163
        
 
164
        periodA.setId( 'A' );
 
165
        periodB.setId( 'B' );
 
166
        
 
167
        NameMappingUtil.addPeriodMapping( periodA.getId(), periodA );
 
168
        NameMappingUtil.addPeriodMapping( periodB.getId(), periodB );
 
169
 
 
170
        int idA = periodService.addPeriod( periodA );
 
171
        int idB = periodService.addPeriod( periodB );
 
172
        
 
173
        Map<Object, Integer> mapping = objectMappingGenerator.getPeriodMapping( false );
 
174
 
 
175
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
 
176
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
 
177
        
 
178
        mapping = objectMappingGenerator.getPeriodMapping( true );
 
179
        
 
180
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
 
181
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );
 
182
    }
 
183
    
 
184
    public void testOrganisationUnitMapping()
 
185
    {
 
186
        OrganisationUnit organisationUnitA = createOrganisationUnit( 'A' );
 
187
        OrganisationUnit organisationUnitB = createOrganisationUnit( 'B' );
 
188
        
 
189
        organisationUnitA.setId( 'A' );
 
190
        organisationUnitB.setId( 'B' );        
 
191
 
 
192
        NameMappingUtil.addOrganisationUnitMapping( organisationUnitA.getId(), organisationUnitA.getName() );
 
193
        NameMappingUtil.addOrganisationUnitMapping( organisationUnitB.getId(), organisationUnitB.getName() );
 
194
 
 
195
        int idA = organisationUnitService.addOrganisationUnit( organisationUnitA );
 
196
        int idB = organisationUnitService.addOrganisationUnit( organisationUnitB );
 
197
 
 
198
        Map<Object, Integer> mapping = objectMappingGenerator.getOrganisationUnitMapping( false );
 
199
 
 
200
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( idA ) );
 
201
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( idB ) );
 
202
        
 
203
        mapping = objectMappingGenerator.getOrganisationUnitMapping( true );
 
204
        
 
205
        assertEquals( mapping.get( Integer.valueOf( 'A' ) ), Integer.valueOf( 'A' ) );
 
206
        assertEquals( mapping.get( Integer.valueOf( 'B' ) ), Integer.valueOf( 'B' ) );        
 
207
    }
 
208
    
 
209
    public void testOrganisationUnitMappingWithUUID()
 
210
    {
 
211
        OrganisationUnit organisationUnitA = createOrganisationUnit( 'A' );
 
212
        OrganisationUnit organisationUnitB = createOrganisationUnit( 'B' );
 
213
        
 
214
        NameMappingUtil.addOrganisationUnitMapping( organisationUnitA.getUuid(), organisationUnitA.getName() );
 
215
        NameMappingUtil.addOrganisationUnitMapping( organisationUnitB.getUuid(), organisationUnitB.getName() );
 
216
 
 
217
        int idA = organisationUnitService.addOrganisationUnit( organisationUnitA );
 
218
        int idB = organisationUnitService.addOrganisationUnit( organisationUnitB );
 
219
 
 
220
        Map<Object, Integer> mapping = objectMappingGenerator.getOrganisationUnitMapping( false );
 
221
 
 
222
        assertEquals( mapping.get( organisationUnitA.getUuid() ), Integer.valueOf( idA ) );
 
223
        assertEquals( mapping.get( organisationUnitB.getUuid() ), Integer.valueOf( idB ) );
 
224
        
 
225
        mapping = objectMappingGenerator.getOrganisationUnitMapping( true );
 
226
        
 
227
        assertEquals( mapping.get( organisationUnitA.getUuid() ), Integer.valueOf( -1 ) );
 
228
        assertEquals( mapping.get( organisationUnitB.getUuid() ), Integer.valueOf( -1 ) );
 
229
    }
 
230
}