← Back to team overview

dhis2-devs team mailing list archive

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

 

------------------------------------------------------------
revno: 12933
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2013-11-13 13:34:57 +0700
message:
  Implement unit tests on ProgramExpressionService service methods 
added:
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java
modified:
  dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java
  dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramDataEntryServiceTest.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-patient/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java	2013-08-23 16:05:01 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/program/DefaultProgramExpressionService.java	2013-11-13 06:34:57 +0000
@@ -53,7 +53,7 @@
     implements ProgramExpressionService
 {
     private final String regExp = "\\[" + OBJECT_PROGRAM_STAGE_DATAELEMENT + SEPARATOR_OBJECT + "([a-zA-Z0-9\\- ]+["
-        + SEPARATOR_ID + "[0-9]*]*)" + "\\]";
+        + SEPARATOR_ID + "[0-9]+]*)" + "\\]";
 
     private final String INVALID_CONDITION = "Invalid condition";
 
@@ -74,7 +74,7 @@
     {
         this.programStageService = programStageService;
     }
-    
+
     private DataElementService dataElementService;
 
     public void setDataElementService( DataElementService dataElementService )
@@ -135,25 +135,29 @@
 
             Pattern pattern = Pattern.compile( regExp );
             Matcher matcher = pattern.matcher( programExpression.getExpression() );
+System.out.println("\n\n matcher " + matcher );
             while ( matcher.find() )
             {
                 String key = matcher.group().replaceAll( "[\\[\\]]", "" ).split( SEPARATOR_OBJECT )[1];
-
+System.out.println("\n\n key " + key );                
                 String dataValue = patientDataValueMap.get( key );
-
+System.out.println("\n\n dataValue " + dataValue );        
                 if ( dataValue == null )
                 {
                     return null;
                 }
 
                 matcher.appendReplacement( description, dataValue );
+System.out.println("\n\n description 1 : " + description.toString() );
             }
 
             matcher.appendTail( description );
+System.out.println("\n\n description 2 : " + description.toString() );
+           
             value = description.toString();
         }
+        
         return value;
-
     }
 
     @Override
@@ -183,12 +187,12 @@
             }
 
             matcher.appendReplacement( description,
-                programStage.getDisplayName() + SEPARATOR_ID + dataElement.getName() );
+                "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT
+                    + programStage.getDisplayName() + SEPARATOR_ID + dataElement.getName() + "]" );
         }
 
         matcher.appendTail( description );
 
         return description.toString();
     }
-
 }

=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramDataEntryServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramDataEntryServiceTest.java	2013-11-13 04:19:10 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramDataEntryServiceTest.java	2013-11-13 06:34:57 +0000
@@ -45,7 +45,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 
 /**
- * @author Lars Helge Overland
+ * @author Chau Thu Tran
  * @version $Id$
  */
 public class ProgramDataEntryServiceTest

