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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-jdbc-test/src/test/java/org/hisp/dhis/jdbc/batchhandler/CompleteDataSetRegistrationBatchHandlerTest.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.jdbc.batchhandler;
 
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.Date;
 
32
 
 
33
import org.hisp.dhis.DhisConvenienceTest;
 
34
import org.hisp.dhis.jdbc.BatchHandler;
 
35
import org.hisp.dhis.jdbc.BatchHandlerFactory;
 
36
import org.hisp.dhis.dataset.CompleteDataSetRegistration;
 
37
import org.hisp.dhis.dataset.CompleteDataSetRegistrationService;
 
38
import org.hisp.dhis.dataset.DataSet;
 
39
import org.hisp.dhis.dataset.DataSetService;
 
40
import org.hisp.dhis.period.MonthlyPeriodType;
 
41
import org.hisp.dhis.period.Period;
 
42
import org.hisp.dhis.period.PeriodService;
 
43
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
44
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
45
 
 
46
/**
 
47
 * @author Lars Helge Overland
 
48
 * @version $Id$
 
49
 */
 
50
public class CompleteDataSetRegistrationBatchHandlerTest
 
51
    extends DhisConvenienceTest
 
52
{
 
53
    private BatchHandlerFactory batchHandlerFactory;
 
54
    
 
55
    private BatchHandler batchHandler;
 
56
        
 
57
    private DataSet dataSetA;
 
58
    private DataSet dataSetB;
 
59
    
 
60
    private Period periodA;
 
61
    private Period periodB;
 
62
    
 
63
    private OrganisationUnit unitA;
 
64
    
 
65
    private Date dateA;
 
66
    private Date dateB;
 
67
    
 
68
    private CompleteDataSetRegistration registrationA;
 
69
    private CompleteDataSetRegistration registrationB;
 
70
    private CompleteDataSetRegistration registrationC;
 
71
    private CompleteDataSetRegistration registrationD;
 
72
    
 
73
    // -------------------------------------------------------------------------
 
74
    // Fixture
 
75
    // -------------------------------------------------------------------------
 
76
    
 
77
    public void setUpTest()
 
78
    {
 
79
        dataSetService = (DataSetService) getBean( DataSetService.ID );
 
80
        periodService = (PeriodService) getBean( PeriodService.ID );
 
81
        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
 
82
        completeDataSetRegistrationService = (CompleteDataSetRegistrationService) getBean( CompleteDataSetRegistrationService.ID );
 
83
        
 
84
        batchHandlerFactory = (BatchHandlerFactory) getBean( BatchHandlerFactory.ID );
 
85
        
 
86
        batchHandler = batchHandlerFactory.createBatchHandler( CompleteDataSetRegistrationBatchHandler.class );
 
87
        
 
88
        dataSetA = createDataSet( 'A', new MonthlyPeriodType() );
 
89
        dataSetB = createDataSet( 'B', new MonthlyPeriodType() );
 
90
        
 
91
        dataSetService.addDataSet( dataSetA );
 
92
        dataSetService.addDataSet( dataSetB );
 
93
        
 
94
        periodA = createPeriod( new MonthlyPeriodType(), getDate( 2000, 1, 1 ), getDate( 2000, 1, 31 ) );
 
95
        periodB = createPeriod( new MonthlyPeriodType(), getDate( 2000, 2, 1 ), getDate( 2000, 2, 28 ) );
 
96
        
 
97
        periodService.addPeriod( periodA );
 
98
        periodService.addPeriod( periodB );        
 
99
        
 
100
        unitA = createOrganisationUnit( 'A' );
 
101
        
 
102
        organisationUnitService.addOrganisationUnit( unitA );
 
103
        
 
104
        dateA = getDate( 2000, 1, 15 );
 
105
        dateB = getDate( 2000, 2, 15 );
 
106
        
 
107
        registrationA = new CompleteDataSetRegistration( dataSetA, periodA, unitA, dateA );
 
108
        registrationB = new CompleteDataSetRegistration( dataSetA, periodB, unitA, dateB );
 
109
        registrationC = new CompleteDataSetRegistration( dataSetB, periodA, unitA, dateA );
 
110
        registrationD = new CompleteDataSetRegistration( dataSetB, periodB, unitA, dateB );
 
111
        
 
112
        batchHandler.init();
 
113
    }
 
114
    
 
115
    public void tearDownTest()
 
116
    {
 
117
        batchHandler.flush();
 
118
    }
 
119
 
 
120
    // -------------------------------------------------------------------------
 
121
    // Tests
 
122
    // -------------------------------------------------------------------------
 
123
    
 
124
    public void testAddObject()
 
125
    {
 
126
        batchHandler.addObject( registrationA );
 
127
        batchHandler.addObject( registrationB );
 
128
        batchHandler.addObject( registrationC );
 
129
        batchHandler.addObject( registrationD );
 
130
        
 
131
        batchHandler.flush();
 
132
        
 
133
        Collection<CompleteDataSetRegistration> registrations = completeDataSetRegistrationService.getAllCompleteDataSetRegistrations();
 
134
        
 
135
        assertNotNull( registrations );
 
136
        assertEquals( 4, registrations.size() );
 
137
        
 
138
        assertTrue( registrations.contains( registrationA ) );
 
139
        assertTrue( registrations.contains( registrationB ) );
 
140
        assertTrue( registrations.contains( registrationC ) );
 
141
        assertTrue( registrations.contains( registrationD ) );
 
142
    }
 
143
    
 
144
    public void testInsertObject()
 
145
    {
 
146
        batchHandler.insertObject( registrationA, false );
 
147
        batchHandler.insertObject( registrationB, false );
 
148
        batchHandler.insertObject( registrationC, false );
 
149
        batchHandler.insertObject( registrationD, false );
 
150
        
 
151
        assertNotNull( completeDataSetRegistrationService.getCompleteDataSetRegistration( dataSetA, periodA, unitA ) );
 
152
        assertNotNull( completeDataSetRegistrationService.getCompleteDataSetRegistration( dataSetA, periodB, unitA ) );
 
153
        assertNotNull( completeDataSetRegistrationService.getCompleteDataSetRegistration( dataSetB, periodA, unitA ) );
 
154
        assertNotNull( completeDataSetRegistrationService.getCompleteDataSetRegistration( dataSetB, periodB, unitA ) );
 
155
    }
 
156
    
 
157
    public void testUpdateObject()
 
158
    {
 
159
        completeDataSetRegistrationService.saveCompleteDataSetRegistration( registrationA );
 
160
        
 
161
        registrationA.setDate( dateB );
 
162
        
 
163
        batchHandler.updateObject( registrationA );
 
164
        
 
165
        registrationA = completeDataSetRegistrationService.getCompleteDataSetRegistration( dataSetA, periodA, unitA );
 
166
        
 
167
        assertEquals( dateB, registrationA.getDate() );
 
168
    }
 
169
    
 
170
    public void testObjectExists()
 
171
    {
 
172
        completeDataSetRegistrationService.saveCompleteDataSetRegistration( registrationA );
 
173
        completeDataSetRegistrationService.saveCompleteDataSetRegistration( registrationB );
 
174
        
 
175
        assertTrue( batchHandler.objectExists( registrationA ) );
 
176
        assertTrue( batchHandler.objectExists( registrationB ) );
 
177
        
 
178
        assertFalse( batchHandler.objectExists( registrationC ) );
 
179
        assertFalse( batchHandler.objectExists( registrationD ) );
 
180
    }
 
181
}