Skip to content

Commit

Permalink
Refractored test for SMSServiceTest
Browse files Browse the repository at this point in the history
  • Loading branch information
atishbeehyv123 committed Jul 7, 2023
1 parent e5ee037 commit a8e6d06
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -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<PersonName> 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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -70,6 +80,8 @@ public class RecurringAppointmentsControllerTest {

@Mock
private AppointmentMapper appointmentMapper;
@Mock
private AdministrationService administrationService;

@Mock
private SingleAppointmentRecurringPatternUpdateService singleAppointmentRecurringPatternUpdateService;
Expand All @@ -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
Expand Down

0 comments on commit a8e6d06

Please sign in to comment.