easycare.alc.service.report.HstPrescriptionReportBeanTest.java Source code

Java tutorial

Introduction

Here is the source code for easycare.alc.service.report.HstPrescriptionReportBeanTest.java

Source

package easycare.alc.service.report;

import com.google.common.collect.Lists;
import easycare.alc.model.HstPatient;
import easycare.alc.model.HstPrescription;
import easycare.alc.web.patient.PrescriptionSettings.HstTherapySettingEnum;
import easycare.common.ApplicationMessageSource;
import easycare.helper.TimeZoneHelper;
import easycare.model.Address;
import easycare.model.ContactInformation;
import easycare.model.Country;
import easycare.model.Gender;
import easycare.model.Organisation;
import easycare.model.PhoneNumber;
import easycare.model.PhoneNumberType;
import easycare.model.PrimaryContact;
import easycare.model.State;
import easycare.model.User;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.LocalDateTime;
import org.joda.time.format.DateTimeFormatter;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.runners.MockitoJUnitRunner;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;

import static easycare.alc.service.report.HstPrescriptionReportBean.REPORT_PDF_SIGNATURE_DETAILS_KEY;
import static easycare.alc.service.report.HstPrescriptionReportBean.REPORT_PDF_SIGNATURE_SIGNED_AT_KEY;
import static easycare.model.Title.DR;
import static easycare.service.report.ReportBean.JAVA_CLOCK_TIME_FORMAT_KEY;
import static easycare.service.report.ReportBean.JAVA_DATE_FORMAT_KEY;
import static easycare.util.DateTimeBuilder.localDate;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public abstract class HstPrescriptionReportBeanTest {

    public static final String SIGNATURE_FIRST_NAME = "Susan";
    public static final String SIGNATURE_LAST_NAME = "Anderson";
    public static final String SIGNATURE_USERNAME = "susana";
    public static final String SIGNATURE_NPI = "1233456";

    public static final String FORMATTED_SIGNED_CET = "11/06/2014 02:34AM";
    public static final String FORMATTED_SIGNED_TZ = "CET";
    public static final LocalDateTime SIGNED_AT = new LocalDateTime(2014, 11, 5, 17, 34, 6);
    public static final LocalDateTime SIGNED_AT_CET = new LocalDateTime(2014, 11, 6, 2, 34, 6);
    public static final String DATE_FORMAT = "MM/dd/yyyy";
    public static final String TIME_FORMAT = "hh:mma";
    public static final String FORMATTED_SIGNED_AT = "11/05/2014 05:34PM";

    @Mock
    protected ApplicationMessageSource messageSource;

    @Mock
    protected HstPatient patient;

    @Mock
    protected Organisation organisation;

    @Mock
    protected ContactInformation contactInformation;

    @Mock
    protected Address address;

    @Mock
    protected State state;

    @Mock
    protected Country country;

    @Mock
    protected TimeZoneHelper timeZoneHelper;

    @Mock
    protected DateTimeFormatter dateTimeFormatter;

    @Mock
    HstPrescription hstPrescription;

    protected static LocalDate stubbedDate = localDate("2009-01-31");

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);

        setupMockMessageSource(messageSource);
        setupMockTimeZonAndDateTimeFormatter(timeZoneHelper, dateTimeFormatter);
        setupMockPatientAndPrescription(patient, hstPrescription, organisation, contactInformation, address,
                country, state);
    }

    public static void setupMockMessageSource(ApplicationMessageSource aMessageSource) {
        when(aMessageSource.getMessage(anyString())).thenAnswer(new Answer<String>() {
            @Override
            public String answer(InvocationOnMock invocation) throws Throwable {
                return invocation.getArguments()[0] + ":{0}";
            }
        });

        when(aMessageSource.getMessage(anyString(), (Object[]) any())).thenAnswer(new Answer<String>() {
            @Override
            public String answer(InvocationOnMock invocation) throws Throwable {
                String messageKey = (String) invocation.getArguments()[0];
                Object[] args = (Object[]) invocation.getArguments()[1];
                String argList = "";
                for (Object o : args) {
                    argList = argList.concat(o.toString() + ",");
                }
                return messageKey + ":" + argList;
            }
        });
        when(aMessageSource.getMessage("java.dateFormat")).thenReturn("dd/MM/yyyy");
        when(aMessageSource.getGenderMessage(Gender.MALE.name())).thenReturn(Gender.MALE.name().toLowerCase());
        when(aMessageSource.getMessage(JAVA_DATE_FORMAT_KEY)).thenReturn(DATE_FORMAT);
        when(aMessageSource.getMessage(JAVA_CLOCK_TIME_FORMAT_KEY)).thenReturn(TIME_FORMAT);
    }

    public static void setupMockTimeZonAndDateTimeFormatter(TimeZoneHelper timeZone,
            DateTimeFormatter dtFormatter) {
        when(timeZone.convertHardwareLocalDateTimeToSystemLocalDateTime(SIGNED_AT)).thenReturn(SIGNED_AT_CET);
        when(timeZone.convertHardwareLocalDateTimeToSystemTZDateTime(SIGNED_AT))
                .thenReturn(SIGNED_AT_CET.toDateTime(DateTimeZone.forID("CET")));
        when(dtFormatter.print(SIGNED_AT)).thenReturn(FORMATTED_SIGNED_AT);
        when(dtFormatter.print(SIGNED_AT_CET)).thenReturn(FORMATTED_SIGNED_CET);
    }

    public static void setupMockPatientAndPrescription(HstPatient aPatient, HstPrescription aPrescription,
            Organisation anOrg, ContactInformation aContact, Address anAddress, Country aCountry, State aState) {

        User physician = mock(User.class);
        PrimaryContact contact = mock(PrimaryContact.class);
        when(aPatient.getPhysician()).thenReturn(physician);
        when(physician.getOrganisation()).thenReturn(anOrg);
        when(anOrg.getPrimaryContact()).thenReturn(contact);
        when(contact.getContactInformation()).thenReturn(aContact);
        when(aContact.getAddress()).thenReturn(anAddress);
        when(aState.getCountry()).thenReturn(aCountry);

        when(aPatient.getDob()).thenReturn(localDate("1990-01-01"));
        when(aPatient.getMrn()).thenReturn("mrn");
        when(aPatient.getAgeInYears(any(LocalDate.class))).thenReturn(33);
        when(aPatient.getGender()).thenReturn(Gender.MALE);
        when(aPatient.getBmi()).thenReturn(22.7);
        when(anOrg.getId()).thenReturn(5L);

        when(aPrescription.getPhysicianFirstName()).thenReturn(SIGNATURE_FIRST_NAME);
        when(aPrescription.getPhysicianLastName()).thenReturn(SIGNATURE_LAST_NAME);
        when(aPrescription.getPhysicianUsername()).thenReturn(SIGNATURE_USERNAME);
        when(aPrescription.getPhysicianLicenseNumber()).thenReturn(SIGNATURE_NPI);
        when(aPrescription.getSignedAt()).thenReturn(SIGNED_AT);

        when(aPrescription.getMaskSelectField()).thenReturn("pillows");

        when(aPrescription.getTherapyDeviceField()).thenReturn("APAP");
        when(aPrescription.getHumidifierRequiredField()).thenReturn("xxxxx");
        when(aPrescription.getTubingSelectField()).thenReturn("xxxxx");
        when(aPrescription.getFilterSelectField()).thenReturn("xxxxx");
        when(aPrescription.getAdditionalNotesField()).thenReturn("xxxxx");
        when(aPrescription.getTherapyDeviceOther()).thenReturn("other");
        when(aPrescription.getMinimumPressure()).thenReturn(4.5);
        when(aPrescription.getMaximumPressure()).thenReturn(11.7);
        when(aPrescription.getSetPressure()).thenReturn(10.8);
        when(aPrescription.isReplenishmentRequiredField()).thenReturn(true);
    }

    @Test
    public void shouldReturnPatientDetail() {
        HstPrescriptionReportBean sut = createTestObject();
        String actualPatientDetails = sut.getPatientDetail();
        String[] lines = { "hstReport.pdf.header.patientId:mrn", "hstReport.pdf.header.dob:01/01/1990",
                "hstReport.pdf.header.age:33", "hstReport.pdf.header.gender:male",
                "hstReport.pdf.header.bmi:22.7" };
        String expectedPatientDetails = StringUtils.join(lines, '\n');
        assertThat(actualPatientDetails, is(expectedPatientDetails));
    }

    @Test
    public void shouldReturnLabAddress() {
        when(organisation.getName()).thenReturn("name");
        when(address.getAddressLine1()).thenReturn("line1");
        when(address.getAddressLine2()).thenReturn("line2");
        when(address.getCitySuburb()).thenReturn("city");
        when(address.getPostcode()).thenReturn("zip");
        when(address.getState()).thenReturn(state);
        when(state.getStateCode()).thenReturn("st");
        when(country.getCountryCode()).thenReturn("us");
        when(messageSource.getMessage("State.us.st")).thenReturn("STATE");

        HstPrescriptionReportBean sut = createTestObject();
        sut.getLabAddress();
        String labAddress = sut.getLabAddress();
        String expected = "name\nline1\nline2\ncity, STATE zip";
        assertThat(labAddress, is(expected));
    }

    @Test
    public void shouldReturnLabContactDetail() {
        PhoneNumber phone = new PhoneNumber("P", PhoneNumberType.WORK);
        PhoneNumber fax = new PhoneNumber("F", PhoneNumberType.FAX);
        when(contactInformation.findPhoneNumber(PhoneNumberType.WORK)).thenReturn(phone);
        when(contactInformation.findPhoneNumber(PhoneNumberType.FAX)).thenReturn(fax);
        when(contactInformation.getEmail()).thenReturn("E");

        HstPrescriptionReportBean sut = createTestObject();
        String contactDetail = sut.getLabContactDetail();
        String expected = "hstReport.pdf.header.phone:P\nhstReport.pdf.header.fax:F\nhstReport.pdf.header.email:E";
        assertThat(contactDetail, is(expected));
    }

    @Test
    public void shouldDisplayPrescriptionTherapyDetailsForAPAP() {
        String therapyName = HstTherapySettingEnum.APAP.name();
        when(hstPrescription.getTherapyDeviceField()).thenReturn(therapyName);
        when(hstPrescription.getMinimumPressure()).thenReturn(6.0);
        when(hstPrescription.getMaximumPressure()).thenReturn(18.0);
        HstPrescriptionReportBean sut = createTestObject();

        assertThat(sut.getDeviceName(), is(messageSource.getMessage("prescriptionReport.pdf.therapy.device."
                + therapyName + (sut.getIsResmedPrescription() ? ".resmed" : ""))));
        String unit = HstTherapySettingEnum.APAP.getTherapySettings().get(0).getUnit();
        assertThat(sut.getDevicePressure(),
                is(messageSource.getMessage(
                        "prescriptionReport.pdf.therapy.device." + therapyName + ".pressure.label",
                        new String[] { unit })));
        assertThat(sut.getDeviceValue(), is("6.0/18.0"));
    }

    @Test
    public void shouldDisplayPrescriptionTherapyDetailsForCPAP() {
        String therapyName = HstTherapySettingEnum.CPAP.name();
        when(hstPrescription.getTherapyDeviceField()).thenReturn(therapyName);
        when(hstPrescription.getSetPressure()).thenReturn(8.0);
        HstPrescriptionReportBean sut = createTestObject();

        assertThat(sut.getDeviceName(), is(messageSource.getMessage("prescriptionReport.pdf.therapy.device."
                + therapyName + (sut.getIsResmedPrescription() ? ".resmed" : ""))));
        String unit = HstTherapySettingEnum.CPAP.getTherapySettings().get(0).getUnit();
        assertThat(sut.getDevicePressure(),
                is(messageSource.getMessage(
                        "prescriptionReport.pdf.therapy.device." + therapyName + ".pressure.label",
                        new String[] { unit })));
        assertThat(sut.getDeviceValue(), is("8.0"));
    }

    @Test
    public void shouldDisplayPrescriptionTherapyDetailsForOthers() {
        String therapyName = HstTherapySettingEnum.Other.name();
        when(hstPrescription.getTherapyDeviceField()).thenReturn(therapyName);
        when(hstPrescription.getTherapyDeviceOther()).thenReturn("Alternate");
        HstPrescriptionReportBean sut = createTestObject();

        if (sut.getIsResmedPrescription()) {
            assertThat(sut.getDeviceName(),
                    is(messageSource.getMessage("prescriptionReport.pdf.therapy.device.Other.resmed",
                            new String[] { "Alternate" })));
        } else {
            assertThat(sut.getDeviceName(), is(messageSource
                    .getMessage("prescriptionReport.pdf.therapy.device.Other", new String[] { "Alternate" })));
        }
        assertThat(sut.getDevicePressure(), is(""));
        assertThat(sut.getDeviceValue(), is(""));
    }

    @Test
    public void shouldDisplayPrescriptionAdditionalNotes() {
        String notes = "A prescription note";
        when(hstPrescription.getAdditionalNotesField()).thenReturn(notes);
        HstPrescriptionReportBean sut = createTestObject();
        assertThat(sut.getAdditionalNotes(), is(notes));
    }

    @Test
    public void shouldDisplaySignatureDetails() {
        String title = "Dr";
        String fullName = String.format("%s %s %s", title, SIGNATURE_FIRST_NAME, SIGNATURE_LAST_NAME);
        String expected = "Electronic Signature";

        when(messageSource.getTitleMessage(DR)).thenReturn(title);
        when(hstPrescription.getPhysicianTitle()).thenReturn(DR);

        HstPrescriptionReportBean sut = createTestObject();

        ArrayList<String> args = Lists.newArrayList(fullName, SIGNATURE_NPI);
        when(messageSource.getMessage(REPORT_PDF_SIGNATURE_DETAILS_KEY, args.toArray())).thenReturn(expected);

        String actual = sut.getSignatureDetails();

        assertThat(actual, is(expected));
    }

    @Test
    public void shouldDisplaySignedAt() {
        String expected = "Signed at";

        HstPrescriptionReportBean sut = createTestObject();

        ArrayList<String> args = Lists.newArrayList(FORMATTED_SIGNED_CET.toLowerCase(), FORMATTED_SIGNED_TZ);
        when(messageSource.getMessage(REPORT_PDF_SIGNATURE_SIGNED_AT_KEY, args.toArray())).thenReturn(expected);

        String actual = sut.getSignedAt();

        assertThat(actual, is(expected));
    }

    @Test
    public void shouldGetReportPath() {
        HstPrescriptionReportBean sut = createTestObject();
        assertThat(sut.getReportPath(), is("/report/alc/hst-prescription-report"));
    }

    protected abstract HstPrescriptionReportBean createTestObject();

}