From a8e6d06b34fb7f60baf636f3e8718f464517ba30 Mon Sep 17 00:00:00 2001 From: atish160384 Date: Fri, 7 Jul 2023 16:08:01 +0530 Subject: [PATCH] Refractored test for SMSServiceTest --- .../service/impl/SMSServiceTest.java | 96 ++++++++++++------- .../controller/AppointmentControllerIT.java | 1 + .../controller/AppointmentsControllerIT.java | 1 + .../RecurringAppointmentsControllerIT.java | 1 + .../RecurringAppointmentsControllerTest.java | 18 +++- 5 files changed, 83 insertions(+), 34 deletions(-) diff --git a/api/src/test/java/org/openmrs/module/appointments/service/impl/SMSServiceTest.java b/api/src/test/java/org/openmrs/module/appointments/service/impl/SMSServiceTest.java index 4f54ea3f5..6814af3d7 100644 --- a/api/src/test/java/org/openmrs/module/appointments/service/impl/SMSServiceTest.java +++ b/api/src/test/java/org/openmrs/module/appointments/service/impl/SMSServiceTest.java @@ -1,89 +1,119 @@ package org.openmrs.module.appointments.service.impl; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.MockitoAnnotations; import org.mockito.junit.MockitoJUnitRunner; -import org.openmrs.GlobalProperty; -import org.openmrs.LocationTag; +import org.openmrs.*; import org.openmrs.api.AdministrationService; import org.openmrs.api.LocationService; import org.openmrs.api.context.Context; import org.openmrs.messagesource.MessageSourceService; import org.openmrs.module.appointments.connection.OpenmrsLogin; +import org.openmrs.module.appointments.model.*; +import org.openmrs.module.appointments.properties.AppointmentProperties; import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; -import java.util.Arrays; -import java.util.Date; -import java.util.Locale; +import java.util.*; +import static java.util.Arrays.asList; import static org.junit.Assert.assertEquals; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.*; -import static org.powermock.api.mockito.PowerMockito.when; @PowerMockIgnore("javax.management.*") @PrepareForTest(Context.class) @RunWith(PowerMockRunner.class) public class SMSServiceTest { - - @Mock - private OpenmrsLogin openmrsLogin; + public static final String PATIENT_IDENTIFIER = "GAN230901"; @Mock private AdministrationService administrationService; @Mock private MessageSourceService messageSourceService; + + @Mock + private LocationService locationService; + @Mock private AppointmentVisitLocation appointmentVisitLocation; @Mock - private LocationService locationService; - private SMSService smsService; + private Provider provider; - private GlobalProperty globalProperty; + private GlobalProperty globalPropertyHelpdesk; + @Mock + private Patient patient; + @InjectMocks + private SMSService smsService; @Before public void setup() { + MockitoAnnotations.initMocks(this); PowerMockito.mockStatic(Context.class); when(Context.getAdministrationService()).thenReturn(administrationService); when(Context.getMessageSourceService()).thenReturn(messageSourceService); when(Context.getLocationService()).thenReturn(locationService); - LocationTag visitLocationTag = new LocationTag(); - visitLocationTag.setName("Visit Location"); - when(locationService.getLocationTagByName("Visit Location")).thenReturn(visitLocationTag); - smsService =new SMSService(); + + + when(administrationService.getGlobalProperty("sms.appointmentBookingSMSTemplate")).thenReturn("Appointment for {patientName}, {identifier} on {date} for {service} is booked at {facilityname}."); + when(administrationService.getGlobalProperty("sms.teleconsultationLinkTemplate")).thenReturn("As you have chosen to book a teleconsultation-appointment click on the link {teleconsultationLink} on {date} to join the consultation."); + when(administrationService.getGlobalProperty("sms.helpdeskTemplate")).thenReturn("For any queries call us on {helpdeskNumber}."); + + String globalPropertyNameHelpdesk = "clinic.helpDeskNumber"; + globalPropertyHelpdesk = new GlobalProperty(globalPropertyNameHelpdesk, "+919999999999"); + when(administrationService.getGlobalPropertyObject(globalPropertyNameHelpdesk)).thenReturn(globalPropertyHelpdesk); + + when(administrationService.getGlobalProperty("bahmni.sms.timezone")).thenReturn("ITC"); + when(administrationService.getGlobalProperty("bahmni.sms.dateformat")).thenReturn("yyyy-MM-dd HH:mm:ss"); + + when(locationService.getLocationTagByName("Visit Location")).thenReturn(new LocationTag()); + + when(appointmentVisitLocation.getFacilityName(anyString())).thenReturn("Hospital"); + + Properties properties = new Properties(); + properties.setProperty("sms.uri", "http://example.com/sendSMS"); + AppointmentProperties.setProperties(properties); + + smsService.setAppointmentVisitLocation(appointmentVisitLocation); } @Test public void testGetAppointmentMessageWithProviders() throws Exception { - String smsTemplate = "Reminder: Appointment for {0} ({1}) on {2} with providers: {3} at {5}. Contact: {6}."; - String smsDateFormat = "yyyy-MM-dd HH:mm:ss"; - String smsTimeZone = "Asia/Kolkata"; + Appointment appointment = new Appointment(); + appointment.setStartDateTime(new Date()); + appointment.setService(new AppointmentServiceDefinition()); + + PatientIdentifier identifier = new PatientIdentifier(); + identifier.setIdentifier(PATIENT_IDENTIFIER); + PatientIdentifierType patientIdentifierType = new PatientIdentifierType(); + patientIdentifierType.setName("Patient Identifier"); + identifier.setIdentifierType(patientIdentifierType); - when(administrationService.getGlobalProperty("sms.appointmentReminderSMSTemplate")).thenReturn(smsTemplate); - when(administrationService.getGlobalProperty("bahmni.sms.timezone")).thenReturn("Asia/Kolkata"); - when(administrationService.getGlobalProperty("bahmni.sms.dateformat")).thenReturn(smsDateFormat); - String globalPropertyName = "clinic.helpDeskNumber"; - globalProperty = new GlobalProperty(globalPropertyName, "+919999999999"); - when(administrationService.getGlobalPropertyObject(globalPropertyName)).thenReturn(globalProperty); - when(messageSourceService.getMessage(smsTemplate, new Object[]{"John Doe", "12345", "2023-07-06 10:00:00", "Dr. Smith, Dr. Johnson", "Cardiology", "Hospital", "1234567890"}, Locale.ENGLISH)) - .thenReturn("Reminder: Appointment for John Doe (12345) on 2023-07-06 10:00:00 with providers: Dr. Smith, Dr. Johnson at Hospital. Contact: 1234567890."); + Patient patient = new Patient(); + patient.setUuid("patientUuid"); + PersonName name = new PersonName(); + name.setGivenName("John"); + name.setFamilyName("Doe"); + Set personNames = new HashSet<>(); + personNames.add(name); + patient.setNames(personNames); + patient.setIdentifiers(new HashSet<>(Arrays.asList(identifier))); - Date appointmentDate = new Date(); - String message = smsService.getAppointmentMessage("John", "Doe", "12345", appointmentDate, "Cardiology", - Arrays.asList("Dr. Smith", "Dr. Johnson"), null); + appointment.setPatient(patient); + appointment.setAppointmentKind(AppointmentKind.Virtual); - assertEquals("Reminder: Appointment for John Doe (12345) on null with providers: Dr. Smith,Dr. Johnson at xxxxx. Contact: +919999999999.", message); + String message = smsService.getAppointmentBookingMessage(appointment); + + assertEquals("Appointment for John Doe, GAN230901 on null for null is booked at xxxxx.As you have chosen to book a teleconsultation-appointment click on the link null on null to join the consultation.For any queries call us on +919999999999.", message); verify(appointmentVisitLocation, never()).getFacilityName(anyString()); } - } \ No newline at end of file diff --git a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java index af5ab264f..606cbcfd7 100644 --- a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java +++ b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentControllerIT.java @@ -46,6 +46,7 @@ public class AppointmentControllerIT extends BaseIntegrationTest { public void setUp() throws Exception { executeDataSet("appointmentTestData.xml"); Context.getAdministrationService().setGlobalProperty("disableDefaultAppointmentValidations", "false"); + Context.getAdministrationService().setGlobalProperty("sms.enableAppointmentBookingSMSAlert", "true"); } @Test diff --git a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentsControllerIT.java b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentsControllerIT.java index 0c1c4b482..b2518b5a1 100644 --- a/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentsControllerIT.java +++ b/omod/src/test/java/org/openmrs/module/appointments/web/controller/AppointmentsControllerIT.java @@ -48,6 +48,7 @@ public class AppointmentsControllerIT extends BaseIntegrationTest { public void setUp() throws Exception { executeDataSet("appointmentTestData.xml"); Context.getAdministrationService().setGlobalProperty("disableDefaultAppointmentValidations", "false"); + Context.getAdministrationService().setGlobalProperty("sms.enableAppointmentBookingSMSAlert", "true"); } @Test diff --git a/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerIT.java b/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerIT.java index e713606d3..d5ee86e62 100644 --- a/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerIT.java +++ b/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerIT.java @@ -46,6 +46,7 @@ public class RecurringAppointmentsControllerIT extends BaseIntegrationTest { public void setUp() throws Exception { executeDataSet("appointmentTestData.xml"); Context.getAdministrationService().setGlobalProperty("disableDefaultAppointmentValidations", "false"); + Context.getAdministrationService().setGlobalProperty("sms.enableAppointmentBookingSMSAlert", "true"); } @Test diff --git a/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerTest.java b/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerTest.java index 92c48c0a4..b60558aff 100644 --- a/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerTest.java +++ b/omod/src/test/java/org/openmrs/module/appointments/web/controller/RecurringAppointmentsControllerTest.java @@ -4,11 +4,15 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import org.openmrs.GlobalProperty; +import org.openmrs.api.AdministrationService; +import org.openmrs.api.context.Context; import org.openmrs.module.appointments.model.Appointment; import org.openmrs.module.appointments.model.AppointmentRecurringPattern; import org.openmrs.module.appointments.service.AppointmentRecurringPatternService; @@ -25,6 +29,10 @@ import org.openmrs.module.appointments.web.service.impl.SingleAppointmentRecurringPatternUpdateService; import org.openmrs.module.appointments.web.validators.RecurringPatternValidator; import org.openmrs.module.appointments.web.validators.TimeZoneValidator; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.validation.Errors; @@ -44,7 +52,9 @@ import static org.mockito.Mockito.verify; import static org.mockito.MockitoAnnotations.initMocks; import static org.powermock.api.mockito.PowerMockito.when; - +@PowerMockIgnore("javax.management.*") +@PrepareForTest(Context.class) +@RunWith(PowerMockRunner.class) public class RecurringAppointmentsControllerTest { @InjectMocks @@ -70,6 +80,8 @@ public class RecurringAppointmentsControllerTest { @Mock private AppointmentMapper appointmentMapper; + @Mock + private AdministrationService administrationService; @Mock private SingleAppointmentRecurringPatternUpdateService singleAppointmentRecurringPatternUpdateService; @@ -86,6 +98,10 @@ public class RecurringAppointmentsControllerTest { @Before public void setUp() throws Exception { initMocks(this); + PowerMockito.mockStatic(Context.class); + Mockito.when(Context.getAdministrationService()).thenReturn(administrationService); + when(Context.getService(AdministrationService.class)).thenReturn(administrationService); + when(administrationService.getGlobalPropertyObject("sms.enableAppointmentBookingSMSAlert")).thenReturn(new GlobalProperty("sms.enableAppointmentBookingSMSAlert", "true")); } @Test