← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17540: Upgraded to quick 1.9. Fixes issue with duplicates during data import. Added unit tests.

 

------------------------------------------------------------
revno: 17540
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2014-11-19 20:11:32 +0100
message:
  Upgraded to quick 1.9. Fixes issue with duplicates during data import. Added unit tests.
added:
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataElementBatchHandlerTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataValueBatchHandlerTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/datavalueset/dataValueSetE.xml
modified:
  dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/GenericBatchHandler.java
  dhis-2/pom.xml


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc'
=== added directory 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler'
=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataElementBatchHandlerTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataElementBatchHandlerTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataElementBatchHandlerTest.java	2014-11-19 19:11:32 +0000
@@ -0,0 +1,178 @@
+package org.hisp.dhis.jdbc.batchhandler;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.amplecode.quick.BatchHandler;
+import org.amplecode.quick.BatchHandlerFactory;
+import org.hisp.dhis.DhisTest;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Lars Helge Overland
+ * @version $Id: DataElementBatchHandlerTest.java 4999 2008-04-23 15:45:08Z larshelg $
+ */
+public class DataElementBatchHandlerTest
+    extends DhisTest
+{
+    @Autowired
+    private BatchHandlerFactory batchHandlerFactory;
+
+    @Autowired
+    private DataElementService dataElementService;
+
+    @Autowired
+    private DataElementCategoryService categoryService;
+
+    private BatchHandler<DataElement> batchHandler;
+    
+    private DataElementCategoryCombo categoryCombo;
+    
+    private DataElement dataElementA;
+    private DataElement dataElementB;
+    private DataElement dataElementC;    
+
+    // -------------------------------------------------------------------------
+    // Fixture
+    // -------------------------------------------------------------------------
+
+    @Override
+    public void setUpTest()
+    {
+        batchHandler = batchHandlerFactory.createBatchHandler( DataElementBatchHandler.class );
+
+        categoryCombo = categoryService.getDataElementCategoryComboByName( DataElementCategoryCombo.DEFAULT_CATEGORY_COMBO_NAME );
+        
+        batchHandler.init();
+        
+        dataElementA = createDataElement( 'A', categoryCombo );
+        dataElementB = createDataElement( 'B', categoryCombo );
+        dataElementC = createDataElement( 'C', categoryCombo );
+    }
+
+    @Override
+    public void tearDownTest()
+    {
+        batchHandler.flush();
+    }
+    
+    @Override
+    public boolean emptyDatabaseAfterTest()
+    {
+        return true;
+    }
+    
+    // -------------------------------------------------------------------------
+    // Tests
+    // -------------------------------------------------------------------------
+
+    @Test
+    public void testAddObject()
+    {
+        batchHandler.addObject( dataElementA );
+        batchHandler.addObject( dataElementB );
+        batchHandler.addObject( dataElementC );
+        
+        batchHandler.flush();
+                
+        Collection<DataElement> dataElements = dataElementService.getAllDataElements();
+        
+        assertTrue( dataElements.contains( dataElementA ) );
+        assertTrue( dataElements.contains( dataElementB ) );
+        assertTrue( dataElements.contains( dataElementC ) );
+    }
+
+    @Test
+    public void testInsertObject()
+    {
+        int idA = batchHandler.insertObject( dataElementA, true );
+        int idB = batchHandler.insertObject( dataElementB, true );
+        int idC = batchHandler.insertObject( dataElementC, true );
+        
+        assertNotNull( dataElementService.getDataElement( idA ) );
+        assertNotNull( dataElementService.getDataElement( idB ) );
+        assertNotNull( dataElementService.getDataElement( idC ) );
+    }
+
+    @Test
+    public void testInsertWithSpecialCharacters()
+    {
+        dataElementA.setDescription( "'quote'" );
+        dataElementB.setDescription( "\\backslash\\" );
+        dataElementC.setDescription( ";semicolon;" );
+        
+        batchHandler.insertObject( dataElementA, false );
+        batchHandler.insertObject( dataElementB, false );
+        batchHandler.insertObject( dataElementC, false );
+    }
+
+    @Test
+    public void testUpdateObject()
+    {
+        int id = batchHandler.insertObject( dataElementA, true );
+        
+        dataElementA.setId( id );
+        dataElementA.setName( "UpdatedName" );
+        
+        batchHandler.updateObject( dataElementA );
+        
+        assertEquals( "UpdatedName", dataElementService.getDataElement( id ).getName() );
+    }
+
+    @Test
+    public void testGetObjectIdentifier()
+    {
+        int referenceId = dataElementService.addDataElement( dataElementA );
+
+        int retrievedId = batchHandler.getObjectIdentifier( "DataElementA" );
+        
+        assertEquals( referenceId, retrievedId );
+    }
+
+    @Test
+    public void testObjectExists()
+    {
+        dataElementService.addDataElement( dataElementA );
+        
+        assertTrue( batchHandler.objectExists( dataElementA ) );
+        
+        assertFalse( batchHandler.objectExists( dataElementB ) );
+    }
+}

