Skip to content

Commit

Permalink
test: appointment dao integration test (#102)
Browse files Browse the repository at this point in the history
- red flags, health diary, patient

Signed-off-by: Kathurima Kimathi <[email protected]>
  • Loading branch information
KathurimaKimathi authored Aug 24, 2023
1 parent 22bc6b5 commit 350e8af
Show file tree
Hide file tree
Showing 13 changed files with 952 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ private DbSession session() {
@Override
public List<Obs> getAppointmentsByLastSyncDate(Date lastSyncDate) {
if (lastSyncDate != null) {
// TODO: 24/10/2022 Extract these queries into a shared place
List<Integer> consentedPatientIds =
sessionFactory.getCurrentSession().createSQLQuery(GET_MYCAREHUB_CONSENTED_PATIENT).list();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public MyCareHubSetting getMyCareHubSettingByType(String settingType) {
@Override
public MyCareHubSetting saveOrUpdateMyCareHubSetting(MyCareHubSetting myCareHubSetting) {
session().saveOrUpdate(myCareHubSetting);
System.out.println(myCareHubSetting);
return myCareHubSetting;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,235 +1,173 @@
package org.openmrs.module.mycarehub.api.db.hibernate;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.openmrs.module.mycarehub.api.db.hibernate.Common.GET_MYCAREHUB_CONSENTED_PATIENT;
import static org.powermock.api.mockito.PowerMockito.*;
import static org.junit.Assert.assertEquals;
import static org.openmrs.module.mycarehub.api.db.hibernate.MyCareHubPatientDaoTest.MCH_CONSENTED_PATIENT_XML_FILENAME;

import java.util.*;
import org.hibernate.Criteria;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.*;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.openmrs.Concept;
import org.openmrs.Encounter;
import org.openmrs.Obs;
import org.openmrs.api.db.hibernate.DbSession;
import org.openmrs.api.db.hibernate.DbSessionFactory;
import org.openmrs.module.mycarehub.api.db.AppointmentDao;
import org.openmrs.module.mycarehub.model.AppointmentRequests;
import org.openmrs.module.mycarehub.model.ConsentedPatient;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.openmrs.test.BaseModuleContextSensitiveTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Restrictions.class})
public class HibernateAppointmentDaoTest {
public class HibernateAppointmentDaoTest extends BaseModuleContextSensitiveTest {

@Mock private DbSessionFactory sessionFactory;
private AppointmentDao appointmentDao;

@Mock private DbSession dbSession;
@Autowired public DbSessionFactory sessionFactory;

@Mock private AppointmentDao appointmentDao;
// protected static final String STANDARD_DATASET_XML_FILENAME =
// "org.openmrs.module.mycarehub/api/include/StandardDataset.xml";
protected static final String INITIAL_APPOINTMENT_XML_FILENAME =
"org.openmrs.module.mycarehub/api/include/AppointmentRequest_Initial.xml";

@InjectMocks HibernateAppointmentDao hibernateAppointmentDao;

@Mock Criteria criteria;
@Override
public Boolean useInMemoryDatabase() {
// Defaults to true. If you want to use your own database specified in your runtime properties
// return false instead, otherwise return super.useInMemoryDatabase();.
return super.useInMemoryDatabase();
}

@Before
public void setup() {
mockStatic(Restrictions.class);
mockStatic(SQLQuery.class);
mockStatic(Criteria.class);
when(sessionFactory.getCurrentSession()).thenReturn(dbSession);
public void setup() throws Exception {
appointmentDao = new HibernateAppointmentDao(sessionFactory);
}

@Test
public void testGetAppointmentsByLastSyncDate() {
Date lastSyncDate = new Date();

List<Integer> consentedPatientIds = consentedPatientIds();
when(dbSession.createSQLQuery(anyString())).thenReturn(mock(org.hibernate.SQLQuery.class));
when(dbSession.createCriteria(Obs.class)).thenReturn(mock(org.hibernate.Criteria.class));

when(dbSession.createSQLQuery(GET_MYCAREHUB_CONSENTED_PATIENT).list())
.thenReturn(consentedPatientIds);
// executeDataSet(STANDARD_DATASET_XML_FILENAME);

List<Obs> expectedAppointments = obsFactory();

when(dbSession.createCriteria(Obs.class).list()).thenReturn(expectedAppointments);

List<Obs> result = hibernateAppointmentDao.getAppointmentsByLastSyncDate(lastSyncDate);

assertEquals(expectedAppointments, result);
executeDataSet(INITIAL_APPOINTMENT_XML_FILENAME);
}

@Test
public void testGetAppointmentsByLastSyncDateShouldReturnNullObs() {
List<Obs> result = hibernateAppointmentDao.getAppointmentsByLastSyncDate(null);

assertEquals(0, result.size());
public void getAppointmentsByLastSyncDate_shouldReturnEmptyObsWhenLastSyncIsNull() {
List<Obs> results = appointmentDao.getAppointmentsByLastSyncDate(null);
assertEquals(0, results.size());
}

@Test
public void getAllAppointmentRequests() {
List<AppointmentRequests> appointmentRequestsList = appointmentRequestsListFactory();
public void getAppointmentsByLastSyncDate() throws Exception {
executeDataSet(MCH_CONSENTED_PATIENT_XML_FILENAME);

when(dbSession.createCriteria(AppointmentRequests.class)).thenReturn(criteria);
when(criteria.add(any(Criterion.class))).thenReturn(criteria);
when(criteria.list()).thenReturn(appointmentRequestsList);
List<AppointmentRequests> appointmentRequestsList = appointmentDao.getAllAppointmentRequests();
assertEquals(1, appointmentRequestsList.size());

List<AppointmentRequests> requests = hibernateAppointmentDao.getAllAppointmentRequests();
assertEquals(1, requests.size());
for (AppointmentRequests appointmentRequest : appointmentRequestsList) {
List<Obs> results =
appointmentDao.getAppointmentsByLastSyncDate(appointmentRequest.getDateCreated());
assertEquals(0, results.size());
}
}

@Test
public void getAllAppointmentRequestsByLastSyncDate() {
Date lastSyncDate = new Date();
List<AppointmentRequests> appointmentRequestsList = appointmentRequestsListFactory();
assertNotNull(appointmentRequestsList);

when(dbSession.createCriteria(AppointmentRequests.class)).thenReturn(criteria);
when(criteria.add(any(Criterion.class))).thenReturn(criteria);
when(criteria.list()).thenReturn(appointmentRequestsList);
public void getAllAppointmentRequests() {
List<AppointmentRequests> allAppointments = appointmentDao.getAllAppointmentRequests();
assertEquals(1, allAppointments.size());
}

List<AppointmentRequests> requests =
hibernateAppointmentDao.getAllAppointmentRequestsByLastSyncDate(lastSyncDate);
assertEquals(1, requests.size());
@Test
public void getAllAppointmentRequestsByLastSyncDate_returnZeroAppointments() {
List<AppointmentRequests> allAppointments =
appointmentDao.getAllAppointmentRequestsByLastSyncDate(new Date());
assertEquals(0, allAppointments.size());
}

@Test
public void saveAppointmentRequestsList() {
@Rollback(value = false)
public void saveAListOfAppointmentRequests() {
List<AppointmentRequests> appointmentRequestsList = appointmentRequestsListFactory();
assertNotNull(appointmentRequestsList);

doNothing().when(dbSession).saveOrUpdate(AppointmentRequests.class);

List<AppointmentRequests> result =
List<AppointmentRequests> allAppointments =
appointmentDao.saveAppointmentRequests(appointmentRequestsList);
assertEquals(appointmentRequestsList, result);
assertEquals(1, allAppointments.size());
}

@Test
public void saveAppointmentRequests() {
AppointmentRequests appointmentRequests = appointmentRequestsListFactory().get(0);
assertNotNull(appointmentRequests);

doNothing().when(dbSession).saveOrUpdate(AppointmentRequests.class);

AppointmentRequests result =
hibernateAppointmentDao.saveAppointmentRequests(appointmentRequests);
assertNotNull(result);
@Rollback(value = false)
public void saveOneInstanceOfAppointmentRequests() {
Date dt = new Date();
AppointmentRequests appointmentRequest = new AppointmentRequests();
appointmentRequest.setAppointmentUUID(String.valueOf(UUID.randomUUID()));
appointmentRequest.setId(41);
appointmentRequest.setMycarehubId(String.valueOf(UUID.randomUUID()));
appointmentRequest.setClientName("Test kazi");
appointmentRequest.setCccNumber("9876");
appointmentRequest.setAppointmentReason("travel");
appointmentRequest.setStatus("PENDING");
appointmentRequest.setClientContact("098765");
appointmentRequest.setDateCreated(dt);
appointmentRequest.setVoided(false);
appointmentRequest.setProgressDate(dt);
appointmentRequest.setDateResolved(dt);

AppointmentRequests result = appointmentDao.saveAppointmentRequests(appointmentRequest);
Integer expected = 41;
assertEquals(expected, result.getId());
}

@Test
public void getAppointmentRequestByMycarehubId() {
List<AppointmentRequests> appointmentRequestsList = appointmentRequestsListFactory();
String myCareHubId = appointmentRequestsList.get(0).getMycarehubId();
assertNotNull(myCareHubId);

when(dbSession.createCriteria(AppointmentRequests.class)).thenReturn(criteria);
when(criteria.add(any(Criterion.class))).thenReturn(criteria);
when(criteria.uniqueResult()).thenReturn(appointmentRequestsList.get(0));

AppointmentRequests appointmentRequests =
hibernateAppointmentDao.getAppointmentRequestByMycarehubId(
appointmentRequestsList.get(0).getAppointmentUUID());
assertNotNull(appointmentRequests);
AppointmentRequests mycarehubAppointment =
appointmentDao.getAppointmentRequestByMycarehubId("cf25712e-f193-48d5-a85a-5fd8d0244c71");
Integer expectedID = 1;
assertEquals(expectedID, mycarehubAppointment.getId());
}

@Test
public void getAppointmentRequestByUuid() {
List<AppointmentRequests> appointmentRequestsList = appointmentRequestsListFactory();
String appointmentUUID = appointmentRequestsList.get(0).getAppointmentUUID();
assertNotNull(appointmentUUID);

when(dbSession.createCriteria(AppointmentRequests.class)).thenReturn(criteria);
when(criteria.add(any(Criterion.class))).thenReturn(criteria);
when(criteria.uniqueResult()).thenReturn(appointmentRequestsList.get(0));

AppointmentRequests appointmentRequests =
hibernateAppointmentDao.getAppointmentRequestByUuid(
appointmentRequestsList.get(0).getAppointmentUUID());
assertNotNull(appointmentRequests);
AppointmentRequests mycarehubAppointment =
appointmentDao.getAppointmentRequestByUuid("cf25712e-f193-48d5-a85a-5fd8d0244c71");
String expectedUUID = "cf25712e-f193-48d5-a85a-5fd8d0244c71";
assertEquals(expectedUUID, mycarehubAppointment.getUuid());
}

@Test
public void testCountAppointmentsNullSearchString() {
String searchString = "";
public void countAppointment() {
String clientPhoneNumber = "+254711223344";
Number appointmentsCount = appointmentDao.countAppointments(clientPhoneNumber);

when(dbSession.createCriteria(AppointmentRequests.class)).thenReturn(criteria);
when(criteria.setProjection(any(Projection.class))).thenReturn(criteria);
Number result = appointmentDao.countAppointments(searchString);
assertNull(result);
Long expectedCount = 1L;
assertEquals(expectedCount, appointmentsCount);
}

@Test
public void getObsByEncounterAndConcept() {

Concept concept = new Concept();
concept.setConceptId(1);
concept.setId(1);

Obs obs = obsFactory().get(0);

when(dbSession.createCriteria(Obs.class)).thenReturn(criteria);
when(criteria.add(any(Criterion.class))).thenReturn(criteria);
when(criteria.uniqueResult()).thenReturn(obs);

Obs obsOne = hibernateAppointmentDao.getObsByEncounterAndConcept(1, 1);
assertNotNull(obsOne);
}

private static List<Integer> consentedPatientIds() {
ConsentedPatient patient = new ConsentedPatient();
patient.setPatientId(1);
patient.setId(1);

ConsentedPatient patient2 = new ConsentedPatient();
patient2.setPatientId(1);
patient2.setId(1);
public void getPagedAppointments() {
String clientPhoneNumber = "+254711223344";
List<AppointmentRequests> appointmentRequestsList =
appointmentDao.getPagedAppointments(clientPhoneNumber, 1, 10);

List<ConsentedPatient> consentedPatients = new ArrayList<ConsentedPatient>();
consentedPatients.add(patient);
consentedPatients.add(patient2);

return Collections.singletonList(consentedPatients.size());
Number expectedCount = 1;
assertEquals(expectedCount, appointmentRequestsList.size());
}

private static List<Obs> obsFactory() {
Obs obsOne = new Obs();
obsOne.setId(1);
obsOne.setObsId(1);
obsOne.setConcept(conceptFactory());
obsOne.setEncounter(encounterFactory());

Obs obsTwo = new Obs();
obsTwo.setId(1);
obsTwo.setObsId(1);

List<Obs> obsList = new ArrayList<Obs>();
obsList.add(obsOne);
obsList.add(obsTwo);

return obsList;
@Test
public void getObsByEncounterAndConcept() throws Exception {
Obs obs = appointmentDao.getObsByEncounterAndConcept(3, 5089);
Integer expectedObsID = 7;
assertEquals(expectedObsID, obs.getObsId());
}

private static List<AppointmentRequests> appointmentRequestsListFactory() {
public static List<AppointmentRequests> appointmentRequestsListFactory() {
Date dt = new Date();
AppointmentRequests appointmentRequests = new AppointmentRequests();
appointmentRequests.setAppointmentUUID(String.valueOf(UUID.randomUUID()));
appointmentRequests.setId(1);
appointmentRequests.setAppointmentUUID("1234");
appointmentRequests.setId(1234);
appointmentRequests.setMycarehubId(String.valueOf(UUID.randomUUID()));
appointmentRequests.setClientName("MtuKazi");
appointmentRequests.setCccNumber("12345");
appointmentRequests.setAppointmentReason("away");
appointmentRequests.setStatus("PENDING");
appointmentRequests.setStatus("RESOLVED");
appointmentRequests.setClientContact("12345");
appointmentRequests.setDateCreated(dt);
appointmentRequests.setVoided(false);
appointmentRequests.setProgressDate(dt);
appointmentRequests.setDateResolved(dt);
appointmentRequests.setUuid("1234");

List<AppointmentRequests> appointmentRequestsList = new ArrayList<AppointmentRequests>();
appointmentRequestsList.add(appointmentRequests);
Expand All @@ -252,4 +190,14 @@ private static Concept conceptFactory() {

return concept;
}

public static ConsentedPatient consentedPatientFactory() {
ConsentedPatient consentedPatient = new ConsentedPatient();
consentedPatient.setId(1);
consentedPatient.setPatientId(1);
consentedPatient.setDateCreated(new Date());
consentedPatient.setUuid(String.valueOf(UUID.randomUUID()));

return consentedPatient;
}
}
Loading

0 comments on commit 350e8af

Please sign in to comment.