=== added file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/program/ProgramExpressionServiceTest.java	2013-11-13 06:34:57 +0000
@@ -0,0 +1,240 @@
+package org.hisp.dhis.program;
+
+/*
+ * 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 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.Date;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementService;
+import org.hisp.dhis.dataentryform.DataEntryFormService;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientService;
+import org.hisp.dhis.patientdatavalue.PatientDataValue;
+import org.hisp.dhis.patientdatavalue.PatientDataValueService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Chau Thu Tran
+ * @version $Id$
+ */
+public class ProgramExpressionServiceTest
+    extends DhisSpringTest
+{
+    @Autowired
+    private ProgramExpressionService programExpressionService;
+
+    @Autowired
+    private PatientService patientService;
+
+    @Autowired
+    private OrganisationUnitService organisationUnitService;
+
+    @Autowired
+    private DataElementService dataElementService;
+
+    @Autowired
+    private ProgramService programService;
+
+    @Autowired
+    private ProgramStageDataElementService programStageDataElementService;
+
+    @Autowired
+    private DataEntryFormService dataEntryFormService;
+
+    @Autowired
+    private ProgramStageService programStageService;
+
+    @Autowired
+    private ProgramInstanceService programInstanceService;
+
+    @Autowired
+    private ProgramStageInstanceService programStageInstanceService;
+
+    @Autowired
+    private PatientDataValueService patientDataValueService;
+
+    private ProgramExpression programExpressionA;
+
+    private ProgramExpression programExpressionB;
+
+    private ProgramStageInstance stageInstance;
+
+    private int stageAId;
+
+    private int deAId;
+
+    private int deBId;
+
+    @Override
+    public void setUpTest()
+    {
+        OrganisationUnit organisationUnit = createOrganisationUnit( 'A' );
+        organisationUnitService.addOrganisationUnit( organisationUnit );
+
+        Program program = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnit );
+        programService.addProgram( program );
+
+        ProgramStage stageA = new ProgramStage( "StageA", program );
+        stageAId = programStageService.saveProgramStage( stageA );
+
+        ProgramStage stageB = new ProgramStage( "StageB", program );
+        programStageService.saveProgramStage( stageB );
+
+        Set<ProgramStage> programStages = new HashSet<ProgramStage>();
+        programStages.add( stageA );
+        programStages.add( stageB );
+        program.setProgramStages( programStages );
+        programService.updateProgram( program );
+
+        DataElement dataElementA = createDataElement( 'A' );
+        DataElement dataElementB = createDataElement( 'B' );
+
+        deAId = dataElementService.addDataElement( dataElementA );
+        deBId = dataElementService.addDataElement( dataElementB );
+
+        Patient patient = createPatient( 'A', organisationUnit );
+        patientService.savePatient( patient );
+
+        ProgramInstance programInstance = programInstanceService.enrollPatient( patient, program, new Date(),
+            new Date(), organisationUnit, null );
+        stageInstance = programStageInstanceService.getProgramStageInstance( programInstance, stageA );
+
+        PatientDataValue dataValueA = new PatientDataValue( stageInstance, dataElementA, "1" );
+        PatientDataValue dataValueB = new PatientDataValue( stageInstance, dataElementB, "2" );
+
+        patientDataValueService.savePatientDataValue( dataValueA );
+        patientDataValueService.savePatientDataValue( dataValueB );
+
+        programExpressionA = new ProgramExpression( "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT
+            + ProgramExpression.SEPARATOR_OBJECT + stageAId + "." + deAId + "]", "A" );
+        programExpressionB = new ProgramExpression( "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT
+            + ProgramExpression.SEPARATOR_OBJECT + stageAId + "." + deBId + "]", "B" );
+    }
+
+    @Test
+    public void tesAddProgramExpression()
+    {
+        int idA = programExpressionService.addProgramExpression( programExpressionA );
+        int idB = programExpressionService.addProgramExpression( programExpressionB );
+
+        assertNotNull( programExpressionService.getProgramExpression( idA ) );
+        assertNotNull( programExpressionService.getProgramExpression( idB ) );
+    }
+
+    @Test
+    public void tesUpdateProgramExpression()
+    {
+        int idA = programExpressionService.addProgramExpression( programExpressionA );
+
+        assertNotNull( programExpressionService.getProgramExpression( idA ) );
+
+        programExpressionA.setDescription( "B" );
+        programExpressionService.updateProgramExpression( programExpressionA );
+
+        assertEquals( "B", programExpressionService.getProgramExpression( idA ).getDescription() );
+    }
+
+    @Test
+    public void tesDeleteProgramExpression()
+    {
+        int idA = programExpressionService.addProgramExpression( programExpressionA );
+        int idB = programExpressionService.addProgramExpression( programExpressionB );
+
+        assertNotNull( programExpressionService.getProgramExpression( idA ) );
+        assertNotNull( programExpressionService.getProgramExpression( idB ) );
+
+        programExpressionService.deleteProgramExpression( programExpressionA );
+
+        assertNull( programExpressionService.getProgramExpression( idA ) );
+        assertNotNull( programExpressionService.getProgramExpression( idB ) );
+
+        programExpressionService.deleteProgramExpression( programExpressionB );
+
+        assertNull( programExpressionService.getProgramExpression( idA ) );
+        assertNull( programExpressionService.getProgramExpression( idB ) );
+    }
+
+    @Test
+    public void tesGetProgramExpression()
+    {
+        int idA = programExpressionService.addProgramExpression( programExpressionA );
+        int idB = programExpressionService.addProgramExpression( programExpressionB );
+
+        assertEquals( programExpressionA, programExpressionService.getProgramExpression( idA ) );
+        assertEquals( programExpressionB, programExpressionService.getProgramExpression( idB ) );
+    }
+
+    @Test
+    public void tesGetAllProgramExpressions()
+    {
+        programExpressionService.addProgramExpression( programExpressionA );
+        programExpressionService.addProgramExpression( programExpressionB );
+
+        assertTrue( equals( programExpressionService.getAllProgramExpressions(), programExpressionA, programExpressionB ) );
+    }
+
+    @Test
+    public void tesGetProgramExpressionValue()
+    {
+        programExpressionService.addProgramExpression( programExpressionA );
+
+        Map<String, String> dataValueMap = new HashMap<String, String>();
+        dataValueMap.put( stageAId + "." + deAId, "1" );
+        dataValueMap.put( stageAId + "." + deBId, "2" );
+
+        String value = programExpressionService.getProgramExpressionValue( programExpressionA, stageInstance,
+            dataValueMap );
+
+        assertEquals( "1", value );
+    }
+
+    @Test
+    public void tesGetExpressionDescription()
+    {
+        programExpressionService.addProgramExpression( programExpressionA );
+
+        String actual = programExpressionService.getExpressionDescription( programExpressionA.getExpression() );
+        String expected = "[" + ProgramExpression.OBJECT_PROGRAM_STAGE_DATAELEMENT + ProgramExpression.SEPARATOR_OBJECT
+            + "StageA.DataElementA]";
+        assertEquals( expected, actual );
+    }
+}