← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12941: Implement unit tests on Program service methods

 

------------------------------------------------------------
revno: 12941
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2013-11-14 16:36:39 +0700
message:
  Implement unit tests on Program service methods
added:
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramStoreTest.java
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramService.java
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationCriteria.java
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramService.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramInstanceStoreTest.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramServiceTest.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/program/ProgramService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramService.java	2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/program/ProgramService.java	2013-11-14 09:36:39 +0000
@@ -84,14 +84,6 @@
     Program getProgramByName( String name );
 
     /**
-     * Returns the {@link Program} with the given code.
-     * 
-     * @param code the code.
-     * @return the Program with the given code, or null if no match.
-     */
-    Program getProgramByCode( String code );
-
-    /**
      * Returns all {@link Program}.
      * 
      * @return a collection of all Program, or an empty collection if there are

=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationCriteria.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationCriteria.java	2013-09-16 17:07:25 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/validation/ValidationCriteria.java	2013-11-14 09:36:39 +0000
@@ -28,16 +28,16 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import org.hisp.dhis.common.BaseIdentifiableObject;
+import org.hisp.dhis.common.DxfNamespaces;
+import org.hisp.dhis.common.view.DetailedView;
+import org.hisp.dhis.common.view.ExportView;
+
 import com.fasterxml.jackson.annotation.JsonProperty;
 import com.fasterxml.jackson.annotation.JsonView;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
 import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
 
-import org.hisp.dhis.common.BaseIdentifiableObject;
-import org.hisp.dhis.common.DxfNamespaces;
-import org.hisp.dhis.common.view.DetailedView;
-import org.hisp.dhis.common.view.ExportView;
-
 /**
  * @author Lars Helge Overland
  * @version $Id$

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramService.java	2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramService.java	2013-11-14 09:36:39 +0000
@@ -183,11 +183,7 @@
         return i18n( i18nService, programStore.getProgramsByDisplayOnAllOrgunit( displayOnAllOrgunit, orgunit ) );
     }
 
-    public Program getProgramByCode( String code )
-    {
-        return i18n( i18nService, programStore.getByCode( code ) );
-    }
-
+    @Override
     public Collection<Program> getProgramsByCurrentUser( OrganisationUnit organisationUnit )
     {
         Collection<Program> programs = new ArrayList<Program>( getProgramsByDisplayOnAllOrgunit( true, null ) );

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramInstanceStoreTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramInstanceStoreTest.java	2013-11-14 08:17:57 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramInstanceStoreTest.java	2013-11-14 09:36:39 +0000
@@ -50,7 +50,7 @@
 /**
  * @author Chau Thu Tran
  * 
- * @version $ ProgramInstanceServiceTest.java Nov 13, 2013 1:34:55 PM $
+ * @version $ ProgramInstanceStoreTest.java Nov 13, 2013 1:34:55 PM $
  */
 public class ProgramInstanceStoreTest
     extends DhisSpringTest

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java	2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramServiceTest.java	2013-11-14 09:36:39 +0000
@@ -29,71 +29,228 @@
  */
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
+import java.util.Collection;
 import java.util.HashSet;
-import java.util.Set;
 
 import org.hisp.dhis.DhisSpringTest;
 import org.hisp.dhis.organisationunit.OrganisationUnit;
 import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.validation.ValidationCriteria;
+import org.hisp.dhis.validation.ValidationCriteriaService;
 import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
 
 /**
  * @author Lars Helge Overland
  * @version $Id$
- */     
+ */
 public class ProgramServiceTest
     extends DhisSpringTest
