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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/datadictionary/DataDictionaryServiceTest.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.datadictionary;
 
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.Collection;
 
31
 
 
32
import org.hisp.dhis.DhisConvenienceTest;
 
33
import org.hisp.dhis.dataelement.DataElement;
 
34
import org.hisp.dhis.dataelement.DataElementService;
 
35
import org.hisp.dhis.indicator.Indicator;
 
36
import org.hisp.dhis.indicator.IndicatorService;
 
37
import org.hisp.dhis.indicator.IndicatorType;
 
38
 
 
39
/**
 
40
 * @author Lars Helge Overland
 
41
 * @version $Id$
 
42
 */
 
43
public class DataDictionaryServiceTest
 
44
    extends DhisConvenienceTest
 
45
{
 
46
    private DataDictionaryService dataDictionaryService;
 
47
    
 
48
    private DataDictionary dataDictionaryA;
 
49
    private DataDictionary dataDictionaryB;
 
50
    
 
51
    private DataElement dataElementA;
 
52
    private DataElement dataElementB;
 
53
 
 
54
    private IndicatorType indicatorType;
 
55
    
 
56
    private Indicator indicatorA;
 
57
    private Indicator indicatorB;
 
58
    
 
59
    // -------------------------------------------------------------------------
 
60
    // Fixture
 
61
    // -------------------------------------------------------------------------
 
62
 
 
63
    public void setUpTest()
 
64
        throws Exception
 
65
    {
 
66
        dataDictionaryService = (DataDictionaryService) getBean( DataDictionaryService.ID );
 
67
        
 
68
        dataElementService = (DataElementService) getBean( DataElementService.ID );
 
69
 
 
70
        indicatorService = (IndicatorService) getBean( IndicatorService.ID );
 
71
        
 
72
        dataDictionaryA = createDataDictionary( 'A' );
 
73
        dataDictionaryB = createDataDictionary( 'B' );
 
74
        
 
75
        dataElementA = createDataElement( 'A' );
 
76
        dataElementB = createDataElement( 'B' );
 
77
 
 
78
        indicatorType = createIndicatorType( 'A' );
 
79
        
 
80
        indicatorService.addIndicatorType( indicatorType );
 
81
        
 
82
        indicatorA = createIndicator( 'A', indicatorType );
 
83
        indicatorB = createIndicator( 'B', indicatorType );                
 
84
    }
 
85
 
 
86
    // -------------------------------------------------------------------------
 
87
    // DataDictionary tests
 
88
    // -------------------------------------------------------------------------
 
89
 
 
90
    public void testSaveGetDataDictionary()
 
91
    {
 
92
        dataElementService.addDataElement( dataElementA );
 
93
        dataElementService.addDataElement( dataElementB );
 
94
        
 
95
        indicatorService.addIndicator( indicatorA );
 
96
        indicatorService.addIndicator( indicatorB );
 
97
                
 
98
        dataDictionaryA.getDataElements().add( dataElementA );
 
99
        dataDictionaryA.getDataElements().add( dataElementB );
 
100
        
 
101
        dataDictionaryA.getIndicators().add( indicatorA );
 
102
        dataDictionaryA.getIndicators().add( indicatorB );
 
103
                
 
104
        int id = dataDictionaryService.saveDataDictionary( dataDictionaryA );
 
105
        
 
106
        dataDictionaryA = dataDictionaryService.getDataDictionary( id );
 
107
        
 
108
        assertEquals( dataDictionaryA.getName(), "DataDictionaryA" );
 
109
        assertEquals( dataDictionaryA.getDescription(), "DescriptionA" );
 
110
        assertEquals( dataDictionaryA.getRegion(), "RegionA" );
 
111
        assertEquals( dataDictionaryA.getDataElements().size(), 2 );
 
112
        
 
113
        assertTrue( dataDictionaryA.getDataElements().contains( dataElementA ) );
 
114
        assertTrue( dataDictionaryA.getDataElements().contains( dataElementB ) );
 
115
        
 
116
        assertTrue( dataDictionaryA.getIndicators().contains( indicatorA ) );
 
117
        assertTrue( dataDictionaryA.getIndicators().contains( indicatorB ) );
 
118
                
 
119
        dataDictionaryA.setName( "DataDictionaryB" );
 
120
        dataDictionaryA.setDescription( "DescriptionB" );
 
121
        dataDictionaryA.setRegion( "RegionB" );
 
122
        
 
123
        dataDictionaryService.saveDataDictionary( dataDictionaryA );
 
124
        
 
125
        dataDictionaryA = dataDictionaryService.getDataDictionary( id );
 
126
        
 
127
        assertEquals( dataDictionaryA.getName(), "DataDictionaryB" );
 
128
        assertEquals( dataDictionaryA.getDescription(), "DescriptionB" );
 
129
        assertEquals( dataDictionaryA.getRegion(), "RegionB" );
 
130
    }
 
131
 
 
132
    public void testGetDataDictionaryByName()
 
133
    {
 
134
        dataDictionaryService.saveDataDictionary( dataDictionaryA );
 
135
        dataDictionaryService.saveDataDictionary( dataDictionaryB );
 
136
        
 
137
        dataDictionaryA = dataDictionaryService.getDataDictionaryByName( "DataDictionaryA" );
 
138
        
 
139
        assertNotNull( dataDictionaryA );        
 
140
        assertEquals( dataDictionaryA.getName(), "DataDictionaryA" );
 
141
        assertEquals( dataDictionaryA.getDescription(), "DescriptionA" );
 
142
    }
 
143
 
 
144
    public void testDeleteDataDictionary()
 
145
    {
 
146
        int idA = dataDictionaryService.saveDataDictionary( dataDictionaryA );
 
147
        int idB = dataDictionaryService.saveDataDictionary( dataDictionaryB );
 
148
        
 
149
        assertNotNull( dataDictionaryService.getDataDictionary( idA ) );
 
150
        assertNotNull( dataDictionaryService.getDataDictionary( idB ) );
 
151
        
 
152
        dataDictionaryService.deleteDataDictionary( dataDictionaryA );
 
153
 
 
154
        assertNull( dataDictionaryService.getDataDictionary( idA ) );
 
155
        assertNotNull( dataDictionaryService.getDataDictionary( idB ) );
 
156
 
 
157
        dataDictionaryService.deleteDataDictionary( dataDictionaryB );
 
158
 
 
159
        assertNull( dataDictionaryService.getDataDictionary( idA ) );
 
160
        assertNull( dataDictionaryService.getDataDictionary( idB ) );
 
161
    }
 
162
    
 
163
    public void testGetAllDataDictionaries()
 
164
    {
 
165
        dataDictionaryService.saveDataDictionary( dataDictionaryA );
 
166
        dataDictionaryService.saveDataDictionary( dataDictionaryB );
 
167
        
 
168
        Collection<DataDictionary> dictionaries = dataDictionaryService.getAllDataDictionaries();
 
169
        
 
170
        assertTrue( dictionaries.size() == 2 );
 
171
        assertTrue( dictionaries.contains( dataDictionaryA ) );
 
172
        assertTrue( dictionaries.contains( dataDictionaryB ) );        
 
173
    }
 
174
}