From fa4335144ebdf221ef8bf2684ff5e5aed935a43e Mon Sep 17 00:00:00 2001 From: pamcdm Date: Tue, 7 Jan 2014 16:28:36 -0200 Subject: [PATCH] UHM-974 Fix unit tests and move then to TimeSlotServiceTest --- .../db/hibernate/HibernateTimeSlotDAO.java | 8 +- .../api/AppointmentServiceTest.java | 34 - .../api/TimeSlotServiceTest.java | 822 +++++++++--------- 3 files changed, 433 insertions(+), 431 deletions(-) diff --git a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateTimeSlotDAO.java b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateTimeSlotDAO.java index dae99061..39a96188 100644 --- a/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateTimeSlotDAO.java +++ b/api/src/main/java/org/openmrs/module/appointmentscheduling/api/db/hibernate/HibernateTimeSlotDAO.java @@ -39,13 +39,15 @@ else if (fromDate != null && toDate != null && !fromDate.before(toDate)) Date startDate = (fromDate == null) ? new Date() : fromDate; String stringQuery = "SELECT timeSlot FROM TimeSlot AS timeSlot WHERE timeSlot.appointmentBlock IN(" - + " FROM AppointmentBlock WHERE :appointmentType IN elements(types)) AND voided = 0 AND endDate > :startDate ORDER BY startDate"; + + " FROM AppointmentBlock WHERE :appointmentType IN elements(types)) AND voided = 0 AND endDate > :startDate"; if (toDate != null) stringQuery += " AND endDate <= :endDate"; if (provider != null) stringQuery += " AND timeSlot.appointmentBlock.provider = :provider"; + stringQuery += " ORDER BY startDate"; + Query query = super.sessionFactory.getCurrentSession().createQuery(stringQuery) .setParameter("appointmentType", appointmentType).setParameter("startDate", startDate); @@ -53,8 +55,8 @@ else if (fromDate != null && toDate != null && !fromDate.before(toDate)) query.setParameter("endDate", toDate); if (provider != null) query.setParameter("provider", provider); - - return query.list(); + + return query.list(); } } diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java index 497d145e..7f9a3b61 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/AppointmentServiceTest.java @@ -390,38 +390,4 @@ public void shouldChangeCorrectAppointments_cleanOpenAppointments() { } - @Test - public void shouldGetAllTimeSlotsByConstraintsSortedByStartDate() throws ParseException { - AppointmentType type = service.getAppointmentType(1); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2005-01-01 00:00:00.0"); - - List result = service.getTimeSlotsByConstraintsIncludingFull(type, fromDate, null, null, null); - assertNotNull(result); - assertTrue(result.size() == 6); - - TimeSlot firstTimeSlot = result.get(0); - assertTrue(firstTimeSlot.getTimeSlotId().equals(5)); - - TimeSlot lastTimeSlot = result.get(result.size() - 1); - assertTrue(lastTimeSlot.getTimeSlotId().equals(4)); - } - - @Test - public void shouldGetOnlyAvailableTimeSlotsByConstraintsSortedByStartDate() throws ParseException { - AppointmentType type = service.getAppointmentType(1); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2005-01-01 00:00:00.0"); - - List result = service.getTimeSlotsByConstraints(type, fromDate, null, null, null); - assertNotNull(result); - assertTrue(result.size() == 4); - - TimeSlot firstTimeSlot = result.get(0); - assertTrue(firstTimeSlot.getTimeSlotId().equals(5)); - - TimeSlot lastTimeSlot = result.get(result.size() - 1); - assertTrue(lastTimeSlot.getTimeSlotId().equals(4)); - } - } diff --git a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/TimeSlotServiceTest.java b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/TimeSlotServiceTest.java index 0642a913..3a620bf2 100644 --- a/api/src/test/java/org/openmrs/module/appointmentscheduling/api/TimeSlotServiceTest.java +++ b/api/src/test/java/org/openmrs/module/appointmentscheduling/api/TimeSlotServiceTest.java @@ -1,394 +1,428 @@ -/** - * The contents of this file are subject to the OpenMRS Public License - * Version 1.0 (the "License"); you may not use this file except in - * compliance with the License. You may obtain a copy of the License at - * http://license.openmrs.org - * - * Software distributed under the License is distributed on an "AS IS" - * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the - * License for the specific language governing rights and limitations - * under the License. - * - * Copyright (C) OpenMRS, LLC. All Rights Reserved. - */ -package org.openmrs.module.appointmentscheduling.api; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.List; - -import junit.framework.Assert; - -import org.junit.Before; -import org.junit.Test; -import org.openmrs.Provider; -import org.openmrs.api.APIException; -import org.openmrs.api.context.Context; -import org.openmrs.module.appointmentscheduling.Appointment; -import org.openmrs.module.appointmentscheduling.AppointmentBlock; -import org.openmrs.module.appointmentscheduling.AppointmentType; -import org.openmrs.module.appointmentscheduling.TimeSlot; -import org.openmrs.module.appointmentscheduling.api.AppointmentService; -import org.openmrs.test.BaseModuleContextSensitiveTest; -import org.openmrs.test.Verifies; - -/** - * Tests Time Slot methods in the {@link $ AppointmentService} . - */ -public class TimeSlotServiceTest extends BaseModuleContextSensitiveTest { - - private AppointmentService service; - - private Integer amountOfTimeSlots = 4; - - @Before - public void before() throws Exception { - service = Context.getService(AppointmentService.class); - executeDataSet("standardAppointmentTestDataset.xml"); - } - - @Test - @Verifies(value = "should get all time slots", method = "getAllTimeSlots()") - public void getAllTimeSlots_shouldGetAllTimeSlots() { - List timeSlots = service.getAllTimeSlots(); - Assert.assertEquals(amountOfTimeSlots, (Integer) timeSlots.size()); - } - - @Test - @Verifies(value = "should get all time slots by a given bool whether to include voided", method = "getAllTimeSlots(boolean)") - public void getAllTimeSlots_shouldGetAllUnvoidedTimeSlots() { - List timeSlots = service.getAllTimeSlots(false); - Assert.assertEquals(3, timeSlots.size()); - } - - @Test - @Verifies(value = "should get all time slots including voided", method = "getAllTimeSlots(boolean)") - public void getAllTimeSlots_shouldGetAllIncludingVoidedTimeSlots() { - List timeSlots = service.getAllTimeSlots(true); - Assert.assertEquals(amountOfTimeSlots, (Integer) timeSlots.size()); - } - - @Test - @Verifies(value = "should save new time slot", method = "saveTimeSlot(TimeSlot)") - public void saveTimeSlot_shouldSaveNewTimeSlot() { - - AppointmentBlock appointmentBlock = service.getAppointmentBlock(1); - TimeSlot timeSlot = new TimeSlot(appointmentBlock, new Date(), new Date()); - timeSlot = service.saveTimeSlot(timeSlot); - List timeSlots = service.getAllTimeSlots(); - - Assert.assertNotNull(timeSlot); - Assert.assertEquals(amountOfTimeSlots + 1, timeSlots.size()); - } - - @Test - @Verifies(value = "should save edited time slot without adding a new row", method = "saveTimeSlot(TimeSlot)") - public void saveTimeSlot_shouldSaveEditedTimeSlot() { - TimeSlot timeSlot = service.getTimeSlot(1); - Date currentDate = new Date(); - timeSlot.setEndDate(currentDate); - timeSlot = service.saveTimeSlot(timeSlot); - Assert.assertNotNull(timeSlot); - - timeSlot = service.getTimeSlot(1); - Assert.assertEquals(currentDate, timeSlot.getEndDate()); - } - - @Test - @Verifies(value = "get correct time slot by a given id", method = "getTimeSlot(Integer timeSlotId)") - public void getTimeSlot_shouldGetCorrectTimeSlot() { - TimeSlot timeSlot = service.getTimeSlot(1); - Date startDate = timeSlot.getStartDate(); - assertNotNull(timeSlot); - assertEquals("2006-01-01 00:00:00.0", startDate.toString()); - - timeSlot = service.getTimeSlot(2); - startDate = timeSlot.getStartDate(); - assertNotNull(timeSlot); - assertEquals("2006-01-01 00:00:00.1", startDate.toString()); - - timeSlot = service.getTimeSlot(3); - startDate = timeSlot.getStartDate(); - assertNotNull(timeSlot); - assertEquals("2007-01-01 00:00:00.2", startDate.toString()); - - timeSlot = service.getTimeSlot(5); - Assert.assertNull(timeSlot); - } - - @Test - @Verifies(value = "get correct time slot by a given uuid", method = "getTimeSlotByUuid(String)") - public void getTimeSlotByUuid_shouldgetCorrectTimeSlot() { - TimeSlot timeSlot = service.getTimeSlotByUuid("c0c579b0-8e59-401d-8a4a-976a0b183604"); - Date startDate = timeSlot.getStartDate(); - assertNotNull(timeSlot); - assertEquals("2006-01-01 00:00:00.0", startDate.toString()); - - timeSlot = service.getTimeSlotByUuid("c0c579b0-8e59-401d-8a4a-976a0b183605"); - startDate = timeSlot.getStartDate(); - assertNotNull(timeSlot); - assertEquals("2006-01-01 00:00:00.1", startDate.toString()); - - timeSlot = service.getTimeSlotByUuid("c0c579b0-8e59-401d-8a4a-976a0b183606"); - startDate = timeSlot.getStartDate(); - assertNotNull(timeSlot); - assertEquals("2007-01-01 00:00:00.2", startDate.toString()); - - timeSlot = service.getTimeSlotByUuid("NOT A UUID"); - Assert.assertNull(timeSlot); - } - - @Test - @Verifies(value = "should void a time slot", method = "voidTimeSlot(TimeSlot, String)") - public void voidTimeSlot_shouldVoidTimeSlot() { - TimeSlot timeSlot = service.getTimeSlot(1); - assertNotNull(timeSlot); - assertFalse(timeSlot.isVoided()); - - service.voidTimeSlot(timeSlot, "void reason"); - - timeSlot = service.getTimeSlot(1); - assertNotNull(timeSlot); - assertTrue(timeSlot.isVoided()); - - assertEquals(amountOfTimeSlots, (Integer) service.getAllTimeSlots().size()); - } - - @Test - @Verifies(value = "should unvoid a time slot", method = "unvoidTimeSlot(TimeSlot)") - public void unvoidTimeSlot_shouldUnvoidTimeSlot() { - TimeSlot timeSlot = service.getTimeSlot(3); - assertNotNull(timeSlot); - assertTrue(timeSlot.isVoided()); - assertEquals("void reason", timeSlot.getVoidReason()); - - service.unvoidTimeSlot(timeSlot); - - timeSlot = service.getTimeSlot(3); - assertNotNull(timeSlot); - assertFalse(timeSlot.isVoided()); - assertNull(timeSlot.getVoidReason()); - - assertEquals(amountOfTimeSlots, (Integer) service.getAllTimeSlots().size()); - } - - @Test - @Verifies(value = "should delete a given time slot", method = "purgeTimeSlot(TimeSlot)") - public void purgeTimeSlot_shouldDeleteTimeSlot() { - TimeSlot timeSlot = service.getTimeSlot(4); - assertNotNull(timeSlot); - - service.purgeTimeSlot(timeSlot); - - timeSlot = service.getTimeSlot(4); - assertNull(timeSlot); - - assertEquals(amountOfTimeSlots - 1, service.getAllTimeSlots().size()); - } - - @Test - @Verifies(value = "retrieve all appointments scheduled in a given time slot", method = "getAppointmentsInTimeSlot(TimeSlot)") - public void getAppointmentsInTimeSlot_shouldGetCorrectAppointments() { - TimeSlot timeSlot = service.getTimeSlot(1); - assertNotNull(timeSlot); - - List appointments = service.getAppointmentsInTimeSlot(timeSlot); - assertNotNull(appointments); - assertEquals(2, appointments.size()); - - timeSlot = service.getTimeSlot(3); - assertNotNull(timeSlot); - appointments = service.getAppointmentsInTimeSlot(timeSlot); - assertNotNull(appointments); - assertEquals(1, appointments.size()); - - } - - @Test - @Verifies(value = "should not include voided time slots", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraintsIncludingFull_shouldNotIncludeVoidedTimeSlots() throws ParseException { - AppointmentType appointmentType = service.getAppointmentType(1); - assertNotNull(appointmentType); - List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, null, null, - null, null); - int countVoided = 0; - for (TimeSlot slot : availableTimeSlots) { - if (slot.isVoided()) - countVoided++; - } - assertEquals(countVoided, 0); - - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2000-01-01 00:00:00.0"); - Date toDate = format.parse("2012-01-01 00:00:00.0"); - availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, null, null); - countVoided = 0; - for (TimeSlot slot : availableTimeSlots) { - if (slot.isVoided()) - countVoided++; - } - assertEquals(countVoided, 0); - - Provider provider = Context.getProviderService().getProvider(1); - Assert.assertNotNull(provider); - availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, provider, - null); - countVoided = 0; - for (TimeSlot slot : availableTimeSlots) { - if (slot.isVoided()) - countVoided++; - } - - assertEquals(countVoided, 0); - } - - @Test - @Verifies(value = "should get correct time slots", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraintsIncludingFull_shouldGetCorrectTimeSlots() throws ParseException { - AppointmentType appointmentType = service.getAppointmentType(2); - assertNotNull(appointmentType); - Provider provider = Context.getProviderService().getProvider(1); - assertNotNull(provider); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2006-01-01 01:00:00.1"); - Date toDate = format.parse("2013-01-01 00:00:00.0"); - - //Filter by dates - List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, - toDate, provider, null); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(2))); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(4))); - assertTrue(availableTimeSlots.size() == 2); - - //Confirm Time Left - appointmentType = service.getAppointmentType(1); - assertNotNull(appointmentType); - fromDate = format.parse("2006-01-01 00:00:00.0"); - availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, provider, - null); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(1))); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(4))); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(2))); - assertTrue(availableTimeSlots.size() == 3); - - //Filter by provider - provider = Context.getProviderService().getProvider(2); - assertNotNull(provider); - availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, provider, - null); - assertTrue(availableTimeSlots.size() == 0); - - } - - @Test - @Verifies(value = "should get correct time slots", method = "getTimeSlotsByConstraints(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraints_shouldGetCorrectTimeSlots() throws ParseException { - AppointmentType appointmentType = service.getAppointmentType(2); - assertNotNull(appointmentType); - Provider provider = Context.getProviderService().getProvider(1); - assertNotNull(provider); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2006-01-01 00:00:00.1"); - Date toDate = format.parse("2013-01-01 00:00:00.0"); - - List availableTimeSlots = null; - - //Confirm Time Left - appointmentType = service.getAppointmentType(1); - assertNotNull(appointmentType); - fromDate = format.parse("2006-01-01 00:00:00.0"); - availableTimeSlots = service.getTimeSlotsByConstraints(appointmentType, fromDate, toDate, provider, null); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(4))); - assertTrue(availableTimeSlots.contains(service.getTimeSlot(2))); - assertTrue(availableTimeSlots.size() == 2); - - } - - @Test(expected = APIException.class) - @Verifies(value = "should throw exception if appointment type is null", method = "getTimeSlotsByConstraints(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraints_shouldThrowExcetpionIfAppointmentTypeIsNull() throws ParseException { - Provider provider = Context.getProviderService().getProvider(1); - Assert.assertNotNull(provider); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2000-01-01 00:00:00.0"); - Date toDate = format.parse("2013-01-01 00:00:00.0"); - List availableTimeSlots = service.getTimeSlotsByConstraints(null, fromDate, toDate, provider, null); - } - - @Test(expected = APIException.class) - @Verifies(value = "should throw exception if an illegal date interval was given", method = "getTimeSlotsByConstraints(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraints_shouldThrowExcetpionIfIllegalDateInterval() throws ParseException { - AppointmentType appointmentType = service.getAppointmentType(1); - assertNotNull(appointmentType); - Provider provider = Context.getProviderService().getProvider(1); - Assert.assertNotNull(provider); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2013-01-01 00:00:00.0"); - Date toDate = format.parse("2001-01-01 00:00:00.0"); - List availableTimeSlots = service.getTimeSlotsByConstraints(null, fromDate, toDate, provider, null); - } - - @Test(expected = APIException.class) - @Verifies(value = "should throw exception if appointment type is null", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraintsIncludingFull_shouldThrowExcetpionIfAppointmentTypeIsNull() throws ParseException { - Provider provider = Context.getProviderService().getProvider(1); - Assert.assertNotNull(provider); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2000-01-01 00:00:00.0"); - Date toDate = format.parse("2013-01-01 00:00:00.0"); - List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(null, fromDate, toDate, provider, - null); - } - - @Test(expected = APIException.class) - @Verifies(value = "should throw exception if an illegal date interval was given", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") - public void getTimeSlotsByConstraintsIncludingFull_shouldThrowExcetpionIfIllegalDateInterval() throws ParseException { - AppointmentType appointmentType = service.getAppointmentType(1); - assertNotNull(appointmentType); - Provider provider = Context.getProviderService().getProvider(1); - Assert.assertNotNull(provider); - SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); - Date fromDate = format.parse("2013-01-01 00:00:00.0"); - Date toDate = format.parse("2001-01-01 00:00:00.0"); - List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(null, fromDate, toDate, provider, - null); - } - - @Test - @Verifies(value = "should return correct time slots", method = "getTimeSlotsInAppointmentBlock(AppointmentBlock)") - public void getTimeSlotsInAppointmentBlock_shouldReturnCorrectTimeSlots() { - AppointmentBlock appointmentBlock = service.getAppointmentBlock(1); - assertNotNull(appointmentBlock); - - List timeSlots = service.getTimeSlotsInAppointmentBlock(appointmentBlock); - assertEquals(4, timeSlots.size()); - - //Should be empty list because appointment block is not exists - timeSlots = service.getTimeSlotsInAppointmentBlock(null); - assertEquals(0, timeSlots.size()); - - //Should get an empty list because there are no time slots associated with the given appointment block - appointmentBlock = service.getAppointmentBlock(2); - timeSlots = service.getTimeSlotsInAppointmentBlock(appointmentBlock); - assertEquals(0, timeSlots.size()); - } - - @Test - @Verifies(value = "should return correct time left in time slot", method = "getTimeLeftInTimeSlot(TimeSlot)") - public void getTimeLeftInTimeSlot_shouldReturnCorrectTimeLeft() { - Integer timeLeft = service.getTimeLeftInTimeSlot(null); - assertNull(timeLeft); - - TimeSlot timeSlot = service.getTimeSlot(1); - Assert.assertNotNull(timeSlot); - timeLeft = service.getTimeLeftInTimeSlot(timeSlot); - assertEquals((Integer) 6, timeLeft); - } -} +/** + * The contents of this file are subject to the OpenMRS Public License + * Version 1.0 (the "License"); you may not use this file except in + * compliance with the License. You may obtain a copy of the License at + * http://license.openmrs.org + * + * Software distributed under the License is distributed on an "AS IS" + * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the + * License for the specific language governing rights and limitations + * under the License. + * + * Copyright (C) OpenMRS, LLC. All Rights Reserved. + */ +package org.openmrs.module.appointmentscheduling.api; + +import junit.framework.Assert; +import org.junit.Before; +import org.junit.Test; +import org.openmrs.Provider; +import org.openmrs.api.APIException; +import org.openmrs.api.context.Context; +import org.openmrs.module.appointmentscheduling.Appointment; +import org.openmrs.module.appointmentscheduling.AppointmentBlock; +import org.openmrs.module.appointmentscheduling.AppointmentType; +import org.openmrs.module.appointmentscheduling.TimeSlot; +import org.openmrs.test.BaseModuleContextSensitiveTest; +import org.openmrs.test.Verifies; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests Time Slot methods in the {@link $ AppointmentService} . + */ +public class TimeSlotServiceTest extends BaseModuleContextSensitiveTest { + + private AppointmentService service; + + private Integer amountOfTimeSlots = 7; + + @Before + public void before() throws Exception { + service = Context.getService(AppointmentService.class); + executeDataSet("standardAppointmentTestDataset.xml"); + } + + @Test + @Verifies(value = "should get all time slots", method = "getAllTimeSlots()") + public void getAllTimeSlots_shouldGetAllTimeSlots() { + List timeSlots = service.getAllTimeSlots(); + Assert.assertEquals(amountOfTimeSlots, (Integer) timeSlots.size()); + } + + @Test + @Verifies(value = "should get all time slots by a given bool whether to include voided", method = "getAllTimeSlots(boolean)") + public void getAllTimeSlots_shouldGetAllUnvoidedTimeSlots() { + List timeSlots = service.getAllTimeSlots(false); + Assert.assertEquals(6, timeSlots.size()); + } + + @Test + @Verifies(value = "should get all time slots including voided", method = "getAllTimeSlots(boolean)") + public void getAllTimeSlots_shouldGetAllIncludingVoidedTimeSlots() { + List timeSlots = service.getAllTimeSlots(true); + Assert.assertEquals(amountOfTimeSlots, (Integer) timeSlots.size()); + } + + @Test + @Verifies(value = "should save new time slot", method = "saveTimeSlot(TimeSlot)") + public void saveTimeSlot_shouldSaveNewTimeSlot() { + + AppointmentBlock appointmentBlock = service.getAppointmentBlock(1); + TimeSlot timeSlot = new TimeSlot(appointmentBlock, new Date(), new Date()); + timeSlot = service.saveTimeSlot(timeSlot); + List timeSlots = service.getAllTimeSlots(); + + Assert.assertNotNull(timeSlot); + Assert.assertEquals(amountOfTimeSlots + 1, timeSlots.size()); + } + + @Test + @Verifies(value = "should save edited time slot without adding a new row", method = "saveTimeSlot(TimeSlot)") + public void saveTimeSlot_shouldSaveEditedTimeSlot() { + TimeSlot timeSlot = service.getTimeSlot(1); + Date currentDate = new Date(); + timeSlot.setEndDate(currentDate); + timeSlot = service.saveTimeSlot(timeSlot); + Assert.assertNotNull(timeSlot); + + timeSlot = service.getTimeSlot(1); + Assert.assertEquals(currentDate, timeSlot.getEndDate()); + } + + @Test + @Verifies(value = "get correct time slot by a given id", method = "getTimeSlot(Integer timeSlotId)") + public void getTimeSlot_shouldGetCorrectTimeSlot() { + TimeSlot timeSlot = service.getTimeSlot(1); + Date startDate = timeSlot.getStartDate(); + assertNotNull(timeSlot); + assertEquals("2006-01-01 00:00:00.0", startDate.toString()); + + timeSlot = service.getTimeSlot(2); + startDate = timeSlot.getStartDate(); + assertNotNull(timeSlot); + assertEquals("2006-01-01 00:00:00.1", startDate.toString()); + + timeSlot = service.getTimeSlot(3); + startDate = timeSlot.getStartDate(); + assertNotNull(timeSlot); + assertEquals("2007-01-01 00:00:00.2", startDate.toString()); + + timeSlot = service.getTimeSlot(8); + Assert.assertNull(timeSlot); + } + + @Test + @Verifies(value = "get correct time slot by a given uuid", method = "getTimeSlotByUuid(String)") + public void getTimeSlotByUuid_shouldgetCorrectTimeSlot() { + TimeSlot timeSlot = service.getTimeSlotByUuid("c0c579b0-8e59-401d-8a4a-976a0b183604"); + Date startDate = timeSlot.getStartDate(); + assertNotNull(timeSlot); + assertEquals("2006-01-01 00:00:00.0", startDate.toString()); + + timeSlot = service.getTimeSlotByUuid("c0c579b0-8e59-401d-8a4a-976a0b183605"); + startDate = timeSlot.getStartDate(); + assertNotNull(timeSlot); + assertEquals("2006-01-01 00:00:00.1", startDate.toString()); + + timeSlot = service.getTimeSlotByUuid("c0c579b0-8e59-401d-8a4a-976a0b183606"); + startDate = timeSlot.getStartDate(); + assertNotNull(timeSlot); + assertEquals("2007-01-01 00:00:00.2", startDate.toString()); + + timeSlot = service.getTimeSlotByUuid("NOT A UUID"); + Assert.assertNull(timeSlot); + } + + @Test + @Verifies(value = "should void a time slot", method = "voidTimeSlot(TimeSlot, String)") + public void voidTimeSlot_shouldVoidTimeSlot() { + TimeSlot timeSlot = service.getTimeSlot(1); + assertNotNull(timeSlot); + assertFalse(timeSlot.isVoided()); + + service.voidTimeSlot(timeSlot, "void reason"); + + timeSlot = service.getTimeSlot(1); + assertNotNull(timeSlot); + assertTrue(timeSlot.isVoided()); + + assertEquals(amountOfTimeSlots, (Integer) service.getAllTimeSlots().size()); + } + + @Test + @Verifies(value = "should unvoid a time slot", method = "unvoidTimeSlot(TimeSlot)") + public void unvoidTimeSlot_shouldUnvoidTimeSlot() { + TimeSlot timeSlot = service.getTimeSlot(3); + assertNotNull(timeSlot); + assertTrue(timeSlot.isVoided()); + assertEquals("void reason", timeSlot.getVoidReason()); + + service.unvoidTimeSlot(timeSlot); + + timeSlot = service.getTimeSlot(3); + assertNotNull(timeSlot); + assertFalse(timeSlot.isVoided()); + assertNull(timeSlot.getVoidReason()); + + assertEquals(amountOfTimeSlots, (Integer) service.getAllTimeSlots().size()); + } + + @Test + @Verifies(value = "should delete a given time slot", method = "purgeTimeSlot(TimeSlot)") + public void purgeTimeSlot_shouldDeleteTimeSlot() { + TimeSlot timeSlot = service.getTimeSlot(4); + assertNotNull(timeSlot); + + service.purgeTimeSlot(timeSlot); + + timeSlot = service.getTimeSlot(4); + assertNull(timeSlot); + + assertEquals(amountOfTimeSlots - 1, service.getAllTimeSlots().size()); + } + + @Test + @Verifies(value = "retrieve all appointments scheduled in a given time slot", method = "getAppointmentsInTimeSlot(TimeSlot)") + public void getAppointmentsInTimeSlot_shouldGetCorrectAppointments() { + TimeSlot timeSlot = service.getTimeSlot(1); + assertNotNull(timeSlot); + + List appointments = service.getAppointmentsInTimeSlot(timeSlot); + assertNotNull(appointments); + assertEquals(2, appointments.size()); + + timeSlot = service.getTimeSlot(3); + assertNotNull(timeSlot); + appointments = service.getAppointmentsInTimeSlot(timeSlot); + assertNotNull(appointments); + assertEquals(1, appointments.size()); + + } + + @Test + @Verifies(value = "should not include voided time slots", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraintsIncludingFull_shouldNotIncludeVoidedTimeSlots() throws ParseException { + AppointmentType appointmentType = service.getAppointmentType(1); + assertNotNull(appointmentType); + List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, null, null, + null, null); + int countVoided = 0; + for (TimeSlot slot : availableTimeSlots) { + if (slot.isVoided()) + countVoided++; + } + assertEquals(countVoided, 0); + + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2000-01-01 00:00:00.0"); + Date toDate = format.parse("2012-01-01 00:00:00.0"); + availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, null, null); + countVoided = 0; + for (TimeSlot slot : availableTimeSlots) { + if (slot.isVoided()) + countVoided++; + } + assertEquals(countVoided, 0); + + Provider provider = Context.getProviderService().getProvider(1); + Assert.assertNotNull(provider); + availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, provider, + null); + countVoided = 0; + for (TimeSlot slot : availableTimeSlots) { + if (slot.isVoided()) + countVoided++; + } + + assertEquals(countVoided, 0); + } + + @Test + @Verifies(value = "should get correct time slots", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraintsIncludingFull_shouldGetCorrectTimeSlots() throws ParseException { + AppointmentType appointmentType = service.getAppointmentType(2); + assertNotNull(appointmentType); + Provider provider = Context.getProviderService().getProvider(1); + assertNotNull(provider); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2006-01-01 01:00:00.1"); + Date toDate = format.parse("2013-01-01 00:00:00.0"); + + //Filter by dates + List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, + toDate, provider, null); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(2))); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(4))); + assertTrue(availableTimeSlots.size() == 2); + + //Confirm Time Left + appointmentType = service.getAppointmentType(1); + assertNotNull(appointmentType); + fromDate = format.parse("2006-01-01 00:00:00.0"); + availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, provider, + null); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(1))); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(4))); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(2))); + assertTrue(availableTimeSlots.size() == 3); + + //Filter by provider + provider = Context.getProviderService().getProvider(2); + assertNotNull(provider); + availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(appointmentType, fromDate, toDate, provider, + null); + assertTrue(availableTimeSlots.size() == 0); + + } + + @Test + @Verifies(value = "should get correct time slots", method = "getTimeSlotsByConstraints(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraints_shouldGetCorrectTimeSlots() throws ParseException { + AppointmentType appointmentType = service.getAppointmentType(2); + assertNotNull(appointmentType); + Provider provider = Context.getProviderService().getProvider(1); + assertNotNull(provider); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2006-01-01 00:00:00.1"); + Date toDate = format.parse("2013-01-01 00:00:00.0"); + + List availableTimeSlots = null; + + //Confirm Time Left + appointmentType = service.getAppointmentType(1); + assertNotNull(appointmentType); + fromDate = format.parse("2006-01-01 00:00:00.0"); + availableTimeSlots = service.getTimeSlotsByConstraints(appointmentType, fromDate, toDate, provider, null); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(4))); + assertTrue(availableTimeSlots.contains(service.getTimeSlot(2))); + assertTrue(availableTimeSlots.size() == 2); + + } + + @Test(expected = APIException.class) + @Verifies(value = "should throw exception if appointment type is null", method = "getTimeSlotsByConstraints(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraints_shouldThrowExcetpionIfAppointmentTypeIsNull() throws ParseException { + Provider provider = Context.getProviderService().getProvider(1); + Assert.assertNotNull(provider); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2000-01-01 00:00:00.0"); + Date toDate = format.parse("2013-01-01 00:00:00.0"); + List availableTimeSlots = service.getTimeSlotsByConstraints(null, fromDate, toDate, provider, null); + } + + @Test(expected = APIException.class) + @Verifies(value = "should throw exception if an illegal date interval was given", method = "getTimeSlotsByConstraints(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraints_shouldThrowExcetpionIfIllegalDateInterval() throws ParseException { + AppointmentType appointmentType = service.getAppointmentType(1); + assertNotNull(appointmentType); + Provider provider = Context.getProviderService().getProvider(1); + Assert.assertNotNull(provider); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2013-01-01 00:00:00.0"); + Date toDate = format.parse("2001-01-01 00:00:00.0"); + List availableTimeSlots = service.getTimeSlotsByConstraints(null, fromDate, toDate, provider, null); + } + + @Test(expected = APIException.class) + @Verifies(value = "should throw exception if appointment type is null", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraintsIncludingFull_shouldThrowExcetpionIfAppointmentTypeIsNull() throws ParseException { + Provider provider = Context.getProviderService().getProvider(1); + Assert.assertNotNull(provider); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2000-01-01 00:00:00.0"); + Date toDate = format.parse("2013-01-01 00:00:00.0"); + List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(null, fromDate, toDate, provider, + null); + } + + @Test(expected = APIException.class) + @Verifies(value = "should throw exception if an illegal date interval was given", method = "getTimeSlotsByConstraintsIncludingFull(AppointmentType, Date, Date, Provider)") + public void getTimeSlotsByConstraintsIncludingFull_shouldThrowExcetpionIfIllegalDateInterval() throws ParseException { + AppointmentType appointmentType = service.getAppointmentType(1); + assertNotNull(appointmentType); + Provider provider = Context.getProviderService().getProvider(1); + Assert.assertNotNull(provider); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2013-01-01 00:00:00.0"); + Date toDate = format.parse("2001-01-01 00:00:00.0"); + List availableTimeSlots = service.getTimeSlotsByConstraintsIncludingFull(null, fromDate, toDate, provider, + null); + } + + @Test + @Verifies(value = "should return correct time slots", method = "getTimeSlotsInAppointmentBlock(AppointmentBlock)") + public void getTimeSlotsInAppointmentBlock_shouldReturnCorrectTimeSlots() { + AppointmentBlock appointmentBlock = service.getAppointmentBlock(1); + assertNotNull(appointmentBlock); + + List timeSlots = service.getTimeSlotsInAppointmentBlock(appointmentBlock); + assertEquals(7, timeSlots.size()); + + //Should be empty list because appointment block is not exists + timeSlots = service.getTimeSlotsInAppointmentBlock(null); + assertEquals(0, timeSlots.size()); + + //Should get an empty list because there are no time slots associated with the given appointment block + appointmentBlock = service.getAppointmentBlock(2); + timeSlots = service.getTimeSlotsInAppointmentBlock(appointmentBlock); + assertEquals(0, timeSlots.size()); + } + + @Test + @Verifies(value = "should return correct time left in time slot", method = "getTimeLeftInTimeSlot(TimeSlot)") + public void getTimeLeftInTimeSlot_shouldReturnCorrectTimeLeft() { + Integer timeLeft = service.getTimeLeftInTimeSlot(null); + assertNull(timeLeft); + + TimeSlot timeSlot = service.getTimeSlot(1); + Assert.assertNotNull(timeSlot); + timeLeft = service.getTimeLeftInTimeSlot(timeSlot); + assertEquals((Integer) 6, timeLeft); + } + + @Test + @Verifies(value = "should return all time slots by constraints and non voided ordered by the earliest start date", method = "getTimeSlotsByConstraintsIncludingFull") + public void shouldGetAllTimeSlotsByConstraintsSortedByStartDate() throws ParseException { + AppointmentType type = service.getAppointmentType(1); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2005-01-01 00:00:00.0"); + + List result = service.getTimeSlotsByConstraintsIncludingFull(type, fromDate, null, null, null); + Assert.assertNotNull(result); + Assert.assertTrue(result.size() == 6); + + TimeSlot firstTimeSlot = result.get(0); + Assert.assertTrue(firstTimeSlot.getTimeSlotId().equals(5)); + + TimeSlot lastTimeSlot = result.get(result.size() - 1); + Assert.assertTrue(lastTimeSlot.getTimeSlotId().equals(4)); + } + + @Test + @Verifies(value = "should return a list of time slots available by appointment type and constraints ordered by the earliest start date", method = "getTimeSlotsByConstraints") + public void shouldGetOnlyAvailableTimeSlotsByConstraintsSortedByStartDate() throws ParseException { + AppointmentType type = service.getAppointmentType(1); + SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S"); + Date fromDate = format.parse("2005-01-01 00:00:00.0"); + + List result = service.getTimeSlotsByConstraints(type, fromDate, null, null, null); + Assert.assertNotNull(result); + Assert.assertTrue(result.size() == 4); + + TimeSlot firstTimeSlot = result.get(0); + Assert.assertTrue(firstTimeSlot.getTimeSlotId().equals(5)); + + TimeSlot lastTimeSlot = result.get(result.size() - 1); + Assert.assertTrue(lastTimeSlot.getTimeSlotId().equals(4)); + } +}