← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 17089: Added mock implementations of BatchHandlerFactory and BatchHandler. Using these to speed up DataV...

 

------------------------------------------------------------
revno: 17089
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-10-13 19:59:47 +0200
message:
  Added mock implementations of BatchHandlerFactory and BatchHandler. Using these to speed up DataValueSetServiceTest.
added:
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandler.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandlerFactory.java
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetServiceTest.java
  dhis-2/dhis-support/dhis-support-test/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
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-10-13 10:52:43 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/datavalueset/DefaultDataValueSetService.java	2014-10-13 17:59:47 +0000
@@ -142,6 +142,11 @@
     @Autowired
     private Notifier notifier;
 
+    public void setBatchHandlerFactory( BatchHandlerFactory batchHandlerFactory )
+    {
+        this.batchHandlerFactory = batchHandlerFactory; // Test purpose
+    }
+
     //--------------------------------------------------------------------------
     // DataValueSet implementation
     //--------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetServiceTest.java	2014-10-13 12:28:52 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/datavalueset/DataValueSetServiceTest.java	2014-10-13 17:59:47 +0000
@@ -39,8 +39,7 @@
 import java.io.InputStream;
 import java.util.Collection;
 
-import org.apache.commons.io.IOUtils;
-import org.hisp.dhis.DhisTest;
+import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.dataelement.DataElement;
 import org.hisp.dhis.dataelement.DataElementCategory;
 import org.hisp.dhis.dataelement.DataElementCategoryCombo;
@@ -53,9 +52,11 @@
 import org.hisp.dhis.dataset.DataSet;
 import org.hisp.dhis.dataset.DataSetService;
 import org.hisp.dhis.datavalue.DataValue;
-import org.hisp.dhis.datavalue.DataValueService;
 import org.hisp.dhis.dxf2.importsummary.ImportSummary;
 import org.hisp.dhis.dxf2.metadata.ImportOptions;
+import org.hisp.dhis.jdbc.batchhandler.DataValueBatchHandler;
+import org.hisp.dhis.mock.batchhandler.MockBatchHandler;
+import org.hisp.dhis.mock.batchhandler.MockBatchHandlerFactory;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
 import org.hisp.dhis.period.MonthlyPeriodType;
@@ -70,7 +71,7 @@
  * @author Lars Helge Overland
  */
 public class DataValueSetServiceTest
-    extends DhisTest
+    extends DhisSpringTest
 {
     @Autowired
     private DataElementService dataElementService;
@@ -91,9 +92,6 @@
     private DataValueSetService dataValueSetService;
     
     @Autowired
-    private DataValueService dataValueService;
-    
-    @Autowired
     private CompleteDataSetRegistrationService registrationService;
 
     private DataElementCategoryOptionCombo ocDef;
@@ -115,16 +113,18 @@
     private Period peB;
     
     private InputStream in;
-    
-    @Override
-    public boolean emptyDatabaseAfterTest()
-    {
-        return true;
-    }
+
+    private MockBatchHandler<DataValue> mockDataValueBatchHandler = null;
+    private MockBatchHandlerFactory mockBatchHandlerFactory = null;
     
     @Override
     public void setUpTest()
     {
+        mockDataValueBatchHandler = new MockBatchHandler<>();
+        mockBatchHandlerFactory = new MockBatchHandlerFactory();
+        mockBatchHandlerFactory.registerBatchHandler( DataValueBatchHandler.class, mockDataValueBatchHandler );
+        setDependency( dataValueSetService, "batchHandlerFactory", mockBatchHandlerFactory );
+        
         categoryOptionA = createCategoryOption( 'A' );
         categoryOptionB = createCategoryOption( 'B' );
         categoryA = createDataElementCategory( 'A', categoryOptionA, categoryOptionB );
@@ -180,12 +180,6 @@
         periodService.addPeriod( peB );        
     }
 
-    @Override
-    public void tearDownTest()
-    {
-        IOUtils.closeQuietly( in );
-    }
-
     // -------------------------------------------------------------------------
     // Tests
     // -------------------------------------------------------------------------
@@ -201,7 +195,7 @@
         assertNotNull( summary );
         assertNotNull( summary.getDataValueCount() );
         
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
         
         assertNotNull( dataValues );
         assertEquals( 3, dataValues.size() );
@@ -228,8 +222,8 @@
         
         assertNotNull( summary );
         assertNotNull( summary.getDataValueCount() );
-        
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
+
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
         
         assertNotNull( dataValues );
         assertEquals( 3, dataValues.size() );
@@ -289,8 +283,8 @@
         ImportOptions options = new ImportOptions( UID, UID, true, true, NEW_AND_UPDATES, false );
         
         dataValueSetService.saveDataValueSet( in, options );
-        
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
+
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
         
         assertNotNull( dataValues );
         assertEquals( 0, dataValues.size() );
@@ -305,8 +299,8 @@
         ImportOptions options = new ImportOptions( UID, UID, false, true, UPDATES, false );
         
         dataValueSetService.saveDataValueSet( in, options );
-        
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
+
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
         
         assertNotNull( dataValues );
         assertEquals( 0, dataValues.size() );
@@ -317,8 +311,8 @@
         throws Exception
     {
         dataValueSetService.saveDataValueSet( new ClassPathResource( "datavalueset/dataValueSetC.xml" ).getInputStream() );
-        
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
+
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
         
         assertNotNull( dataValues );
         assertEquals( 3, dataValues.size() );
@@ -331,8 +325,8 @@
         in = new ClassPathResource( "datavalueset/dataValueSetD.xml" ).getInputStream();
         
         dataValueSetService.saveDataValueSet( in );
-        
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
+
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
         
         assertNotNull( dataValues );
         assertEquals( 3, dataValues.size() );
@@ -350,8 +344,8 @@
         assertNotNull( summary );
         assertNotNull( summary.getDataValueCount() );
 
-        Collection<DataValue> dataValues = dataValueService.getAllDataValues();
-
+        Collection<DataValue> dataValues = mockDataValueBatchHandler.getInserts();
+        
         assertNotNull( dataValues );
         assertEquals( 12, dataValues.size() );
         assertTrue( dataValues.contains( new DataValue( deA, peA, ouA, ocDef, ocDef ) ) );
@@ -367,5 +361,4 @@
         assertTrue( dataValues.contains( new DataValue( deC, peB, ouA, ocDef, ocDef ) ) );
         assertTrue( dataValues.contains( new DataValue( deC, peB, ouB, ocDef, ocDef ) ) );        
     }
-    
 }