-{    
+{
+    @Autowired
     private ProgramService programService;
-    
-    private ProgramStageService programStageService;
-    
-    private Set<ProgramStage> programStages = new HashSet<ProgramStage>();
-    
-    private OrganisationUnit organisationUnit;
-    
-    private ProgramStage programStageA;    
-    private ProgramStage programStageB;    
-    private ProgramStage programStageC;
-    
+
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private ValidationCriteriaService validationCriteriaService;
+
+    private OrganisationUnit organisationUnitA;
+
+    private OrganisationUnit organisationUnitB;
+
+    private Program programA;
+
+    private Program programB;
+
+    private Program programC;
+
     @Override
     public void setUpTest()
     {
-        programStageService = (ProgramStageService) getBean( ProgramStageService.ID );
-        
-        organisationUnitService = (OrganisationUnitService) getBean( OrganisationUnitService.ID );       
-        
-        programService = (ProgramService) getBean( ProgramService.ID );
-        
-        organisationUnit = createOrganisationUnit( 'A' );
-        
-        organisationUnitService.addOrganisationUnit( organisationUnit );              
-        
-        programStageA = createProgramStage( 'A', 5 );       
-        programStageB = createProgramStage( 'B', 5 );
-        programStageC = createProgramStage( 'C', 5 );        
-
-        programStageService.saveProgramStage( programStageA );
-        programStageService.saveProgramStage( programStageB );
-        programStageService.saveProgramStage( programStageC );
-        
-        programStages.add( programStageA );
-        programStages.add( programStageB );
-        programStages.add( programStageC );            
-    }
-    
-    @Test
-    public void addGet()
-    {
-        Program programA = createProgram( 'A', programStages, organisationUnit );
-        Program programB = createProgram( 'B', programStages, organisationUnit );
-        
-        int idA = programService.addProgram( programA );
-        int idB = programService.addProgram( programB );
-        
+        organisationUnitA = createOrganisationUnit( 'A' );
+        organisationUnitService.addOrganisationUnit( organisationUnitA );
+
+        organisationUnitB = createOrganisationUnit( 'B' );
+        organisationUnitService.addOrganisationUnit( organisationUnitB );
+
+        programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnitA );
+        programA.setUid( "UID-A" );
+
+        programB = createProgram( 'B', new HashSet<ProgramStage>(), organisationUnitA );
+        programB.setUid( "UID-B" );
+
+        programC = createProgram( 'C', new HashSet<ProgramStage>(), organisationUnitB );
+        programC.setUid( "UID-C" );
+    }
+
+    @Test
+    public void testAddProgram()
+    {
+        int idA = programService.addProgram( programA );
+        int idB = programService.addProgram( programB );
+
+        assertNotNull( programService.getProgram( idA ) );
+        assertNotNull( programService.getProgram( idB ) );
+    }
+
+    @Test
+    public void testUpdateProgram()
+    {
+        int idA = programService.addProgram( programA );
+
+        assertNotNull( programService.getProgram( idA ) );
+
+        programA.setName( "B" );
+        programService.updateProgram( programA );
+
+        assertEquals( "B", programService.getProgram( idA ).getName() );
+    }
+
+    @Test
+    public void testDeleteProgram()
+    {
+        int idA = programService.addProgram( programA );
+        int idB = programService.addProgram( programB );
+
+        assertNotNull( programService.getProgram( idA ) );
+        assertNotNull( programService.getProgram( idB ) );
+
+        programService.deleteProgram( programA );
+
+        assertNull( programService.getProgram( idA ) );
+        assertNotNull( programService.getProgram( idB ) );
+
+        programService.deleteProgram( programB );
+
+        assertNull( programService.getProgram( idA ) );
+        assertNull( programService.getProgram( idB ) );
+    }
+
+    @Test
+    public void testGetProgramById()
+    {
+        int idA = programService.addProgram( programA );
+        int idB = programService.addProgram( programB );
+
         assertEquals( programA, programService.getProgram( idA ) );
         assertEquals( programB, programService.getProgram( idB ) );
-    }    
+    }
+
+    @Test
+    public void testGetProgramByName()
+    {
+        int idA = programService.addProgram( programA );
+
+        assertNotNull( programService.getProgram( idA ) );
+        assertEquals( "ProgramA", programService.getProgramByName( "ProgramA" ).getName() );
+    }
+
+    @Test
+    public void testGetAllPrograms()
+    {
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+
+        assertTrue( equals( programService.getAllPrograms(), programA, programB ) );
+    }
+
+    @Test
+    public void testGetProgramsByOu()
+    {
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+        programService.addProgram( programC );
+
+        Collection<Program> programs = programService.getPrograms( organisationUnitA );
+        assertTrue( equals( programs, programA, programB ) );
+
+        programs = programService.getPrograms( organisationUnitB );
+        assertTrue( equals( programs, programC ) );
+    }
+
+    @Test
+    public void testGetProgramsByCriteria()
+    {
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+
+        ValidationCriteria validationCriteria = createValidationCriteria( 'A', "gender", 0, "F" );
+        validationCriteriaService.saveValidationCriteria( validationCriteria );
+
+        programA.getPatientValidationCriteria().add( validationCriteria );
+        programService.updateProgram( programA );
+
+        programB.getPatientValidationCriteria().add( validationCriteria );
+        programService.updateProgram( programB );
+
+        Collection<Program> programs = programService.getPrograms( validationCriteria );
+        assertEquals( 2, programs.size() );
+        assertTrue( programs.contains( programA ) );
+        assertTrue( programs.contains( programB ) );
+    }
+
+    @Test
+    public void testGetProgramsByType()
+    {
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+
+        programC.setType( Program.SINGLE_EVENT_WITH_REGISTRATION );
+        programService.addProgram( programC );
+
+        Collection<Program> programs = programService.getPrograms( Program.MULTIPLE_EVENTS_WITH_REGISTRATION );
+        assertTrue( equals( programs, programA, programB ) );
+
+        programs = programService.getPrograms( Program.SINGLE_EVENT_WITH_REGISTRATION );
+        assertTrue( equals( programs, programC ) );
+    }
+
+    @Test
+    public void testGetProgramsByTypeOu()
+    {
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+        programService.addProgram( programC );
+
+        Collection<Program> programs = programService.getPrograms( Program.MULTIPLE_EVENTS_WITH_REGISTRATION,
+            organisationUnitA );
+        assertTrue( equals( programs, programA, programB ) );
+    }
+
+    @Test
+    public void testGetProgramByUid()
+    {
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+
+        assertEquals( programA, programService.getProgram( "UID-A" ) );
+        assertEquals( programB, programService.getProgram( "UID-B" ) );
+    }
+
+    @Test
+    public void testGetProgramsByDisplayOnAllOrgunit()
+    {
+        programA.setDisplayOnAllOrgunit( true );
+        programB.setDisplayOnAllOrgunit( true );
+        programC.setDisplayOnAllOrgunit( false );
+
+        programService.addProgram( programA );
+        programService.addProgram( programB );
+        programService.addProgram( programC );
+
+        Collection<Program> programs = programService.getProgramsByDisplayOnAllOrgunit( true, organisationUnitA );
+        assertEquals( 2, programs.size() );
+        assertTrue( programs.contains( programA ) );
+        assertTrue( programs.contains( programB ) );
+
+        programs = programService.getProgramsByDisplayOnAllOrgunit( false, organisationUnitB );
+        assertEquals( 1, programs.size() );
+        assertTrue( programs.contains( programC ) );
+    }
 }
