← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 15073: Refactored DataSetServiceTest

 

------------------------------------------------------------
revno: 15073
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2014-04-28 20:51:54 +0200
message:
  Refactored DataSetServiceTest
modified:
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.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-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java	2014-03-18 08:10:10 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataset/DataSetServiceTest.java	2014-04-28 18:51:54 +0000
@@ -46,13 +46,25 @@
 
 /**
  * @author Lars Helge Overland
- * @version $Id$
  */
 public class DataSetServiceTest
     extends DhisSpringTest
 {
     private PeriodType periodType;
 
+    private Period period;
+    
+    private OrganisationUnit unitA;
+    private OrganisationUnit unitB;
+    private OrganisationUnit unitC;
+    private OrganisationUnit unitD;
+    private OrganisationUnit unitE;
+    private OrganisationUnit unitF;
+    
+    // -------------------------------------------------------------------------
+    // Fixture
+    // -------------------------------------------------------------------------
+
     @Override
     public void setUpTest()
         throws Exception
@@ -66,6 +78,23 @@
         periodService = (PeriodService) getBean( PeriodService.ID );
 
         periodType = new MonthlyPeriodType();
+
+        period = createPeriod( periodType, getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) );
+        periodService.addPeriod( period );
+        
+        unitA = createOrganisationUnit( 'A' );
+        unitB = createOrganisationUnit( 'B' );
+        unitC = createOrganisationUnit( 'C' );
+        unitD = createOrganisationUnit( 'D' );
+        unitE = createOrganisationUnit( 'E' );
+        unitF = createOrganisationUnit( 'F' );
+        
+        organisationUnitService.addOrganisationUnit( unitA );
+        organisationUnitService.addOrganisationUnit( unitB );
+        organisationUnitService.addOrganisationUnit( unitC );
+        organisationUnitService.addOrganisationUnit( unitD );
+        organisationUnitService.addOrganisationUnit( unitE );
+        organisationUnitService.addOrganisationUnit( unitF );
     }
 
     // -------------------------------------------------------------------------
