Skip to content
Closed
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
6c2fb73
BED-17: Queries for BedPatientAssignment should account for the field
Oct 2, 2025
e683740
Fix build failure
Oct 2, 2025
dded9cb
BED-17: Queries for BedPatientAssignment should account for the field
Oct 2, 2025
8d5dd34
Update the branch
Oct 12, 2025
6f37ff3
Update tests to account for voided BedPatientAssignments
Oct 12, 2025
d08fd7b
Add unit tests to handle voided BedPatientAssignments
Oct 12, 2025
30761d2
Ensure voided is correctly set
Oct 13, 2025
dc3e4f6
correctly mock getBedById for voided assignment tests
Oct 13, 2025
004ec4d
Refactor imports
Oct 13, 2025
a88772b
BED-17: Queries for BedPatientAssignment should account for the field
Oct 2, 2025
5a50714
Merge branch 'BED-17' of https://github.com/jwnasambu/openmrs-module-…
Oct 15, 2025
3507c3e
BED-17: Call methods directly in tests instead of mocking
Oct 16, 2025
4f00ac5
BED-17:Refactor some tests to call real methods instead of mocking
Oct 17, 2025
6bd7366
BED-17: Use real DAO for key bed management methods, keep other mocks.
Oct 21, 2025
70d8c9e
Bed-17: Remove wildcard imports
Oct 21, 2025
f67a3ea
Bed-17: Create BedManagementDaoImplTest to test DAO methods without m…
Oct 26, 2025
5ef4f73
Bed-17: code refactor
Oct 26, 2025
e4d74bd
BED-17: Added missing moduleApplicationContext.xml
Oct 27, 2025
4442096
BED-17: Resolve the Spring sessionFactory bean error
Oct 27, 2025
2993ebe
Bed-17: Refactored applicationContext-service.xml
Oct 27, 2025
83ea660
BED-17: Fix Surefire test failure in bedmanagement-atom-feed
Oct 27, 2025
08a982c
BED-17: Fix Spring XML schema in moduleApplicationContext.xml
Oct 27, 2025
cae1697
Move BedManagementDaoImplTest to API main folder
Oct 28, 2025
af3b9a3
esolve ApplicationContext loading errors in BedManagementDaoImplTest
Oct 29, 2025
c27cb95
BED-23: chore: remove unused moduleApplicationContext.xml from omod r…
Oct 29, 2025
9dddb07
BED-17: load moduleApplicationContext.xml in BedManagementDaoImplTest
Oct 29, 2025
a76f280
BED-17: Updated BedManagementDaoImplTest to use TestingApplicationCon…
Oct 29, 2025
28cdceb
BED-17: Code refactor
Oct 31, 2025
1c9f492
Bed-17: Remove redundant @ContextConfiguration from BedManagementDaoI…
Nov 4, 2025
934f011
Merge branch 'master' of https://github.com/jwnasambu/openmrs-module-…
Nov 4, 2025
65fb6d0
BED-17: Remove custom configuration
Nov 4, 2025
1b2a2a7
BED-17: Add unit tests to verify DAO methods exclude voided BedPatien…
Nov 11, 2025
11ba938
BED-17: Remove trailing whitespace
Nov 11, 2025
d5b3f8f
BED-17: Remove trailing whitespace from BedManagementServiceImplTest.…
Nov 12, 2025
ed7822c
BED-17: Ensures test coverage aligns with real database query behavior
Nov 12, 2025
ffa2279
BED-17: code cleaning
Nov 12, 2025
d41c0f3
Merge branch 'master' into BED-17
jwnasambu Nov 16, 2025
999567f
BED-17: Add voided rows to test dataset
Nov 18, 2025
4915bb9
Merge branch 'master' into BED-17
jwnasambu Nov 18, 2025
0fd4758
BED-17: Remove voided column from LOCATION_TAG_MAP
Nov 19, 2025
027b83e
BED-17: Refactor BedManagementDaoImplTest to use EncounterService.get…
Nov 21, 2025
0826974
BED-17: load existing patients instead of creating new ones in BedMan…
Nov 23, 2025
bcc9b7b
BED-17: Refactor BedManagementDaoImplTest to use existing test datase…
Nov 24, 2025
d1d70dd
BED-17: Use valid dataset patients and encounters
Nov 25, 2025
ad199ce
BED-17: Eliminate unnecessary null checks
Nov 25, 2025
4863cb8
Merge branch 'master' of https://github.com/openmrs/openmrs-module-be…
Nov 25, 2025
589b7f9
BED-17: Removed unnecessary null checks for visits and encounters
Nov 25, 2025
a5f69ca
BED-17: Refactor BedManagementDaoImplTest to fetch Visit and Encounte…
Nov 25, 2025
63026d6
BED-17: Retrieve the correct single Visit/Encounter
Nov 26, 2025
37a3a38
BED-17: Fix merge conflicts
Nov 30, 2025
5d41799
BED-17: Removed unnecessary manual object creation
Dec 2, 2025
a16c521
BED-17: Update test dataset path to load bedManagementDAOComponentTes…
Dec 3, 2025
11d4ba4
Merge branch 'master' of https://github.com/openmrs/openmrs-module-be…
Dec 10, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,20 @@
*/
package org.openmrs.module.bedmanagement.dao.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.BooleanUtils;
import org.hibernate.Criteria;
import org.hibernate.FlushMode;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.query.Query;
import org.hibernate.transform.Transformers;
import org.openmrs.Location;
import org.openmrs.Patient;
Expand All @@ -35,12 +41,6 @@
import org.openmrs.module.bedmanagement.entity.BedTagMap;
import org.openmrs.module.bedmanagement.entity.BedType;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BedManagementDaoImpl implements BedManagementDao {

SessionFactory sessionFactory;
Expand All @@ -67,8 +67,8 @@ public Bed getBedByUuid(String uuid) {
public Bed getBedByPatient(Patient patient) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have the getBedByPatient method called/not mocked, in a test?

Session session = sessionFactory.getCurrentSession();
Bed bed = (Bed) session
.createQuery("select bpa.bed.bedNumber as bedNumber,bpa.bed.id as id from BedPatientAssignment bpa "
+ "where bpa.patient = :patient and bpa.endDatetime is null")
.createQuery("select bpa.bed.bedNumber as bedNumber, bpa.bed.id as id from BedPatientAssignment bpa "
+ "where bpa.patient = :patient and bpa.endDatetime is null and bpa.voided = false")
.setParameter("patient", patient).setResultTransformer(Transformers.aliasToBean(Bed.class)).uniqueResult();
return bed;
}
Expand All @@ -89,7 +89,7 @@ public Location getWardForBed(Bed bed) {
@Override
public BedPatientAssignment getBedPatientAssignmentByUuid(String uuid) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have the getBedPatientAssignmentByUuid method called/not mocked, in a test?

Session session = sessionFactory.getCurrentSession();
return (BedPatientAssignment) session.createQuery("from BedPatientAssignment bpa " + "where bpa.uuid = :uuid")
return (BedPatientAssignment) session.createQuery("from BedPatientAssignment bpa where bpa.uuid = :uuid")
.setParameter("uuid", uuid).uniqueResult();
}

Expand Down Expand Up @@ -125,9 +125,10 @@ public List<BedPatientAssignment> getBedPatientAssignmentByEncounter(String enco
return bpaList;
}

@Override
public List<BedPatientAssignment> getBedPatientAssignmentByVisit(String visitUuid, boolean includeEnded) {
Session session = sessionFactory.getCurrentSession();
List<BedPatientAssignment> bpaList = (List<BedPatientAssignment>) session
List<BedPatientAssignment> bpaList = session
.createQuery(
"select bpa from BedPatientAssignment bpa " + "inner join bpa.encounter enc " + "inner join enc.visit v "
+ "where v.uuid = :visitUuid AND " + "(bpa.endDatetime IS NULL OR :includeEnded IS TRUE) AND "
Expand All @@ -141,19 +142,21 @@ public List<BedPatientAssignment> getBedPatientAssignmentByVisit(String visitUui
public List<BedPatientAssignment> getCurrentAssignmentsByBed(Bed bed) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have the getCurrentAssignmentsByBed method called/not mocked, in a test?

Session session = sessionFactory.getCurrentSession();
List<BedPatientAssignment> assignments = session
.createQuery("from BedPatientAssignment where bed=:bed and endDatetime is null").setParameter("bed", bed)
.list();
.createQuery(
"from BedPatientAssignment bpa where bpa.bed = :bed and bpa.endDatetime is null and bpa.voided = false")
.setParameter("bed", bed).list();
return assignments;
}

@Override
public Bed getLatestBedByVisit(String visitUuid) {
Session session = sessionFactory.getCurrentSession();
Bed bed = (Bed) session
.createQuery("select bpa.bed from BedPatientAssignment bpa " + "inner join bpa.encounter enc "
+ "inner join enc.visit v where v.uuid = :visitUuid order by bpa.startDatetime DESC")
.setParameter("visitUuid", visitUuid).setMaxResults(1).uniqueResult();
return bed;

String hql = "select bpa.bed " + "from BedPatientAssignment bpa " + "inner join bpa.encounter enc "
+ "inner join enc.visit v " + "where v.uuid = :visitUuid " + "and bpa.voided = false "
+ "order by bpa.startDatetime DESC";

return (Bed) session.createQuery(hql).setParameter("visitUuid", visitUuid).setMaxResults(1).uniqueResult();
}

@Override
Expand Down Expand Up @@ -287,23 +290,28 @@ private Criteria createGetBedsCriteria(Location location, BedType bedType, BedSt
criteria.createAlias("blm.location", "location");
criteria.add(Restrictions.eq("location", location));
criteria.add(Restrictions.eq("bed.voided", false));
if (bedStatus != null)
if (bedStatus != null) {
criteria.add(Restrictions.eq("bed.status", bedStatus.toString()));
if (bedType != null)
}
if (bedType != null) {
criteria.add(Restrictions.eq("bed.bedType", bedType));
}
} else {
criteria = session.createCriteria(Bed.class, "bed");
criteria.add(Restrictions.eq("voided", false));
if (bedStatus != null)
if (bedStatus != null) {
criteria.add(Restrictions.eq("status", bedStatus.toString()));
if (bedType != null)
}
if (bedType != null) {
criteria.add(Restrictions.eq("bedType", bedType));
}
}

if (limit != null) {
criteria.setMaxResults(limit);
if (offset != null)
if (offset != null) {
criteria.setFirstResult(offset);
}
}

return criteria;
Expand Down Expand Up @@ -350,13 +358,15 @@ public BedTag getBedTagByUuid(String uuid) {
public List<BedTag> getBedTags(String name, Integer limit, Integer offset) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(BedTag.class);
criteria.add(Restrictions.eq("voided", false));
if (name != null)
if (name != null) {
criteria.add(Restrictions.eq("name", name));
}

if (limit != null) {
criteria.setMaxResults(limit);
if (offset != null)
if (offset != null) {
criteria.setFirstResult(offset);
}
}
return criteria.list();
}
Expand Down Expand Up @@ -393,12 +403,14 @@ public BedType getBedTypeByUuid(String uuid) {
public List<BedType> getBedTypes(String name, Integer limit, Integer offset) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(BedType.class);
criteria.add(Restrictions.eq("retired", false));
if (name != null)
if (name != null) {
criteria.add(Restrictions.eq("name", name));
}
if (limit != null) {
criteria.setMaxResults(limit);
if (offset != null)
if (offset != null) {
criteria.setFirstResult(offset);
}
}
return criteria.list();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package org.openmrs.module.bedmanagement;

import org.junit.Before;
import org.junit.Test;
import org.openmrs.Encounter;
import org.openmrs.EncounterType;
import org.openmrs.Patient;
import org.openmrs.Visit;
import org.openmrs.api.context.Context;
import org.openmrs.module.bedmanagement.dao.BedManagementDao;
import org.openmrs.module.bedmanagement.entity.Bed;
import org.openmrs.module.bedmanagement.entity.BedPatientAssignment;
import org.openmrs.module.bedmanagement.entity.BedType;
import org.openmrs.test.BaseModuleContextSensitiveTest;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class BedManagementDaoImplTest extends BaseModuleContextSensitiveTest {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the sub-module/location of the BedManagementDaoImpl class?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @wikumChamith. I now understand why the duplicate ehcache jars/configs on classpath causing the Spring fails to create the sessionFactory. I have deleted the file.


private BedManagementDao bedManagementDao;

private EncounterType defaultEncounterType;

@Before
public void setup() throws Exception {
executeDataSet("org/openmrs/include/standardTestDataset.xml");

bedManagementDao = Context.getRegisteredComponent("bedManagementDao", BedManagementDao.class);

defaultEncounterType = Context.getEncounterService().getEncounterType("Scheduled");
}

private Bed createBed(String bedNumber) {
BedType bedType = new BedType();
bedType.setName("Test BedType");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't we just load this bed from the test dataset?

bedType.setDisplayName("TB");
bedType.setDescription("Test Bed Type");
bedType = bedManagementDao.saveBedType(bedType);

Bed bed = new Bed();
bed.setBedNumber(bedNumber);
bed.setStatus("AVAILABLE");
bed.setBedType(bedType);

return bedManagementDao.saveBed(bed);
}

private Visit getTestVisit(Patient patient) {
List<Visit> visits = Context.getVisitService().getVisitsByPatient(patient);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What i mean is Context.getVisitService().getVisit(visitId)

assertNotNull(visits);
assertEquals(true, !visits.isEmpty());
return visits.get(0);
}

private Encounter getTestEncounter(Patient patient) {
List<Encounter> encounters = Context.getEncounterService().getEncountersByPatient(patient);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What i mean is Context.getEncounterService().getEncounter(encounterId)

assertNotNull(encounters);
assertEquals(true, !encounters.isEmpty());
return encounters.get(0);
}

@Test
public void shouldReturnBedByPatient() {
Patient patient = Context.getPatientService().getPatient(2);
Bed bed = createBed("B101");

Encounter encounter = getTestEncounter(patient);

BedPatientAssignment assignment = new BedPatientAssignment();
assignment.setBed(bed);
assignment.setPatient(patient);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of doing all this, just have this data in the test dataset file and then it gets loaded as expected.

assignment.setEncounter(encounter);
assignment.setStartDatetime(new Date());

bedManagementDao.saveBedPatientAssignment(assignment);

Bed result = bedManagementDao.getBedByPatient(patient);

assertNotNull(result);
assertEquals("B101", result.getBedNumber());
}

@Test
public void shouldReturnBedPatientAssignmentByUuid() {
Patient patient = Context.getPatientService().getPatient(2);
Bed bed = createBed("B102");

Encounter encounter = getTestEncounter(patient);

BedPatientAssignment assignment = new BedPatientAssignment();
assignment.setBed(bed);
assignment.setPatient(patient);
assignment.setEncounter(encounter);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review @dkayiwa. I am fixing the issue now.

assignment.setStartDatetime(new Date());

BedPatientAssignment saved = bedManagementDao.saveBedPatientAssignment(assignment);

BedPatientAssignment result = bedManagementDao.getBedPatientAssignmentByUuid(saved.getUuid());

assertNotNull(result);
assertEquals(patient.getUuid(), result.getPatient().getUuid());
}

@Test
public void shouldReturnCurrentAssignmentsByBed() {
Bed bed = createBed("B103");

Patient patient1 = Context.getPatientService().getPatient(2);
Patient patient2 = Context.getPatientService().getPatient(7);

Encounter encounter1 = getTestEncounter(patient1);
Encounter encounter2 = getTestEncounter(patient2);

BedPatientAssignment a1 = new BedPatientAssignment();
a1.setBed(bed);
a1.setPatient(patient1);
a1.setEncounter(encounter1);
a1.setStartDatetime(new Date());
bedManagementDao.saveBedPatientAssignment(a1);

BedPatientAssignment a2 = new BedPatientAssignment();
a2.setBed(bed);
a2.setPatient(patient2);
a2.setEncounter(encounter2);
a2.setStartDatetime(new Date());
bedManagementDao.saveBedPatientAssignment(a2);

List<BedPatientAssignment> results = bedManagementDao.getCurrentAssignmentsByBed(bed);

assertNotNull(results);
assertEquals(2, results.size());
}

@Test
public void shouldReturnLatestBedByVisit() {
Patient patient = Context.getPatientService().getPatient(2);
Visit visit = getTestVisit(patient);

Encounter encounter1 = new Encounter();
encounter1.setPatient(patient);
encounter1.setVisit(visit);
encounter1.setEncounterType(defaultEncounterType);
encounter1.setEncounterDatetime(new Date(System.currentTimeMillis() - 100000L));
encounter1 = Context.getEncounterService().saveEncounter(encounter1);

Encounter encounter2 = new Encounter();
encounter2.setPatient(patient);
encounter2.setVisit(visit);
encounter2.setEncounterType(defaultEncounterType);
encounter2.setEncounterDatetime(new Date());
encounter2 = Context.getEncounterService().saveEncounter(encounter2);

Bed bed1 = createBed("B104");
Bed bed2 = createBed("B105");

BedPatientAssignment assignment1 = new BedPatientAssignment();
assignment1.setBed(bed1);
assignment1.setPatient(patient);
assignment1.setEncounter(encounter1);
assignment1.setStartDatetime(new Date(System.currentTimeMillis() - 100000L));
assignment1.setEndDatetime(new Date(System.currentTimeMillis() - 50000L));
bedManagementDao.saveBedPatientAssignment(assignment1);

BedPatientAssignment assignment2 = new BedPatientAssignment();
assignment2.setBed(bed2);
assignment2.setPatient(patient);
assignment2.setEncounter(encounter2);
assignment2.setStartDatetime(new Date());
bedManagementDao.saveBedPatientAssignment(assignment2);

Bed latest = bedManagementDao.getLatestBedByVisit(visit.getUuid());

assertNotNull(latest);
assertEquals("B105", latest.getBedNumber());
}
}
Loading