=== modified file 'dhis-2/dhis-support/dhis-support-test/pom.xml'
--- dhis-2/dhis-support/dhis-support-test/pom.xml	2014-10-02 09:41:32 +0000
+++ dhis-2/dhis-support/dhis-support-test/pom.xml	2014-10-13 17:59:47 +0000
@@ -63,6 +63,10 @@
       <groupId>org.hibernate</groupId>
       <artifactId>hibernate-core</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.amplecode</groupId>
+      <artifactId>quick</artifactId>
+    </dependency>
   </dependencies>
   <properties>
     <rootDir>../../</rootDir>

=== added directory 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler'
=== added file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandler.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandler.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandler.java	2014-10-13 17:59:47 +0000
@@ -0,0 +1,124 @@
+package org.hisp.dhis.mock.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 java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.List;
+
+import org.amplecode.quick.BatchHandler;
+import org.amplecode.quick.JdbcConfiguration;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class MockBatchHandler<T>
+    implements BatchHandler<T>
+{
+    private List<T> inserts = new ArrayList<>();
+    private List<T> updates = new ArrayList<>();
+    private List<T> deletes = new ArrayList<>();
+    
+    @Override
+    public BatchHandler<T> init()
+    {
+        return this;
+    }
+
+    @Override
+    public JdbcConfiguration getConfiguration()
+    {
+        return null;
+    }
+
+    @Override
+    public BatchHandler<T> setTableName( String name )
+    {
+        return this;
+    }
+
+    @Override
+    public void addObject( T object )
+    {
+        inserts.add( object );
+    }
+
+    @Override
+    public int insertObject( T object, boolean returnGeneratedIdentifier )
+    {
+        inserts.add( object );
+        return 0;
+    }
+
+    @Override
+    public void updateObject( T object )
+    {
+        updates.add( object );        
+    }
+
+    @Override
+    public void deleteObject( T object )
+    {
+        deletes.add( object );        
+    }
+
+    @Override
+    public boolean objectExists( T object )
+    {
+        return false;
+    }
+
+    @Override
+    public int getObjectIdentifier( Object object )
+    {
+        return 0;
+    }
+
+    @Override
+    public Collection<Integer> flush()
+    {
+        return new HashSet<>();
+    }
+
+    public List<T> getInserts()
+    {
+        return inserts;
+    }
+
+    public List<T> getUpdates()
+    {
+        return updates;
+    }
+
+    public List<T> getDeletes()
+    {
+        return deletes;
+    }
+}

=== added file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandlerFactory.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandlerFactory.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/mock/batchhandler/MockBatchHandlerFactory.java	2014-10-13 17:59:47 +0000
@@ -0,0 +1,57 @@
+package org.hisp.dhis.mock.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 java.util.HashMap;
+import java.util.Map;
+
+import org.amplecode.quick.BatchHandler;
+import org.amplecode.quick.BatchHandlerFactory;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class MockBatchHandlerFactory
+    implements BatchHandlerFactory
+{
+    private Map<String, BatchHandler<?>> batchHandlers = new HashMap<>();
+    
+    public <T> BatchHandlerFactory registerBatchHandler( Class<? extends BatchHandler<T>> clazz, BatchHandler<T> batchHandler )
+    {
+        batchHandlers.put( clazz.getName(), batchHandler );
+        return this;
+    }
+    
+    @Override
+    @SuppressWarnings("unchecked")
+    public <T> BatchHandler<T> createBatchHandler( Class<? extends BatchHandler<T>> clazz )
+    {
+        return (BatchHandler<T>) batchHandlers.get( clazz.getName() );
+    }
+}