1
package org.hisp.dhis.jdbc.batchhandler;
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.
30
import java.util.Collection;
31
import java.util.Date;
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;
47
* @author Lars Helge Overland
50
public class CompleteDataSetRegistrationBatchHandlerTest
51
extends DhisConvenienceTest
53
private BatchHandlerFactory batchHandlerFactory;
55
private BatchHandler batchHandler;
57
private DataSet dataSetA;
58
private DataSet dataSetB;
60
private Period periodA;
61
private Period periodB;
63
private OrganisationUnit unitA;
68
private CompleteDataSetRegistration registrationA;
69
private CompleteDataSetRegistration registrationB;
70
private CompleteDataSetRegistration registrationC;
71
private CompleteDataSetRegistration registrationD;
73
// -------------------------------------------------------------------------
75
// -------------------------------------------------------------------------
77
public void setUpTest()
79
dataSetService = (DataSetService) getBean( DataSetService.ID );
80
periodService = (PeriodService) getBean( PeriodService.ID );
81
organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
82
completeDataSetRegistrationService = (CompleteDataSetRegistrationService) getBean( CompleteDataSetRegistrationService.ID );
84
batchHandlerFactory = (BatchHandlerFactory) getBean( BatchHandlerFactory.ID );
86
batchHandler = batchHandlerFactory.createBatchHandler( CompleteDataSetRegistrationBatchHandler.class );
88
dataSetA = createDataSet( 'A', new MonthlyPeriodType() );
89
dataSetB = createDataSet( 'B', new MonthlyPeriodType() );
91
dataSetService.addDataSet( dataSetA );
92
dataSetService.addDataSet( dataSetB );
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 ) );
97
periodService.addPeriod( periodA );
98
periodService.addPeriod( periodB );
100
unitA = createOrganisationUnit( 'A' );
102
organisationUnitService.addOrganisationUnit( unitA );
104
dateA = getDate( 2000, 1, 15 );
105
dateB = getDate( 2000, 2, 15 );
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 );
115
public void tearDownTest()
117
batchHandler.flush();
120
// -------------------------------------------------------------------------
122
// -------------------------------------------------------------------------
124
public void testAddObject()
126
batchHandler.addObject( registrationA );
127
batchHandler.addObject( registrationB );
128
batchHandler.addObject( registrationC );
129
batchHandler.addObject( registrationD );
131
batchHandler.flush();
133
Collection<CompleteDataSetRegistration> registrations = completeDataSetRegistrationService.getAllCompleteDataSetRegistrations();
135
assertNotNull( registrations );
136
assertEquals( 4, registrations.size() );
138
assertTrue( registrations.contains( registrationA ) );
139
assertTrue( registrations.contains( registrationB ) );
140
assertTrue( registrations.contains( registrationC ) );
141
assertTrue( registrations.contains( registrationD ) );
144
public void testInsertObject()
146
batchHandler.insertObject( registrationA, false );
147
batchHandler.insertObject( registrationB, false );
148
batchHandler.insertObject( registrationC, false );
149
batchHandler.insertObject( registrationD, false );
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 ) );
157
public void testUpdateObject()
159
completeDataSetRegistrationService.saveCompleteDataSetRegistration( registrationA );
161
registrationA.setDate( dateB );
163
batchHandler.updateObject( registrationA );
165
registrationA = completeDataSetRegistrationService.getCompleteDataSetRegistration( dataSetA, periodA, unitA );
167
assertEquals( dateB, registrationA.getDate() );
170
public void testObjectExists()
172
completeDataSetRegistrationService.saveCompleteDataSetRegistration( registrationA );
173
completeDataSetRegistrationService.saveCompleteDataSetRegistration( registrationB );
175
assertTrue( batchHandler.objectExists( registrationA ) );
176
assertTrue( batchHandler.objectExists( registrationB ) );
178
assertFalse( batchHandler.objectExists( registrationC ) );
179
assertFalse( batchHandler.objectExists( registrationD ) );