=== added file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataValueBatchHandlerTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataValueBatchHandlerTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/jdbc/batchhandler/DataValueBatchHandlerTest.java	2014-11-19 19:11:32 +0000
@@ -0,0 +1,240 @@
+package org.hisp.dhis.jdbc.batchhandler;
+
+/*
+ * Copyright (c) 2004-2014, University of Oslo
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ * Redistributions of source code must retain the above copyright notice, this
+ * list of conditions and the following disclaimer.
+ *
+ * Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * Neither the name of the HISP project nor the names of its contributors may
+ * be used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.amplecode.quick.BatchHandler;
+import org.amplecode.quick.BatchHandlerFactory;
+import org.hisp.dhis.DhisTest;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.datavalue.DataValue;
+import org.hisp.dhis.datavalue.DataValueService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.period.MonthlyPeriodType;
+import org.hisp.dhis.period.Period;
+import org.hisp.dhis.period.PeriodService;
+import org.hisp.dhis.period.PeriodType;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class DataValueBatchHandlerTest
+    extends DhisTest
+{
+    @Autowired
+    private BatchHandlerFactory batchHandlerFactory;
+
+    @Autowired
+    private DataValueService dataValueService;
+
+    @Autowired
+    private DataElementService dataElementService;
+
+    @Autowired
+    private PeriodService periodService;
+
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private DataElementCategoryService categoryService;
+
+    private BatchHandler<DataValue> batchHandler;
+    
+    private DataElement dataElementA;
+    
+    private DataElementCategoryOptionCombo categoryOptionComboA;
+    
+    private PeriodType periodTypeA;
+    
+    private Period periodA;
+    private Period periodB;
+    
+    private OrganisationUnit unitA;
+    private OrganisationUnit unitB;
+    
+    private DataValue dataValueA;
+    private DataValue dataValueB;
+    private DataValue dataValueC;
+    private DataValue dataValueD;
+    private DataValue dataValueE;
+    private DataValue dataValueF;
+    
+    // -------------------------------------------------------------------------
+    // Fixture
+    // -------------------------------------------------------------------------
+
+    @Override
+    public void setUpTest()
+    {
+        batchHandler = batchHandlerFactory.createBatchHandler( DataValueBatchHandler.class );
+        
+        dataElementA = createDataElement( 'A' );
+        
+        dataElementService.addDataElement( dataElementA );        
+        
+        categoryOptionComboA = categoryService.getDefaultDataElementCategoryOptionCombo();
+        
+        periodTypeA = PeriodType.getPeriodTypeByName( MonthlyPeriodType.NAME );
+        
+        periodA = createPeriod( periodTypeA, getDate( 2000, 1, 1 ), getDate( 2000, 1, 31 ) );
+        periodB = createPeriod( periodTypeA, getDate( 2000, 2, 1 ), getDate( 2000, 2, 28 ) );
+        
+        periodService.addPeriod( periodA );
+        periodService.addPeriod( periodB );
+                
+        unitA = createOrganisationUnit( 'A' );
+        unitB = createOrganisationUnit( 'B' );
+        
+        organisationUnitService.addOrganisationUnit( unitA );
+        organisationUnitService.addOrganisationUnit( unitB );        
+        
+        dataValueA = createDataValue( dataElementA, periodA, unitA, "10", categoryOptionComboA, categoryOptionComboA );
+        dataValueB = createDataValue( dataElementA, periodA, unitB, "10", categoryOptionComboA, categoryOptionComboA );
+        dataValueC = createDataValue( dataElementA, periodB, unitA, "10", categoryOptionComboA, categoryOptionComboA );
+        dataValueD = createDataValue( dataElementA, periodB, unitB, "10", categoryOptionComboA, categoryOptionComboA );
+        dataValueE = createDataValue( dataElementA, periodA, unitB, "10", categoryOptionComboA, categoryOptionComboA ); // Duplicate with 2nd
+        dataValueF = createDataValue( dataElementA, periodB, unitB, "10", categoryOptionComboA, categoryOptionComboA ); // Duplicate with 4th
+        
+        batchHandler.init();
+    }
+
+    @Override
+    public void tearDownTest()
+    {
+        batchHandler.flush();
+    }
+    
+    @Override
+    public boolean emptyDatabaseAfterTest()
+    {
+        return true;
+    }
+    
+    // -------------------------------------------------------------------------
+    // Tests
+    // -------------------------------------------------------------------------
+
+    @Test
+    public void testAddObject()
+    {
+        batchHandler.addObject( dataValueA );
+        batchHandler.addObject( dataValueB );
+        batchHandler.addObject( dataValueC );
+        batchHandler.addObject( dataValueD );
+        
+        batchHandler.flush();
+        
+        Collection<DataValue> values = dataValueService.getAllDataValues();
+        
+        assertNotNull( values );
+        assertEquals( 4, values.size() );
+        
+        assertTrue( values.contains( dataValueA ) );
+        assertTrue( values.contains( dataValueB ) );
+        assertTrue( values.contains( dataValueC ) );
+        assertTrue( values.contains( dataValueD ) );
+    }
+
+    @Test
+    public void testAddObjectDuplicates()
+    {
+        batchHandler.addObject( dataValueA );
+        batchHandler.addObject( dataValueB );
+        batchHandler.addObject( dataValueC );
+        batchHandler.addObject( dataValueD );
+        batchHandler.addObject( dataValueE );
+        batchHandler.addObject( dataValueF );
+        
+        batchHandler.flush();
+        
+        Collection<DataValue> values = dataValueService.getAllDataValues();
+        
+        assertNotNull( values );
+        assertEquals( 4, values.size() );
+        
+        assertTrue( values.contains( dataValueA ) );
+        assertTrue( values.contains( dataValueB ) );
+        assertTrue( values.contains( dataValueC ) );
+        assertTrue( values.contains( dataValueD ) );
+    }
+
+    @Test
+    public void testInsertObject()
+    {
+        batchHandler.insertObject( dataValueA, false );
+        batchHandler.insertObject( dataValueB, false );
+        batchHandler.insertObject( dataValueC, false );
+        batchHandler.insertObject( dataValueD, false );
+        
+        assertNotNull( dataValueService.getDataValue( dataElementA, periodA, unitA, categoryOptionComboA ) );
+        assertNotNull( dataValueService.getDataValue( dataElementA, periodA, unitB, categoryOptionComboA ) );
+        assertNotNull( dataValueService.getDataValue( dataElementA, periodB, unitA, categoryOptionComboA ) );
+        assertNotNull( dataValueService.getDataValue( dataElementA, periodB, unitB, categoryOptionComboA ) );
+    }
+
+    @Test
+    public void testUpdateObject()
+    {
+        batchHandler.insertObject( dataValueA, false );
+        
+        dataValueA.setValue( "20" );
+        
+        batchHandler.updateObject( dataValueA );
+        
+        dataValueA = dataValueService.getDataValue( dataElementA, periodA, unitA, categoryOptionComboA );
+        
+        assertEquals( "20", dataValueA.getValue() );
+    }
+
+    @Test
+    public void testObjectExists()
+    {
+        dataValueService.addDataValue( dataValueA );
+        dataValueService.addDataValue( dataValueC );
+        
+        assertTrue( batchHandler.objectExists( dataValueA ) );
+        assertTrue( batchHandler.objectExists( dataValueC ) );
+        
+        assertFalse( batchHandler.objectExists( dataValueB ) );
+        assertFalse( batchHandler.objectExists( dataValueD ) );
+    }
+}
\ No newline at end of file

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/datavalueset/dataValueSetE.xml'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/datavalueset/dataValueSetE.xml	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/datavalueset/dataValueSetE.xml	2014-11-19 19:11:32 +0000
@@ -0,0 +1,6 @@
+<dataValueSet xmlns="http://dhis2.org/schema/dxf/2.0"; dataSet="pBOMPrpg1QX" completeDate="2012-01-09" attributeOptionCombo="kjuiHgy67hg">
+    <dataValue dataElement="f7n9E0hX8qk" period="201201" orgUnit="DiszpKrYNg8" value="10001" storedBy="john" timestamp="2012-01-01" comment="comment" followup="false"/>
+    <dataValue dataElement="Ix2HsbDMLea" period="201201" orgUnit="DiszpKrYNg8" value="10002" storedBy="john" timestamp="2012-01-02" comment="comment" followup="false"/>
+    <dataValue dataElement="f7n9E0hX8qk" period="201201" orgUnit="DiszpKrYNg8" value="10004" storedBy="john" timestamp="2012-01-01" comment="comment" followup="false"/> 
+    <dataValue dataElement="eY5ehpbEsB7" period="201201" orgUnit="DiszpKrYNg8" value="10003" storedBy="john" timestamp="2012-01-03" comment="comment" followup="false"/>
+</dataValueSet>
\ No newline at end of file

=== modified file 'dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/GenericBatchHandler.java'
--- dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/GenericBatchHandler.java	2014-10-16 06:17:19 +0000
+++ dhis-2/dhis-support/dhis-support-jdbc/src/main/java/org/hisp/dhis/jdbc/batchhandler/GenericBatchHandler.java	2014-11-19 19:11:32 +0000
@@ -74,7 +74,7 @@
     @Override
     protected void setUniqueValues( Object object )
     {
-        throw new UnsupportedOperationException();
+        // Cannot be known
     }
     
     @Override

=== modified file 'dhis-2/pom.xml'
--- dhis-2/pom.xml	2014-11-13 07:43:09 +0000
+++ dhis-2/pom.xml	2014-11-19 19:11:32 +0000
@@ -552,7 +552,7 @@
       <dependency>
         <groupId>org.amplecode</groupId>
         <artifactId>quick</artifactId>
-        <version>1.8</version>
+        <version>1.9</version>
       </dependency>
 	  <dependency>
 	    <groupId>org.amplecode</groupId>