Skip to content

Commit

Permalink
Fixed schedule test with patient service dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
ElviraMjeshtri committed Aug 28, 2023
1 parent 19c0b44 commit b1a1933
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,14 +17,21 @@ public class ScheduleServiceIT extends BaseIT {

@Autowired
ScheduleService scheduleService;
@Autowired
PatientService patientService;

@Test
void add_schedule_test() {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
//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());
Expand All @@ -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());
Expand All @@ -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()
Expand All @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -29,6 +32,8 @@ class ScheduleServiceTest {
private VersionService versionService;
@Mock
private NoteService noteService;
@Mock
private PatientService patientService;
@InjectMocks
private ScheduleService scheduleService;

Expand Down Expand Up @@ -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
Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -18,6 +21,9 @@ public class VersionServiceIT extends BaseIT {
@Autowired
VersionService versionService;

@Autowired
PatientService patientService;

@Test
void add_version_test() {
//given
Expand All @@ -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());
Expand Down

0 comments on commit b1a1933

Please sign in to comment.