diff --git a/hla-product-ordering/backend/src/main/java/com/amigoscode/domain/schedule/ScheduleService.java b/hla-product-ordering/backend/src/main/java/com/amigoscode/domain/schedule/ScheduleService.java index dafd518..8af5539 100644 --- a/hla-product-ordering/backend/src/main/java/com/amigoscode/domain/schedule/ScheduleService.java +++ b/hla-product-ordering/backend/src/main/java/com/amigoscode/domain/schedule/ScheduleService.java @@ -60,7 +60,7 @@ public Schedule save(Schedule scheduleToSave, Integer userId) { } ZonedDateTime createdAt = ZonedDateTime.now(ZoneOffset.UTC); Schedule schedule = scheduleRepository.save(scheduleToSave); - if (schedule.getVersion() != null) { + if (scheduleToSave.getVersion() != null) { Version versionToSave = getVersion(scheduleToSave, createdAt, schedule, userId); Version version = versionService.save(versionToSave); Note noteToSave = getNote(scheduleToSave.getNote(), createdAt, schedule.getId(), version.getVersion(), userId); diff --git a/hla-product-ordering/backend/src/test/java/com/amigoscode/api/schedule/ScheduleControllerIT.java b/hla-product-ordering/backend/src/test/java/com/amigoscode/api/schedule/ScheduleControllerIT.java index 8f8006b..ff5b760 100644 --- a/hla-product-ordering/backend/src/test/java/com/amigoscode/api/schedule/ScheduleControllerIT.java +++ b/hla-product-ordering/backend/src/test/java/com/amigoscode/api/schedule/ScheduleControllerIT.java @@ -1,10 +1,13 @@ package com.amigoscode.api.schedule; import com.amigoscode.BaseIT; +import com.amigoscode.TestPatientFactory; import com.amigoscode.TestScheduleFactory; import com.amigoscode.TestUserFactory; import com.amigoscode.api.response.ErrorResponse; import com.amigoscode.api.response.MessageResponse; +import com.amigoscode.domain.patient.Patient; +import com.amigoscode.domain.patient.PatientService; import com.amigoscode.domain.schedule.Schedule; import com.amigoscode.domain.schedule.ScheduleService; import com.amigoscode.domain.schedule.Status; @@ -21,12 +24,18 @@ public class ScheduleControllerIT extends BaseIT { @Autowired ScheduleService service; + @Autowired + PatientService patientService; + @Test void admin_should_get_information_about_any_schedule() { //given User user = TestUserFactory.createTechnologist(); User savedUser = userService.save(user); + Patient patient = TestPatientFactory.create(); + Patient savedPatient = patientService.save(patient); Schedule schedule = TestScheduleFactory.create(); + schedule.setPatientId(savedPatient.getId()); Schedule savedSchedule = service.save(schedule, savedUser.getId()); String token = getAccessTokenForAdmin(); @@ -210,7 +219,10 @@ void admin_should_get_pageable_list_of_schedules() { //given User user = TestUserFactory.createTechnologist(); User savedUser = userService.save(user); + Patient patient = TestPatientFactory.create(); + Patient savedPatient = patientService.save(patient); Schedule schedule = TestScheduleFactory.create(); + schedule.setPatientId(savedPatient.getId()); service.save(schedule, savedUser.getId()); String adminAccessToken = getAccessTokenForAdmin(); diff --git a/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceIT.java b/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceIT.java index ccc36f7..688531e 100644 --- a/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceIT.java +++ b/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceIT.java @@ -1,8 +1,11 @@ package com.amigoscode.domain.schedule; import com.amigoscode.BaseIT; +import com.amigoscode.TestPatientFactory; import com.amigoscode.TestScheduleFactory; import com.amigoscode.TestUserFactory; +import com.amigoscode.domain.patient.Patient; +import com.amigoscode.domain.patient.PatientService; import com.amigoscode.domain.user.User; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -14,6 +17,8 @@ public class ScheduleServiceIT extends BaseIT { @Autowired ScheduleService scheduleService; + @Autowired + PatientService patientService; @Test void add_schedule_test() { @@ -21,7 +26,12 @@ void add_schedule_test() { //given User user = TestUserFactory.createTechnologist(); User savedUser = userService.save(user); + + Patient patient = TestPatientFactory.create(); + Patient savedPatient = patientService.save(patient); + Schedule scheduleToSave = TestScheduleFactory.create(); + scheduleToSave.setPatientId(savedPatient.getId()); Schedule savedSchedule = scheduleService.save(scheduleToSave, savedUser.getId()); //when Schedule readSchedule = scheduleService.findById(savedSchedule.getId()); @@ -40,9 +50,15 @@ void get_by_id_should_return_correct_schedule() throws InterruptedException { User user1 = TestUserFactory.createTechnologist(); User savedUser = userService.save(user1); + Patient patient = TestPatientFactory.create(); + Patient savedPatient = patientService.save(patient); + Schedule schedule1 = TestScheduleFactory.create(); + schedule1.setPatientId(savedPatient.getId()); Schedule schedule2 = TestScheduleFactory.create(); + schedule2.setPatientId(savedPatient.getId()); Schedule schedule3 = TestScheduleFactory.create(); + schedule3.setPatientId(savedPatient.getId()); Schedule savedSchedule1 = scheduleService.save(schedule1, savedUser.getId()); Schedule savedSchedule2 = scheduleService.save(schedule2, savedUser.getId()); @@ -66,11 +82,15 @@ void update_schedule_test() { //given User user = TestUserFactory.createTechnologist(); User savedUser = userService.save(user); + Patient patient = TestPatientFactory.create(); + Patient savedPatient = patientService.save(patient); + Schedule scheduleToSave = TestScheduleFactory.create(); + scheduleToSave.setPatientId(savedPatient.getId()); Schedule savedSchedule = scheduleService.save(scheduleToSave, savedUser.getId()); Schedule scheduleToUpdate = new Schedule( savedSchedule.getId(), - 2, + savedPatient.getId(), Status.REVIEW, savedSchedule.getVersion(), savedSchedule.getNote() @@ -80,7 +100,7 @@ void update_schedule_test() { Schedule readSchedule = scheduleService.findById(savedSchedule.getId()); //then - Assertions.assertEquals(2, readSchedule.getPatientId()); + Assertions.assertEquals(patient.getId(), readSchedule.getPatientId()); Assertions.assertEquals(scheduleToUpdate.getVersion().getUpdatedAt().format(formatter), readSchedule.getVersion().getUpdatedAt().format(formatter)); Assertions.assertEquals(scheduleToUpdate.getNote().getId(), readSchedule.getNote().getId()); Assertions.assertEquals(scheduleToUpdate.getNote().getCreatedAt().format(formatter), readSchedule.getNote().getCreatedAt().format(formatter)); diff --git a/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceTest.java b/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceTest.java index b301ea6..df695b6 100644 --- a/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceTest.java +++ b/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/schedule/ScheduleServiceTest.java @@ -2,6 +2,9 @@ import com.amigoscode.domain.note.Note; import com.amigoscode.domain.note.NoteService; +import com.amigoscode.domain.patient.Gender; +import com.amigoscode.domain.patient.Patient; +import com.amigoscode.domain.patient.PatientService; import com.amigoscode.domain.user.User; import com.amigoscode.domain.user.UserRole; import com.amigoscode.domain.version.State; @@ -29,6 +32,8 @@ class ScheduleServiceTest { private VersionService versionService; @Mock private NoteService noteService; + @Mock + private PatientService patientService; @InjectMocks private ScheduleService scheduleService; @@ -119,6 +124,14 @@ class ScheduleServiceTest { UserRole.TECHNOLOGIST, ZonedDateTime.of(2023, 6, 17, 12, 40, 00, 0, ZoneId.of("UTC")) ); + private final Patient patient = new Patient( + 25, + "John Doe", + "mrn1", + Gender.MALE, + ZonedDateTime.of(2003, 6, 17, 12, 40, 00, 0, ZoneId.of("UTC")), + ZonedDateTime.now() + ); @Test @@ -127,6 +140,7 @@ void find_by_id_method_should_return_founded_schedule_when_schedule_exist() { Mockito.when(scheduleRepository.findById(schedule.getId())).thenReturn(Optional.of(schedule)); Mockito.when(versionService.findAllVersionsByScheduleId(schedule.getId())).thenReturn(List.of(version)); Mockito.when(noteService.findByScheduleIdAndVersion(schedule.getId(), version.getVersion())).thenReturn(note); + Mockito.when(patientService.findById(schedule.getPatientId())).thenReturn(patient); //when Schedule foundedSchedule = scheduleService.findById(schedule.getId()); diff --git a/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/version/VersionServiceIT.java b/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/version/VersionServiceIT.java index 18de195..25fb403 100644 --- a/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/version/VersionServiceIT.java +++ b/hla-product-ordering/backend/src/test/java/com/amigoscode/domain/version/VersionServiceIT.java @@ -1,9 +1,12 @@ package com.amigoscode.domain.version; import com.amigoscode.BaseIT; +import com.amigoscode.TestPatientFactory; import com.amigoscode.TestScheduleFactory; import com.amigoscode.TestUserFactory; import com.amigoscode.domain.note.NoteService; +import com.amigoscode.domain.patient.Patient; +import com.amigoscode.domain.patient.PatientService; import com.amigoscode.domain.schedule.Schedule; import com.amigoscode.domain.schedule.ScheduleService; import com.amigoscode.domain.user.User; @@ -18,6 +21,9 @@ public class VersionServiceIT extends BaseIT { @Autowired VersionService versionService; + @Autowired + PatientService patientService; + @Test void add_version_test() { //given @@ -40,9 +46,15 @@ void get_by_id_should_return_correct_version() { //given User user1 = TestUserFactory.createTechnologist(); User savedUser = userService.save(user1); + Patient patient = TestPatientFactory.create(); + Patient savedPatient = patientService.save(patient); Schedule schedule1 = TestScheduleFactory.create(); + schedule1.setPatient(savedPatient); + schedule1.setPatientId(savedPatient.getId()); Schedule schedule2 = TestScheduleFactory.create(); + schedule2.setPatient(savedPatient); + schedule2.setPatientId(savedPatient.getId()); Schedule savedSchedule1 = scheduleService.save(schedule1, savedUser.getId()); Schedule savedSchedule2 = scheduleService.save(schedule2, savedUser.getId());