← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12184: made it possible to mock out i18nformat in Abstract{Enrollment, Event}Service. Added basic tests f...

 

------------------------------------------------------------
revno: 12184
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2013-09-20 10:14:40 +0200
message:
  made it possible to mock out i18nformat in Abstract{Enrollment,Event}Service. Added basic tests for EnrollmentService.
modified:
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.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/events/enrollment/AbstractEnrollmentService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2013-09-18 14:03:19 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java	2013-09-20 08:14:40 +0000
@@ -72,7 +72,30 @@
     @Autowired
     private I18nManager i18nManager;
 
-    private I18nFormat format;
+    private I18nFormat _format;
+
+    public void setFormat( I18nFormat format )
+    {
+        this._format = format;
+    }
+
+    public I18nFormat getFormat()
+    {
+        if ( _format != null )
+        {
+            return _format;
+        }
+
+        try
+        {
+            _format = i18nManager.getI18nFormat();
+        }
+        catch ( I18nManagerException ignored )
+        {
+        }
+
+        return _format;
+    }
 
     // -------------------------------------------------------------------------
     // READ
@@ -226,18 +249,6 @@
     @Override
     public ImportSummary saveEnrollment( Enrollment enrollment )
     {
-        try
-        {
-            format = i18nManager.getI18nFormat();
-        }
-        catch ( I18nManagerException ex )
-        {
-            ImportSummary importSummary = new ImportSummary( ImportStatus.ERROR, ex.getMessage() );
-            importSummary.getImportCount().incrementIgnored();
-
-            return importSummary;
-        }
-
         Patient patient = getPatient( enrollment.getPerson() );
         Person person = personService.getPerson( patient );
         Program program = getProgram( enrollment.getProgram() );
@@ -254,7 +265,7 @@
         }
 
         ProgramInstance programInstance = programInstanceService.enrollPatient( patient, program, enrollment.getDateOfEnrollment(), enrollment.getDateOfIncident(),
-            patient.getOrganisationUnit(), format );
+            patient.getOrganisationUnit(), getFormat() );
 
         if ( programInstance == null )
         {
@@ -301,16 +312,8 @@
     @Override
     public void completeEnrollment( Enrollment enrollment )
     {
-        try
-        {
-            format = i18nManager.getI18nFormat();
-        }
-        catch ( I18nManagerException ignored )
-        {
-        }
-
         ProgramInstance programInstance = programInstanceService.getProgramInstance( enrollment.getEnrollment() );
-        programInstanceService.completeProgramInstanceStatus( programInstance, format );
+        programInstanceService.completeProgramInstanceStatus( programInstance, getFormat() );
     }
 
     // -------------------------------------------------------------------------

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2013-09-19 12:37:27 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/event/AbstractEventService.java	2013-09-20 08:14:40 +0000
@@ -115,7 +115,30 @@
     @Autowired
     private I18nManager i18nManager;
 
-    private I18nFormat format;
+    private I18nFormat _format;
+
+    public void setFormat( I18nFormat format )
+    {
+        this._format = format;
+    }
+
+    public I18nFormat getFormat()
+    {
+        if ( _format != null )
+        {
+            return _format;
+        }
+
+        try
+        {
+            _format = i18nManager.getI18nFormat();
+        }
+        catch ( I18nManagerException ignored )
+        {
+        }
+
+        return _format;
+    }
 
     // -------------------------------------------------------------------------
     // CREATE
@@ -123,15 +146,6 @@
 
     protected ImportSummary saveEvent( Event event, ImportOptions importOptions )
     {
-        try
-        {
-            format = i18nManager.getI18nFormat();
-        }
-        catch ( I18nManagerException ex )
-        {
-            return new ImportSummary( ImportStatus.ERROR, ex.getMessage() );
-        }
-
         Program program = programService.getProgram( event.getProgram() );
         ProgramInstance programInstance = null;
         ProgramStage programStage = programStageService.getProgramStage( event.getProgramStage() );
@@ -572,7 +586,7 @@
             return new ImportSummary( ImportStatus.ERROR, "Multi-event programs are not supported right now." );
         }
 
-        Date eventDate = format.parseDate( event.getEventDate() );
+        Date eventDate = getFormat().parseDate( event.getEventDate() );
 
         if ( eventDate == null )
         {

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java	2013-09-19 17:42:17 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/events/EnrollmentServiceTest.java	2013-09-20 08:14:40 +0000
@@ -30,10 +30,26 @@
 
 import org.hisp.dhis.DhisTest;
 import org.hisp.dhis.common.IdentifiableObjectManager;
+import org.hisp.dhis.dxf2.events.enrollment.AbstractEnrollmentService;
+import org.hisp.dhis.dxf2.events.enrollment.Enrollment;
 import org.hisp.dhis.dxf2.events.enrollment.EnrollmentService;
+import org.hisp.dhis.dxf2.importsummary.ImportStatus;
+import org.hisp.dhis.dxf2.importsummary.ImportSummary;
+import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.program.ProgramStage;
 import org.junit.Test;
+import org.mockito.Mockito;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.HashSet;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -46,9 +62,52 @@
     @Autowired
     private IdentifiableObjectManager manager;
 
+    @Autowired
+    private ProgramInstanceService programInstanceService;
+
+    private Patient maleA;
+    private Patient maleB;
+    private Patient femaleA;
+    private Patient femaleB;
+
+    private OrganisationUnit organisationUnitA;
+    private OrganisationUnit organisationUnitB;
+
+    private Program programA;
+
     @Override
     protected void setUpTest() throws Exception
     {
+        organisationUnitA = createOrganisationUnit( 'A' );
+        organisationUnitB = createOrganisationUnit( 'B' );
+
+        organisationUnitB.setParent( organisationUnitA );
+
+        maleA = createPatient( 'A', Patient.MALE, organisationUnitA );
+        maleB = createPatient( 'B', Patient.MALE, organisationUnitB );
+        femaleA = createPatient( 'C', Patient.FEMALE, organisationUnitA );
+        femaleB = createPatient( 'D', Patient.FEMALE, organisationUnitB );
+
+        programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnitA );
+        programA.setUseBirthDateAsEnrollmentDate( true );
+        programA.setUseBirthDateAsIncidentDate( true );
+
+        manager.save( organisationUnitA );
+        manager.save( organisationUnitB );
+        manager.save( maleA );
+        manager.save( maleB );
+        manager.save( femaleA );
+        manager.save( femaleB );
+        manager.save( programA );
+
+        /*
+        programInstanceService.enrollPatient( maleA, programA, null, null, organisationUnitA, null );
+        programInstanceService.enrollPatient( femaleA, programA, null, null, organisationUnitA, null );
+        */
+
+        // mocked format
+        I18nFormat mockFormat = Mockito.mock( I18nFormat.class );
+        ((AbstractEnrollmentService) enrollmentService).setFormat( mockFormat );
     }
 
     @Override
@@ -58,8 +117,19 @@
     }
 
     @Test
-    public void noTest()
+    public void testSaveEnrollment()
     {
-
+        Enrollment enrollment = new Enrollment();
+        enrollment.setPerson( maleA.getUid() );
+        enrollment.setProgram( programA.getUid() );
+
+        ImportSummary importSummary = enrollmentService.saveEnrollment( enrollment );
+        assertEquals( ImportStatus.SUCCESS, importSummary.getStatus() );
+
+        List<Enrollment> enrollments = enrollmentService.getEnrollments( maleA ).getEnrollments();
+
+        assertEquals( 1, enrollments.size() );
+        assertEquals( maleA.getUid(), enrollments.get( 0 ).getPerson() );
+        assertEquals( programA.getUid(), enrollments.get( 0 ).getProgram() );
     }
 }