Skip to content

Commit

Permalink
OP-1130 improve JPA methods - patvac package
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmalkovsky committed Nov 8, 2023
1 parent 9b97e70 commit 1d87e45
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 85 deletions.
28 changes: 13 additions & 15 deletions src/main/java/org/isf/patvac/manager/PatVacManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 <code>true</code> return the last week
* @param minusOneWeek - if {@code true} return the last week
* @return the list of {@link PatientVaccine}s
* @throws OHServiceException
*/
Expand All @@ -63,8 +63,7 @@ public List<PatientVaccine> getPatientVaccine(boolean minusOneWeek) throws OHSer
}

/**
* Returns all {@link PatientVaccine}s within <code>dateFrom</code> and
* <code>dateTo</code>
* Returns all {@link PatientVaccine}s within {@code dateFrom} and {@code dateTo}.
*
* @param vaccineTypeCode
* @param vaccineCode
Expand All @@ -82,10 +81,10 @@ public List<PatientVaccine> getPatientVaccine(String vaccineTypeCode, String vac
}

/**
* Inserts a {@link PatientVaccine} in the DB
* Inserts a {@link PatientVaccine}.
*
* @param patVac - the {@link PatientVaccine} to insert
* @return <code>true</code> if the item has been inserted, <code>false</code> otherwise
* @return the newly {@link PatientVaccine} object.
* @throws OHServiceException
*/
public PatientVaccine newPatientVaccine(PatientVaccine patVac) throws OHServiceException {
Expand All @@ -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 <code>true</code> if the item has been updated, <code>false</code> otherwise
* @return the updated {@link PatientVaccine} object.
* @throws OHServiceException
*/
public PatientVaccine updatePatientVaccine(PatientVaccine patVac) throws OHServiceException {
Expand All @@ -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 <code>true</code> if the item has been deleted, <code>false</code> 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 <code>0</code>.
* Returns the max progressive number within specified year or within current year if {@code 0}.
*
* @param year
* @return <code>int</code> - the progressive number in the year
* @return {@code int} - the progressive number in the year
* @throws OHServiceException
*/
public int getProgYear(int year) throws OHServiceException {
Expand All @@ -132,7 +130,7 @@ public Optional<PatientVaccine> 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
Expand Down
28 changes: 13 additions & 15 deletions src/main/java/org/isf/patvac/service/PatVacIoOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public class PatVacIoOperations {
/**
* Returns all {@link PatientVaccine}s of today or one week ago
*
* @param minusOneWeek - if <code>true</code> return the last week
* @param minusOneWeek - if {@code true} return the last week
* @return the list of {@link PatientVaccine}s
* @throws OHServiceException
*/
Expand All @@ -71,8 +71,8 @@ public List<PatientVaccine> getPatientVaccine(boolean minusOneWeek) throws OHSer
}

/**
* Returns all {@link PatientVaccine}s within <code>dateFrom</code> and
* <code>dateTo</code>
* Returns all {@link PatientVaccine}s within {@code dateFrom} and
* {@code dateTo}
*
* @param vaccineTypeCode
* @param vaccineCode
Expand Down Expand Up @@ -101,44 +101,42 @@ public List<PatientVaccine> findForPatient(int patientCode) {
}

/**
* Inserts a {@link PatientVaccine} in the DB
* Inserts a {@link PatientVaccine} object.
*
* @param patVac - the {@link PatientVaccine} to insert
* @return <code>true</code> if the item has been inserted, <code>false</code> otherwise
* @return the newly inserted {@link PatientVaccine} object.
* @throws OHServiceException
*/
public PatientVaccine newPatientVaccine(PatientVaccine patVac) throws OHServiceException {
return repository.save(patVac);
}

/**
* Updates a {@link PatientVaccine}
* Updates a {@link PatientVaccine}.
*
* @param patVac - the {@link PatientVaccine} to update
* @return <code>true</code> if the item has been updated, <code>false</code> otherwise
* @return the newly updated {@link PatientVaccine} object.
* @throws OHServiceException
*/
public PatientVaccine updatePatientVaccine(PatientVaccine patVac) throws OHServiceException {
return repository.save(patVac);
}

/**
* Deletes a {@link PatientVaccine}
* Delete a {@link PatientVaccine}.
*
* @param patVac - the {@link PatientVaccine} to delete
* @return <code>true</code> if the item has been deleted, <code>false</code> 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 <code>0</code>.
* Returns the max progressive number within specified year or within current year if {@code 0}.
*
* @param year
* @return <code>int</code> - the progressive number in the year
* @return {@code int} - the progressive number in the year
* @throws OHServiceException
*/
public int getProgYear(int year) throws OHServiceException {
Expand All @@ -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 <code>true</code> if the code is already in use, <code>false</code> otherwise
* @return {@code true} if the code is already in use, {@code false} otherwise
* @throws OHServiceException
*/
public boolean isCodePresent(Integer code) throws OHServiceException {
Expand Down
111 changes: 56 additions & 55 deletions src/test/java/org/isf/patvac/test/Tests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PatientVaccine> patientVaccines = patvacIoOperation.getPatientVaccine(
foundPatientVaccine.getVaccine().getVaccineType().getCode(),
foundPatientVaccine.getVaccine().getCode(),
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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());
}

Expand Down Expand Up @@ -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<PatientVaccine> patientVaccines = patVacManager.getPatientVaccine(
foundPatientVaccine.getVaccine().getVaccineType().getCode(),
foundPatientVaccine.getVaccine().getCode(),
Expand All @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -386,11 +386,11 @@ public void testMgrValidationVaccineDateIsNull() {

patVacManager.newPatientVaccine(patientVaccine);
})
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
(e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")
);
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error")
);
}

@Test
Expand All @@ -405,11 +405,11 @@ public void testMgrValidationProgrLessThanZero() {

patVacManager.newPatientVaccine(patientVaccine);
})
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
(e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")
);
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error")
);
}

@Test
Expand All @@ -424,11 +424,11 @@ public void testMgrValidationVaccineIsNull() {

patVacManager.newPatientVaccine(patientVaccine);
})
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
(e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")
);
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error")
);
}

@Test
Expand All @@ -443,11 +443,11 @@ public void testMgrValidationPatientIsNull() {

patVacManager.newPatientVaccine(patientVaccine);
})
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
(e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")
);
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error")
);
}

@Test
Expand All @@ -462,11 +462,11 @@ public void testMgrValidationPatientNameIsEmpty() {

patVacManager.newPatientVaccine(patientVaccine);
})
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
(e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")
);
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error")
);
}

@Test
Expand All @@ -481,11 +481,11 @@ public void testMgrValidationPatientSexIsEmpty() {

patVacManager.newPatientVaccine(patientVaccine);
})
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
(e -> ((OHServiceException) e).getMessages().size() == 1), "Expecting single validation error")
);
.isInstanceOf(OHServiceException.class)
.has(
new Condition<Throwable>(
e -> ((OHServiceException) e).getMessages().size() == 1, "Expecting single validation error")
);
}

@Test
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1d87e45

Please sign in to comment.