-

=== added file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramStoreTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramStoreTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramStoreTest.java	2013-11-14 09:36:39 +0000
@@ -0,0 +1,136 @@
+/*
+ * 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.
+ */
+
+package org.hisp.dhis.program;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+import java.util.HashSet;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.validation.ValidationCriteriaService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Chau Thu Tran
+ * 
+ * @version $ ProgramStoreTest.java Nov 14, 2013 4:22:27 PM $
+ */
+public class ProgramStoreTest
+    extends DhisSpringTest
+{
+    @Autowired
+    private ProgramStore programStore;
+
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private ValidationCriteriaService validationCriteriaService;
+
+    private OrganisationUnit organisationUnitA;
+
+    private OrganisationUnit organisationUnitB;
+
+    private Program programA;
+
+    private Program programB;
+
+    private Program programC;
+
+    @Override
+    public void setUpTest()
+    {
+        organisationUnitA = createOrganisationUnit( 'A' );
+        organisationUnitService.addOrganisationUnit( organisationUnitA );
+
+        organisationUnitB = createOrganisationUnit( 'B' );
+        organisationUnitService.addOrganisationUnit( organisationUnitB );
+
+        programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnitA );
+        programA.setUid( "UID-A" );
+
+        programB = createProgram( 'B', new HashSet<ProgramStage>(), organisationUnitA );
+        programB.setUid( "UID-B" );
+
+        programC = createProgram( 'C', new HashSet<ProgramStage>(), organisationUnitB );
+        programC.setUid( "UID-C" );
+    }
+
+    @Test
+    public void testGetProgramsByType()
+    {
+        programStore.save( programA );
+        programStore.save( programB );
+
+        programC.setType( Program.SINGLE_EVENT_WITH_REGISTRATION );
+        programStore.save( programC );
+
+        Collection<Program> programs = programStore.getByType( Program.MULTIPLE_EVENTS_WITH_REGISTRATION );
+        assertTrue( equals( programs, programA, programB ) );
+
+        programs = programStore.getByType( Program.SINGLE_EVENT_WITH_REGISTRATION );
+        assertTrue( equals( programs, programC ) );
+    }
+
+    @Test
+    public void testGetProgramsByTypeOu()
+    {
+        programStore.save( programA );
+        programStore.save( programB );
+        programStore.save( programC );
+
+        Collection<Program> programs = programStore.get( Program.MULTIPLE_EVENTS_WITH_REGISTRATION, organisationUnitA );
+        assertTrue( equals( programs, programA, programB ) );
+    }
+
+    @Test
+    public void testGetProgramsByDisplayOnAllOrgunit()
+    {
+        programA.setDisplayOnAllOrgunit( true );
+        programB.setDisplayOnAllOrgunit( true );
+        programC.setDisplayOnAllOrgunit( false );
+
+        programStore.save( programA );
+        programStore.save( programB );
+        programStore.save( programC );
+
+        Collection<Program> programs = programStore.getProgramsByDisplayOnAllOrgunit( true, organisationUnitA );
+        assertEquals( 2, programs.size() );
+        assertTrue( programs.contains( programA ) );
+        assertTrue( programs.contains( programB ) );
+
+        programs = programStore.getProgramsByDisplayOnAllOrgunit( false, organisationUnitB );
+        assertEquals( 1, programs.size() );
+        assertTrue( programs.contains( programC ) );
+    }
+}
\ No newline at end of file