~dhis2-devs-core/+junk/dhis2-xml-ng

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataanalysis/MinMaxOutlierAnalysisServiceTest.java

  • Committer: Bob Jolliffe
  • Date: 2009-12-28 00:10:10 UTC
  • mfrom: (1265.1.24 trunk)
  • Revision ID: bobj@bobj-laptop-20091228001010-74keze4t25s96xi0
Merged in changes from trunk (trying to stay in synch)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.dataanalysis;
 
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.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.HashSet;
 
33
import java.util.Set;
 
34
 
 
35
import org.hisp.dhis.DhisTest;
 
36
import org.hisp.dhis.dataelement.DataElement;
 
37
import org.hisp.dhis.dataelement.DataElementCategoryCombo;
 
38
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
39
import org.hisp.dhis.dataelement.DataElementCategoryService;
 
40
import org.hisp.dhis.dataelement.DataElementService;
 
41
import org.hisp.dhis.dataset.DataSetService;
 
42
import org.hisp.dhis.datavalue.DataValue;
 
43
import org.hisp.dhis.datavalue.DataValueService;
 
44
import org.hisp.dhis.minmax.MinMaxDataElement;
 
45
import org.hisp.dhis.minmax.MinMaxDataElementService;
 
46
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
47
import org.hisp.dhis.organisationunit.OrganisationUnitService;
 
48
import org.hisp.dhis.period.MonthlyPeriodType;
 
49
import org.hisp.dhis.period.Period;
 
50
import org.hisp.dhis.period.PeriodService;
 
51
import org.junit.Test;
 
52
 
 
53
/**
 
54
 * @author eirikmi
 
55
 * @version $Id: MinMaxOutlierAnalysisServiceTest.java 883 2009-05-15 00:42:45Z daghf $
 
56
 */
 
57
@SuppressWarnings( "unused" )
 
58
public class MinMaxOutlierAnalysisServiceTest
 
59
    extends DhisTest
 
60
{
 
61
    private DataAnalysisService minMaxOutlierAnalysisService;
 
62
 
 
63
    private MinMaxDataElementService minMaxDataElementService;
 
64
 
 
65
    private DataElement dataElementA;
 
66
    private DataElement dataElementB;
 
67
    private DataElement dataElementC;
 
68
    private DataElement dataElementD;
 
69
 
 
70
    private DataValue dataValueA;
 
71
    private DataValue dataValueB;
 
72
 
 
73
    private Set<DataElement> dataElementsA = new HashSet<DataElement>();
 
74
    private Set<DataElement> dataElementsB = new HashSet<DataElement>();
 
75
    private Set<DataElement> dataElementsC = new HashSet<DataElement>();
 
76
 
 
77
    private DataElementCategoryCombo categoryCombo;
 
78
 
 
79
    private DataElementCategoryOptionCombo categoryOptionCombo;
 
80
 
 
81
    private Period periodA;
 
82
    private Period periodB;
 
83
    private Period periodC;
 
84
    private Period periodD;
 
85
    private Period periodE;
 
86
    private Period periodF;
 
87
    private Period periodG;
 
88
    private Period periodH;
 
89
    private Period periodI;
 
90
    private Period periodJ;
 
91
 
 
92
    private OrganisationUnit organisationUnitA;
 
93
 
 
94
    private MinMaxDataElement minMaxDataElement;
 
95
 
 
96
    // ----------------------------------------------------------------------
 
97
    // Fixture
 
98
    // ----------------------------------------------------------------------
 
99
 
 
100
    @Override
 
101
    public void setUpTest()
 
102
        throws Exception
 
103
    {
 
104
        minMaxOutlierAnalysisService = (DataAnalysisService) getBean( "org.hisp.dhis.dataanalysis.MinMaxOutlierAnalysisService" );
 
105
 
 
106
        dataElementService = (DataElementService) getBean( DataElementService.ID );
 
107
 
 
108
        minMaxDataElementService = (MinMaxDataElementService) getBean( MinMaxDataElementService.ID );
 
109
 
 
110
        categoryService = (DataElementCategoryService) getBean( DataElementCategoryService.ID );
 
111
 
 
112
        dataSetService = (DataSetService) getBean( DataSetService.ID );
 
113
 
 
114
        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );
 
115
 
 
116
        dataValueService = (DataValueService) getBean( DataValueService.ID );
 
117
 
 
118
        periodService = (PeriodService) getBean( PeriodService.ID );
 
119
 
 
120
        categoryCombo = categoryService.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
 
121
 
 
122
        dataElementA = createDataElement( 'A', categoryCombo );
 
123
        dataElementB = createDataElement( 'B', categoryCombo );
 
124
        dataElementC = createDataElement( 'C', categoryCombo );
 
125
        dataElementD = createDataElement( 'D', categoryCombo );
 
126
 
 
127
        dataElementService.addDataElement( dataElementA );
 
128
        dataElementService.addDataElement( dataElementB );
 
129
        dataElementService.addDataElement( dataElementC );
 
130
        dataElementService.addDataElement( dataElementD );
 
131
 
 
132
        dataElementsA.add( dataElementA );
 
133
        dataElementsA.add( dataElementB );
 
134
        dataElementsB.add( dataElementC );
 
135
        dataElementsB.add( dataElementD );
 
136
        dataElementsC.add( dataElementB );
 
137
 
 
138
        categoryOptionCombo = categoryCombo.getOptionCombos().iterator().next();
 
139
 
 
140
        periodA = createPeriod( new MonthlyPeriodType(), getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) );
 
