~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/dataelement/DataElementCategoryStoreTest.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.dataelement;
 
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
import java.util.HashSet;
 
32
import java.util.Set;
 
33
 
 
34
import org.hisp.dhis.DhisConvenienceTest;
 
35
 
 
36
/**
 
37
 * @author Lars Helge Overland
 
38
 * @version $Id$
 
39
 */
 
40
public class DataElementCategoryStoreTest
 
41
    extends DhisConvenienceTest
 
42
{
 
43
    private DataElementCategoryStore categoryStore;
 
44
    
 
45
    private DataElementCategoryOption categoryOptionA;
 
46
    private DataElementCategoryOption categoryOptionB;
 
47
    private DataElementCategoryOption categoryOptionC;
 
48
    
 
49
    private DataElementCategory categoryA;
 
50
    private DataElementCategory categoryB;
 
51
    private DataElementCategory categoryC;
 
52
    
 
53
    private Set<DataElementCategoryOption> categoryOptions;
 
54
 
 
55
    // -------------------------------------------------------------------------
 
56
    // Fixture
 
57
    // -------------------------------------------------------------------------
 
58
 
 
59
    @Override
 
60
    public void setUpTest()
 
61
    {
 
62
        categoryOptionService = (DataElementCategoryOptionService) getBean( DataElementCategoryOptionService.ID );
 
63
        
 
64
        categoryStore = (DataElementCategoryStore) getBean( DataElementCategoryStore.ID );
 
65
 
 
66
        categoryOptionA = new DataElementCategoryOption( "CategoryOptionA" );
 
67
        categoryOptionB = new DataElementCategoryOption( "CategoryOptionB" );
 
68
        categoryOptionC = new DataElementCategoryOption( "CategoryOptionC" );
 
69
        
 
70
        categoryOptionService.addDataElementCategoryOption( categoryOptionA );
 
71
        categoryOptionService.addDataElementCategoryOption( categoryOptionB );
 
72
        categoryOptionService.addDataElementCategoryOption( categoryOptionC );
 
73
        
 
74
        categoryOptions = new HashSet<DataElementCategoryOption>();
 
75
        
 
76
        categoryOptions.add( categoryOptionA );
 
77
        categoryOptions.add( categoryOptionB );
 
78
        categoryOptions.add( categoryOptionC );         
 
79
    }
 
80
 
 
81
    // -------------------------------------------------------------------------
 
82
    // Tests
 
83
    // -------------------------------------------------------------------------
 
84
 
 
85
    public void testAddGet()
 
86
    {
 
87
        categoryA = new DataElementCategory( "CategoryA", categoryOptions );
 
88
        categoryB = new DataElementCategory( "CategoryB", categoryOptions );
 
89
        categoryC = new DataElementCategory( "CategoryC", categoryOptions );
 
90
        
 
91
        int idA = categoryStore.addDataElementCategory( categoryA );
 
92
        int idB = categoryStore.addDataElementCategory( categoryB );
 
93
        int idC = categoryStore.addDataElementCategory( categoryC );
 
94
        
 
95
        assertEquals( categoryA, categoryStore.getDataElementCategory( idA ) );
 
96
        assertEquals( categoryB, categoryStore.getDataElementCategory( idB ) );
 
97
        assertEquals( categoryC, categoryStore.getDataElementCategory( idC ) );
 
98
        
 
99
        assertEquals( categoryOptions, categoryStore.getDataElementCategory( idA ).getCategoryOptions() );
 
100
        assertEquals( categoryOptions, categoryStore.getDataElementCategory( idB ).getCategoryOptions() );
 
101
        assertEquals( categoryOptions, categoryStore.getDataElementCategory( idC ).getCategoryOptions() );        
 
102
    }
 
103
    
 
104
    public void testDelete()
 
105
    {
 
106
        categoryA = new DataElementCategory( "CategoryA", categoryOptions );
 
107
        categoryB = new DataElementCategory( "CategoryB", categoryOptions );
 
108
        categoryC = new DataElementCategory( "CategoryC", categoryOptions );
 
109
        
 
110
        int idA = categoryStore.addDataElementCategory( categoryA );
 
111
        int idB = categoryStore.addDataElementCategory( categoryB );
 
112
        int idC = categoryStore.addDataElementCategory( categoryC );
 
113
        
 
114
        assertNotNull( categoryStore.getDataElementCategory( idA ) );
 
115
        assertNotNull( categoryStore.getDataElementCategory( idB ) );
 
116
        assertNotNull( categoryStore.getDataElementCategory( idC ) );
 
117
        
 
118
        categoryStore.deleteDataElementCategory( categoryA );
 
119
 
 
120
        assertNull( categoryStore.getDataElementCategory( idA ) );
 
121
        assertNotNull( categoryStore.getDataElementCategory( idB ) );
 
122
        assertNotNull( categoryStore.getDataElementCategory( idC ) );
 
123
 
 
124
        categoryStore.deleteDataElementCategory( categoryB );
 
125
 
 
126
        assertNull( categoryStore.getDataElementCategory( idA ) );
 
127
        assertNull( categoryStore.getDataElementCategory( idB ) );
 
128
        assertNotNull( categoryStore.getDataElementCategory( idC ) );        
 
129
    }
 
130
    
 
131
    public void testGetAll()
 
132
    {
 
133
        categoryA = new DataElementCategory( "CategoryA", categoryOptions );
 
134
        categoryB = new DataElementCategory( "CategoryB", categoryOptions );
 
135
        categoryC = new DataElementCategory( "CategoryC", categoryOptions );
 
136
 
 
137
        categoryStore.addDataElementCategory( categoryA );
 
138
        categoryStore.addDataElementCategory( categoryB );
 
139
        categoryStore.addDataElementCategory( categoryC );
 
140
        
 
141
        Collection<DataElementCategory> categories = categoryStore.getAllDataElementCategories();
 
142
        
 
143
        assertEquals( 4, categories.size() ); // Including default
 
144
        assertTrue( categories.contains( categoryA ) );
 
145
        assertTrue( categories.contains( categoryB ) );
 
146
        assertTrue( categories.contains( categoryC ) );        
 
147
    }
 
148
}