@@ -194,13 +223,6 @@
     @Test
     public void testGetDataSetsBySources()
     {
-        OrganisationUnit unitA = createOrganisationUnit( 'A' );
-        OrganisationUnit unitB = createOrganisationUnit( 'B' );
-        OrganisationUnit unitC = createOrganisationUnit( 'C' );
-        organisationUnitService.addOrganisationUnit( unitA );
-        organisationUnitService.addOrganisationUnit( unitB );
-        organisationUnitService.addOrganisationUnit( unitC );
-
         DataSet dataSetA = createDataSet( 'A', periodType );
         DataSet dataSetB = createDataSet( 'B', periodType );
         DataSet dataSetC = createDataSet( 'C', periodType );
@@ -239,40 +261,26 @@
     @Test
     public void testGetSourcesAssociatedWithDataSet()
     {
-        OrganisationUnit sourceA = createOrganisationUnit( 'A' );
-        OrganisationUnit sourceB = createOrganisationUnit( 'B' );
-        OrganisationUnit sourceC = createOrganisationUnit( 'C' );
-        OrganisationUnit sourceD = createOrganisationUnit( 'D' );
-        OrganisationUnit sourceE = createOrganisationUnit( 'E' );
-        OrganisationUnit sourceF = createOrganisationUnit( 'F' );
-
-        organisationUnitService.addOrganisationUnit( sourceA );
-        organisationUnitService.addOrganisationUnit( sourceB );
-        organisationUnitService.addOrganisationUnit( sourceC );
-        organisationUnitService.addOrganisationUnit( sourceD );
-        organisationUnitService.addOrganisationUnit( sourceE );
-        organisationUnitService.addOrganisationUnit( sourceF );
-
         DataSet dataSetA = createDataSet( 'A', periodType );
         DataSet dataSetB = createDataSet( 'B', periodType );
 
-        dataSetA.getSources().add( sourceA );
-        dataSetA.getSources().add( sourceB );
-        dataSetA.getSources().add( sourceC );
+        dataSetA.getSources().add( unitA );
+        dataSetA.getSources().add( unitB );
+        dataSetB.getSources().add( unitE );
 
-        dataSetB.getSources().add( sourceC );
-        dataSetB.getSources().add( sourceD );
-        dataSetB.getSources().add( sourceE );
+        dataSetB.getSources().add( unitC );
+        dataSetB.getSources().add( unitD );
+        dataSetB.getSources().add( unitE );
 
         dataSetService.addDataSet( dataSetA );
         dataSetService.addDataSet( dataSetB );
 
         Collection<OrganisationUnit> sources = new HashSet<OrganisationUnit>();
 
-        sources.add( sourceA );
-        sources.add( sourceB );
-        sources.add( sourceD );
-        sources.add( sourceE );
+        sources.add( unitA );
+        sources.add( unitB );
+        sources.add( unitC );
+        sources.add( unitD );
 
         assertEquals( 2, dataSetService.getSourcesAssociatedWithDataSet( dataSetA, sources ) );
         assertEquals( 2, dataSetService.getSourcesAssociatedWithDataSet( dataSetB, sources ) );
@@ -285,21 +293,19 @@
     @Test
     public void testSaveGet()
     {
-        OrganisationUnit unit = createOrganisationUnit( 'A' );
         Period period = periodType.createPeriod();
         DataSet dataSet = createDataSet( 'A', periodType );
 
-        organisationUnitService.addOrganisationUnit( unit );
         dataSetService.addDataSet( dataSet );
 
-        LockException lockException = new LockException( period, unit, dataSet );
+        LockException lockException = new LockException( period, unitA, dataSet );
 
         int id = dataSetService.addLockException( lockException );
 
         lockException = dataSetService.getLockException( id );
 
         assertNotNull( lockException );
-        assertEquals( unit, lockException.getOrganisationUnit() );
+        assertEquals( unitA, lockException.getOrganisationUnit() );
         assertEquals( period, lockException.getPeriod() );
         assertEquals( dataSet, lockException.getDataSet() );
     }
@@ -307,8 +313,6 @@
     @Test
     public void testIsLockedDataElement()
     {
-        OrganisationUnit unit = createOrganisationUnit( 'A' );
-        Period period = createPeriod( periodType, getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) );
         DataSet dataSetA = createDataSet( 'A', periodType );
         DataSet dataSetB = createDataSet( 'B', periodType );
         dataSetA.setExpiryDays( 20 );
@@ -323,8 +327,6 @@
         dataSetA.getDataElements().add( dataElementA );
         dataSetB.getDataElements().add( dataElementA );
 
-        organisationUnitService.addOrganisationUnit( unit );
-        periodService.addPeriod( period );
         dataElementService.addDataElement( dataElementA );
         dataElementService.addDataElement( dataElementB );
         dataSetService.addDataSet( dataSetA );
@@ -334,31 +336,29 @@
         // Expiry days
         // ---------------------------------------------------------------------
 
-        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 1 ) ) );
-        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 5 ) ) );
-        assertTrue( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 15 ) ) );
-        assertTrue( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 25 ) ) );
-        assertFalse( dataSetService.isLocked( dataElementB, period, unit, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 1 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 5 ) ) );
+        assertTrue( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 15 ) ) );
+        assertTrue( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementB, period, unitA, getDate( 2000, 4, 25 ) ) );
 
         // ---------------------------------------------------------------------
         // Lock exception
         // ---------------------------------------------------------------------
 
-        LockException lockException = new LockException( period, unit, dataSetA );
+        LockException lockException = new LockException( period, unitA, dataSetA );
         dataSetService.addLockException( lockException );
 