141
        periodB = createPeriod( new MonthlyPeriodType(), getDate( 2000, 4, 1 ), getDate( 2000, 4, 30 ) );
 
142
        periodC = createPeriod( new MonthlyPeriodType(), getDate( 2000, 5, 1 ), getDate( 2000, 5, 30 ) );
 
143
        periodD = createPeriod( new MonthlyPeriodType(), getDate( 2000, 6, 1 ), getDate( 2000, 6, 30 ) );
 
144
        periodE = createPeriod( new MonthlyPeriodType(), getDate( 2000, 7, 1 ), getDate( 2000, 7, 30 ) );
 
145
        periodF = createPeriod( new MonthlyPeriodType(), getDate( 2000, 8, 1 ), getDate( 2000, 8, 30 ) );
 
146
        periodG = createPeriod( new MonthlyPeriodType(), getDate( 2000, 9, 1 ), getDate( 2000, 9, 30 ) );
 
147
        periodH = createPeriod( new MonthlyPeriodType(), getDate( 2000, 10, 1 ), getDate( 2000, 10, 30 ) );
 
148
        periodI = createPeriod( new MonthlyPeriodType(), getDate( 2000, 11, 1 ), getDate( 2000, 11, 30 ) );
 
149
        periodJ = createPeriod( new MonthlyPeriodType(), getDate( 2000, 12, 1 ), getDate( 2000, 12, 30 ) );
 
150
 
 
151
        organisationUnitA = createOrganisationUnit( 'A' );
 
152
 
 
153
        organisationUnitService.addOrganisationUnit( organisationUnitA );
 
154
    }
 
155
 
 
156
    @Override
 
157
    public boolean emptyDatabaseAfterTest()
 
158
    {
 
159
        return true;
 
160
    }
 
161
    
 
162
    // ----------------------------------------------------------------------
 
163
    // Business logic tests
 
164
    // ----------------------------------------------------------------------
 
165
 
 
166
    @Test
 
167
    public void testGetFindOutliers()
 
168
    {
 
169
        // testvalues = [5, 5, -5, -5, 10, -10, 13, -13, 41, -41]
 
170
        // mean(testvalues) = 0.0
 
171
        // std(testvalues) = 20.0
 
172
        
 
173
        dataValueA = createDataValue( dataElementA, periodI, organisationUnitA, "41", categoryOptionCombo );
 
174
        dataValueB = createDataValue( dataElementA, periodJ, organisationUnitA, "-41", categoryOptionCombo );
 
175
 
 
176
        dataValueService.addDataValue( createDataValue( dataElementA, periodA, organisationUnitA, "5", categoryOptionCombo ) );
 
177
        dataValueService.addDataValue( createDataValue( dataElementA, periodB, organisationUnitA, "-5", categoryOptionCombo ) );
 
178
        dataValueService.addDataValue( createDataValue( dataElementA, periodC, organisationUnitA, "5", categoryOptionCombo ) );
 
179
        dataValueService.addDataValue( createDataValue( dataElementA, periodD, organisationUnitA, "-5", categoryOptionCombo ) );
 
180
        dataValueService.addDataValue( createDataValue( dataElementA, periodE, organisationUnitA, "10", categoryOptionCombo ) );
 
181
        dataValueService.addDataValue( createDataValue( dataElementA, periodF, organisationUnitA, "-10", categoryOptionCombo ) );
 
182
        dataValueService.addDataValue( createDataValue( dataElementA, periodG, organisationUnitA, "13", categoryOptionCombo ) );
 
183
        dataValueService.addDataValue( createDataValue( dataElementA, periodH, organisationUnitA, "-13", categoryOptionCombo ) );
 
184
        
 
185
        dataValueService.addDataValue( dataValueA );
 
186
        dataValueService.addDataValue( dataValueB );
 
187
        
 
188
        minMaxDataElement = new MinMaxDataElement( organisationUnitA, dataElementA, categoryOptionCombo, -40, 40, false );
 
189
        minMaxDataElementService.addMinMaxDataElement( minMaxDataElement );
 
190
 
 
191
        Collection<Period> periods = new ArrayList<Period>();
 
192
        periods.add( periodI );
 
193
        periods.add( periodJ );
 
194
        periods.add( periodA );
 
195
        periods.add( periodE );
 
196
 
 
197
        //Collection<DeflatedDataValue> result = minMaxOutlierAnalysisService.findOutliers( organisationUnitA, dataElementsA, periods, null );
 
198
 
 
199
        //assertEquals( 2, result.size() );
 
200
    }
 
201
}