← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 13447: Data approval, improved unit testing

 

------------------------------------------------------------
revno: 13447
committer: Lars Helge Øverland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-12-27 15:02:54 +0100
message:
  Data approval, improved unit testing
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementCategoryStoreTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java
  dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.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-api/src/main/java/org/hisp/dhis/user/UserCredentials.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java	2013-12-27 12:17:16 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/user/UserCredentials.java	2013-12-27 14:02:54 +0000
@@ -184,10 +184,9 @@
         
         final Set<String> auths = getAllAuthorities();
         
-        return auths.contains( UserAuthorityGroup.AUTHORITY_ALL ) || auths.contains( auths );
+        return auths.contains( UserAuthorityGroup.AUTHORITY_ALL ) || auths.contains( auth );
     }
     
-
     /**
      * Indicates whether this user credentials is a super user, implying that the
      * ALL authority is present in at least one of the user authority groups of

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java	2013-12-27 12:17:16 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/dataapproval/DefaultDataApprovalService.java	2013-12-27 14:02:54 +0000
@@ -149,14 +149,14 @@
     {
         User user = currentUserService.getCurrentUser();
         
-        boolean mayApprove = user != null && user.getUserCredentials().isAuthorized( DataApproval.AUTH_APPROVE );
+        boolean mayApprove = ( user != null && user.getUserCredentials().isAuthorized( DataApproval.AUTH_APPROVE ) );
         
         if ( mayApprove && user.getOrganisationUnits().contains( organisationUnit ) )
         {
             return true;
         }
 
-        boolean mayApproveAtLowerLevels = user != null && user.getUserCredentials().isAuthorized( DataApproval.AUTH_APPROVE_LOWER_LEVELS );
+        boolean mayApproveAtLowerLevels = ( user != null && user.getUserCredentials().isAuthorized( DataApproval.AUTH_APPROVE_LOWER_LEVELS ) );
         
         if ( mayApproveAtLowerLevels && CollectionUtils.containsAny( user.getOrganisationUnits(), organisationUnit.getAncestors() ) )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceTest.java	2013-12-27 12:17:16 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataapproval/DataApprovalServiceTest.java	2013-12-27 14:02:54 +0000
@@ -34,8 +34,11 @@
 import static org.junit.Assert.fail;
 
 import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
 
 import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.common.IdentifiableObjectManager;
 import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataset.DataSet;
@@ -71,9 +74,6 @@
     private DataSetService dataSetService;
     
     @Autowired
-    private UserService userService;
-
-    @Autowired
     private OrganisationUnitService organisationUnitService;
     
     // -------------------------------------------------------------------------
@@ -109,6 +109,9 @@
     @Override
     public void setUpTest() throws Exception
     {
+        identifiableObjectManager = (IdentifiableObjectManager) getBean( IdentifiableObjectManager.ID );
+        userService = (UserService) getBean( UserService.ID );
+        
         // ---------------------------------------------------------------------
         // Add supporting data
         // ---------------------------------------------------------------------
@@ -298,32 +301,34 @@
     }
 
     @Test
-    @Ignore
     public void testMayApprove() throws Exception
     {
-        userB.addOrganisationUnit( organisationUnitB );
-
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitA ) );
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitB ) );
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitC ) );
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitD ) );
-
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitA ) );
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitB ) );
-        assertEquals( true, dataApprovalService.mayApprove( organisationUnitC ) );
-        assertEquals( true, dataApprovalService.mayApprove( organisationUnitD ) );
-
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitA ) );
-        assertEquals( true, dataApprovalService.mayApprove( organisationUnitB ) );
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitC ) );
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitD ) );
-
-        assertEquals( false, dataApprovalService.mayApprove( organisationUnitA ) );
-        assertEquals( true, dataApprovalService.mayApprove( organisationUnitB ) );
-        assertEquals( true, dataApprovalService.mayApprove( organisationUnitC ) );
-        assertEquals( true, dataApprovalService.mayApprove( organisationUnitD ) );
-    }
-
+        Set<OrganisationUnit> units = new HashSet<OrganisationUnit>();
+        units.add( organisationUnitB );        
+        createUserAndInjectSecurityContext( units, false, DataApproval.AUTH_APPROVE );
+
+        assertEquals( false, dataApprovalService.mayApprove( organisationUnitA ) );
+        assertEquals( true, dataApprovalService.mayApprove( organisationUnitB ) );
+        assertEquals( false, dataApprovalService.mayApprove( organisationUnitC ) );
+        assertEquals( false, dataApprovalService.mayApprove( organisationUnitD ) );
+    }
+
+    @Test
+    public void testMayApproveLowerLevels() throws Exception
+    {
+        Set<OrganisationUnit> units = new HashSet<OrganisationUnit>();
+        units.add( organisationUnitC );
+        createUserAndInjectSecurityContext( units, false, DataApproval.AUTH_APPROVE_LOWER_LEVELS );
+
+        assertEquals( false, dataApprovalService.mayApprove( organisationUnitB ) );
+        assertEquals( false, dataApprovalService.mayApprove( organisationUnitC ) );
+        assertEquals( true, dataApprovalService.mayApprove( organisationUnitD ) );
+    }
+    
+    //TODO better test coverage
+    //TODO split testMayUnapprove into multiple tests with an injected user in each
+    //TODO remove ignore from testMayUnapprove
+    
     @Test
     @Ignore
     public void testMayUnapprove() throws Exception

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementCategoryStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementCategoryStoreTest.java	2013-12-20 12:55:20 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementCategoryStoreTest.java	2013-12-27 14:02:54 +0000
@@ -38,7 +38,6 @@
 import java.util.List;
 
 import org.hisp.dhis.DhisSpringTest;
-import org.hisp.dhis.concept.Concept;
 import org.hisp.dhis.common.GenericIdentifiableObjectStore;
 import org.junit.Test;
 
@@ -52,12 +51,6 @@
 {
     private GenericIdentifiableObjectStore<DataElementCategory> categoryStore;
 
-    private Concept conceptA;
-
-    private Concept conceptB;
-
-    private Concept conceptC;
-
     private DataElementCategoryOption categoryOptionA;
 
     private DataElementCategoryOption categoryOptionB;
@@ -83,10 +76,6 @@
 
         categoryStore = (GenericIdentifiableObjectStore<DataElementCategory>) getBean( "org.hisp.dhis.dataelement.CategoryStore" );
 
-        conceptA = new Concept( "ConceptA" );
-        conceptB = new Concept( "ConceptB" );
-        conceptC = new Concept( "ConceptC" );
-
         categoryOptionA = new DataElementCategoryOption( "CategoryOptionA" );
         categoryOptionB = new DataElementCategoryOption( "CategoryOptionB" );
         categoryOptionC = new DataElementCategoryOption( "CategoryOptionC" );
@@ -110,9 +99,9 @@
     @Test
     public void testAddGet()
     {
-        categoryA = new DataElementCategory( "CategoryA", conceptA, categoryOptions );
-        categoryB = new DataElementCategory( "CategoryB", conceptB, categoryOptions );
-        categoryC = new DataElementCategory( "CategoryC", conceptC, categoryOptions );
+        categoryA = new DataElementCategory( "CategoryA", categoryOptions );
+        categoryB = new DataElementCategory( "CategoryB", categoryOptions );
+        categoryC = new DataElementCategory( "CategoryC", categoryOptions );
 
         int idA = categoryStore.save( categoryA );
         int idB = categoryStore.save( categoryB );
@@ -122,10 +111,6 @@
         assertEquals( categoryB, categoryStore.get( idB ) );
         assertEquals( categoryC, categoryStore.get( idC ) );
 
-        assertEquals( "ConceptA", categoryStore.get( idA ).getConcept().getName() );
-        assertEquals( "ConceptB", categoryStore.get( idB ).getConcept().getName() );
-        assertEquals( "ConceptC", categoryStore.get( idC ).getConcept().getName() );
-
         assertEquals( categoryOptions, categoryStore.get( idA ).getCategoryOptions() );
         assertEquals( categoryOptions, categoryStore.get( idB ).getCategoryOptions() );
         assertEquals( categoryOptions, categoryStore.get( idC ).getCategoryOptions() );

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataelement/DataElementStoreTest.java	2013-12-27 14:02:54 +0000
@@ -32,7 +32,6 @@
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -81,23 +80,11 @@
         DataElement dataElementA = createDataElement( 'A' );
         DataElement dataElementB = createDataElement( 'B' );
         DataElement dataElementC = createDataElement( 'C' );
-        DataElement dataElementD = createDataElement( 'A' );
 
         int idA = dataElementStore.save( dataElementA );
         int idB = dataElementStore.save( dataElementB );
         int idC = dataElementStore.save( dataElementC );
 
-        try
-        {
-            // Should give unique constraint violation
-            dataElementStore.save( dataElementD );
-            fail();
-        }
-        catch ( Exception e )
-        {
-            // Expected
-        }
-
         dataElementA = dataElementStore.get( idA );
         assertNotNull( dataElementA );
         assertEquals( idA, dataElementA.getId() );

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java	2013-11-02 16:06:03 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/GeoUtils.java	2013-12-27 14:02:54 +0000
@@ -1,12 +1,42 @@
 package org.hisp.dhis.system.util;
 
+/*
+ * Copyright (c) 2004-2013, 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.awt.geom.Point2D;
 
 import org.geotools.referencing.GeodeticCalculator;
 
+/**
+ * @author Lars Helge Overland
+ */
 public class GeoUtils
