diff --git a/pom.xml b/pom.xml index 5fb5fc3d9..17bb1353b 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,10 @@ ${java.version} + + maven-surefire-plugin + 2.22.2 + org.apache.maven.plugins maven-resources-plugin @@ -162,6 +166,19 @@ + + + + + org.junit + junit-bom + 5.8.2 + pom + import + + + + com.drewnoakes @@ -220,6 +237,11 @@ 4.13.2 test + + org.junit.jupiter + junit-jupiter + test + com.h2database h2 diff --git a/src/test/java/org/isf/OHApplicationContextAware.java b/src/test/java/org/isf/OHApplicationContextAware.java new file mode 100644 index 000000000..de0784d39 --- /dev/null +++ b/src/test/java/org/isf/OHApplicationContextAware.java @@ -0,0 +1,43 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +public class OHApplicationContextAware implements ApplicationContextAware { + + public static ApplicationContext applicationContext; + + public OHApplicationContextAware() { + ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); + setApplicationContext(context); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + +} diff --git a/src/test/java/org/isf/OHCoreTestCase5.java b/src/test/java/org/isf/OHCoreTestCase5.java new file mode 100755 index 000000000..3226b34c5 --- /dev/null +++ b/src/test/java/org/isf/OHCoreTestCase5.java @@ -0,0 +1,59 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf; + +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; + +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(locations = { "classpath:applicationContext.xml" }) +public abstract class OHCoreTestCase5 { + + @Autowired + private EntityManagerFactory entityManagerFactory; + + public void cleanH2InMemoryDb() { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + entityManager.getTransaction().begin(); + List showTables = entityManager.createNativeQuery("SHOW TABLES").getResultList(); + showTables + .stream() + .map(result -> (String) result[0]) + .forEach(s -> truncateTable(s, entityManager)); + entityManager.getTransaction().commit(); + entityManager.close(); + } + + public void truncateTable(String name, EntityManager entityManager) { + entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY FALSE").executeUpdate(); + entityManager.createNativeQuery("TRUNCATE TABLE " + name).executeUpdate(); + entityManager.createNativeQuery("SET REFERENTIAL_INTEGRITY TRUE").executeUpdate(); + } + +} diff --git a/src/test/java/org/isf/patient/TestMergePatient.java b/src/test/java/org/isf/patient/TestMergePatient.java index f0e2a6067..bb32e0b18 100644 --- a/src/test/java/org/isf/patient/TestMergePatient.java +++ b/src/test/java/org/isf/patient/TestMergePatient.java @@ -1,6 +1,6 @@ /* * Open Hospital (www.open-hospital.org) - * Copyright © 2006-2021 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) * * Open Hospital is a free and open source software for healthcare data management. * @@ -83,31 +83,31 @@ public class TestMergePatient extends OHCoreTestCase { private static TestWard testWard; @Autowired - PatientIoOperations patientIoOperation; + private PatientIoOperations patientIoOperation; @Autowired - PatientIoOperationRepository patientIoOperationRepository; + private PatientIoOperationRepository patientIoOperationRepository; @Autowired - PatientBrowserManager patientBrowserManager; + private PatientBrowserManager patientBrowserManager; @Autowired - ExaminationIoOperationRepository examinationIoOperationRepository; + private ExaminationIoOperationRepository examinationIoOperationRepository; @Autowired - VisitsIoOperationRepository visitsIoOperationRepository; + private VisitsIoOperationRepository visitsIoOperationRepository; @Autowired - TestPatientMergedEventListener testPatientMergedEventListener; + private TestPatientMergedEventListener testPatientMergedEventListener; @Autowired - PricesListIoOperationRepository priceListIoOperationRepository; + private PricesListIoOperationRepository priceListIoOperationRepository; @Autowired - AccountingBillIoOperationRepository accountingBillIoOperationRepository; + private AccountingBillIoOperationRepository accountingBillIoOperationRepository; @Autowired - AdmissionTypeIoOperationRepository admissionTypeIoOperationRepository; + private AdmissionTypeIoOperationRepository admissionTypeIoOperationRepository; @Autowired - DiseaseTypeIoOperationRepository diseaseTypeIoOperationRepository; + private DiseaseTypeIoOperationRepository diseaseTypeIoOperationRepository; @Autowired - DiseaseIoOperationRepository diseaseIoOperationRepository; + private DiseaseIoOperationRepository diseaseIoOperationRepository; @Autowired - AdmissionIoOperationRepository admissionIoOperationRepository; + private AdmissionIoOperationRepository admissionIoOperationRepository; @Autowired - WardIoOperationRepository wardIoOperationRepository; + private WardIoOperationRepository wardIoOperationRepository; @BeforeClass public static void setUpClass() { @@ -432,27 +432,27 @@ private void assertThatObsoletePatientWasDeletedAndMergedIsTheActiveOne(Patient assertThat(mergedPatientResult.getDeleted()).isEqualTo("N"); } - private void assertThatObsoletePatientWasNotDeletedAndIsTheActiveOne(Patient obsoletePatient) throws OHException { + private void assertThatObsoletePatientWasNotDeletedAndIsTheActiveOne(Patient obsoletePatient) { Patient obsoletePatientResult = patientIoOperationRepository.findById(obsoletePatient.getCode()).get(); assertThat(obsoletePatientResult.getDeleted()).isEqualTo("N"); } - private void assertThatVisitWasMovedFromObsoleteToMergedPatient(Visit visit, Patient mergedPatient) throws OHException { + private void assertThatVisitWasMovedFromObsoleteToMergedPatient(Visit visit, Patient mergedPatient) { Visit visitResult = visitsIoOperationRepository.findById(visit.getVisitID()).get(); assertThat(visitResult.getPatient().getCode()).isEqualTo(mergedPatient.getCode()); } - private void assertThatVisitIsStillAssignedToObsoletePatient(Visit visit, Patient obsoletePatient) throws OHException { + private void assertThatVisitIsStillAssignedToObsoletePatient(Visit visit, Patient obsoletePatient) { Visit visitResult = visitsIoOperationRepository.findById(visit.getVisitID()).get(); assertThat(visitResult.getPatient().getCode()).isEqualTo(obsoletePatient.getCode()); } - private void assertThatExaminationWasMovedFromObsoleteToMergedPatient(PatientExamination examination, Patient mergedPatient) throws OHException { + private void assertThatExaminationWasMovedFromObsoleteToMergedPatient(PatientExamination examination, Patient mergedPatient) { PatientExamination patientResult = examinationIoOperationRepository.findById(examination.getPex_ID()).get(); assertThat(patientResult.getPatient().getCode()).isEqualTo(mergedPatient.getCode()); } - private void assertThatExaminationIsStillAssignedToObsoletePatient(PatientExamination patientExamination, Patient obsoletePatient) throws OHException { + private void assertThatExaminationIsStillAssignedToObsoletePatient(PatientExamination patientExamination, Patient obsoletePatient) { PatientExamination patientResult = examinationIoOperationRepository.findById(patientExamination.getPex_ID()).get(); assertThat(patientResult.getPatient().getCode()).isEqualTo(obsoletePatient.getCode()); } diff --git a/src/test/java/org/isf/patient/data/PatientHelper.java b/src/test/java/org/isf/patient/data/PatientHelper.java new file mode 100644 index 000000000..b975254d1 --- /dev/null +++ b/src/test/java/org/isf/patient/data/PatientHelper.java @@ -0,0 +1,170 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf.patient.data; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.time.LocalDate; +import java.time.Period; +import java.util.ArrayList; +import java.util.List; + +import org.isf.OHApplicationContextAware; +import org.isf.patient.model.Patient; +import org.isf.patient.service.PatientIoOperationRepository; + +public class PatientHelper extends OHApplicationContextAware { + + public static Patient[] PATIENT_DATA_TABLE = null; + + private PatientIoOperationRepository patientIoOperationRepository; + + public PatientHelper() { + super(); + patientIoOperationRepository = (PatientIoOperationRepository) applicationContext.getBean("patientIoOperationRepository"); + + PATIENT_DATA_TABLE = new Patient[] { + new Patient("Isaiah", "Alford", LocalDate.parse("2021-04-22"), 0, "", 'M', "134 Entebbe Rd.", "Ssabagabo", "", "33532330", + "Emily", 'U', "Cooper", 'A', "AB-", 'Y', 'U', "", "unknown", "unknown"), + new Patient("Bale", "Burch", LocalDate.parse("2021-06-24"), 0, "", 'M', "165 Galiraya Road", "Busolwe", "", "53936466", + "Kaylee", 'D', "Eli", 'A', "O-", 'N', 'N', "", "unknown", "unknown"), + new Patient("Andrea", "Lawson", LocalDate.parse("2021-07-20"), 0, "", 'F', "100 Dokolo Rd.", "Kanoni", "", "32445036296", + "Natalie", 'D', "Andrew", 'A', "A-", 'N', 'N', "", "unknown", "unknown"), + new Patient("Ashley", "Wright", LocalDate.parse("2021-05-11"), 0, "", 'F', "18 Mbarara Bypass Road", "Kiryandongo", "", "53320192", + "Julia", 'D', "Adam", 'D', "AB+", 'Y', 'N', "", "unknown", "unknown"), + new Patient("Kaikara", "Garner", LocalDate.parse("2018-08-24"), 3, "", 'M', "71 Acholibur St.", "Kayunga", "", "32397908482", + "Layla", 'A', "Henry", 'U', "O+", 'Y', 'U', "", "unknown", "unknown"), + new Patient("Nathan", "Park", LocalDate.parse("2019-10-11"), 2, "", 'M', "47 Galiraya Road", "Namutumba", "", "05469155", + "Olivia", 'U', "Balondemu", 'D', "A+", 'N', 'U', "", "unknown", "unknown"), + new Patient("Sophie", "Atkinson", LocalDate.parse("2019-12-05"), 2, "", 'F', "197 Mubende Rd.", "Isingiro", "", "31583796942", + "Emma", 'U', "Bryson", 'D', "AB+", 'Y', 'U', "", "unknown", "unknown"), + new Patient("Victoria", "Francis", LocalDate.parse("2018-10-24"), 3, "", 'F', "105 Kyenjojo Rd.", "Kyotera", "", "9080261246", + "Samantha", 'D', "Gavin", 'U', "B-", 'Y', 'U', "", "unknown", "unknown"), + new Patient("Logan", "Melendez", LocalDate.parse("2011-07-04"), 10, "", 'M', "65 Kaiso Rd.", "Luweero", "", "13850080", + "Dembe", 'U', "Julian", 'A', "A+", 'N', 'U', "", "unknown", "unknown"), + new Patient("Charles", "Watson", LocalDate.parse("2012-03-12"), 9, "", 'M', "153 Rukungiri Rd.", "Abim", "", "7694177734", + "Jasmine", 'D', "Ryan", 'D', "O+", 'Y', 'N', "", "unknown", "unknown"), + new Patient("Jacob", "Adams", LocalDate.parse("2016-01-25"), 6, "", 'M', "38 Fort Portal St.", "Pader", "", "0123738433", + "Emma", 'U', "Cameron", 'U', "A-", 'N', 'U', "", "unknown", "unknown"), + new Patient("Charlotte", "Nunez", LocalDate.parse("2015-09-13"), 6, "", 'F', "188 Kaiso Rd.", "Ibanda", "", "46062967", + "Arianna", 'A', "Gavin", 'D', "Unknown", 'Y', 'N', "", "unknown", "unknown"), + new Patient("Abbo", "Wynn", LocalDate.parse("2014-03-25"), 7, "", 'F', "15 Tonya Road", "Namayingo", "", "59007137", + "Audrey", 'U', "Charles", 'U', "Unknown", 'N', 'U', "", "unknown", "unknown"), + new Patient("Mia", "Perkins", LocalDate.parse("2011-09-02"), 10, "", 'F', "38 Katosi Road", "Bukedea", "", "92602464", + "Amelia", 'U', "Bryson", 'U', "AB+", 'N', 'U', "", "unknown", "unknown"), + new Patient("David", "Taylor", LocalDate.parse("2005-10-02"), 16, "", 'M', "60 Masaka Rd.", "Mbale", "", "8136095828", + "Faith", 'U', "Parker", 'A', "AB+", 'Y', 'U', "29504582438", "unknown", "unknown"), + new Patient("Eli", "Donaldson", LocalDate.parse("2001-06-18"), 20, "", 'M', "55 Rwekunye Rd.", "Kalongo", "", "96384905445", + "Sarah", 'U', "Xavier", 'D', "A+", 'Y', 'U', "20133901514", "Married", "Mechanic"), + new Patient("Hunter", "Solomon", LocalDate.parse("2002-11-14"), 19, "", 'M', "82 Nimule Road", "Fort Portal", "", "4466498864", + "Julia", 'D', "Grayson", 'A', "AB-", 'N', 'N', "48512357054", "unknown", "unknown"), + new Patient("Austin", "Sanders", LocalDate.parse("2001-12-08"), 20, "", 'M', "45 Mbale Rd.", "Apac", "", "90066587", + "Natukunda", 'U', "Liam", 'D', "O-", 'Y', 'U', "11900098927", "Married", "Farming"), + new Patient("Maya", "Whitfield", LocalDate.parse("2007-06-15"), 14, "", 'F', "40 Entebbe Rd.", "Mayuge", "", "70669061213", + "Brooklyn", 'D', "Landon", 'A', "O+", 'Y', 'N', "", "unknown", "unknown"), + new Patient("Molly", "Brock", LocalDate.parse("2001-07-29"), 20, "", 'F', "145 Ntungamo Rd.", "Mitooma", "", "59191252975", + "Sofia", 'A', "Cooper", 'D', "AB-", 'N', 'N', "67934855298", "Widowed", "Homemaker"), + new Patient("Samantha", "Pollard", LocalDate.parse("1997-06-17"), 24, "", 'F', "26 Dokolo Rd.", "Alebtong", "", "06869564034", + "Chloe", 'D', "Carson", 'U', "A-", 'Y', 'U', "00015661712", "Widowed", "Unknown"), + new Patient("Claire", "Jensen", LocalDate.parse("1999-08-10"), 22, "", 'F', "13 Tonya Road", "Buliisa", "", "35533963", + "Bacia", 'U', "Connor", 'D', "AB-", 'N', 'U', "95621441883", "Married", "Other"), + new Patient("Matthew", "Crawford", LocalDate.parse("1977-07-01"), 44, "", 'M', "60 Kisoro Road", "Masaka", "", "86260997", + "Grace", 'D', "Luis", 'A', "AB-", 'Y', 'N', "77855263050", "Widowed", "Farming"), + new Patient("Noah", "Armstrong", LocalDate.parse("1976-01-12"), 46, "", 'M', "122 Kikagati Road", "Adjumani", "", "2019363843", + "Taylor", 'D', "Dominic", 'A', "O+", 'Y', 'N', "64926450736", "Widowed", "Farming"), + new Patient("Angel", "Hodges", LocalDate.parse("1980-08-25"), 41, "", 'M', "73 Bundibugyo Road", "Ntungamo", "", "30694691080", + "Gabriella", 'D', "Nathan", 'A', "B-", 'N', 'N', "08009506460", "Single", "Medicine"), + new Patient("Jack", "Wyatt", LocalDate.parse("1986-08-15"), 35, "", 'M', "13 Kabale Rd.", "Butaleja", "", "80036397546", + "Zoey", 'U', "Jose", 'A', "O-", 'N', 'U', "39600123632", "Unknown", "Farming"), + new Patient("Jackson", "Padilla", LocalDate.parse("1966-04-08"), 55, "", 'M', "123 Vurra Rd.", "Bugembe", "", "27048635875", + "Lillian", 'U', "Aiden", 'U', "B-", 'N', 'U', "00007313969", "Unknown", "Janitorial Services"), + new Patient("Zachary", "Grant", LocalDate.parse("1994-12-03"), 27, "", 'M', "183 Lwakhakha St.", "Alebtong", "", "2095709929", + "Madelyn", 'D', "Kayden", 'D', "Unknown", 'N', 'N', "22677615518", "Unknown", "Medicine"), + new Patient("Kaylee", "Wade", LocalDate.parse("1966-06-04"), 55, "", 'F', "126 Lokitanyala Road", "Nakasongola", "", "2235711609", + "Alexis", 'A', "Grayson", 'A', "AB-", 'Y', 'Y', "55157320712", "Divorced", "Unknown"), + new Patient("Molly", "Massey", LocalDate.parse("1987-11-18"), 34, "", 'F', "33 Ntungamo Rd.", "Sironko", "", "40550531684", + "Abbo", 'D', "Nathaniel", 'D', "B+", 'N', 'N', "58591629576", "Single", "Food/Hospitality"), + new Patient("Addison", "Goff", LocalDate.parse("1968-10-30"), 53, "", 'F', "187 Matugga Rd.", "Rakai", "", "14338164502", + "Morgan", 'U', "Ethan", 'D', "AB+", 'Y', 'U', "46518903166", "Single", "Unknown"), + new Patient("Arianna", "Chan", LocalDate.parse("1963-12-24"), 58, "", 'F', "87 Mbale Rd.", "Mitooma", "", "97022069057", + "Madeline", 'U', "Aaron", 'U', "O+", 'Y', 'U', "71735788441", "Divorced", "Homemaker"), + new Patient("Aria", "Watson", LocalDate.parse("1974-12-08"), 47, "", 'F', "23 Nimule Road", "Masaka", "", "75317264475", + "Sofia", 'A', "Blake", 'D', "AB-", 'Y', 'N', "67848140477", "Divorced", "Other"), + new Patient("Melanie", "Gay", LocalDate.parse("1967-02-25"), 54, "", 'F', "141 Acholibur St.", "Oyam", "", "75502833076", + "Ariana", 'A', "Gabriel", 'D', "O-", 'Y', 'N', "60957304684", "Widowed", "Medicine"), + new Patient("Ryder", "Wong", LocalDate.parse("1956-08-21"), 65, "", 'M', "189 Katuna Road", "Soroti", "", "79167110677", + "Brianna", 'A', "Michael", 'D', "A-", 'Y', 'N', "41619319887", "Married", "Other"), + new Patient("Acanit", "Moore", LocalDate.parse("1942-07-04"), 79, "", 'F', "50 Lwakhakha St.", "Budaka", "", "71924245419", + "Kimberly", 'A', "Jackson", 'A', "B-", 'N', 'N', "00582998832", "Single", "Food/Hospitality") + }; + } + + public List createPatient() { + return createPatient(PATIENT_DATA_TABLE.length); + } + + public List createPatient(int numberOfInstances) { + assertThat(numberOfInstances) + .isPositive() + .isLessThanOrEqualTo(PATIENT_DATA_TABLE.length); + List codes = new ArrayList<>(numberOfInstances); + LocalDate now = LocalDate.now(); + for (int idx = 0; idx < numberOfInstances; idx++) { + // ensure the age is correct with the corrent year + LocalDate birthdate = PATIENT_DATA_TABLE[idx].getBirthDate(); + Period period = birthdate.until(now); + int age = period.getYears(); + PATIENT_DATA_TABLE[idx].setAge(age); + Patient patient = PATIENT_DATA_TABLE[idx]; + Patient savedPatient = patientIoOperationRepository.saveAndFlush(patient); + codes.add(savedPatient.getCode()); + } + return codes; + } + + public void checkPatientInDb(Integer code) { + Patient patient = patientIoOperationRepository.findById(code).orElse(null); + assertThat(patient).isNotNull(); + int row = code - 1; + assertThat(patient.getFirstName()).isEqualTo(PATIENT_DATA_TABLE[row].getFirstName()); + assertThat(patient.getSecondName()).isEqualTo(PATIENT_DATA_TABLE[row].getSecondName()); + assertThat(patient.getBirthDate()).isEqualTo(PATIENT_DATA_TABLE[row].getBirthDate()); + assertThat(patient.getAge()).isEqualTo(PATIENT_DATA_TABLE[row].getAge()); + assertThat(patient.getAgetype()).isEqualTo(PATIENT_DATA_TABLE[row].getAgetype()); + assertThat(patient.getSex()).isEqualTo(PATIENT_DATA_TABLE[row].getSex()); + assertThat(patient.getAddress()).isEqualTo(PATIENT_DATA_TABLE[row].getAddress()); + assertThat(patient.getCity()).isEqualTo(PATIENT_DATA_TABLE[row].getCity()); + assertThat(patient.getNextKin()).isEqualTo(PATIENT_DATA_TABLE[row].getNextKin()); + assertThat(patient.getTelephone()).isEqualTo(PATIENT_DATA_TABLE[row].getTelephone()); + assertThat(patient.getMotherName()).isEqualTo(PATIENT_DATA_TABLE[row].getMotherName()); + assertThat(patient.getMother()).isEqualTo(PATIENT_DATA_TABLE[row].getMother()); + assertThat(patient.getFatherName()).isEqualTo(PATIENT_DATA_TABLE[row].getFatherName()); + assertThat(patient.getFather()).isEqualTo(PATIENT_DATA_TABLE[row].getFather()); + assertThat(patient.getBloodType()).isEqualTo(PATIENT_DATA_TABLE[row].getBloodType()); + assertThat(patient.getHasInsurance()).isEqualTo(PATIENT_DATA_TABLE[row].getHasInsurance()); + assertThat(patient.getParentTogether()).isEqualTo(PATIENT_DATA_TABLE[row].getParentTogether()); + assertThat(patient.getTaxCode()).isEqualTo(PATIENT_DATA_TABLE[row].getTaxCode()); + assertThat(patient.getMaritalStatus()).isEqualTo(PATIENT_DATA_TABLE[row].getMaritalStatus()); + assertThat(patient.getProfession()).isEqualTo(PATIENT_DATA_TABLE[row].getProfession()); + } + +} diff --git a/src/test/java/org/isf/patient/manager/PatientManagerTests.java b/src/test/java/org/isf/patient/manager/PatientManagerTests.java new file mode 100644 index 000000000..05bbc6b94 --- /dev/null +++ b/src/test/java/org/isf/patient/manager/PatientManagerTests.java @@ -0,0 +1,436 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf.patient.manager; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.isf.patient.data.PatientHelper.PATIENT_DATA_TABLE; + +import java.time.LocalDate; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.assertj.core.api.Condition; +import org.isf.OHCoreTestCase5; +import org.isf.patient.data.PatientHelper; +import org.isf.patient.model.Patient; +import org.isf.patient.service.PatientIoOperations; +import org.isf.utils.exception.OHServiceException; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class PatientManagerTests extends OHCoreTestCase5 { + + private static PatientHelper createPatient; + + @Autowired + private PatientIoOperations patientIoOperation; + @Autowired + private PatientBrowserManager patientBrowserManager; + + @BeforeAll + public void beforeAll() { + createPatient = new PatientHelper(); + } + + @BeforeEach + public void beforeEach() { + cleanH2InMemoryDb(); + } + + private Pageable createPageRequest() { + return PageRequest.of(0, 10); // Page size 10 + } + + @Test + void getPatients() throws Exception { + createPatient.createPatient(); + List patients = patientBrowserManager.getPatient(); + createPatient.checkPatientInDb(patients.get(patients.size() - 1).getCode()); + } + + @Test + void getPatientsByParams() throws Exception { + createPatient.createPatient(); + Map params = new HashMap<>(); + params.put("firstName", PATIENT_DATA_TABLE[2].getFirstName()); + params.put("birthDate", PATIENT_DATA_TABLE[2].getBirthDate().atStartOfDay()); + params.put("address", PATIENT_DATA_TABLE[2].getAddress()); + List patients = patientBrowserManager.getPatients(params); + assertThat(patients.size()).isPositive(); + } + + @Test + void getPatientsPageable() throws Exception { + createPatient.createPatient(15); + + // First page of 10 + List patients = patientBrowserManager.getPatient(0, 10); + assertThat(patients).hasSize(10); + createPatient.checkPatientInDb(patients.get(patients.size() - 1).getCode()); + + // Go get the next page or 10 + patients = patientBrowserManager.getPatient(1, 10); + assertThat(patients).hasSize(5); + } + + @Test + void getPatientsByOneOfFieldsLike() throws Exception { + createPatient.createPatient(); + // Pay attention that query return with PAT_ID descendant + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(null); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeFirstName() throws Exception { + List codes = createPatient.createPatient(7); + Patient foundPatient = patientIoOperation.getPatient(codes.get(3)); + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getFirstName()); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeMiddleOfFirstName() throws Exception { + List codes = createPatient.createPatient(5); + Patient foundPatient = patientIoOperation.getPatient(codes.get(2)); + + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike( + foundPatient.getFirstName().substring(1, foundPatient.getFirstName().length() - 2)); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeSecondName() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(4)); + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeNote() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(8)); + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeTaxCode() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(2)); + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getTaxCode()); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeNotExistingStringShouldNotFindAnything() throws Exception { + createPatient.createPatient(); + List patients = patientBrowserManager.getPatientsByOneOfFieldsLike("dupaxyzzy"); + assertThat(patients).isEmpty(); + } + + @Test + void getPatientByName() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(2)); + Patient patient = patientBrowserManager.getPatientByName(foundPatient.getName()); + assertThat(patient.getName()).isEqualTo(foundPatient.getName()); + } + + @Test + void getPatientByNameDoesNotExist() throws Exception { + assertThat(patientBrowserManager.getPatientByName("someUnusualNameThatWillNotBeFound")).isNull(); + } + + @Test + void getPatientById() throws Exception { + List codes = createPatient.createPatient(6); + Patient foundPatient = patientIoOperation.getPatient(codes.get(4)); + Patient patient = patientBrowserManager.getPatientById(codes.get(4)); + assertThat(patient.getName()).isEqualTo(foundPatient.getName()); + } + + @Test + void getPatienByIdDoesNotExist() throws Exception { + assertThat(patientBrowserManager.getPatientById(-987654321)).isNull(); + } + + @Test + void getPatientAll() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(0)); + Patient patient = patientBrowserManager.getPatientAll(codes.get(0)); + assertThat(patient.getName()).isEqualTo(foundPatient.getName()); + } + + @Test + void saveNewPatient() throws Exception { + assertThat(patientBrowserManager.savePatient(PATIENT_DATA_TABLE[0])).isNotNull(); + } + + @Test + void updatePatient() throws Exception { + List codes = createPatient.createPatient(); + Patient patient = patientIoOperation.getPatient(codes.get(6)); + patient.setFirstName("someNewFirstName"); + Patient updatedPatient = patientBrowserManager.savePatient(patient); + assertThat(updatedPatient).isNotNull(); + assertThat(updatedPatient.getFirstName()).isEqualTo(patient.getFirstName()); + } + + @Test + void deletePatient() throws Exception { + List codes = createPatient.createPatient(); + Patient patient = patientIoOperation.getPatient(codes.get(1)); + assertThat(patientBrowserManager.deletePatient(patient)).isTrue(); + } + + @Test + void isNamePresent() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(8)); + assertThat(patientBrowserManager.isNamePresent(foundPatient.getName())).isTrue(); + } + + @Test + void isNamePresentNotFound() throws Exception { + assertThat(patientBrowserManager.isNamePresent("someNameWeAreSureDoesNotExist")).isFalse(); + } + + @Test + void getNextPatientCode() throws Exception { + List codes = createPatient.createPatient(); + int max = patientBrowserManager.getNextPatientCode(); + assertThat((codes.size() + 1)).isEqualTo(max); + } + + @Test + void getMaritalList() { + resetHashMaps(); + String[] maritalDescriptionList = patientBrowserManager.getMaritalList(); + assertThat(maritalDescriptionList).isNotEmpty(); + } + + @Test + void getMaritalTranslated() { + resetHashMaps(); + // TODO: if resource bundles are made avaiable in core then the values being compared will need to change + assertThat(patientBrowserManager.getMaritalTranslated(null)).isEqualTo("angal.patient.maritalstatusunknown.txt"); + assertThat(patientBrowserManager.getMaritalTranslated("someKeyNotInTheList")).isEqualTo("angal.patient.maritalstatusunknown.txt"); + assertThat(patientBrowserManager.getMaritalTranslated("married")).isEqualTo("angal.patient.maritalstatusmarried.txt"); + } + + @Test + void getMaritalKey() { + resetHashMaps(); + // TODO: if resource bundles are made avaiable in core then the values being compared will need to change + assertThat(patientBrowserManager.getMaritalKey(null)).isEqualTo("undefined"); + assertThat(patientBrowserManager.getMaritalKey("someKeyNotInTheList")).isEqualTo("undefined"); + assertThat(patientBrowserManager.getMaritalKey("angal.patient.maritalstatusmarried.txt")).isEqualTo("married"); + } + + @Test + void getProfessionList() { + resetHashMaps(); + String[] maritalDescriptionList = patientBrowserManager.getProfessionList(); + assertThat(maritalDescriptionList).isNotEmpty(); + } + + @Test + void getProfessionTranslated() { + resetHashMaps(); + // TODO: if resource bundles are made avaiable in core then the values being compared will need to change + assertThat(patientBrowserManager.getProfessionTranslated(null)).isEqualTo("angal.patient.profession.unknown.txt"); + assertThat(patientBrowserManager.getProfessionTranslated("someKeyNotInTheList")).isEqualTo("angal.patient.profession.unknown.txt"); + assertThat(patientBrowserManager.getProfessionTranslated("mining")).isEqualTo("angal.patient.profession.mining.txt"); + } + + @Test + void getProfessionKey() { + resetHashMaps(); + // TODO: if resource bundles are made avaiable in core then the values being compared will need to change + assertThat(patientBrowserManager.getProfessionKey(null)).isEqualTo("undefined"); + assertThat(patientBrowserManager.getProfessionKey("someKeyNotInTheList")).isEqualTo("undefined"); + assertThat(patientBrowserManager.getProfessionKey("angal.patient.profession.mining.txt")).isEqualTo("mining"); + } + + @Test + void patientValidationNoFirstName() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(7); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setFirstName(""); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationNullFirstName() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(8); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setFirstName(null); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationNoSecondName() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(9); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setSecondName(""); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationNullSecondName() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(10); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setSecondName(null); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationBirthDateNull() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(11); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setBirthDate(null); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationBirthDateTooFarInFuture() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(12); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setBirthDate(LocalDate.of(999, 1, 1)); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationAgeLessThanZero() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(13); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setBirthDate(null); + patient.setAge(-1); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationAgeToHigh() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(14); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setBirthDate(null); + patient.setAge(201); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + @Test + void patientValidationSexEmpty() { + assertThatThrownBy(() -> { + List codes = createPatient.createPatient(15); + Patient patient = patientIoOperation.getPatient(codes.get(codes.size() - 1)); + + patient.setSex(' '); + + patientBrowserManager.savePatient(patient); + }) + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); + } + + private void resetHashMaps() { + patientBrowserManager.maritalHashMap = null; + patientBrowserManager.professionHashMap = null; + } + +} diff --git a/src/test/java/org/isf/patient/model/PatientTests.java b/src/test/java/org/isf/patient/model/PatientTests.java new file mode 100644 index 000000000..e0ca24927 --- /dev/null +++ b/src/test/java/org/isf/patient/model/PatientTests.java @@ -0,0 +1,270 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf.patient.model; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.File; +import java.nio.file.Files; +import java.time.LocalDate; +import java.time.Period; +import java.util.Calendar; +import java.util.List; + +import org.isf.OHCoreTestCase5; +import org.isf.opd.model.Opd; +import org.isf.opd.test.TestOpd; +import org.isf.patient.data.PatientHelper; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class PatientTests extends OHCoreTestCase5 { + + private static PatientHelper createPatient; + + @BeforeAll + public void beforeAll() { + createPatient = new PatientHelper(); + } + + @BeforeEach + public void beforeEach() { + cleanH2InMemoryDb(); + } + + @Test + void gets() { + List codes = createPatient.createPatient(20); + for (Integer code : codes) { + createPatient.checkPatientInDb(code); + } + } + + @Test + void sets() { + String firstName = "TestFirstName"; + String secondName = "TestSecondName"; + LocalDate birthDate = LocalDate.of(1984, Calendar.AUGUST, 14); + Period period = birthDate.until(LocalDate.now()); + int age = period.getYears(); + String agetype = "Date"; + char sex = 'F'; + String address = "TestAddress"; + String city = "TestCity"; + String nextKin = "TestNextKin"; + String telephone = "TestTelephone"; + String motherName = "TestMotherName"; + char mother = 'A'; + String fatherName = "TestFatherName"; + char father = 'D'; + String bloodType = "O+"; + char hasInsurance = 'Y'; + char parentTogether = 'U'; + String taxCode = "TestTaxCode"; + String maritalStatus = "divorced"; + String profession = "business"; + + Patient patient = new Patient(); + patient.setFirstName(firstName); + patient.setSecondName(secondName); + patient.setBirthDate(birthDate); + patient.setAge(age); + patient.setAgetype(agetype); + patient.setSex(sex); + patient.setAddress(address); + patient.setCity(city); + patient.setNextKin(nextKin); + patient.setTelephone(telephone); + patient.setMotherName(motherName); + patient.setMother(mother); + patient.setFatherName(fatherName); + patient.setFather(father); + patient.setBloodType(bloodType); + patient.setHasInsurance(hasInsurance); + patient.setParentTogether(parentTogether); + patient.setTaxCode(taxCode); + patient.setMaritalStatus(maritalStatus); + patient.setProfession(profession); + patient.setPatientProfilePhoto(null); + + assertThat(patient.getFirstName()).isEqualTo(firstName); + assertThat(patient.getSecondName()).isEqualTo(secondName); + assertThat(patient.getBirthDate()).isEqualTo(birthDate); + assertThat(patient.getAge()).isEqualTo(age); + assertThat(patient.getAgetype()).isEqualTo(agetype); + assertThat(patient.getSex()).isEqualTo(sex); + assertThat(patient.getAddress()).isEqualTo(address); + assertThat(patient.getCity()).isEqualTo(city); + assertThat(patient.getNextKin()).isEqualTo(nextKin); + assertThat(patient.getTelephone()).isEqualTo(telephone); + assertThat(patient.getMotherName()).isEqualTo(motherName); + assertThat(patient.getMother()).isEqualTo(mother); + assertThat(patient.getFatherName()).isEqualTo(fatherName); + assertThat(patient.getFather()).isEqualTo(father); + assertThat(patient.getBloodType()).isEqualTo(bloodType); + assertThat(patient.getHasInsurance()).isEqualTo(hasInsurance); + assertThat(patient.getParentTogether()).isEqualTo(parentTogether); + assertThat(patient.getTaxCode()).isEqualTo(taxCode); + assertThat(patient.getMaritalStatus()).isEqualTo(maritalStatus); + assertThat(patient.getProfession()).isEqualTo(profession); + assertThat(patient.getPatientProfilePhoto()).isNull(); + } + + @Test + void opdConstructor() throws Exception { + Opd opd = new TestOpd().setup(null, null, false); + Patient patient = new Patient(opd); + + assertThat(patient.getSex()).isEqualTo('F'); + assertThat(patient.getCode()).isNull(); + assertThat(patient.getBirthDate()).isNull(); + + assertThat(patient.getDeleted()).isEqualTo("N"); + patient.setDeleted("Y"); + assertThat(patient.getDeleted()).isEqualTo("Y"); + } + + @Test + void patientConstructor() { + Patient patient = new Patient(99, "firstName", "secondName", "name", null, 99, " ", 'F', "address", + "city", "nextOfKin", "noPhone", "note", "motherName", ' ', "fatherName", ' ', + "bloodType", ' ', ' ', "personalCode", "maritalStatus", "profession"); + + assertThat(patient.getCode()).isEqualTo(99); + assertThat(patient.getSex()).isEqualTo('F'); + assertThat(patient.getBirthDate()).isNull(); + + assertThat(patient.getLock()).isZero(); + patient.setLock(99); + assertThat(patient.getLock()).isEqualTo(99); + } + + @Test + void patientGetSearchString() { + Patient patient = new Patient(99, "testFirstname", "testSecondname", "testFirstname testSecondname", null, 99, " ", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", null, "motherName", ' ', "fatherName", ' ', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + + assertThat(patient.getSearchString()).isEqualTo("99 testfirstname testsecondname testcity testaddress testTelephone testtaxcode "); + } + + @Test + void patientGetInformations() { + Patient patient = new Patient("testFirstname", "testSecondname", LocalDate.now(), 99, "", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxCode", "maritalStatus", + "profession"); + + patient.setNote("someNote"); + assertThat(patient.getInformations()).isEqualTo("testCity - testAddress - testTelephone - someNote - testTaxCode"); + } + + @Test + void patientEquals() { + Patient patient = new Patient("testFirstname", "testSecondname", LocalDate.now(), 99, "", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + assertThat(patient.equals(patient)).isTrue(); + assertThat(patient) + .isNotNull() + .isNotEqualTo("someString"); + + Patient patient2 = new Patient("testFirstname2", "testSecondname2", LocalDate.now(), 99, "", 'M', "testAddress2", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + patient.setCode(1); + patient2.setCode(2); + assertThat(patient).isNotEqualTo(patient2); + patient2.setCode(patient.getCode()); + assertThat(patient).isEqualTo(patient2); + } + + @Test + void patientHashCode() { + Patient patient = new Patient("testFirstname", "testSecondname", LocalDate.now(), 99, "", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + patient.setCode(1); + // compute + int hashCode = patient.hashCode(); + assertThat(hashCode).isEqualTo(23 * 133 + 1); + // check stored value + assertThat(patient.hashCode()).isEqualTo(hashCode); + + Patient patient2 = new Patient("testFirstname2", "testSecondname2", LocalDate.now(), 99, "", 'M', "testAddress2", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + patient2.setCode(null); + assertThat(patient2.hashCode()).isEqualTo(23 * 133); + } + + @Test + void profilePhoto() throws Exception { + Patient patient = new Patient("testFirstname", "testSecondname", LocalDate.now(), 99, "", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + PatientProfilePhoto patientProfilePhoto = new PatientProfilePhoto(); + + File file = new File(getClass().getResource("patient.jpg").getFile()); + byte[] bytes = Files.readAllBytes(file.toPath()); + patientProfilePhoto.setPhoto(bytes); + assertThat(patientProfilePhoto.getPhotoAsImage()).isNotNull(); + + patientProfilePhoto.setPhoto(null); + assertThat(patientProfilePhoto.getPhoto()).isNull(); + + patientProfilePhoto.setPatient(patient); + assertThat(patientProfilePhoto.getPatient()).isEqualTo(patient); + } + + @Test + void profilePhotoNull() throws Exception { + Patient patient = new Patient("testFirstname", "testSecondname", LocalDate.now(), 99, "", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxcode", "maritalStatus", + "profession"); + PatientProfilePhoto patientProfilePhoto = new PatientProfilePhoto(); + + File file = new File(getClass().getResource("patient.jpg").getFile()); + byte[] bytes = Files.readAllBytes(file.toPath()); + patientProfilePhoto.setPhoto(bytes); + assertThat(patientProfilePhoto.getPhotoAsImage()).isNotNull(); + + patient.setPatientProfilePhoto(patientProfilePhoto); + assertThat(patient.getPatientProfilePhoto()).isNotNull(); + + patient.setPatientProfilePhoto(null); + assertThat(patient.getPatientProfilePhoto()).isNull(); + } + + @Test + void patientToString() { + Patient patient = new Patient("testFirstname", "testSecondname", LocalDate.now(), 99, "", 'F', "testAddress", "testCity", + "nextOfKin", "testTelephone", "motherName", 'U', "fatherName", 'U', "bloodType", ' ', ' ', "testTaxCode", "maritalStatus", + "profession"); + + patient.setNote("someNote"); + assertThat(patient.toString()).isEqualTo("testFirstname testSecondname"); + } + +} diff --git a/src/test/java/org/isf/patient/service/PatientServiceTests.java b/src/test/java/org/isf/patient/service/PatientServiceTests.java new file mode 100644 index 000000000..bae019183 --- /dev/null +++ b/src/test/java/org/isf/patient/service/PatientServiceTests.java @@ -0,0 +1,282 @@ +/* + * Open Hospital (www.open-hospital.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * + * Open Hospital is a free and open source software for healthcare data management. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * https://www.gnu.org/licenses/gpl-3.0-standalone.html + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.isf.patient.service; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.isf.patient.data.PatientHelper.PATIENT_DATA_TABLE; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.isf.OHCoreTestCase5; +import org.isf.patient.data.PatientHelper; +import org.isf.patient.model.Patient; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.domain.PageRequest; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class PatientServiceTests extends OHCoreTestCase5 { + + private static PatientHelper createPatient; + + @Autowired + private PatientIoOperations patientIoOperation; + @Autowired + private PatientIoOperationRepository patientIoOperationRepository; + + @BeforeAll + public void beforeAll() { + createPatient = new PatientHelper(); + } + + @BeforeEach + public void beforeEach() { + cleanH2InMemoryDb(); + } + + @Test + void getPatients() throws Exception { + createPatient.createPatient(); + List patients = patientIoOperation.getPatients(); + createPatient.checkPatientInDb(patients.get(patients.size() - 1).getCode()); + } + + @Test + void getPatientsPageable() throws Exception { + createPatient.createPatient(15); + List patients = patientIoOperation.getPatients(PageRequest.of(0, 10)); + assertThat(patients).hasSize(10); + createPatient.checkPatientInDb(patients.get(patients.size() - 1).getCode()); + + patients = patientIoOperation.getPatients(PageRequest.of(1, 10)); + assertThat(patients).hasSize(5); + } + + @Test + void getPatientsByOneOfFieldsLike() throws Exception { + createPatient.createPatient(); + // Pay attention that query return with PAT_ID descendant + List patients = patientIoOperation.getPatientsByOneOfFieldsLike(null); + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeFirstName() throws Exception { + // given: + List codes = createPatient.createPatient(7); + Patient foundPatient = patientIoOperation.getPatient(codes.get(3)); + + // when: + List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getFirstName()); + + // then: + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeMiddleOfFirstName() throws Exception { + // given: + List codes = createPatient.createPatient(5); + Patient foundPatient = patientIoOperation.getPatient(codes.get(2)); + + // when: + List patients = patientIoOperation.getPatientsByOneOfFieldsLike( + foundPatient.getFirstName().substring(1, foundPatient.getFirstName().length() - 2)); + + // then: + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeSecondName() throws Exception { + // given: + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(4)); + + // when: + List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); + + // then: + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeNote() throws Exception { + // given: + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(8)); + + // when: + List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); + + // then: + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeTaxCode() throws Exception { + // given: + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(2)); + + // when: + List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getTaxCode()); + + // then: + createPatient.checkPatientInDb(patients.get(0).getCode()); + } + + @Test + void getPatientsByOneOfFieldsLikeNotExistingStringShouldNotFindAnything() throws Exception { + createPatient.createPatient(); + List patients = patientIoOperation.getPatientsByOneOfFieldsLike("dupaxyzzy"); + assertThat(patients).isEmpty(); + } + + @Test + void getPatientFromName() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(2)); + Patient patient = patientIoOperation.getPatient(foundPatient.getName()); + assertThat(patient.getName()).isEqualTo(foundPatient.getName()); + } + + @Test + void getPatientFromNameDoesNotExist() throws Exception { + assertThat(patientIoOperation.getPatient("someUnusualNameThatWillNotBeFound")).isNull(); + } + + @Test + void getPatientFromCode() throws Exception { + List codes = createPatient.createPatient(); + Patient patient = patientIoOperation.getPatient(codes.get(2)); + assertThat(patient.getName()).isEqualTo(PATIENT_DATA_TABLE[2].getName()); + } + + @Test + void getPatientFromCodeDoesNotExist() throws Exception { + assertThat(patientIoOperation.getPatient(-987654321)).isNull(); + } + + @Test + void getPatientAll() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(0)); + Patient patient = patientIoOperation.getPatientAll(codes.get(0)); + assertThat(patient.getName()).isEqualTo(foundPatient.getName()); + } + + @Test + void getPatientsByParams() throws Exception { + createPatient.createPatient(); + Map params = new HashMap<>(); + params.put("firstName", PATIENT_DATA_TABLE[2].getFirstName()); + params.put("birthDate", PATIENT_DATA_TABLE[2].getBirthDate().atStartOfDay()); + params.put("address", PATIENT_DATA_TABLE[2].getAddress()); + List patients = patientIoOperation.getPatients(params); + assertThat(patients.size()).isPositive(); + } + + @Test + void saveNewPatient() { + Patient patient = PATIENT_DATA_TABLE[17]; + assertThat(patientIoOperation.savePatient(patient)).isNotNull(); + } + + @Test + void updatePatient() throws Exception { + List codes = createPatient.createPatient(); + int code = codes.get(6); + Patient patient = patientIoOperation.getPatient(code); + patient.setFirstName("someNewFirstName"); + assertThat(patientIoOperation.updatePatient(patient)).isTrue(); + Patient updatedPatient = patientIoOperation.getPatient(code); + assertThat(updatedPatient.getFirstName()).isEqualTo(patient.getFirstName()); + } + + @Test + void deletePatient() throws Exception { + List codes = createPatient.createPatient(); + Patient patient = patientIoOperation.getPatient(codes.get(1)); + assertThat(patientIoOperation.deletePatient(patient)).isTrue(); + } + + @Test + void patientIsPresent() throws Exception { + List codes = createPatient.createPatient(); + Patient foundPatient = patientIoOperation.getPatient(codes.get(8)); + assertThat(patientIoOperation.isPatientPresentByName(foundPatient.getName())).isTrue(); + } + + @Test + void patientIsNotPresent() throws Exception { + createPatient.createPatient(); + assertThat(patientIoOperation.isPatientPresentByName("someNameWeAreSureDoesNotExist")).isFalse(); + } + + @Test + void getNextPatientCode() throws Exception { + List codes = createPatient.createPatient(); + int max = patientIoOperation.getNextPatientCode(); + assertThat((codes.size() + 1)).isEqualTo(max); + } + + @Test + void codeIsPresent() throws Exception { + List codes = createPatient.createPatient(); + assertThat(patientIoOperation.isCodePresent(codes.get(18))).isTrue(); + } + + @Test + void codeIsNotPresent() throws Exception { + List codes = createPatient.createPatient(); + assertThat(patientIoOperation.isCodePresent(-987)).isFalse(); + } + + // @Test + // void testMergePatientHistory() throws Exception { + // // given: + // Patient mergedPatient = testPatient.setup(false); + // Patient obsoletePatient = testPatient.setup(false); + // patientIoOperationRepository.saveAndFlush(mergedPatient); + // patientIoOperationRepository.saveAndFlush(obsoletePatient); + // + // // when: + // patientIoOperation.mergePatientHistory(mergedPatient, obsoletePatient); + // + // // then: + // assertThatObsoletePatientWasDeletedAndMergedIsTheActiveOne(mergedPatient, obsoletePatient); + // } + + private void assertThatObsoletePatientWasDeletedAndMergedIsTheActiveOne(Patient mergedPatient, Patient obsoletePatient) { + Patient mergedPatientResult = patientIoOperationRepository.findById(mergedPatient.getCode()).get(); + Patient obsoletePatientResult = patientIoOperationRepository.findById(obsoletePatient.getCode()).get(); + assertThat(obsoletePatientResult.getDeleted()).isEqualTo("Y"); + assertThat(mergedPatientResult.getDeleted()).isEqualTo("N"); + } + +} diff --git a/src/test/java/org/isf/patient/test/TestPatient.java b/src/test/java/org/isf/patient/test/TestPatient.java index 494db56a3..212336fdb 100644 --- a/src/test/java/org/isf/patient/test/TestPatient.java +++ b/src/test/java/org/isf/patient/test/TestPatient.java @@ -1,6 +1,6 @@ /* * Open Hospital (www.open-hospital.org) - * Copyright © 2006-2021 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) + * Copyright © 2006-2022 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) * * Open Hospital is a free and open source software for healthcare data management. * @@ -28,7 +28,6 @@ import org.isf.patient.model.Patient; import org.isf.patient.model.PatientProfilePhoto; -import org.isf.utils.exception.OHException; public class TestPatient { @@ -55,7 +54,7 @@ public class TestPatient { //private static Blob photo; //private static Image photoImage; - public Patient setup(boolean usingSet) throws OHException { + public Patient setup(boolean usingSet) { Patient patient; if (usingSet) { diff --git a/src/test/java/org/isf/patient/test/Tests.java b/src/test/java/org/isf/patient/test/Tests.java deleted file mode 100644 index 7a4e6f71b..000000000 --- a/src/test/java/org/isf/patient/test/Tests.java +++ /dev/null @@ -1,754 +0,0 @@ -/* - * Open Hospital (www.open-hospital.org) - * Copyright © 2006-2021 Informatici Senza Frontiere (info@informaticisenzafrontiere.org) - * - * Open Hospital is a free and open source software for healthcare data management. - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * https://www.gnu.org/licenses/gpl-3.0-standalone.html - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.isf.patient.test; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -import java.io.File; -import java.lang.reflect.Field; -import java.nio.file.Files; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.util.Calendar; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.assertj.core.api.Condition; -import org.isf.OHCoreTestCase; -import org.isf.opd.model.Opd; -import org.isf.opd.test.TestOpd; -import org.isf.patient.manager.PatientBrowserManager; -import org.isf.patient.model.Patient; -import org.isf.patient.model.PatientProfilePhoto; -import org.isf.patient.service.PatientIoOperationRepository; -import org.isf.patient.service.PatientIoOperations; -import org.isf.utils.exception.OHException; -import org.isf.utils.exception.OHServiceException; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.PageRequest; -import org.springframework.data.domain.Pageable; - -public class Tests extends OHCoreTestCase { - - private static TestPatient testPatient; - private static TestOpd testOpd; - - @Autowired - PatientIoOperations patientIoOperation; - @Autowired - PatientIoOperationRepository patientIoOperationRepository; - @Autowired - PatientBrowserManager patientBrowserManager; - - @BeforeClass - public static void setUpClass() { - testPatient = new TestPatient(); - testOpd = new TestOpd(); - } - - @Before - public void setUp() { - cleanH2InMemoryDb(); - } - - @Test - public void testPatientGets() throws Exception { - Integer code = setupTestPatient(false); - checkPatientIntoDb(code); - } - - @Test - public void testPatientSets() throws Exception { - Integer code = setupTestPatient(true); - checkPatientIntoDb(code); - } - - @Test - public void testIoGetPatients() throws Exception { - setupTestPatient(false); - List patients = patientIoOperation.getPatients(); - testPatient.check(patients.get(patients.size() - 1)); - } - - @Test - public void testIoGetPatientsPageable() throws Exception { - setupTestPatient(false); - List patients = patientIoOperation.getPatients(createPageRequest()); - testPatient.check(patients.get(patients.size() - 1)); - } - - private Pageable createPageRequest() { - return PageRequest.of(0, 10); // Page size 10 - } - - @Test - public void testIoGetPatientsByOneOfFieldsLike() throws Exception { - setupTestPatient(false); - // Pay attention that query return with PAT_ID descendant - List patients = patientIoOperation.getPatientsByOneOfFieldsLike(null); - testPatient.check(patients.get(0)); - } - - @Test - public void testIoGetPatientsByOneOfFieldsLikeFirstName() throws Exception { - // given: - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - - // when: - List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getFirstName()); - - // then: - testPatient.check(patients.get(0)); - } - - @Test - public void testIoGetPatientsByOneOfFieldsLikeMiddleOfFirstName() throws Exception { - // given: - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - - // when: - List patients = patientIoOperation - .getPatientsByOneOfFieldsLike(foundPatient.getFirstName().substring(1, foundPatient.getFirstName().length() - 2)); - - // then: - testPatient.check(patients.get(0)); - } - - @Test - public void testIoGetPatientsByOneOfFieldsLikeSecondName() throws Exception { - // given: - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - - // when: - List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); - - // then: - testPatient.check(patients.get(0)); - } - - @Test - public void testIoGetPatientsByOneOfFieldsLikeNote() throws Exception { - // given: - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - - // when: - List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); - - // then: - testPatient.check(patients.get(0)); - } - - @Test - public void testIoGetPatientsByOneOfFieldsLikeTaxCode() throws Exception { - // given: - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - - // when: - List patients = patientIoOperation.getPatientsByOneOfFieldsLike(foundPatient.getTaxCode()); - - // then: - testPatient.check(patients.get(0)); - } - - @Test - public void testIoGetPatientsByOneOfFieldsLikeNotExistingStringShouldNotFindAnything() throws Exception { - setupTestPatient(false); - List patients = patientIoOperation.getPatientsByOneOfFieldsLike("dupa"); - assertThat(patients).isEmpty(); - } - - @Test - public void testIoGetPatientFromName() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - Patient patient = patientIoOperation.getPatient(foundPatient.getName()); - assertThat(patient.getName()).isEqualTo(foundPatient.getName()); - } - - @Test - public void testIoGetPatientFromNameDoesNotExist() throws Exception { - assertThat(patientIoOperation.getPatient("someUnusualNameThatWillNotBeFound")).isNull(); - } - - @Test - public void testIoGetPatientFromCode() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - Patient patient = patientIoOperation.getPatient(code); - assertThat(patient.getName()).isEqualTo(foundPatient.getName()); - } - - @Test - public void testIoGetPatientFromCodeDoesNotExist() throws Exception { - assertThat(patientIoOperation.getPatient(-987654321)).isNull(); - } - - @Test - public void testIoGetPatientAll() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - Patient patient = patientIoOperation.getPatientAll(code); - assertThat(patient.getName()).isEqualTo(foundPatient.getName()); - } - - @Test - public void testIoGetPatientsByParams() throws Exception { - setupTestPatient(false); - Map params = new HashMap<>(); - params.put("firstName", "TestFirstName"); - params.put("birthDate", LocalDateTime.of(1984, Calendar.AUGUST, 14, 0, 0, 0)); - params.put("address", "TestAddress"); - List patients = patientIoOperation.getPatients(params); - assertThat(patients.size()).isPositive(); - } - - @Test - public void testIoSaveNewPatient() throws Exception { - Patient patient = testPatient.setup(true); - assertThat(patientIoOperation.savePatient(patient)).isNotNull(); - } - - @Test - public void testIoUpdatePatient() throws Exception { - Integer code = setupTestPatient(false); - Patient patient = patientIoOperation.getPatient(code); - patient.setFirstName("someNewFirstName"); - assertThat(patientIoOperation.updatePatient(patient)).isTrue(); - Patient updatedPatient = patientIoOperation.getPatient(code); - assertThat(updatedPatient.getFirstName()).isEqualTo(patient.getFirstName()); - } - - @Test - public void testIoDeletePatient() throws Exception { - Integer code = setupTestPatient(false); - Patient patient = patientIoOperation.getPatient(code); - boolean result = patientIoOperation.deletePatient(patient); - assertThat(result).isTrue(); - } - - @Test - public void testIoIsPatientPresent() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - boolean result = patientIoOperation.isPatientPresentByName(foundPatient.getName()); - assertThat(result).isTrue(); - } - - @Test - public void testIoGetNextPatientCode() throws Exception { - Integer code = setupTestPatient(false); - Integer max = patientIoOperation.getNextPatientCode(); - assertThat((code + 1)).isEqualTo(max); - } - - @Test - public void testIoIsCodePresent() throws Exception { - Integer code = setupTestPatient(false); - assertThat(patientIoOperation.isCodePresent(code)).isTrue(); - assertThat(patientIoOperation.isCodePresent(-987)).isFalse(); - } - - @Test - public void testMergePatientHistory() throws Exception { - // given: - Patient mergedPatient = testPatient.setup(false); - Patient obsoletePatient = testPatient.setup(false); - patientIoOperationRepository.saveAndFlush(mergedPatient); - patientIoOperationRepository.saveAndFlush(obsoletePatient); - - // when: - patientIoOperation.mergePatientHistory(mergedPatient, obsoletePatient); - - // then: - assertThatObsoletePatientWasDeletedAndMergedIsTheActiveOne(mergedPatient, obsoletePatient); - } - - @Test - public void testMgrGetPatients() throws Exception { - setupTestPatient(false); - List patients = patientBrowserManager.getPatient(); - testPatient.check(patients.get(patients.size() - 1)); - } - - @Test - public void testMgrGetPatientsPageable() throws Exception { - for (int idx = 0; idx < 15; idx++) { - setupTestPatient(false); - } - - // First page of 10 - List patients = patientBrowserManager.getPatient(0, 10); - assertThat(patients).hasSize(10); - testPatient.check(patients.get(patients.size() - 1)); - - // Go get the next page or 10 - patients = patientBrowserManager.getPatient(1, 10); - assertThat(patients).hasSize(5); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLike() throws Exception { - setupTestPatient(false); - // Pay attention that query return with PAT_ID descendant - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(null); - testPatient.check(patients.get(0)); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLikeFirstName() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getFirstName()); - testPatient.check(patients.get(0)); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLikeMiddleOfFirstName() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getFirstName().substring(1, foundPatient.getFirstName().length() - 2)); - testPatient.check(patients.get(0)); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLikeSecondName() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); - testPatient.check(patients.get(0)); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLikeNote() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getSecondName()); - testPatient.check(patients.get(0)); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLikeTaxCode() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike(foundPatient.getTaxCode()); - testPatient.check(patients.get(0)); - } - - @Test - public void testMgrGetPatientsByOneOfFieldsLikeNotExistingStringShouldNotFindAnything() throws Exception { - setupTestPatient(false); - List patients = patientBrowserManager.getPatientsByOneOfFieldsLike("dupa"); - assertThat(patients).isEmpty(); - } - - @Test - public void testMgrGetPatientByName() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - Patient patient = patientBrowserManager.getPatientByName(foundPatient.getName()); - assertThat(patient.getName()).isEqualTo(foundPatient.getName()); - } - - @Test - public void testMgrGetPatientByNameDoesNotExist() throws Exception { - assertThat(patientBrowserManager.getPatientByName("someUnusualNameThatWillNotBeFound")).isNull(); - } - - @Test - public void testMgrGetPatientById() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - Patient patient = patientBrowserManager.getPatientById(code); - assertThat(patient.getName()).isEqualTo(foundPatient.getName()); - } - - @Test - public void testMgrGetPatienByIdDoesNotExist() throws Exception { - assertThat(patientBrowserManager.getPatientById(-987654321)).isNull(); - } - - @Test - public void testMgrGetPatientAll() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - Patient patient = patientBrowserManager.getPatientAll(code); - assertThat(patient.getName()).isEqualTo(foundPatient.getName()); - } - - @Test - public void testMgrSaveNewPatient() throws Exception { - Patient patient = testPatient.setup(true); - assertThat(patientBrowserManager.savePatient(patient)).isNotNull(); - } - - @Test - public void testMgrUpdatePatient() throws Exception { - Integer code = setupTestPatient(false); - Patient patient = patientIoOperation.getPatient(code); - patient.setFirstName("someNewFirstName"); - Patient updatedPatient = patientBrowserManager.savePatient(patient); - assertThat(updatedPatient).isNotNull(); - assertThat(updatedPatient.getFirstName()).isEqualTo(patient.getFirstName()); - } - - @Test - public void testMgrDeletePatient() throws Exception { - Integer code = setupTestPatient(false); - Patient patient = patientIoOperation.getPatient(code); - assertThat(patientBrowserManager.deletePatient(patient)).isTrue(); - } - - @Test - public void testMgrIsNamePresent() throws Exception { - Integer code = setupTestPatient(false); - Patient foundPatient = patientIoOperation.getPatient(code); - assertThat(patientBrowserManager.isNamePresent(foundPatient.getName())).isTrue(); - } - - @Test - public void testMgrIsNamePresentNotFound() throws Exception { - assertThat(patientBrowserManager.isNamePresent("someNameWeAreSureDoesNotExist")).isFalse(); - } - - @Test - public void testMgrGetNextPatientCode() throws Exception { - Integer code = setupTestPatient(false); - Integer max = patientBrowserManager.getNextPatientCode(); - assertThat((code + 1)).isEqualTo(max); - } - - @Test - public void testMgrGetMaritalList() throws Exception { - resetHashMaps(); - String[] maritalDescriptionList = patientBrowserManager.getMaritalList(); - assertThat(maritalDescriptionList).isNotEmpty(); - } - - @Test - public void testMgrGetMaritalTranslated() throws Exception { - resetHashMaps(); - // TODO: if resource bundles are made avaiable in core then the values being compared will need to change - assertThat(patientBrowserManager.getMaritalTranslated(null)).isEqualTo("angal.patient.maritalstatusunknown.txt"); - assertThat(patientBrowserManager.getMaritalTranslated("someKeyNotInTheList")).isEqualTo("angal.patient.maritalstatusunknown.txt"); - assertThat(patientBrowserManager.getMaritalTranslated("married")).isEqualTo("angal.patient.maritalstatusmarried.txt"); - } - - @Test - public void testMgrGetMaritalKey() throws Exception { - resetHashMaps(); - // TODO: if resource bundles are made avaiable in core then the values being compared will need to change - assertThat(patientBrowserManager.getMaritalKey(null)).isEqualTo("undefined"); - assertThat(patientBrowserManager.getMaritalKey("someKeyNotInTheList")).isEqualTo("undefined"); - assertThat(patientBrowserManager.getMaritalKey("angal.patient.maritalstatusmarried.txt")).isEqualTo("married"); - } - - @Test - public void testMgrGetProfessionList() throws Exception { - resetHashMaps(); - String[] maritalDescriptionList = patientBrowserManager.getProfessionList(); - assertThat(maritalDescriptionList).isNotEmpty(); - } - - @Test - public void testMgrGetProfessionTranslated() throws Exception { - resetHashMaps(); - // TODO: if resource bundles are made avaiable in core then the values being compared will need to change - assertThat(patientBrowserManager.getProfessionTranslated(null)).isEqualTo("angal.patient.profession.unknown.txt"); - assertThat(patientBrowserManager.getProfessionTranslated("someKeyNotInTheList")).isEqualTo("angal.patient.profession.unknown.txt"); - assertThat(patientBrowserManager.getProfessionTranslated("mining")).isEqualTo("angal.patient.profession.mining.txt"); - } - - @Test - public void testMgrGetProfessionKey() throws Exception { - resetHashMaps(); - // TODO: if resource bundles are made avaiable in core then the values being compared will need to change - assertThat(patientBrowserManager.getProfessionKey(null)).isEqualTo("undefined"); - assertThat(patientBrowserManager.getProfessionKey("someKeyNotInTheList")).isEqualTo("undefined"); - assertThat(patientBrowserManager.getProfessionKey("angal.patient.profession.mining.txt")).isEqualTo("mining"); - } - - @Test - public void testMgrPatientValidationNoFirstName() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setFirstName(""); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationNullFirstName() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setFirstName(null); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationNoSecondName() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setSecondName(""); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationNullSecondName() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setSecondName(null); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationBirthDateNull() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setBirthDate(null); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationBirthDateTooFarInFuture() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setBirthDate(LocalDate.of(999, 1, 1)); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationAgeLessThanZero() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setBirthDate(null); - patient.setAge(-1); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationAgeToHigh() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setBirthDate(null); - patient.setAge(201); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testMgrPatientValidationSexEmpty() { - assertThatThrownBy(() -> { - Patient patient = testPatient.setup(true); - - patient.setSex(' '); - - patientBrowserManager.savePatient(patient); - }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")); - } - - @Test - public void testPatientOpdConstructor() throws Exception { - Opd opd = testOpd.setup(null, null, false); - Patient patient = new Patient(opd); - - assertThat(patient.getSex()).isEqualTo('F'); - assertThat(patient.getCode()).isNull(); - assertThat(patient.getBirthDate()).isNull(); - - assertThat(patient.getDeleted()).isEqualTo("N"); - patient.setDeleted("Y"); - assertThat(patient.getDeleted()).isEqualTo("Y"); - } - - @Test - public void testPatientConstructor() { - Patient patient = new Patient(99, "firstName", "secondName", "name", null, 99, " ", 'F', "address", - "city", "nextOfKin", "noPhone", "note", "motherName", ' ', "fatherName", ' ', - "bloodType", ' ', ' ', "personalCode", "maritalStatus", "profession"); - - assertThat(patient.getCode()).isEqualTo(99); - assertThat(patient.getSex()).isEqualTo('F'); - assertThat(patient.getBirthDate()).isNull(); - - assertThat(patient.getLock()).isZero(); - patient.setLock(99); - assertThat(patient.getLock()).isEqualTo(99); - } - - @Test - public void testPatientGetSearchString() throws Exception { - Patient patient = testPatient.setup(false); - patient.setCode(1); - assertThat(patient.getSearchString()).isEqualTo("1 testfirstname testsecondname testcity testaddress TestTelephone testtaxcode "); - } - - @Test - public void testPatientGetInformations() throws Exception { - Patient patient = testPatient.setup(false); - patient.setNote("someNote"); - assertThat(patient.getInformations()).isEqualTo("TestCity - TestAddress - TestTelephone - someNote - TestTaxCode"); - } - - @Test - public void testPatientEquals() throws Exception { - Patient patient = testPatient.setup(false); - assertThat(patient.equals(patient)).isTrue(); - assertThat(patient) - .isNotNull() - .isNotEqualTo("someString"); - Patient patient2 = testPatient.setup(true); - patient.setCode(1); - patient.setCode(2); - assertThat(patient).isNotEqualTo(patient2); - patient2.setCode(patient.getCode()); - assertThat(patient).isEqualTo(patient2); - } - - @Test - public void testPatientHashCode() throws Exception { - Patient patient = testPatient.setup(false); - patient.setCode(1); - // compute - int hashCode = patient.hashCode(); - assertThat(hashCode).isEqualTo(23 * 133 + 1); - // check stored value - assertThat(patient.hashCode()).isEqualTo(hashCode); - - Patient patent2 = testPatient.setup(true); - patent2.setCode(null); - assertThat(patent2.hashCode()).isEqualTo(23 * 133); - } - - @Test - public void testPatientProfilePhoto() throws Exception { - Patient patient = testPatient.setup(true); - PatientProfilePhoto patientProfilePhoto = new PatientProfilePhoto(); - - File file = new File(getClass().getResource("patient.jpg").getFile()); - byte[] bytes = Files.readAllBytes(file.toPath()); - patientProfilePhoto.setPhoto(bytes); - assertThat(patientProfilePhoto.getPhotoAsImage()).isNotNull(); - - patientProfilePhoto.setPhoto(null); - assertThat(patientProfilePhoto.getPhoto()).isNull(); - - patientProfilePhoto.setPatient(patient); - assertThat(patientProfilePhoto.getPatient()).isEqualTo(patient); - } - - private void resetHashMaps() throws Exception { - Field diuresisDescriptionHashMap = patientBrowserManager.getClass().getDeclaredField("maritalHashMap"); - diuresisDescriptionHashMap.setAccessible(true); - diuresisDescriptionHashMap.set(patientBrowserManager, null); - - Field bowelDescriptionHashMap = patientBrowserManager.getClass().getDeclaredField("professionHashMap"); - bowelDescriptionHashMap.setAccessible(true); - bowelDescriptionHashMap.set(patientBrowserManager, null); - } - - private void assertThatObsoletePatientWasDeletedAndMergedIsTheActiveOne(Patient mergedPatient, Patient obsoletePatient) throws OHException { - Patient mergedPatientResult = patientIoOperationRepository.findById(mergedPatient.getCode()).get(); - Patient obsoletePatientResult = patientIoOperationRepository.findById(obsoletePatient.getCode()).get(); - assertThat(obsoletePatientResult.getDeleted()).isEqualTo("Y"); - assertThat(mergedPatientResult.getDeleted()).isEqualTo("N"); - } - - private Integer setupTestPatient(boolean usingSet) throws OHException { - Patient patient = testPatient.setup(usingSet); - patientIoOperationRepository.saveAndFlush(patient); - return patient.getCode(); - } - - private void checkPatientIntoDb(Integer code) throws OHServiceException { - Patient foundPatient = patientIoOperation.getPatient(code); - testPatient.check(foundPatient); - } -} diff --git a/src/test/resources/org/isf/patient/test/patient.jpg b/src/test/resources/org/isf/patient/model/patient.jpg similarity index 100% rename from src/test/resources/org/isf/patient/test/patient.jpg rename to src/test/resources/org/isf/patient/model/patient.jpg