-        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 1 ) ) );
-        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 5 ) ) );
-        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 15 ) ) );
-        assertFalse( dataSetService.isLocked( dataElementA, period, unit, getDate( 2000, 4, 25 ) ) );
-        assertFalse( dataSetService.isLocked( dataElementB, period, unit, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 1 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 5 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 15 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementA, period, unitA, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataElementB, period, unitA, getDate( 2000, 4, 25 ) ) );
     }
 
     @Test
     public void testIsLockedDataSet()
     {
-        OrganisationUnit unit = createOrganisationUnit( 'A' );
-        Period period = createPeriod( periodType, getDate( 2000, 3, 1 ), getDate( 2000, 3, 31 ) );
         DataSet dataSetA = createDataSet( 'A', periodType );
         DataSet dataSetB = createDataSet( 'B', periodType );
         dataSetA.setExpiryDays( 10 );
@@ -366,8 +366,6 @@
         dataSetB.setExpiryDays( 15 );
         dataSetB.setTimelyDays( 15 );
 
-        organisationUnitService.addOrganisationUnit( unit );
-        periodService.addPeriod( period );
         dataSetService.addDataSet( dataSetA );
         dataSetService.addDataSet( dataSetB );
 
@@ -375,25 +373,25 @@
         // Expiry days
         // ---------------------------------------------------------------------
 
-        assertFalse( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 1 ) ) );
-        assertFalse( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 5 ) ) );
-        assertTrue( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 15 ) ) );
-        assertTrue( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 25 ) ) );
-        assertFalse( dataSetService.isLocked( dataSetB, period, unit, null, getDate( 2000, 4, 10 ) ) );
-        assertTrue( dataSetService.isLocked( dataSetB, period, unit, null, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 1 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 5 ) ) );
+        assertTrue( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 15 ) ) );
+        assertTrue( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetB, period, unitA, null, getDate( 2000, 4, 10 ) ) );
+        assertTrue( dataSetService.isLocked( dataSetB, period, unitA, null, getDate( 2000, 4, 25 ) ) );
 
         // ---------------------------------------------------------------------
         // Lock exception
         // ---------------------------------------------------------------------
 
-        LockException lockException = new LockException( period, unit, dataSetA );
+        LockException lockException = new LockException( period, unitA, dataSetA );
         dataSetService.addLockException( lockException );
 
-        assertFalse( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 1 ) ) );
-        assertFalse( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 5 ) ) );
-        assertFalse( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 15 ) ) );
-        assertFalse( dataSetService.isLocked( dataSetA, period, unit, null, getDate( 2000, 4, 25 ) ) );
-        assertFalse( dataSetService.isLocked( dataSetB, period, unit, null, getDate( 2000, 4, 10 ) ) );
-        assertTrue( dataSetService.isLocked( dataSetB, period, unit, null, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 1 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 5 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 15 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetA, period, unitA, null, getDate( 2000, 4, 25 ) ) );
+        assertFalse( dataSetService.isLocked( dataSetB, period, unitA, null, getDate( 2000, 4, 10 ) ) );
+        assertTrue( dataSetService.isLocked( dataSetB, period, unitA, null, getDate( 2000, 4, 25 ) ) );
     }
 }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.java	2014-04-14 06:52:39 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/user/UserSettingServiceTest.java	2014-04-28 18:51:54 +0000
@@ -32,6 +32,7 @@
 
 import org.hisp.dhis.DhisSpringTest;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * @author Kiran Prakash
@@ -39,31 +40,32 @@
 public class UserSettingServiceTest
     extends DhisSpringTest
 {
+    @Autowired
     private UserSettingService userSettingService;
 
-    private UserStore userStore;
+    @Autowired
+    private UserService userService;
 
+    @Autowired
     private UserCredentialsStore userCredentialStore;
 
+    private User testUser;
+    
     @Override
     protected void setUpTest()
         throws Exception
     {
-        userStore = (UserStore) getBean( UserStore.ID );
-        userService = (UserService) getBean( UserService.ID );
-        userSettingService = (UserSettingService) getBean( UserSettingService.ID );
-        userCredentialStore = (UserCredentialsStore) getBean( UserCredentialsStore.ID );
-    }
-
-    @Test
-    public void testSaveUserPreferences()
-        throws Exception
-    {
-        User testUser = createUser( 'D' );
-        userStore.save( testUser );
+        testUser = createUser( 'D' );
+        userService.addUser( testUser );
         UserCredentials userCredentials = testUser.getUserCredentials();
         userCredentials.setUser( testUser );
         userCredentialStore.addUserCredentials( userCredentials );
+    }
+
+    @Test
+    public void testSaveUserPreferences()
+        throws Exception
+    {
         userSettingService.saveUserSetting( "mykey", "myvalue", "username" );
         UserSetting setting = userCredentialStore.getUserSetting( testUser, "mykey" );
         assertEquals( "myvalue", setting.getValue() );