-{
-    
+{    
     /**
      * Returns boundaries of a box shape which centre is the point defined by the 
      * given longitude and latitude. The distance between the center point and the
@@ -63,6 +93,5 @@
         calc.setDestinationGeographicPoint( to);
         
         return calc.getOrthodromicDistance();
-    }
-    
+    }    
 }

=== modified file 'dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java'
--- dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2013-12-27 12:47:38 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java	2013-12-27 14:02:54 +0000
@@ -1346,15 +1346,49 @@
      */
     public User createUserAndInjectSecurityContext( boolean allAuth, String... auths )
     {
-        Assert.notNull( identifiableObjectManager );
-        Assert.notNull( userService );
+        return createUserAndInjectSecurityContext( null, allAuth, auths );
+    }
+    
+    /**
+     * Creates a user and injects into the security context with username 
+     * "username". Requires <code>identifiableObjectManager</code> and 
+     * <code>userService</code> to be injected into the test.
+     * 
+     * @param organisationUnits the organisation units of the user.
+     * @param allAuth whether to grant the ALL authority to user.
+     * @param auths authorities to grant to user.
+     * @return the user.
+     */
+    public User createUserAndInjectSecurityContext( Set<OrganisationUnit> organisationUnits, boolean allAuth, String... auths )
+    {
+        Assert.notNull( identifiableObjectManager, "IdentifiableObjectManager must be injected in test" );
+        Assert.notNull( userService, "UserService must be injected in test" );
         
         UserAuthorityGroup userAuthorityGroup = new UserAuthorityGroup();
         userAuthorityGroup.setName( "Superuser" );
-        userAuthorityGroup.getAuthorities().add( "ALL" );
+        
+        if ( allAuth )
+        {
+            userAuthorityGroup.getAuthorities().add( "ALL" );
+        }
+        
+        if ( auths != null )
+        {
+            for ( String auth : auths )
+            {
+                userAuthorityGroup.getAuthorities().add( auth );
+            }
+        }
+        
         identifiableObjectManager.save( userAuthorityGroup );
 
         User user = createUser( 'A' );
+        
+        if ( organisationUnits != null )
+        {
+            user.setOrganisationUnits( organisationUnits );
+        }
+        
         user.getUserCredentials().getUserAuthorityGroups().add( userAuthorityGroup );
         userService.addUser( user );
         user.getUserCredentials().setUser( user );