← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 21832: adding basic tests for imports (no real implementation of importer yet)

 

------------------------------------------------------------
revno: 21832
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2016-01-29 09:18:42 +0700
message:
  adding basic tests for imports (no real implementation of importer yet)
added:
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant1.json
  dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant2.json
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataImportService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataImportParams.java


--
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/metadata2/DefaultMetadataImportService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataImportService.java	2016-01-28 09:41:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/DefaultMetadataImportService.java	2016-01-29 02:18:42 +0000
@@ -28,8 +28,17 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.preheat.PreheatService;
+import org.hisp.dhis.schema.SchemaService;
+import org.hisp.dhis.user.CurrentUserService;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -38,10 +47,33 @@
 @Component
 public class DefaultMetadataImportService implements MetadataImportService
 {
+    private static final Log log = LogFactory.getLog( MetadataImportService.class );
+
+    @Autowired
+    private SchemaService schemaService;
+
+    @Autowired
+    private PreheatService preheatService;
+
+    @Autowired
+    private CurrentUserService currentUserService;
+
+    @Autowired
+    private IdentifiableObjectManager manager;
+
     @Override
     public void importMetadata( MetadataImportParams params )
     {
+        if ( params.getUser() == null )
+        {
+            params.setUser( currentUserService.getCurrentUser() );
+        }
 
+        for ( Class<? extends IdentifiableObject> klass : params.getClasses() )
+        {
+            List<? extends IdentifiableObject> objects = params.getObjects( klass );
+            objects.forEach( this::importObject );
+        }
     }
 
     @Override
@@ -56,4 +88,13 @@
         MetadataImportParams params = new MetadataImportParams();
         return params;
     }
+
+    //------------------------------------------------------------------------------------------------
+    // Helpers
+    //------------------------------------------------------------------------------------------------
+
+    private void importObject( IdentifiableObject object )
+    {
+        manager.save( object );
+    }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataImportParams.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataImportParams.java	2016-01-28 09:41:29 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/metadata2/MetadataImportParams.java	2016-01-29 02:18:42 +0000
@@ -29,7 +29,9 @@
  */
 
 import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.importexport.ImportStrategy;
 import org.hisp.dhis.preheat.PreheatMode;
+import org.hisp.dhis.user.User;
 
 import java.util.ArrayList;
 import java.util.HashMap;
@@ -39,11 +41,15 @@
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
-public class MetadataImportParams<T extends IdentifiableObject>
+public class MetadataImportParams
 {
     private PreheatMode preheatMode = PreheatMode.ALL;
 
-    private Map<Class<T>, List<T>> objects = new HashMap<>();
+    private ImportStrategy importStrategy = ImportStrategy.CREATE_AND_UPDATE;
+
+    private User user;
+
+    private Map<Class<? extends IdentifiableObject>, List<IdentifiableObject>> objects = new HashMap<>();
 
     public MetadataImportParams()
     {
@@ -59,37 +65,56 @@
         this.preheatMode = preheatMode;
     }
 
+    public ImportStrategy getImportStrategy()
+    {
+        return importStrategy;
+    }
+
+    public void setImportStrategy( ImportStrategy importStrategy )
+    {
+        this.importStrategy = importStrategy;
+    }
+
+    public User getUser()
+    {
+        return user;
+    }
+
+    public void setUser( User user )
+    {
+        this.user = user;
+    }
+
     public List<Class<? extends IdentifiableObject>> getClasses()
     {
         return new ArrayList<>( objects.keySet() );
     }
 
-    public List<T> getObjects( Class<T> klass )
+    public List<? extends IdentifiableObject> getObjects( Class<? extends IdentifiableObject> klass )
     {
         return objects.get( klass );
     }
 
-    @SuppressWarnings( "unchecked" )
-    public MetadataImportParams addObject( T object )
+    public MetadataImportParams addObject( IdentifiableObject object )
     {
         if ( object == null )
         {
             return this;
         }
 
-        Class<T> klass = (Class<T>) object.getClass();
+        Class<? extends IdentifiableObject> klass = object.getClass();
 
         if ( !objects.containsKey( klass ) )
         {
             objects.put( klass, new ArrayList<>() );
         }
 
-        objects.get( klass ).add( object );
+        objects.get( klass ).add( klass.cast( object ) );
 
         return this;
     }
 
-    public MetadataImportParams addObjects( List<T> objects )
+    public MetadataImportParams addObjects( List<? extends IdentifiableObject> objects )
     {
         objects.forEach( this::addObject );
         return this;

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/metadata2/MetadataImportServiceTest.java	2016-01-29 02:18:42 +0000
@@ -0,0 +1,82 @@
+package org.hisp.dhis.dxf2.metadata2;
+
+/*
+ * Copyright (c) 2004-2016, 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 org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObject;
+import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.constant.Constant;
+import org.hisp.dhis.dxf2.render.RenderService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.core.io.ClassPathResource;
+
+import java.io.IOException;
+
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public class MetadataImportServiceTest
+    extends DhisSpringTest
+{
+    @Autowired
+    private MetadataImportService metadataImportService;
+
+    @Autowired
+    private RenderService renderService;
+
+    @Autowired
+    private IdentifiableObjectManager manager;
+
+    @Test
+    public void testConstantImport() throws IOException
+    {
+        Constant constant1 = fromJson( "dxf2/constant1.json", Constant.class );
+        Constant constant2 = fromJson( "dxf2/constant2.json", Constant.class );
+
+        MetadataImportParams params = new MetadataImportParams();
+        params.addObject( constant1 );
+        params.addObject( constant2 );
+
+        metadataImportService.importMetadata( params );
+
+        constant1 = manager.get( Constant.class, "abcdefghijA" );
+        constant2 = manager.get( Constant.class, "abcdefghijB" );
+
+        assertNotNull( constant1 );
+        assertNotNull( constant2 );
+    }
+
+    private <T extends IdentifiableObject> T fromJson( String path, Class<T> klass ) throws IOException
+    {
+        return renderService.fromJson( new ClassPathResource( path ).getInputStream(), klass );
+    }
+}

=== added directory 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2'
=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant1.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant1.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant1.json	2016-01-29 02:18:42 +0000
@@ -0,0 +1,5 @@
+{
+  "id": "abcdefghijA",
+  "name": "Pi",
+  "value": 3.14
+}
\ No newline at end of file

=== added file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant2.json'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant2.json	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/resources/dxf2/constant2.json	2016-01-29 02:18:42 +0000
@@ -0,0 +1,5 @@
+{
+  "id": "abcdefghijB",
+  "name": "Commodity ordering overhead",
+  "value": 5
+}
\ No newline at end of file