dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #26038
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 12917: Implement unit tests on PatientAttributeValue service methods
------------------------------------------------------------
revno: 12917
committer: Tran Chau <tran.hispvietnam@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2013-11-11 14:56:27 +0700
message:
Implement unit tests on PatientAttributeValue service methods
added:
dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/
dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueServiceTest.java
dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueStoreTest.java
modified:
dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueService.java
dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java
dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java
dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientAttributeGroupServiceTest.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/patientattributevalue/PatientAttributeValueService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueService.java 2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueService.java 2013-11-11 07:56:27 +0000
@@ -48,7 +48,6 @@
*
* @param patientAttribute The to PatientAttribute add.
*
- * @return A generated unique id of the added {@link PatientAttribute}.
*/
void savePatientAttributeValue( PatientAttributeValue patientAttributeValue );
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java 2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/patientattributevalue/PatientAttributeValueStore.java 2013-11-11 07:56:27 +0000
@@ -49,7 +49,6 @@
*
* @param patientAttribute The to PatientAttribute add.
*
- * @return A generated unique id of the added {@link PatientAttribute}.
*/
void saveVoid( PatientAttributeValue patientAttributeValue );
@@ -144,5 +143,5 @@
* @param patientAttributeOption PatientAttributeOption
*/
void updatePatientAttributeValues( PatientAttributeOption patientAttributeOption );
-
+
}
=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java'
--- dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java 2013-11-04 03:13:27 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/main/java/org/hisp/dhis/patientattributevalue/hibernate/HibernatePatientAttributeValueStore.java 2013-11-11 07:56:27 +0000
@@ -120,11 +120,11 @@
public void updatePatientAttributeValues( PatientAttributeOption patientAttributeOption )
{
- //TODO use hql for caching reasons
- String sql = "UPDATE patientattributevalue SET value='" + patientAttributeOption.getName()
- + "' WHERE patientattributeoptionid='" + patientAttributeOption.getId() + "'";
-
- jdbcTemplate.execute( sql );
+ String hql = "UPDATE PatientAttributeValue SET value=:value where patientAttributeOption=:patientAttributeOption";
+ Query query = getQuery( hql );
+ query.setString( "value", patientAttributeOption.getName() );
+ query.setEntity( "patientAttributeOption", patientAttributeOption );
+ query.executeUpdate();
}
@SuppressWarnings( "unchecked" )
=== modified file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientAttributeGroupServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientAttributeGroupServiceTest.java 2013-11-05 08:49:50 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/PatientAttributeGroupServiceTest.java 2013-11-11 07:56:27 +0000
@@ -63,7 +63,6 @@
PatientAttribute attributeA = createPatientAttribute( 'A' );
PatientAttribute attributeB = createPatientAttribute( 'B' );
PatientAttribute attributeC = createPatientAttribute( 'C' );
- PatientAttribute attributeD = createPatientAttribute( 'B' );
attributeService.savePatientAttribute( attributeA );
attributeService.savePatientAttribute( attributeB );
@@ -75,7 +74,6 @@
List<PatientAttribute> attributesB = new ArrayList<PatientAttribute>();
attributesB.add( attributeC );
- attributesB.add( attributeD );
attributeGroupA = createPatientAttributeGroup( 'A', attributesA );
attributeGroupB = createPatientAttributeGroup( 'B', attributesB );
=== added directory 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue'
=== added file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueServiceTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueServiceTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueServiceTest.java 2013-11-11 07:56:27 +0000
@@ -0,0 +1,400 @@
+/*
+ * 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.patientattributevalue;
+
+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.Map;
+
+import org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientAttribute;
+import org.hisp.dhis.patient.PatientAttributeOption;
+import org.hisp.dhis.patient.PatientAttributeOptionService;
+import org.hisp.dhis.patient.PatientAttributeService;
+import org.hisp.dhis.patient.PatientService;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValueService;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Chau Thu Tran
+ *
+ * @version $ PatientAttributeValueServiceTest.java Nov 11, 2013 9:45:10 AM $
+ */
+public class PatientAttributeValueServiceTest
+ extends DhisSpringTest
+{
+
+ @Autowired
+ private PatientAttributeValueService attributeValueService;
+
+ @Autowired
+ private PatientService patientService;
+
+ @Autowired
+ private OrganisationUnitService organisationUnitService;
+
+ @Autowired
+ private PatientAttributeService attributeService;
+
+ @Autowired
+ private PatientAttributeOptionService attributeOptionService;
+
+ PatientAttribute attributeA;
+
+ PatientAttribute attributeB;
+
+ PatientAttribute attributeC;
+
+ PatientAttributeOption attributeOpionA;
+
+ PatientAttributeOption attributeOpionB;
+
+ private Patient patientA;
+
+ private Patient patientB;
+
+ private Patient patientC;
+
+ private Patient patientD;
+
+ private int patientAId;
+
+ private int patientBId;
+
+ private PatientAttributeValue attributeValueA;
+
+ private PatientAttributeValue attributeValueB;
+
+ private PatientAttributeValue attributeValueC;
+
+ private PatientAttributeValue attributeValueD;
+
+ private PatientAttributeValue attributeValueE;
+
+ @Override
+ public void setUpTest()
+ {
+ OrganisationUnit organisationUnit = createOrganisationUnit( 'A' );
+ organisationUnitService.addOrganisationUnit( organisationUnit );
+
+ patientA = createPatient( 'A', organisationUnit );
+ patientB = createPatient( 'B', organisationUnit );
+ patientC = createPatient( 'C', organisationUnit );
+ patientD = createPatient( 'D', organisationUnit );
+
+ patientAId = patientService.savePatient( patientA );
+ patientBId = patientService.savePatient( patientB );
+ patientService.savePatient( patientC );
+ patientService.savePatient( patientD );
+
+ attributeA = createPatientAttribute( 'A' );
+ attributeB = createPatientAttribute( 'B' );
+ attributeC = createPatientAttribute( 'C' );
+
+ attributeService.savePatientAttribute( attributeA );
+ attributeService.savePatientAttribute( attributeB );
+ attributeService.savePatientAttribute( attributeC );
+
+ attributeOpionA = createPatientAttributeOption( 'A', attributeC );
+ attributeOpionB = createPatientAttributeOption( 'B', attributeC );
+
+ attributeOptionService.addPatientAttributeOption( attributeOpionA );
+ attributeOptionService.addPatientAttributeOption( attributeOpionB );
+
+ attributeValueA = new PatientAttributeValue( attributeA, patientA, "A" );
+ attributeValueB = new PatientAttributeValue( attributeB, patientA, "B" );
+ attributeValueC = new PatientAttributeValue( attributeA, patientB, "C" );
+ attributeValueD = new PatientAttributeValue( attributeC, patientC, "AttributeOptionA" );
+ attributeValueD.setPatientAttributeOption( attributeOpionA );
+ attributeValueE = new PatientAttributeValue( attributeC, patientD, "AttributeOptionB" );
+ attributeValueE.setPatientAttributeOption( attributeOpionB );
+ }
+
+ @Test
+ public void testSavePatientAttributeValue()
+ {
+
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ }
+
+ @Test
+ public void testUpdatePatientAttributeValue()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+
+ attributeValueA.setValue( "B" );
+ attributeValueService.updatePatientAttributeValue( attributeValueA );
+
+ assertEquals( "B", attributeValueService.getPatientAttributeValue( patientA, attributeA ).getValue() );
+ }
+
+ @Test
+ public void testDeletePatientAttributeValue()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+
+ attributeValueService.deletePatientAttributeValue( attributeValueA );
+
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+
+ attributeValueService.deletePatientAttributeValue( attributeValueB );
+
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+ }
+
+ @Test
+ public void testDeletePatientAttributeValueByPatient()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+
+ attributeValueService.deletePatientAttributeValue( patientA );
+
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+
+ attributeValueService.deletePatientAttributeValue( patientB );
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+ }
+
+ @Test
+ public void testDeletePatientAttributeValueByAttribute()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+
+ attributeValueService.deletePatientAttributeValue( attributeA );
+
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+ assertNotNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+
+ attributeValueService.deletePatientAttributeValue( attributeB );
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientA, attributeB ) );
+ assertNull( attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+
+ }
+
+ @Test
+ public void testGetPatientAttributeValue()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ assertEquals( attributeValueA, attributeValueService.getPatientAttributeValue( patientA, attributeA ) );
+ assertEquals( attributeValueC, attributeValueService.getPatientAttributeValue( patientB, attributeA ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValuesByPatient()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueService.getPatientAttributeValues( patientA );
+
+ assertEquals( 2, attributeValues.size() );
+ assertTrue( equals( attributeValues, attributeValueA, attributeValueB ) );
+
+ attributeValues = attributeValueService.getPatientAttributeValues( patientB );
+
+ assertEquals( 1, attributeValues.size() );
+ assertTrue( equals( attributeValues, attributeValueC ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValuesbyAttribute()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueService
+ .getPatientAttributeValues( attributeA );
+ assertEquals( 2, attributeValues.size() );
+ assertTrue( attributeValues.contains( attributeValueA ) );
+ assertTrue( attributeValues.contains( attributeValueC ) );
+
+ attributeValues = attributeValueService.getPatientAttributeValues( attributeB );
+ assertEquals( 1, attributeValues.size() );
+ assertTrue( attributeValues.contains( attributeValueB ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValuesbyPatientList()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<Patient> patients = new HashSet<Patient>();
+ patients.add( patientA );
+ patients.add( patientB );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueService.getPatientAttributeValues( patients );
+ assertEquals( 3, attributeValues.size() );
+ assertTrue( equals( attributeValues, attributeValueA, attributeValueB, attributeValueC ) );
+ }
+
+ @Test
+ public void testGetAllPatientAttributeValues()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ assertTrue( equals( attributeValueService.getAllPatientAttributeValues(), attributeValueA, attributeValueB,
+ attributeValueC ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValueMapForPatients()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<Patient> patients = new HashSet<Patient>();
+ patients.add( patientA );
+ patients.add( patientB );
+
+ Map<Integer, Collection<PatientAttributeValue>> attributeValueMap = attributeValueService
+ .getPatientAttributeValueMapForPatients( patients );
+
+ assertEquals( 2, attributeValueMap.keySet().size() );
+ assertTrue( equals( attributeValueMap.get( patientAId ), attributeValueA, attributeValueB ) );
+ assertTrue( equals( attributeValueMap.get( patientBId ), attributeValueC ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValueMapForPatientsAttributes()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<Patient> patients = new HashSet<Patient>();
+ patients.add( patientA );
+ patients.add( patientB );
+
+ Map<Integer, PatientAttributeValue> attributeValueMap = attributeValueService
+ .getPatientAttributeValueMapForPatients( patients, attributeA );
+
+ assertEquals( 2, attributeValueMap.keySet().size() );
+ assertEquals( attributeValueA, attributeValueMap.get( patientAId ) );
+ assertEquals( attributeValueC, attributeValueMap.get( patientBId ) );
+ }
+
+ @Test
+ public void testSearchPatientAttributeValue()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueService.searchPatientAttributeValue(
+ attributeA, "A" );
+ assertTrue( equals( attributeValues, attributeValueA ) );
+ }
+
+ @Test
+ public void testCopyPatientAttributeValues()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ attributeValueService.copyPatientAttributeValues( patientA, patientB );
+
+ PatientAttributeValue attributeValue = attributeValueService.getPatientAttributeValue( patientB, attributeB );
+ assertEquals( "B", attributeValue.getValue() );
+
+ attributeValue = attributeValueService.getPatientAttributeValue( patientB, attributeA );
+ assertNull( attributeValue );
+ }
+
+ @Test
+ public void testGetPatients()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueB );
+ attributeValueService.savePatientAttributeValue( attributeValueC );
+
+ Collection<Patient> patients = attributeValueService.getPatient( attributeA, "A" );
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA ) );
+ }
+
+ @Test
+ public void testCountByPatientAttributeoption()
+ {
+ attributeValueService.savePatientAttributeValue( attributeValueA );
+ attributeValueService.savePatientAttributeValue( attributeValueD );
+ attributeValueService.savePatientAttributeValue( attributeValueE );
+
+ int count = attributeValueService.countByPatientAttributeoption( attributeOpionA );
+ assertEquals( 1, count );
+ }
+
+}
=== added file 'dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueStoreTest.java'
--- dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueStoreTest.java 1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-patient/src/test/java/org/hisp/dhis/patient/patientattributevalue/PatientAttributeValueStoreTest.java 2013-11-11 07:56:27 +0000
@@ -0,0 +1,295 @@
+/*
+ * 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.patientattributevalue;
+
+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 org.hisp.dhis.DhisSpringTest;
+import org.hisp.dhis.organisationunit.OrganisationUnit;
+import org.hisp.dhis.organisationunit.OrganisationUnitService;
+import org.hisp.dhis.patient.Patient;
+import org.hisp.dhis.patient.PatientAttribute;
+import org.hisp.dhis.patient.PatientAttributeOption;
+import org.hisp.dhis.patient.PatientAttributeOptionService;
+import org.hisp.dhis.patient.PatientAttributeService;
+import org.hisp.dhis.patient.PatientService;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValue;
+import org.hisp.dhis.patientattributevalue.PatientAttributeValueStore;
+import org.junit.Test;
+import org.springframework.beans.factory.annotation.Autowired;
+
+/**
+ * @author Chau Thu Tran
+ *
+ * @version $ PatientattributeValueStoreTest.java Nov 11, 2013 9:45:10 AM $
+ */
+public class PatientAttributeValueStoreTest
+ extends DhisSpringTest
+{
+
+ @Autowired
+ private PatientAttributeValueStore attributeValueStore;
+
+ @Autowired
+ private PatientService patientService;
+
+ @Autowired
+ private OrganisationUnitService organisationUnitService;
+
+ @Autowired
+ private PatientAttributeService attributeService;
+
+ @Autowired
+ private PatientAttributeOptionService attributeOptionService;
+
+ PatientAttribute attributeA;
+
+ PatientAttribute attributeB;
+
+ PatientAttribute attributeC;
+
+ PatientAttributeOption attributeOpionA;
+
+ PatientAttributeOption attributeOpionB;
+
+ private Patient patientA;
+
+ private Patient patientB;
+
+ private Patient patientC;
+
+ private Patient patientD;
+
+ private PatientAttributeValue attributeValueA;
+
+ private PatientAttributeValue attributeValueB;
+
+ private PatientAttributeValue attributeValueC;
+
+ private PatientAttributeValue attributeValueD;
+
+ private PatientAttributeValue attributeValueE;
+
+ @Override
+ public void setUpTest()
+ {
+ OrganisationUnit organisationUnit = createOrganisationUnit( 'A' );
+ organisationUnitService.addOrganisationUnit( organisationUnit );
+
+ patientA = createPatient( 'A', organisationUnit );
+ patientB = createPatient( 'B', organisationUnit );
+ patientC = createPatient( 'C', organisationUnit );
+ patientD = createPatient( 'D', organisationUnit );
+
+ patientService.savePatient( patientA );
+ patientService.savePatient( patientB );
+ patientService.savePatient( patientC );
+ patientService.savePatient( patientD );
+
+ attributeA = createPatientAttribute( 'A' );
+ attributeB = createPatientAttribute( 'B' );
+ attributeC = createPatientAttribute( 'C' );
+
+ attributeService.savePatientAttribute( attributeA );
+ attributeService.savePatientAttribute( attributeB );
+ attributeService.savePatientAttribute( attributeC );
+
+ attributeOpionA = createPatientAttributeOption( 'A', attributeC );
+ attributeOpionB = createPatientAttributeOption( 'B', attributeC );
+
+ attributeOptionService.addPatientAttributeOption( attributeOpionA );
+ attributeOptionService.addPatientAttributeOption( attributeOpionB );
+
+ attributeValueA = new PatientAttributeValue( attributeA, patientA, "A" );
+ attributeValueB = new PatientAttributeValue( attributeB, patientA, "B" );
+ attributeValueC = new PatientAttributeValue( attributeA, patientB, "C" );
+ attributeValueD = new PatientAttributeValue( attributeC, patientC, "AttributeOptionA" );
+ attributeValueD.setPatientAttributeOption( attributeOpionA );
+ attributeValueE = new PatientAttributeValue( attributeC, patientD, "AttributeOptionB" );
+ attributeValueE.setPatientAttributeOption( attributeOpionB );
+ }
+
+ @Test
+ public void testSavePatientAttributeValue()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+
+ assertNotNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNotNull( attributeValueStore.get( patientA, attributeA ) );
+ }
+
+ @Test
+ public void testDeletePatientAttributeValueByPatient()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ assertNotNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNotNull( attributeValueStore.get( patientA, attributeB ) );
+ assertNotNull( attributeValueStore.get( patientB, attributeA ) );
+
+ attributeValueStore.deleteByPatient( patientA );
+
+ assertNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNull( attributeValueStore.get( patientA, attributeB ) );
+ assertNotNull( attributeValueStore.get( patientB, attributeA ) );
+
+ attributeValueStore.deleteByPatient( patientB );
+ assertNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNull( attributeValueStore.get( patientA, attributeB ) );
+ assertNull( attributeValueStore.get( patientB, attributeA ) );
+ }
+
+ @Test
+ public void testDeleteByAttribute()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ assertNotNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNotNull( attributeValueStore.get( patientA, attributeB ) );
+ assertNotNull( attributeValueStore.get( patientB, attributeA ) );
+
+ attributeValueStore.deleteByAttribute( attributeA );
+
+ assertNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNull( attributeValueStore.get( patientB, attributeA ) );
+ assertNotNull( attributeValueStore.get( patientA, attributeB ) );
+
+ attributeValueStore.deleteByAttribute( attributeB );
+ assertNull( attributeValueStore.get( patientA, attributeA ) );
+ assertNull( attributeValueStore.get( patientA, attributeB ) );
+ assertNull( attributeValueStore.get( patientB, attributeA ) );
+
+ }
+
+ @Test
+ public void testGetPatientAttributeValue()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ assertEquals( attributeValueA, attributeValueStore.get( patientA, attributeA ) );
+ assertEquals( attributeValueC, attributeValueStore.get( patientB, attributeA ) );
+ }
+
+ @Test
+ public void testGetByPatient()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueStore.get( patientA );
+
+ assertEquals( 2, attributeValues.size() );
+ assertTrue( equals( attributeValues, attributeValueA, attributeValueB ) );
+
+ attributeValues = attributeValueStore.get( patientB );
+
+ assertEquals( 1, attributeValues.size() );
+ assertTrue( equals( attributeValues, attributeValueC ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValuesbyAttribute()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueStore.get( attributeA );
+ assertEquals( 2, attributeValues.size() );
+ assertTrue( attributeValues.contains( attributeValueA ) );
+ assertTrue( attributeValues.contains( attributeValueC ) );
+
+ attributeValues = attributeValueStore.get( attributeB );
+ assertEquals( 1, attributeValues.size() );
+ assertTrue( attributeValues.contains( attributeValueB ) );
+ }
+
+ @Test
+ public void testGetPatientAttributeValuesbyPatientList()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ Collection<Patient> patients = new HashSet<Patient>();
+ patients.add( patientA );
+ patients.add( patientB );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueStore.get( patients );
+ assertEquals( 3, attributeValues.size() );
+ assertTrue( equals( attributeValues, attributeValueA, attributeValueB, attributeValueC ) );
+ }
+
+ @Test
+ public void testSearchPatientAttributeValue()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ Collection<PatientAttributeValue> attributeValues = attributeValueStore.searchByValue( attributeA, "A" );
+ assertTrue( equals( attributeValues, attributeValueA ) );
+ }
+
+ @Test
+ public void testGetPatients()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueB );
+ attributeValueStore.saveVoid( attributeValueC );
+
+ Collection<Patient> patients = attributeValueStore.getPatient( attributeA, "A" );
+ assertEquals( 1, patients.size() );
+ assertTrue( patients.contains( patientA ) );
+ }
+
+ @Test
+ public void testCountByPatientAttributeoption()
+ {
+ attributeValueStore.saveVoid( attributeValueA );
+ attributeValueStore.saveVoid( attributeValueD );
+ attributeValueStore.saveVoid( attributeValueE );
+
+ int count = attributeValueStore.countByPatientAttributeoption( attributeOpionA );
+ assertEquals( 1, count );
+ }
+
+}