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 bd89818
Showing 1 changed file with 63 additions and 33 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());
}

}

0 comments on commit bd89818

Please sign in to comment.