dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #25944
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12882: Implement unit tests on patient service methods.
------------------------------------------------------------
revno: 12882
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2013-11-05 15:07:19 +0700
message:
Implement unit tests on patient service methods.
added:
dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/Patient.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java
dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/person/AbstractPersonService.java
dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java
dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java
dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.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/patient/Patient.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/Patient.java 2013-10-14 12:41:35 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/Patient.java 2013-11-05 08:07:19 +0000
@@ -71,6 +71,9 @@
public static final char AGE_TYPE_MONTH = 'M';
public static final char AGE_TYPE_DAY = 'D';
+
+ public static String SEARCH_SAPERATE = "_";
+
public static String PREFIX_IDENTIFIER_TYPE = "iden";
public static String PREFIX_FIXED_ATTRIBUTE = "fixedAttr";
public static String PREFIX_PATIENT_ATTRIBUTE = "attr";
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java 2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientService.java 2013-11-05 08:07:19 +0000
@@ -113,14 +113,6 @@
Collection<Patient> getPatients( String name, Date birthdate, String gender );
/**
- * Retrieve patients base on birthDate
- *
- * @param birthDate
- * @return Patient List
- */
- Collection<Patient> getPatientsByBirthDate( Date birthDate );
-
- /**
* Retrieve patients by full name (performs partial search )
*
* @param name fullName
@@ -129,9 +121,13 @@
Collection<Patient> getPatientsByNames( String name, Integer min, Integer max );
/**
- * Retrieve patients base on full-name or identifier value
+ * Retrieve patients base on full-name or identifier value with result
+ * limited
*
* @param searchText value
+ * @param min
+ * @param max
+ *
* @return Patient List
*/
Collection<Patient> getPatients( String searchText, Integer min, Integer max );
@@ -141,6 +137,7 @@
*
* @param searchText value
* @param orgUnitId
+ *
* @return Patient List
*/
Collection<Patient> getPatientsForMobile( String searchText, int orgUnitId );
@@ -149,19 +146,14 @@
* Retrieve patients base on organization unit with result limited
*
* @param organisationUnit organisationUnit
+ * @param min
+ * @param max
+ *
* @return Patient List
*/
Collection<Patient> getPatients( OrganisationUnit organisationUnit, Integer min, Integer max );
/**
- * Retrieve patients base on organization unit with result limited
- *
- * @param organisationUnit organisationUnit
- * @return Patient List
- */
- Collection<Patient> getPatients( OrganisationUnit organisationUnit );
-
- /**
* Retrieve patients who enrolled into a program with active status
*
* @param program Program
@@ -178,20 +170,7 @@
* @return
*/
Collection<Patient> getPatients( OrganisationUnit organisationUnit, Program program );
-
- /**
- * Retrieve patients base on organization unit and sort the result by
- * PatientAttribute
- *
- * @param organisationUnit organisationUnit
- * @param patientAttribute
- * @param min
- * @param max
- * @return Patient List
- */
- Collection<Patient> getPatients( OrganisationUnit organisationUnit, PatientAttribute patientAttribute, Integer min,
- Integer max );
-
+
/**
* Retrieve patients base on organisationUnit and identifier value name
*
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java 2013-10-28 07:12:28 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patient/PatientStore.java 2013-11-05 08:07:19 +0000
@@ -49,15 +49,6 @@
final int MAX_RESULTS = 50000;
/**
- * Search patients by birth date
- *
- * @param birthDate Data of birth
- *
- * @return List of patient
- */
- Collection<Patient> getByBirthDate( Date birthDate );
-
- /**
* Search patients by full name (performs partial search )
*
* @param name Full name of patients
@@ -90,8 +81,8 @@
Collection<Patient> getByOrgUnit( OrganisationUnit organisationUnit, Integer min, Integer max );
/**
- * Search patients who registered in a certain organisation unit by full name
- * (performs partial search)
+ * Search patients who registered in a certain organisation unit by full
+ * name (performs partial search)
*
* @param organisationUnit Organisation unit where patients registered
* @param nameLike A string for searching by full name
=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/person/AbstractPersonService.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/person/AbstractPersonService.java 2013-10-15 08:07:02 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/person/AbstractPersonService.java 2013-11-05 08:07:19 +0000
@@ -144,7 +144,7 @@
@Override
public Persons getPersons( OrganisationUnit organisationUnit )
{
- List<Patient> patients = new ArrayList<Patient>( patientService.getPatients( organisationUnit ) );
+ List<Patient> patients = new ArrayList<Patient>( patientService.getPatients( organisationUnit, null, null ) );
return getPersons( patients );
}
=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java 2013-10-23 12:24:18 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/DefaultPatientService.java 2013-11-05 08:07:19 +0000
@@ -202,13 +202,7 @@
{
return patientStore.get( name, birthdate, gender );
}
-
- @Override
- public Collection<Patient> getPatientsByBirthDate( Date birthDate )
- {
- return patientStore.getByBirthDate( birthDate );
- }
-
+
@Override
public Collection<Patient> getPatients( String searchText, Integer min, Integer max )
{
@@ -283,13 +277,7 @@
{
return patientStore.getByOrgUnit( organisationUnit, min, max );
}
-
- @Override
- public Collection<Patient> getPatients( OrganisationUnit organisationUnit )
- {
- return patientStore.getByOrgUnit( organisationUnit, 0, Integer.MAX_VALUE );
- }
-
+
@Override
public Collection<Patient> getPatients( Program program )
{
@@ -303,22 +291,6 @@
}
@Override
- public Collection<Patient> getPatients( OrganisationUnit organisationUnit, PatientAttribute patientAttribute,
- Integer min, Integer max )
- {
- List<Patient> patientList = new ArrayList<Patient>( patientStore.getByOrgUnit( organisationUnit, min, max ) );
-
- if ( patientAttribute != null )
- {
- List<Patient> sortedPatientList = (ArrayList<Patient>) sortPatientsByAttribute( patientList,
- patientAttribute );
- return sortedPatientList.subList( min, max );
- }
-
- return patientList.subList( min, max );
- }
-
- @Override
public Collection<Patient> getPatientsLikeName( OrganisationUnit organisationUnit, String name, Integer min,
Integer max )
{
@@ -424,7 +396,6 @@
List<PatientAttributeValue> valuesForSave, List<PatientAttributeValue> valuesForUpdate,
Collection<PatientAttributeValue> valuesForDelete )
{
-
patientStore.update( patient );
for ( PatientAttributeValue av : valuesForSave )
=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java 2013-10-28 07:12:28 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patient/hibernate/HibernatePatientStore.java 2013-11-05 08:07:19 +0000
@@ -101,14 +101,7 @@
// -------------------------------------------------------------------------
// Implementation methods
// -------------------------------------------------------------------------
-
- @Override
- @SuppressWarnings( "unchecked" )
- public Collection<Patient> getByBirthDate( Date birthDate )
- {
- return getCriteria( Restrictions.eq( "birthDate", birthDate ) ).list();
- }
-
+
@Override
public Collection<Patient> getByNames( String fullName, Integer min, Integer max )
{
@@ -194,8 +187,8 @@
@SuppressWarnings( "unchecked" )
public Collection<Patient> getByProgram( Program program, Integer min, Integer max )
{
- String hql = "select pt from Patient pt " + "inner join pt.programInstances pi "
- + "where pi.program = :program " + "and pi.status = :status";
+ String hql = "select pt from Patient pt inner join pt.programInstances pi "
+ + "where pi.program = :program and pi.status = :status";
Query query = getQuery( hql );
query.setEntity( "program", program );
=== added file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientServiceTest.java 2013-11-05 08:07:19 +0000
@@ -0,0 +1,656 @@
+/*
+ * 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.patient;
+
+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.ArrayList;
+import java.util.Calendar;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.List;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValueService;
+import org.hisp.dhis.program.Program;
+import org.hisp.dhis.program.ProgramInstanceService;
+import org.hisp.dhis.program.ProgramService;
+import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.relationship.RelationshipType;
+import org.hisp.dhis.relationship.RelationshipTypeService;
+import org.hisp.dhis.validation.ValidationCriteria;
+import org.hisp.dhis.validation.ValidationCriteriaService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Chau Thu Tran
+ *
+ * @version $ PatientServiceTest.java Nov 5, 2013 10:35:31 AM $
+ */
+public class PatientServiceTest
+ extends DhisSpringTest
+{
+ @Autowired
+ private PatientService patientService;
+
+ @Autowired
+ private ProgramService programService;
+
+ @Autowired
+ private ProgramInstanceService programInstanceService;
+
+ @Autowired
+ private OrganisationUnitService organisationUnitService;
+
+ @Autowired
+ private PatientAttributeService patientAttributeService;
+
+ @Autowired
+ private PatientIdentifierTypeService identifierTypeService;
+
+ @Autowired
+ private PatientAttributeValueService patientAttributeValueService;
+
+ @Autowired
+ private ValidationCriteriaService validationCriteriaService;
+
+ @Autowired
+ private RelationshipTypeService relationshipTypeService;
+
+ private Patient patientA1;
+
+ private Patient patientA2;
+
+ private Patient patientA3;
+
+ private Patient patientB1;
+
+ private Patient patientB2;
+
+ private PatientAttribute patientAttribute;
+
+ private int attributeId;
+
+ private int identifierTypeId;
+
+ private Program programA;
+
+ private Program programB;
+
+ private OrganisationUnit organisationUnit;
+
+ private Date date = new Date();
+
+ @Override
+ public void setUpTest()
+ {
+ organisationUnit = createOrganisationUnit( 'A' );
+ organisationUnitService.addOrganisationUnit( organisationUnit );
+
+ PatientIdentifierType patientIdentifierType = createPatientIdentifierType( 'A' );
+ identifierTypeId = identifierTypeService.savePatientIdentifierType( patientIdentifierType );
+
+ patientAttribute = createPatientAttribute( 'A' );
+ attributeId = patientAttributeService.savePatientAttribute( patientAttribute );
+
+ patientA1 = createPatient( 'A', "F", organisationUnit );
+ patientA2 = createPatient( 'A', "F", null );
+ patientA3 = createPatient( 'A', organisationUnit, patientIdentifierType );
+ patientB1 = createPatient( 'B', "M", organisationUnit );
+ patientB2 = createPatient( 'B', organisationUnit );
+
+ programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnit );
+ programB = createProgram( 'B', new HashSet<ProgramStage>(), organisationUnit );
+ }
+
+ @Test
+ public void testSavePatient()
+ {
+ int idA = patientService.savePatient( patientA1 );
+ int idB = patientService.savePatient( patientB1 );
+
+ assertNotNull( patientService.getPatient( idA ) );
+ assertNotNull( patientService.getPatient( idB ) );
+ }
+
+ @Test
+ public void testDeletePatient()
+ {
+ int idA = patientService.savePatient( patientA1 );
+ int idB = patientService.savePatient( patientB1 );
+
+ assertNotNull( patientService.getPatient( idA ) );
+ assertNotNull( patientService.getPatient( idB ) );
+
+ patientService.deletePatient( patientA1 );
+
+ assertNull( patientService.getPatient( idA ) );
+ assertNotNull( patientService.getPatient( idB ) );
+
+ patientService.deletePatient( patientB1 );
+
+ assertNull( patientService.getPatient( idA ) );
+ assertNull( patientService.getPatient( idB ) );
+ }
+
+ @Test
+ public void testUpdatePatient()
+ {
+ int idA = patientService.savePatient( patientA1 );
+
+ assertNotNull( patientService.getPatient( idA ) );
+
+ patientA1.setName( "B" );
+ patientService.updatePatient( patientA1 );
+
+ assertEquals( "B", patientService.getPatient( idA ).getName() );
+ }
+
+ @Test
+ public void testGetPatientById()
+ {
+ int idA = patientService.savePatient( patientA1 );
+ int idB = patientService.savePatient( patientB1 );
+
+ assertEquals( patientA1, patientService.getPatient( idA ) );
+ assertEquals( patientB1, patientService.getPatient( idB ) );
+ }
+
+ @Test
+ public void testGetPatientByUid()
+ {
+ patientA1.setUid( "A1" );
+ patientB1.setUid( "B1" );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientB1 );
+
+ assertEquals( patientA1, patientService.getPatient( "A1" ) );
+ assertEquals( patientB1, patientService.getPatient( "B1" ) );
+ }
+
+ @Test
+ public void testGetAllPatients()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientB1 );
+
+ assertTrue( equals( patientService.getAllPatients(), patientA1, patientB1 ) );
+ }
+
+ @Test
+ public void testGetByNameGenderBirthdate()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+
+ Collection<Patient> patients = patientService.getPatients( "NameA", patientA1.getBirthDate(),
+ patientA1.getGender() );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA2 ) );
+ }
+
+ @Test
+ public void testGetPatientsByNames()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+
+ Collection<Patient> patients = patientService.getPatientsByNames( "NameA", null, null );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA2 ) );
+ }
+
+ @Test
+ public void testSearchByLikeNames()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ Collection<Patient> patients = patientService.getPatients( "B", 0, 10 );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientB1 ) );
+ assertTrue( patients.contains( patientB2 ) );
+ }
+
+ @Test
+ public void testGetPatientsByOu()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ Collection<Patient> patients = patientService.getPatients( organisationUnit, null, null );
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testGetPatientsByProgram()
+ {
+ programService.addProgram( programA );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ programInstanceService.enrollPatient( patientA1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA3, programA, date, date, organisationUnit, null );
+
+ Collection<Patient> patients = patientService.getPatients( programA );
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testGetPatientsbyOuProgram()
+ {
+ programService.addProgram( programA );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ programInstanceService.enrollPatient( patientA1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA2, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA3, programA, date, date, organisationUnit, null );
+
+ Collection<Patient> patients = patientService.getPatients( organisationUnit, programA );
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testGetPatientsLikeName()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ Collection<Patient> patients = patientService.getPatientsLikeName( organisationUnit, "A", null, null );
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testGetPatientsByAttributeAndIdentifier()
+ {
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
+ List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+ patientAttributeValues.add( attributeValue );
+
+ patientService.createPatient( patientA3, null, null, patientAttributeValues );
+
+ Collection<Patient> patients = patientService.getPatient( null, attributeId, "AttributeA" );
+
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA3 ) );
+
+
+ patients = patientService.getPatient( identifierTypeId, null, "IdentifierA" );
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testGetPatientsByProgramOu()
+ {
+ programService.addProgram( programA );
+ programService.addProgram( programB );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientB2 );
+
+ programInstanceService.enrollPatient( patientA1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA2, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB2, programB, date, date, organisationUnit, null );
+
+ Collection<Patient> patients = patientService.getPatients( organisationUnit, programA, 0, 100 );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientB1 ) );
+
+ patients = patientService.getPatients( organisationUnit, programB, 0, 100 );
+
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientB2 ) );
+ }
+
+ @Test
+ public void testGetRepresentatives()
+ {
+ patientService.savePatient( patientB1 );
+
+ patientA1.setRepresentative( patientB1 );
+ patientA2.setRepresentative( patientB1 );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+
+ assertEquals( 2, patientService.getRepresentatives( patientB1 ).size() );
+ }
+
+ @Test
+ public void testCountGetPatientsByNameIdentifier()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ assertEquals( 2, patientService.countGetPatients( "b" ) );
+ }
+
+ @Test
+ public void testCountGetPatientsByName()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ assertEquals( 3, patientService.countGetPatientsByName( "a" ) );
+ }
+
+ @Test
+ public void testCreatePatientAndRelative()
+ {
+ int idB = patientService.savePatient( patientB1 );
+
+ RelationshipType relationshipType = createRelationshipType( 'A' );
+ int relationshipTypeId = relationshipTypeService.saveRelationshipType( relationshipType );
+
+ patientA1.setUnderAge( true );
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA1, patientAttribute );
+ List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+ patientAttributeValues.add( attributeValue );
+
+ int idA = patientService.createPatient( patientA1, idB, relationshipTypeId, patientAttributeValues );
+ assertNotNull( patientService.getPatient( idA ) );
+ }
+
+ @Test
+ public void testUpdatePatientAndRelative()
+ {
+ int idB = patientService.savePatient( patientB1 );
+
+ RelationshipType relationshipType = createRelationshipType( 'A' );
+ int relationshipTypeId = relationshipTypeService.saveRelationshipType( relationshipType );
+
+ patientA3.setUnderAge( true );
+ patientA3.setName( "B" );
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
+ List<PatientAttributeValue> patientAttributeValues = new ArrayList<PatientAttributeValue>();
+ patientAttributeValues.add( attributeValue );
+ int idA = patientService.createPatient( patientA3, idB, relationshipTypeId, patientAttributeValues );
+ assertNotNull( patientService.getPatient( idA ) );
+
+ attributeValue.setValue( "AttributeB" );
+ patientAttributeValues = new ArrayList<PatientAttributeValue>();
+ patientAttributeValues.add( attributeValue );
+
+ patientService.updatePatient( patientA3, idB, relationshipTypeId, patientAttributeValues, new ArrayList<PatientAttributeValue>(), new ArrayList<PatientAttributeValue>() );
+ assertEquals( "B", patientService.getPatient( idA ).getName() );
+ }
+
+ @Test
+ public void testCountGetPatientsByOrgUnit()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ assertEquals( 2, patientService.countGetPatientsByOrgUnit( organisationUnit ) );
+ }
+
+ @Test
+ public void testCountGetPatientsByOrgUnitProgram()
+ {
+ programService.addProgram( programA );
+ programService.addProgram( programB );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientB2 );
+
+ programInstanceService.enrollPatient( patientA1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA2, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB2, programB, date, date, organisationUnit, null );
+
+ assertEquals( 2, patientService.countGetPatientsByOrgUnitProgram( organisationUnit, programA ) );
+ assertEquals( 1, patientService.countGetPatientsByOrgUnitProgram( organisationUnit, programB ) );
+ }
+
+ @Test
+ public void testSearchPatients()
+ {
+ int idA = programService.addProgram( programA );
+ programService.addProgram( programB );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
+ patientAttributeValueService.savePatientAttributeValue( attributeValue );
+
+ programInstanceService.enrollPatient( patientA3, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+
+ List<String> searchKeys = new ArrayList<String>();
+ searchKeys.add( Patient.PREFIX_IDENTIFIER_TYPE + Patient.SEARCH_SAPERATE + "a" + Patient.SEARCH_SAPERATE
+ + organisationUnit.getId() );
+ searchKeys.add( Patient.PREFIX_PATIENT_ATTRIBUTE + Patient.SEARCH_SAPERATE + attributeId
+ + Patient.SEARCH_SAPERATE + "a" );
+ searchKeys.add( Patient.PREFIX_PROGRAM + Patient.SEARCH_SAPERATE + idA );
+
+ Collection<OrganisationUnit> orgunits = new HashSet<OrganisationUnit>();
+ orgunits.add( organisationUnit );
+
+ Collection<Patient> patients = patientService.searchPatients( searchKeys, orgunits, null, null, null,
+ ProgramStageInstance.ACTIVE_STATUS, null, null );
+
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testCountSearchPatients()
+ {
+ int idA = programService.addProgram( programA );
+ programService.addProgram( programB );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
+ patientAttributeValueService.savePatientAttributeValue( attributeValue );
+
+ programInstanceService.enrollPatient( patientA3, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+
+ List<String> searchKeys = new ArrayList<String>();
+ searchKeys.add( Patient.PREFIX_IDENTIFIER_TYPE + Patient.SEARCH_SAPERATE + "a" + Patient.SEARCH_SAPERATE
+ + organisationUnit.getId() );
+ searchKeys.add( Patient.PREFIX_PATIENT_ATTRIBUTE + Patient.SEARCH_SAPERATE + attributeId
+ + Patient.SEARCH_SAPERATE + "a" );
+ searchKeys.add( Patient.PREFIX_PROGRAM + Patient.SEARCH_SAPERATE + idA );
+
+ Collection<OrganisationUnit> orgunits = new HashSet<OrganisationUnit>();
+ orgunits.add( organisationUnit );
+
+ assertEquals( 1,
+ patientService.countSearchPatients( searchKeys, orgunits, null, ProgramStageInstance.ACTIVE_STATUS ) );
+ }
+
+ @Test
+ public void testGetPatientPhoneNumbers()
+ {
+ int idA = programService.addProgram( programA );
+ programService.addProgram( programB );
+
+ patientA3.setPhoneNumber( "123456789" );
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
+ patientAttributeValueService.savePatientAttributeValue( attributeValue );
+
+ programInstanceService.enrollPatient( patientA3, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+
+ List<String> searchKeys = new ArrayList<String>();
+ searchKeys.add( Patient.PREFIX_IDENTIFIER_TYPE + Patient.SEARCH_SAPERATE + "a" + Patient.SEARCH_SAPERATE
+ + organisationUnit.getId() );
+ searchKeys.add( Patient.PREFIX_PATIENT_ATTRIBUTE + Patient.SEARCH_SAPERATE + attributeId
+ + Patient.SEARCH_SAPERATE + "a" );
+ searchKeys.add( Patient.PREFIX_PROGRAM + Patient.SEARCH_SAPERATE + idA );
+
+ Collection<OrganisationUnit> orgunits = new HashSet<OrganisationUnit>();
+ orgunits.add( organisationUnit );
+
+ Collection<String> phoneNumbers = patientService.getPatientPhoneNumbers( searchKeys, orgunits, null,
+ ProgramStageInstance.ACTIVE_STATUS, null, null );
+
+ assertEquals( 1, phoneNumbers.size() );
+ }
+
+ @Test
+ public void testGetPatientsByPhone()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientA3 );
+
+ Collection<Patient> patients = patientService.getPatientsByPhone( "123456789", null, null );
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA2 ) );
+ }
+
+ @Test
+ public void testGetPatientByFullname()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ Collection<Patient> patients = patientService.getPatientByFullname( "NameA", organisationUnit );
+
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+
+ patients = patientService.getPatientByFullname( "NameB", organisationUnit );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientB1 ) );
+ assertTrue( patients.contains( patientB2 ) );
+ }
+
+ @Test
+ public void testGetRegistrationOrgunitIds()
+ {
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientA2 );
+ patientService.savePatient( patientB1 );
+ patientService.savePatient( patientB2 );
+
+ Calendar yesterday = Calendar.getInstance();
+ yesterday.add( Calendar.DATE, -1 );
+ Calendar tomorrow = Calendar.getInstance();
+ tomorrow.add( Calendar.DATE, 1 );
+
+ Collection<Integer> orgunitIds = patientService.getRegistrationOrgunitIds( yesterday.getTime(),
+ tomorrow.getTime() );
+
+ assertEquals( 1, orgunitIds.size() );
+ assertEquals( organisationUnit.getId(), orgunitIds.iterator().next().intValue() );
+ }
+
+ @Test
+ public void testValidatePatient()
+ {
+ programService.addProgram( programA );
+
+ ValidationCriteria validationCriteria = createValidationCriteria( 'A', "gender", 0, "F" );
+ validationCriteriaService.saveValidationCriteria( validationCriteria );
+
+ programA.getPatientValidationCriteria().add( validationCriteria );
+ programService.updateProgram( programA );
+
+ patientService.savePatient( patientA1 );
+ patientService.savePatient( patientB1 );
+
+ int validatePatientA1 = patientService.validatePatient( patientA1, programA );
+ int validatePatientB1 = patientService.validatePatient( patientB1, programA );
+
+ assertEquals( 0, validatePatientA1 );
+ assertEquals( 2, validatePatientB1 );
+ }
+}
=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java 2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientStoreTest.java 2013-11-05 08:07:19 +0000
@@ -33,17 +33,24 @@
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
+import java.util.List;
import org.hisp.dhis.DhisSpringTest;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValueService;
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramInstanceService;
import org.hisp.dhis.program.ProgramService;
import org.hisp.dhis.program.ProgramStage;
+import org.hisp.dhis.program.ProgramStageInstance;
+import org.hisp.dhis.validation.ValidationCriteria;
+import org.hisp.dhis.validation.ValidationCriteriaService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
@@ -58,23 +65,45 @@
@Autowired
private ProgramService programService;
-
+
@Autowired
private ProgramInstanceService programInstanceService;
-
+
@Autowired
private OrganisationUnitService organisationUnitService;
-
- private Patient patientA;
- private Patient patientB;
- private Patient patientC;
- private Patient patientD;
-
+
+ @Autowired
+ private PatientAttributeService patientAttributeService;
+
+ @Autowired
+ private PatientIdentifierTypeService identifierTypeService;
+
+ @Autowired
+ private PatientAttributeValueService patientAttributeValueService;
+
+ @Autowired
+ private ValidationCriteriaService validationCriteriaService;
+
+ private Patient patientA1;
+
+ private Patient patientA2;
+
+ private Patient patientA3;
+
+ private Patient patientB1;
+
+ private Patient patientB2;
+
+ private PatientAttribute patientAttribute;
+
+ private int attributeId;
+
private Program programA;
+
private Program programB;
-
+
private OrganisationUnit organisationUnit;
-
+
private Date date = new Date();
@Override
@@ -82,111 +111,118 @@
{
organisationUnit = createOrganisationUnit( 'A' );
organisationUnitService.addOrganisationUnit( organisationUnit );
-
- patientA = createPatient( 'A', organisationUnit );
- patientB = createPatient( 'B', organisationUnit );
- patientC = createPatient( 'A', null );
- patientD = createPatient( 'B', organisationUnit );
-
+
+ PatientIdentifierType patientIdentifierType = createPatientIdentifierType( 'A' );
+ identifierTypeService.savePatientIdentifierType( patientIdentifierType );
+
+ patientAttribute = createPatientAttribute( 'A' );
+ attributeId = patientAttributeService.savePatientAttribute( patientAttribute );
+
+ patientA1 = createPatient( 'A', "F", organisationUnit );
+ patientA2 = createPatient( 'A', "F", null );
+ patientA3 = createPatient( 'A', organisationUnit, patientIdentifierType );
+ patientB1 = createPatient( 'B', "M", organisationUnit );
+ patientB2 = createPatient( 'B', organisationUnit );
+
programA = createProgram( 'A', new HashSet<ProgramStage>(), organisationUnit );
programB = createProgram( 'B', new HashSet<ProgramStage>(), organisationUnit );
}
-
- @Test
- public void addGet()
- {
- int idA = patientStore.save( patientA );
- int idB = patientStore.save( patientB );
-
- assertEquals( patientA.getName(), patientStore.get( idA ).getName() );
- assertEquals( patientB.getName(), patientStore.get( idB ).getName() );
- }
-
- @Test
- public void addGetbyOu()
- {
- int idA = patientStore.save( patientA );
- int idB = patientStore.save( patientB );
-
- assertEquals( patientA.getName(), patientStore.get( idA ).getName() );
- assertEquals( patientB.getName(), patientStore.get( idB ).getName() );
- }
-
- @Test
- public void delete()
- {
- int idA = patientStore.save( patientA );
- int idB = patientStore.save( patientB );
-
- assertNotNull( patientStore.get( idA ) );
- assertNotNull( patientStore.get( idB ) );
-
- patientStore.delete( patientA );
-
- assertNull( patientStore.get( idA ) );
- assertNotNull( patientStore.get( idB ) );
-
- patientStore.delete( patientB );
-
- assertNull( patientStore.get( idA ) );
- assertNull( patientStore.get( idB ) );
- }
-
- @Test
- public void getAll()
- {
- patientStore.save( patientA );
- patientStore.save( patientB );
-
- assertTrue( equals( patientStore.getAll(), patientA, patientB ) );
- }
-
+
+ @Test
+ public void testAddGet()
+ {
+ int idA = patientStore.save( patientA1 );
+ int idB = patientStore.save( patientB1 );
+
+ assertNotNull( patientStore.get( idA ) );
+ assertNotNull( patientStore.get( idB ) );
+ }
+
+ @Test
+ public void testAddGetbyOu()
+ {
+ int idA = patientStore.save( patientA1 );
+ int idB = patientStore.save( patientB1 );
+
+ assertEquals( patientA1.getName(), patientStore.get( idA ).getName() );
+ assertEquals( patientB1.getName(), patientStore.get( idB ).getName() );
+ }
+
+ @Test
+ public void testDelete()
+ {
+ int idA = patientStore.save( patientA1 );
+ int idB = patientStore.save( patientB1 );
+
+ assertNotNull( patientStore.get( idA ) );
+ assertNotNull( patientStore.get( idB ) );
+
+ patientStore.delete( patientA1 );
+
+ assertNull( patientStore.get( idA ) );
+ assertNotNull( patientStore.get( idB ) );
+
+ patientStore.delete( patientB1 );
+
+ assertNull( patientStore.get( idA ) );
+ assertNull( patientStore.get( idB ) );
+ }
+
+ @Test
+ public void testGetAll()
+ {
+ patientStore.save( patientA1 );
+ patientStore.save( patientB1 );
+
+ assertTrue( equals( patientStore.getAll(), patientA1, patientB1 ) );
+ }
+
@Test
public void testGetByFullName()
{
- patientStore.save( patientA );
- patientStore.save( patientB );
- patientStore.save( patientC );
- patientStore.save( patientD );
-
+ patientStore.save( patientA1 );
+ patientStore.save( patientA2 );
+ patientStore.save( patientB1 );
+ patientStore.save( patientB2 );
+
Collection<Patient> patients = patientStore.getByFullName( "NameA", organisationUnit );
-
+
assertEquals( 1, patients.size() );
- assertTrue( patients.contains( patientA ) );
-
+ assertTrue( patients.contains( patientA1 ) );
+
patients = patientStore.getByFullName( "NameB", organisationUnit );
-
+
assertEquals( 2, patients.size() );
- assertTrue( patients.contains( patientB ) );
- assertTrue( patients.contains( patientD ) );
+ assertTrue( patients.contains( patientB1 ) );
+ assertTrue( patients.contains( patientB2 ) );
}
-
+
@Test
public void testGetByOrgUnitProgram()
{
programService.addProgram( programA );
programService.addProgram( programB );
-
- patientStore.save( patientA );
- patientStore.save( patientB );
- patientStore.save( patientC );
- patientStore.save( patientD );
-
- programInstanceService.enrollPatient( patientA, programA, date, date, organisationUnit, null );
- programInstanceService.enrollPatient( patientB, programA, date, date, organisationUnit, null );
- programInstanceService.enrollPatient( patientC, programA, date, date, organisationUnit, null );
- programInstanceService.enrollPatient( patientD, programB, date, date, organisationUnit, null );
-
+
+ patientStore.save( patientA1 );
+ patientStore.save( patientB1 );
+ patientStore.save( patientA2 );
+ patientStore.save( patientB2 );
+
+ programInstanceService.enrollPatient( patientA1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA2, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB2, programB, date, date, organisationUnit, null );
+
Collection<Patient> patients = patientStore.getByOrgUnitProgram( organisationUnit, programA, 0, 100 );
-
+
assertEquals( 2, patients.size() );
- assertTrue( patients.contains( patientA ) );
- assertTrue( patients.contains( patientB ) );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientB1 ) );
patients = patientStore.getByOrgUnitProgram( organisationUnit, programB, 0, 100 );
-
+
assertEquals( 1, patients.size() );
- assertTrue( patients.contains( patientD ) );
+ assertTrue( patients.contains( patientB2 ) );
}
@Test
@@ -194,27 +230,145 @@
{
programService.addProgram( programA );
programService.addProgram( programB );
-
- patientStore.save( patientA );
- patientStore.save( patientB );
- patientStore.save( patientC );
- patientStore.save( patientD );
-
- programInstanceService.enrollPatient( patientA, programA, date, date, organisationUnit, null );
- programInstanceService.enrollPatient( patientB, programA, date, date, organisationUnit, null );
- programInstanceService.enrollPatient( patientC, programA, date, date, organisationUnit, null );
- programInstanceService.enrollPatient( patientD, programB, date, date, organisationUnit, null );
-
+
+ patientStore.save( patientA1 );
+ patientStore.save( patientB1 );
+ patientStore.save( patientA2 );
+ patientStore.save( patientB2 );
+
+ programInstanceService.enrollPatient( patientA1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientA2, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB2, programB, date, date, organisationUnit, null );
+
Collection<Patient> patients = patientStore.getByProgram( programA, 0, 100 );
-
+
assertEquals( 3, patients.size() );
- assertTrue( patients.contains( patientA ) );
- assertTrue( patients.contains( patientB ) );
- assertTrue( patients.contains( patientC ) );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA2 ) );
+ assertTrue( patients.contains( patientB1 ) );
patients = patientStore.getByOrgUnitProgram( organisationUnit, programB, 0, 100 );
-
- assertEquals( 1, patients.size() );
- assertTrue( patients.contains( patientD ) );
- }
+
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientB2 ) );
+ }
+
+ @Test
+ public void testGetByNames()
+ {
+ patientStore.save( patientA1 );
+ patientStore.save( patientA2 );
+
+ Collection<Patient> patients = patientStore.getByNames( "NameA", null, null );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA2 ) );
+ }
+
+ @Test
+ public void testGetByNameGenderBirthdate()
+ {
+ patientStore.save( patientA1 );
+ patientStore.save( patientA2 );
+
+ Collection<Patient> patients = patientStore.get( "NameA", patientA1.getBirthDate(), patientA1.getGender() );
+
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA2 ) );
+ }
+
+ @Test
+ public void testGetByOrgUnitAndNameLike()
+ {
+ patientStore.save( patientA1 );
+ patientStore.save( patientA2 );
+ patientStore.save( patientA3 );
+
+ Collection<Patient> patients = patientStore.getByOrgUnitAndNameLike( organisationUnit, "A", null, null );
+ assertEquals( 2, patients.size() );
+ assertTrue( patients.contains( patientA1 ) );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testGetRepresentatives()
+ {
+ patientStore.save( patientB1 );
+
+ patientA1.setRepresentative( patientB1 );
+ patientA2.setRepresentative( patientB1 );
+ patientStore.save( patientA1 );
+ patientStore.save( patientA2 );
+
+ assertEquals( 2, patientStore.getRepresentatives( patientB1 ).size() );
+ }
+
+ @Test
+ public void testGetByPhoneNumber()
+ {
+ patientStore.save( patientA1 );
+ patientStore.save( patientB1 );
+
+ assertEquals( 2, patientStore.getByPhoneNumber( "123456789", null, null ).size() );
+ }
+
+ @Test
+ public void testSearch()
+ {
+ int idA = programService.addProgram( programA );
+ programService.addProgram( programB );
+
+ patientStore.save( patientA1 );
+ patientStore.save( patientA2 );
+ patientStore.save( patientA3 );
+ patientStore.save( patientB1 );
+ patientStore.save( patientB2 );
+
+ PatientAttributeValue attributeValue = createPatientAttributeValue( 'A', patientA3, patientAttribute );
+ patientAttributeValueService.savePatientAttributeValue( attributeValue );
+
+ programInstanceService.enrollPatient( patientA3, programA, date, date, organisationUnit, null );
+ programInstanceService.enrollPatient( patientB1, programA, date, date, organisationUnit, null );
+
+ List<String> searchKeys = new ArrayList<String>();
+ searchKeys.add( Patient.PREFIX_IDENTIFIER_TYPE + Patient.SEARCH_SAPERATE + "a" + Patient.SEARCH_SAPERATE
+ + organisationUnit.getId() );
+ searchKeys.add( Patient.PREFIX_PATIENT_ATTRIBUTE + Patient.SEARCH_SAPERATE + attributeId
+ + Patient.SEARCH_SAPERATE + "a" );
+ searchKeys.add( Patient.PREFIX_PROGRAM + Patient.SEARCH_SAPERATE + idA );
+
+ Collection<OrganisationUnit> orgunits = new HashSet<OrganisationUnit>();
+ orgunits.add( organisationUnit );
+
+ Collection<Patient> patients = patientStore.search( searchKeys, orgunits, null, null, null,
+ ProgramStageInstance.ACTIVE_STATUS, null, null );
+
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA3 ) );
+ }
+
+ @Test
+ public void testValidate()
+ {
+ programService.addProgram( programA );
+
+ ValidationCriteria validationCriteria = createValidationCriteria( 'A', "gender", 0, "F" );
+ validationCriteriaService.saveValidationCriteria( validationCriteria );
+
+ programA.getPatientValidationCriteria().add( validationCriteria );
+ programService.updateProgram( programA );
+
+ patientStore.save( patientA1 );
+ patientStore.save( patientB1 );
+
+ int validatePatientA1 = patientStore.validate( patientA1, programA );
+ int validatePatientB1 = patientStore.validate( patientB1, programA );
+
+ assertEquals( 0, validatePatientA1 );
+ assertEquals( 2, validatePatientB1 );
+ }
+
}
=== 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-10-16 12:39:47 +0000
+++ dhis-2/dhis-support/dhis-support-test/src/main/java/org/hisp/dhis/DhisConvenienceTest.java 2013-11-05 08:07:19 +0000
@@ -93,6 +93,10 @@
import org.hisp.dhis.organisationunit.OrganisationUnitGroupSet;
import org.hisp.dhis.organisationunit.OrganisationUnitService;
import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientAttribute;
+import org.hisp.dhis.patient.PatientIdentifier;
+import org.hisp.dhis.patient.PatientIdentifierType;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
import org.hisp.dhis.period.MonthlyPeriodType;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodService;
@@ -100,6 +104,7 @@
import org.hisp.dhis.program.Program;
import org.hisp.dhis.program.ProgramStage;
import org.hisp.dhis.program.ProgramStageService;
+import org.hisp.dhis.relationship.RelationshipType;
import org.hisp.dhis.resourcetable.ResourceTableService;
import org.hisp.dhis.sqlview.SqlView;
import org.hisp.dhis.user.User;
@@ -124,9 +129,13 @@
protected static final Log log = LogFactory.getLog( DhisConvenienceTest.class );
protected static final String BASE_UID = "123456789a";
+
protected static final String BASE_IN_UID = "inabcdefgh";
+
protected static final String BASE_DE_UID = "deabcdefgh";
+
protected static final String BASE_DS_UID = "dsabcdefgh";
+
protected static final String BASE_OU_UID = "ouabcdefgh";
private static final String EXT_TEST_DIR = System.getProperty( "user.home" ) + File.separator + "dhis2_test_dir";
@@ -200,10 +209,10 @@
/**
* Creates a date.
- *
- * @param year the year.
+ *
+ * @param year the year.
* @param month the month.
- * @param day the day of month.
+ * @param day the day of month.
* @return a date.
*/
public static Date getDate( int year, int month, int day )
@@ -220,7 +229,7 @@
/**
* Creates a date.
- *
+ *
* @param day the day of the year.
* @return a date.
*/
@@ -239,8 +248,8 @@
* implementation type of the collection in contrast to the native equals
* method. This is useful for black-box testing where one will not know the
* implementation type of the returned collection for a method.
- *
- * @param actual the actual collection to check.
+ *
+ * @param actual the actual collection to check.
* @param reference the reference objects to check against.
* @return true if the collections are equal, false otherwise.
*/
@@ -262,7 +271,8 @@
if ( actual.size() != collection.size() )
{
- log.warn( "Actual collection has different size compared to reference collection: " + actual.size() + " / " + collection.size() );
+ log.warn( "Actual collection has different size compared to reference collection: " + actual.size() + " / "
+ + collection.size() );
return false;
}
@@ -309,10 +319,10 @@
* service; making the test unaware of the implementation and thus
* re-usable. A weakness is that the field name of the dependency must be
* assumed.
- *
+ *
* @param targetService the target service.
- * @param fieldName the name of the dependency field in the target service.
- * @param dependency the dependency.
+ * @param fieldName the name of the dependency field in the target service.
+ * @param dependency the dependency.
*/
protected void setDependency( Object targetService, String fieldName, Object dependency )
{
@@ -329,11 +339,11 @@
* service; making the test unaware of the implementation and thus
* re-usable. A weakness is that the field name of the dependency must be
* assumed.
- *
+ *
* @param targetService the target service.
- * @param fieldName the name of the dependency field in the target service.
- * @param dependency the dependency.
- * @param clazz the class type of the dependency.
+ * @param fieldName the name of the dependency field in the target service.
+ * @param dependency the dependency.
+ * @param clazz the class type of the dependency.
*/
protected void setDependency( Object targetService, String fieldName, Object dependency, Class<?> clazz )
{
@@ -344,7 +354,7 @@
String setMethodName = "set" + fieldName.substring( 0, 1 ).toUpperCase()
+ fieldName.substring( 1, fieldName.length() );
- Class<?>[] argumentClass = new Class<?>[]{ clazz };
+ Class<?>[] argumentClass = new Class<?>[] { clazz };
Method method = targetService.getClass().getMethod( setMethodName, argumentClass );
@@ -357,12 +367,13 @@
}
/**
- * If the given class is advised by Spring AOP it will return the target class,
- * i.e. the advised class. If not the given class is returned unchanged.
- *
+ * If the given class is advised by Spring AOP it will return the target
+ * class, i.e. the advised class. If not the given class is returned
+ * unchanged.
+ *
* @param object the object.
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings( "unchecked" )
private <T> T getRealObject( T object )
throws Exception
{
@@ -400,7 +411,7 @@
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param categoryCombo The category combo.
+ * @param categoryCombo The category combo.
*/
public static DataElement createDataElement( char uniqueCharacter, DataElementCategoryCombo categoryCombo )
{
@@ -413,8 +424,8 @@
}
/**
- * @param uniqueCharacter A unique character to identify the object.
- * @param type The value type.
+ * @param uniqueCharacter A unique character to identify the object.
+ * @param type The value type.
* @param aggregationOperator The aggregation operator.
*/
public static DataElement createDataElement( char uniqueCharacter, String type, String aggregationOperator )
@@ -428,10 +439,10 @@
}
/**
- * @param uniqueCharacter A unique character to identify the object.
- * @param type The value type.
+ * @param uniqueCharacter A unique character to identify the object.
+ * @param type The value type.
* @param aggregationOperator The aggregation operator.
- * @param categoryCombo The category combo.
+ * @param categoryCombo The category combo.
*/
public static DataElement createDataElement( char uniqueCharacter, String type, String aggregationOperator,
DataElementCategoryCombo categoryCombo )
@@ -447,10 +458,9 @@
/**
* @param categoryComboUniqueIdentifier A unique character to identify the
- * category combo.
- * @param categoryOptionUniqueIdentifiers
- * Unique characters to identify the
- * category options.
+ * category combo.
+ * @param categoryOptionUniqueIdentifiers Unique characters to identify the
+ * category options.
* @return DataElementCategoryOptionCombo
*/
public static DataElementCategoryOptionCombo createCategoryOptionCombo( char categoryComboUniqueIdentifier,
@@ -471,11 +481,12 @@
}
/**
- * @param categoryCombo the category combo.
+ * @param categoryCombo the category combo.
* @param categoryOptions the category options.
* @return DataElementCategoryOptionCombo
*/
- public static DataElementCategoryOptionCombo createCategoryOptionCombo( DataElementCategoryCombo categoryCombo, DataElementCategoryOption... categoryOptions )
+ public static DataElementCategoryOptionCombo createCategoryOptionCombo( DataElementCategoryCombo categoryCombo,
+ DataElementCategoryOption... categoryOptions )
{
DataElementCategoryOptionCombo categoryOptionCombo = new DataElementCategoryOptionCombo();
@@ -546,7 +557,7 @@
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param type The type.
+ * @param type The type.
*/
public static Indicator createIndicator( char uniqueCharacter, IndicatorType type )
{
@@ -595,7 +606,7 @@
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param periodType The period type.
+ * @param periodType The period type.
*/
public static DataSet createDataSet( char uniqueCharacter, PeriodType periodType )
{
@@ -632,7 +643,7 @@
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param parent The parent.
+ * @param parent The parent.
*/
public static OrganisationUnit createOrganisationUnit( char uniqueCharacter, OrganisationUnit parent )
{
@@ -674,9 +685,9 @@
}
/**
- * @param type The PeriodType.
+ * @param type The PeriodType.
* @param startDate The start date.
- * @param endDate The end date.
+ * @param endDate The end date.
*/
public static Period createPeriod( PeriodType type, Date startDate, Date endDate )
{
@@ -699,7 +710,7 @@
/**
* @param startDate The start date.
- * @param endDate The end date.
+ * @param endDate The end date.
*/
public static Period createPeriod( Date startDate, Date endDate )
{
@@ -713,14 +724,14 @@
}
/**
- * @param dataElement The data element.
- * @param period The period.
- * @param source The source.
- * @param value The value.
+ * @param dataElement The data element.
+ * @param period The period.
+ * @param source The source.
+ * @param value The value.
* @param categoryOptionCombo The data element category option combo.
*/
- public static DataValue createDataValue( DataElement dataElement, Period period, OrganisationUnit source, String value,
- DataElementCategoryOptionCombo categoryOptionCombo )
+ public static DataValue createDataValue( DataElement dataElement, Period period, OrganisationUnit source,
+ String value, DataElementCategoryOptionCombo categoryOptionCombo )
{
DataValue dataValue = new DataValue();
@@ -737,15 +748,15 @@
}
/**
- * @param dataElement The data element.
- * @param period The period.
- * @param source The source.
- * @param value The value.
- * @param lastupdated The date.
+ * @param dataElement The data element.
+ * @param period The period.
+ * @param source The source.
+ * @param value The value.
+ * @param lastupdated The date.
* @param categoryOptionCombo The data element category option combo.
*/
- public static DataValue createDataValue( DataElement dataElement, Period period, OrganisationUnit source, String value,
- Date lastupdated, DataElementCategoryOptionCombo categoryOptionCombo )
+ public static DataValue createDataValue( DataElement dataElement, Period period, OrganisationUnit source,
+ String value, Date lastupdated, DataElementCategoryOptionCombo categoryOptionCombo )
{
DataValue dataValue = new DataValue();
@@ -763,10 +774,10 @@
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param operator The operator.
- * @param leftSide The left side expression.
- * @param rightSide The right side expression.
- * @param periodType The period-type.
+ * @param operator The operator.
+ * @param leftSide The left side expression.
+ * @param rightSide The right side expression.
+ * @param periodType The period-type.
*/
public static ValidationRule createValidationRule( char uniqueCharacter, Operator operator, Expression leftSide,
Expression rightSide, PeriodType periodType )
@@ -788,15 +799,18 @@
* Creates a ValidationRule of RULE_TYPE_MONITORING
*
* @param uniqueCharacter A unique character to identify the object.
- * @param operator The operator.
- * @param leftSide The left side expression.
- * @param rightSide The right side expression.
- * @param periodType The period-type.
- * @param organisationUnitLevel The unit level of organisations to be evaluated by this rule.
- * @param sequentialSampleCount How many sequential past periods to sample.
- * @param annualSampleCount How many years of past periods to sample.
- * @param highOutliers How many high outlying past samples to discard before averaging.
- * @param lowOutliers How many low outlying past samples to discard before averaging.
+ * @param operator The operator.
+ * @param leftSide The left side expression.
+ * @param rightSide The right side expression.
+ * @param periodType The period-type.
+ * @param organisationUnitLevel The unit level of organisations to be
+ * evaluated by this rule.
+ * @param sequentialSampleCount How many sequential past periods to sample.
+ * @param annualSampleCount How many years of past periods to sample.
+ * @param highOutliers How many high outlying past samples to discard before
+ * averaging.
+ * @param lowOutliers How many low outlying past samples to discard before
+ * averaging.
*/
public static ValidationRule createMonitoringRule( char uniqueCharacter, Operator operator, Expression leftSide,
Expression rightSide, PeriodType periodType, int organisationUnitLevel, int sequentialSampleCount,
@@ -836,10 +850,10 @@
}
/**
- * @param uniqueCharacter A unique character to identify the object.
- * @param expressionString The expression string.
+ * @param uniqueCharacter A unique character to identify the object.
+ * @param expressionString The expression string.
* @param dataElementsInExpression A collection of the data elements
- * entering into the expression.
+ * entering into the expression.
*/
public static Expression createExpression( char uniqueCharacter, String expressionString,
Set<DataElement> dataElementsInExpression, Set<DataElementCategoryOptionCombo> optionCombosInExpression )
@@ -855,12 +869,12 @@
}
/**
- * @param dataElementId The data element identifier.
+ * @param dataElementId The data element identifier.
* @param categoryOptionComboId The data element category option combo
- * identifier.
- * @param periodId The period identifier.
- * @param sourceId The source identifier.
- * @param status The status.
+ * identifier.
+ * @param periodId The period identifier.
+ * @param sourceId The source identifier.
+ * @param status The status.
*/
public static ImportDataValue createImportDataValue( int dataElementId, int categoryOptionComboId, int periodId,
int sourceId, ImportObjectStatus status )
@@ -910,7 +924,8 @@
chart.setIndicators( indicators );
chart.setPeriods( periods );
chart.setOrganisationUnits( units );
- chart.setDimensions( DimensionalObject.DATA_X_DIM_ID, DimensionalObject.PERIOD_DIM_ID, DimensionalObject.ORGUNIT_DIM_ID );
+ chart.setDimensions( DimensionalObject.DATA_X_DIM_ID, DimensionalObject.PERIOD_DIM_ID,
+ DimensionalObject.ORGUNIT_DIM_ID );
return chart;
}
@@ -990,6 +1005,7 @@
patient.setGender( Patient.MALE );
patient.setDobType( Patient.DOB_TYPE_VERIFIED );
patient.setBirthDate( getDate( 1970, 1, 1 ) );
+ patient.setPhoneNumber( "123456789" );
patient.setRegistrationDate( new Date() );
patient.setOrganisationUnit( organisationUnit );
@@ -1005,10 +1021,74 @@
patient.setGender( gender );
patient.setDobType( Patient.DOB_TYPE_VERIFIED );
patient.setBirthDate( getDate( 1970, 1, 1 ) );
- patient.setRegistrationDate( new Date() );
- patient.setOrganisationUnit( organisationUnit );
-
- return patient;
+ patient.setPhoneNumber( "123456789" );
+ patient.setRegistrationDate( new Date() );
+ patient.setOrganisationUnit( organisationUnit );
+
+ return patient;
+ }
+
+ public static Patient createPatient( char uniqueChar, OrganisationUnit organisationUnit,
+ PatientIdentifierType patientIdentifierType )
+ {
+ Patient patient = new Patient();
+ patient.setAutoFields();
+
+ patient.setName( "Name" + uniqueChar );
+ patient.setGender( Patient.MALE );
+ patient.setDobType( Patient.DOB_TYPE_VERIFIED );
+ patient.setBirthDate( getDate( 1970, 1, 1 ) );
+ patient.setRegistrationDate( new Date() );
+ patient.setOrganisationUnit( organisationUnit );
+
+ PatientIdentifier pIdentifier = new PatientIdentifier();
+ pIdentifier.setIdentifierType( patientIdentifierType );
+ pIdentifier.setPatient( patient );
+ pIdentifier.setIdentifier( "Identifier" + uniqueChar );
+ patient.getIdentifiers().add( pIdentifier );
+
+ return patient;
+ }
+
+ public static PatientAttributeValue createPatientAttributeValue( char uniqueChar, Patient patient,
+ PatientAttribute patientAttribute )
+ {
+ PatientAttributeValue attributeValue = new PatientAttributeValue();
+ attributeValue.setPatient( patient );
+ attributeValue.setPatientAttribute( patientAttribute );
+ attributeValue.setValue( "Attribute" + uniqueChar );
+
+ return attributeValue;
+ }
+
+ /**
+ * @param uniqueCharacter A unique character to identify the object.
+ * @return PatientAttribute
+ */
+ public static PatientAttribute createPatientAttribute( char uniqueChar )
+ {
+ PatientAttribute patientAttribute = new PatientAttribute();
+
+ patientAttribute.setName( "Attribute" + uniqueChar );
+ patientAttribute.setDescription( "Attribute" + uniqueChar );
+ patientAttribute.setValueType( PatientAttribute.TYPE_STRING );
+
+ return patientAttribute;
+ }
+
+ /**
+ * @param uniqueCharacter A unique character to identify the object.
+ * @return PatientAttribute
+ */
+ public static PatientIdentifierType createPatientIdentifierType( char uniqueChar )
+ {
+ PatientIdentifierType identifierType = new PatientIdentifierType();
+
+ identifierType.setName( "IdentifierType" + uniqueChar );
+ identifierType.setDescription( "IdentifierType" + uniqueChar );
+ identifierType.setType( PatientIdentifierType.VALUE_TYPE_TEXT );
+
+ return identifierType;
}
/**
@@ -1031,7 +1111,22 @@
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param sql A query statement to retreive record/data from database.
+ * @return RelationshipType
+ */
+ public static RelationshipType createRelationshipType( char uniqueChar )
+ {
+ RelationshipType relationshipType = new RelationshipType();
+
+ relationshipType.setaIsToB( "aIsToB" );
+ relationshipType.setbIsToA( "bIsToA" );
+ relationshipType.setName( "RelationshipType" + uniqueChar );
+
+ return relationshipType;
+ }
+
+ /**
+ * @param uniqueCharacter A unique character to identify the object.
+ * @param sql A query statement to retreive record/data from database.
* @return a sqlView instance
*/
protected static SqlView createSqlView( char uniqueCharacter, String sql )
@@ -1058,10 +1153,9 @@
return concept;
}
-
/**
* @param uniqueCharacter A unique character to identify the object.
- * @param value The value for constant
+ * @param value The value for constant
* @return a constant instance
*/
protected static Constant createConstant( char uniqueCharacter, double value )
@@ -1082,9 +1176,9 @@
* Injects the externalDir property of LocationManager to
* user.home/dhis2_test_dir. LocationManager dependency must be retrieved
* from the context up front.
- *
+ *
* @param locationManager The LocationManager to be injected with the
- * external directory.
+ * external directory.
*/
public void setExternalTestDir( LocationManager locationManager )
{