From 1d87e455db69b2b186ed925ab8b849151dd6f01c Mon Sep 17 00:00:00 2001 From: dbm Date: Wed, 8 Nov 2023 08:06:49 -0500 Subject: [PATCH] OP-1130 improve JPA methods - patvac package --- .../org/isf/patvac/manager/PatVacManager.java | 28 ++--- .../patvac/service/PatVacIoOperations.java | 28 ++--- src/test/java/org/isf/patvac/test/Tests.java | 111 +++++++++--------- 3 files changed, 82 insertions(+), 85 deletions(-) diff --git a/src/main/java/org/isf/patvac/manager/PatVacManager.java b/src/main/java/org/isf/patvac/manager/PatVacManager.java index 9e3867e5f..28b240e59 100644 --- a/src/main/java/org/isf/patvac/manager/PatVacManager.java +++ b/src/main/java/org/isf/patvac/manager/PatVacManager.java @@ -52,9 +52,9 @@ public class PatVacManager { private PatVacIoOperations ioOperations; /** - * Returns all {@link PatientVaccine}s of today or one week ago + * Returns all {@link PatientVaccine}s for today or one week ago. * - * @param minusOneWeek - if true return the last week + * @param minusOneWeek - if {@code true} return the last week * @return the list of {@link PatientVaccine}s * @throws OHServiceException */ @@ -63,8 +63,7 @@ public List getPatientVaccine(boolean minusOneWeek) throws OHSer } /** - * Returns all {@link PatientVaccine}s within dateFrom and - * dateTo + * Returns all {@link PatientVaccine}s within {@code dateFrom} and {@code dateTo}. * * @param vaccineTypeCode * @param vaccineCode @@ -82,10 +81,10 @@ public List getPatientVaccine(String vaccineTypeCode, String vac } /** - * Inserts a {@link PatientVaccine} in the DB + * Inserts a {@link PatientVaccine}. * * @param patVac - the {@link PatientVaccine} to insert - * @return true if the item has been inserted, false otherwise + * @return the newly {@link PatientVaccine} object. * @throws OHServiceException */ public PatientVaccine newPatientVaccine(PatientVaccine patVac) throws OHServiceException { @@ -94,10 +93,10 @@ public PatientVaccine newPatientVaccine(PatientVaccine patVac) throws OHServiceE } /** - * Updates a {@link PatientVaccine} + * Updates a {@link PatientVaccine}. * * @param patVac - the {@link PatientVaccine} to update - * @return true if the item has been updated, false otherwise + * @return the updated {@link PatientVaccine} object. * @throws OHServiceException */ public PatientVaccine updatePatientVaccine(PatientVaccine patVac) throws OHServiceException { @@ -106,21 +105,20 @@ public PatientVaccine updatePatientVaccine(PatientVaccine patVac) throws OHServi } /** - * Deletes a {@link PatientVaccine} + * Deletes a {@link PatientVaccine}. * * @param patVac - the {@link PatientVaccine} to delete - * @return true if the item has been deleted, false otherwise * @throws OHServiceException */ - public boolean deletePatientVaccine(PatientVaccine patVac) throws OHServiceException { - return ioOperations.deletePatientVaccine(patVac); + public void deletePatientVaccine(PatientVaccine patVac) throws OHServiceException { + ioOperations.deletePatientVaccine(patVac); } /** - * Returns the max progressive number within specified year or within current year if 0. + * Returns the max progressive number within specified year or within current year if {@code 0}. * * @param year - * @return int - the progressive number in the year + * @return {@code int} - the progressive number in the year * @throws OHServiceException */ public int getProgYear(int year) throws OHServiceException { @@ -132,7 +130,7 @@ public Optional getPatientVaccine(int code) throws OHServiceExce } /** - * Verify if the object is valid for CRUD and return a list of errors, if any + * Verify if the object is valid for CRUD and return a list of errors, if any. * * @param patientVaccine * @throws OHDataValidationException diff --git a/src/main/java/org/isf/patvac/service/PatVacIoOperations.java b/src/main/java/org/isf/patvac/service/PatVacIoOperations.java index 80d05c046..40d574ca5 100644 --- a/src/main/java/org/isf/patvac/service/PatVacIoOperations.java +++ b/src/main/java/org/isf/patvac/service/PatVacIoOperations.java @@ -55,7 +55,7 @@ public class PatVacIoOperations { /** * Returns all {@link PatientVaccine}s of today or one week ago * - * @param minusOneWeek - if true return the last week + * @param minusOneWeek - if {@code true} return the last week * @return the list of {@link PatientVaccine}s * @throws OHServiceException */ @@ -71,8 +71,8 @@ public List getPatientVaccine(boolean minusOneWeek) throws OHSer } /** - * Returns all {@link PatientVaccine}s within dateFrom and - * dateTo + * Returns all {@link PatientVaccine}s within {@code dateFrom} and + * {@code dateTo} * * @param vaccineTypeCode * @param vaccineCode @@ -101,10 +101,10 @@ public List findForPatient(int patientCode) { } /** - * Inserts a {@link PatientVaccine} in the DB + * Inserts a {@link PatientVaccine} object. * * @param patVac - the {@link PatientVaccine} to insert - * @return true if the item has been inserted, false otherwise + * @return the newly inserted {@link PatientVaccine} object. * @throws OHServiceException */ public PatientVaccine newPatientVaccine(PatientVaccine patVac) throws OHServiceException { @@ -112,10 +112,10 @@ public PatientVaccine newPatientVaccine(PatientVaccine patVac) throws OHServiceE } /** - * Updates a {@link PatientVaccine} + * Updates a {@link PatientVaccine}. * * @param patVac - the {@link PatientVaccine} to update - * @return true if the item has been updated, false otherwise + * @return the newly updated {@link PatientVaccine} object. * @throws OHServiceException */ public PatientVaccine updatePatientVaccine(PatientVaccine patVac) throws OHServiceException { @@ -123,22 +123,20 @@ public PatientVaccine updatePatientVaccine(PatientVaccine patVac) throws OHServi } /** - * Deletes a {@link PatientVaccine} + * Delete a {@link PatientVaccine}. * * @param patVac - the {@link PatientVaccine} to delete - * @return true if the item has been deleted, false otherwise * @throws OHServiceException */ - public boolean deletePatientVaccine(PatientVaccine patVac) throws OHServiceException { + public void deletePatientVaccine(PatientVaccine patVac) throws OHServiceException { repository.delete(patVac); - return true; } /** - * Returns the max progressive number within specified year or within current year if 0. + * Returns the max progressive number within specified year or within current year if {@code 0}. * * @param year - * @return int - the progressive number in the year + * @return {@code int} - the progressive number in the year * @throws OHServiceException */ public int getProgYear(int year) throws OHServiceException { @@ -150,10 +148,10 @@ public int getProgYear(int year) throws OHServiceException { } /** - * Checks if the code is already in use + * Checks if the code is already in use. * * @param code - the patient vaccine code - * @return true if the code is already in use, false otherwise + * @return {@code true} if the code is already in use, {@code false} otherwise * @throws OHServiceException */ public boolean isCodePresent(Integer code) throws OHServiceException { diff --git a/src/test/java/org/isf/patvac/test/Tests.java b/src/test/java/org/isf/patvac/test/Tests.java index c243083ad..1351a1c7a 100644 --- a/src/test/java/org/isf/patvac/test/Tests.java +++ b/src/test/java/org/isf/patvac/test/Tests.java @@ -139,7 +139,8 @@ public void testIoGetPatientVaccineLastWeek() throws Exception { @Test public void testIoGetPatientVaccine() throws Exception { int code = setupTestPatientVaccine(false); - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); List patientVaccines = patvacIoOperation.getPatientVaccine( foundPatientVaccine.getVaccine().getVaccineType().getCode(), foundPatientVaccine.getVaccine().getCode(), @@ -155,14 +156,12 @@ public void testIoGetPatientVaccine() throws Exception { @Transactional // requires active session because of lazy loading patient public void testIoUpdatePatientVaccine() throws Exception { int code = setupTestPatientVaccine(false); - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); LocalDateTime newDate = TimeTools.getNow(); foundPatientVaccine.setVaccineDate(newDate); - PatientVaccine result = patvacIoOperation.updatePatientVaccine(foundPatientVaccine); - assertThat(result).isNotNull(); - PatientVaccine updatePatientVaccine = patVacIoOperationRepository.findById(code).get(); - assertThat(updatePatientVaccine).isNotNull(); - assertThat(updatePatientVaccine.getVaccineDate()).isEqualTo(newDate); + PatientVaccine updatedPatientVaccine = patvacIoOperation.updatePatientVaccine(foundPatientVaccine); + assertThat(updatedPatientVaccine.getVaccineDate()).isEqualTo(newDate); } @Test @@ -182,11 +181,10 @@ public void testIoNewPatientVaccine() throws Exception { @Test public void testIoDeletePatientVaccine() throws Exception { int code = setupTestPatientVaccine(false); - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); - boolean result = patvacIoOperation.deletePatientVaccine(foundPatientVaccine); - assertThat(result).isTrue(); - result = patvacIoOperation.isCodePresent(code); - assertThat(result).isFalse(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); + patvacIoOperation.deletePatientVaccine(foundPatientVaccine); + assertThat(patvacIoOperation.isCodePresent(code)).isFalse(); } @Test @@ -211,14 +209,16 @@ public void testIoGetProgYear() throws Exception { public void testListenerShouldUpdatePatientToMergedWhenPatientMergedEventArrive() throws Exception { // given: int id = setupTestPatientVaccine(false); - PatientVaccine found = patVacIoOperationRepository.findById(id).get(); + PatientVaccine found = patVacIoOperationRepository.findById(id).orElse(null); + assertThat(found).isNotNull(); Patient mergedPatient = setupTestPatient(false); // when: applicationEventPublisher.publishEvent(new PatientMergedEvent(found.getPatient(), mergedPatient)); // then: - PatientVaccine result = patVacIoOperationRepository.findById(id).get(); + PatientVaccine result = patVacIoOperationRepository.findById(id).orElse(null); + assertThat(result).isNotNull(); assertThat(result.getPatient().getCode()).isEqualTo(mergedPatient.getCode()); } @@ -308,7 +308,8 @@ public void testMgrGetPatientVaccineLastWeek() throws Exception { @Test public void testMgrGetPatientVaccine() throws Exception { int code = setupTestPatientVaccine(false); - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); List patientVaccines = patVacManager.getPatientVaccine( foundPatientVaccine.getVaccine().getVaccineType().getCode(), foundPatientVaccine.getVaccine().getCode(), @@ -324,14 +325,12 @@ public void testMgrGetPatientVaccine() throws Exception { @Transactional // requires active session because of lazy loading patient public void testMgrUpdatePatientVaccine() throws Exception { int code = setupTestPatientVaccine(false); - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); LocalDateTime newDate = TimeTools.getNow(); foundPatientVaccine.setVaccineDate(newDate); - PatientVaccine result = patVacManager.updatePatientVaccine(foundPatientVaccine); - assertThat(result).isNotNull(); - PatientVaccine updatePatientVaccine = patVacIoOperationRepository.findById(code).get(); - assertThat(updatePatientVaccine).isNotNull(); - assertThat(updatePatientVaccine.getVaccineDate()).isEqualTo(newDate); + PatientVaccine updatedPatientVaccine = patVacManager.updatePatientVaccine(foundPatientVaccine); + assertThat(updatedPatientVaccine.getVaccineDate()).isEqualTo(newDate); } @Test @@ -351,8 +350,9 @@ public void testMgrNewPatientVaccine() throws Exception { @Test public void testMgrDeletePatientVaccine() throws Exception { int code = setupTestPatientVaccine(true); - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); - assertThat(patVacManager.deletePatientVaccine(foundPatientVaccine)).isTrue(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); + patVacManager.deletePatientVaccine(foundPatientVaccine); assertThat(patvacIoOperation.isCodePresent(code)).isFalse(); } @@ -386,11 +386,11 @@ public void testMgrValidationVaccineDateIsNull() { patVacManager.newPatientVaccine(patientVaccine); }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error") + ); } @Test @@ -405,11 +405,11 @@ public void testMgrValidationProgrLessThanZero() { patVacManager.newPatientVaccine(patientVaccine); }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error") + ); } @Test @@ -424,11 +424,11 @@ public void testMgrValidationVaccineIsNull() { patVacManager.newPatientVaccine(patientVaccine); }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error") + ); } @Test @@ -443,11 +443,11 @@ public void testMgrValidationPatientIsNull() { patVacManager.newPatientVaccine(patientVaccine); }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error") + ); } @Test @@ -462,11 +462,11 @@ public void testMgrValidationPatientNameIsEmpty() { patVacManager.newPatientVaccine(patientVaccine); }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error") + ); } @Test @@ -481,11 +481,11 @@ public void testMgrValidationPatientSexIsEmpty() { patVacManager.newPatientVaccine(patientVaccine); }) - .isInstanceOf(OHServiceException.class) - .has( - new Condition( - (e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error") - ); + .isInstanceOf(OHServiceException.class) + .has( + new Condition( + e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error") + ); } @Test @@ -500,9 +500,9 @@ public void testPatientVaccineEquals() { PatientVaccine patientVaccine1 = new PatientVaccine(0, 0, null, new Patient(), null, 0); PatientVaccine patientVaccine2 = new PatientVaccine(0, 0, null, new Patient(), null, 0); - assertThat(patientVaccine1).isEqualTo(patientVaccine1); assertThat(patientVaccine1) .isNotNull() + .isEqualTo(patientVaccine1) .isNotEqualTo("someString"); patientVaccine2.setCode(-99); @@ -572,7 +572,8 @@ private int setupTestPatientVaccine(boolean usingSet) throws Exception { } private void checkPatientVaccineIntoDb(int code) { - PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).get(); + PatientVaccine foundPatientVaccine = patVacIoOperationRepository.findById(code).orElse(null); + assertThat(foundPatientVaccine).isNotNull(); testPatientVaccine.check(foundPatientVaccine); } } \ No newline at end of file