diff options
| author | lucashemi <lucasxberger@gmail.com> | 2023-02-24 16:14:01 -0300 |
|---|---|---|
| committer | lucashemi <lucasxberger@gmail.com> | 2023-02-24 16:14:01 -0300 |
| commit | 9f82c403098bb96acd8b7116f84416d3b4643b57 (patch) | |
| tree | f69200376357e803dab229ff5cfea0c001e1803c /api/src/test | |
| parent | 514f2e7194a875cfc53d7e1bccd922db2bbb3f3f (diff) | |
api-appointments
Diffstat (limited to 'api/src/test')
4 files changed, 266 insertions, 13 deletions
diff --git a/api/src/test/java/med/voll/api/ApiApplicationTests.java b/api/src/test/java/med/voll/api/ApiApplicationTests.java deleted file mode 100644 index eb360a5..0000000 --- a/api/src/test/java/med/voll/api/ApiApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package med.voll.api; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class ApiApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/api/src/test/java/med/voll/api/controller/AppointmentControllerTest.java b/api/src/test/java/med/voll/api/controller/AppointmentControllerTest.java new file mode 100644 index 0000000..6a0356c --- /dev/null +++ b/api/src/test/java/med/voll/api/controller/AppointmentControllerTest.java @@ -0,0 +1,82 @@ +package med.voll.api.controller; + +import med.voll.api.domain.appointment.AppointmentListingData; +import med.voll.api.domain.appointment.AppointmentRegistrationData; +import med.voll.api.domain.appointment.AppointmentsSchedule; +import med.voll.api.domain.doctor.Specialty; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; + +@SpringBootTest +@AutoConfigureMockMvc +@AutoConfigureJsonTesters +class AppointmentControllerTest { + + @Autowired + private MockMvc mockMvc; + + @Autowired + private JacksonTester<AppointmentRegistrationData> jacksonTesterRegistration; + + @Autowired + private JacksonTester<AppointmentListingData> jacksonTesterListing; + + @MockBean + private AppointmentsSchedule appointmentsSchedule; + + @Test + @DisplayName("Should return http error 400 when receiving invalid data") + @WithMockUser + void registerTest1() throws Exception { + var response = mockMvc.perform(post("/appointments")) + .andReturn().getResponse(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + @Test + @DisplayName("Should return http error 200 when receiving valid data") + @WithMockUser + void registerTest2() throws Exception { + var date = LocalDateTime.now().plusHours(1); + var specialty = Specialty.CARDIOLOGY; + + var listingData = new AppointmentListingData(null, date, 1l, 1l); + + when(appointmentsSchedule.schedule(any())).thenReturn(listingData); + + var response = mockMvc.perform(post("/appointments") + .contentType(MediaType.APPLICATION_JSON) + .content(jacksonTesterRegistration.write( + new AppointmentRegistrationData(1l, 1l, date, specialty) + ).getJson()) + ) + .andReturn().getResponse(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.OK.value()); + + var expectedJson = jacksonTesterListing.write( + listingData + ).getJson(); + + assertThat(response.getContentAsString()).isEqualTo(expectedJson); + } +}
\ No newline at end of file diff --git a/api/src/test/java/med/voll/api/controller/DoctorControllerTest.java b/api/src/test/java/med/voll/api/controller/DoctorControllerTest.java new file mode 100644 index 0000000..dbd3338 --- /dev/null +++ b/api/src/test/java/med/voll/api/controller/DoctorControllerTest.java @@ -0,0 +1,77 @@ +package med.voll.api.controller; + +import med.voll.api.domain.address.Address; +import med.voll.api.domain.address.AddressData; +import med.voll.api.domain.appointment.AppointmentListingData; +import med.voll.api.domain.appointment.AppointmentRegistrationData; +import med.voll.api.domain.doctor.*; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.json.AutoConfigureJsonTesters; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.json.JacksonTester; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + +import java.time.LocalDateTime; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; + +@SpringBootTest +@AutoConfigureMockMvc +@AutoConfigureJsonTesters +class DoctorControllerTest { + + @Autowired + private MockMvc mockMvc; + @Autowired + private JacksonTester<DoctorRegistrationData> jacksonTesterRegistration; + @Autowired + private JacksonTester<DoctorDetailingData> jacksonTesterListing; + @MockBean + private DoctorRepository doctorRepository; + + @Test + @DisplayName("Should return http error 400 when receiving invalid data") + @WithMockUser + void registerTest1() throws Exception { + var response = mockMvc.perform(post("/doctors")) + .andReturn().getResponse(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + @Test + @DisplayName("Should return http error 200 when receiving valid data") + @WithMockUser + void registerTest2() throws Exception { + var addressData = new AddressData("Route 66", "", "55555", "Los Angeles", "CA"); + var registerData = new DoctorRegistrationData("doctor", "doctor@voll.med", "9999999999", Specialty.CARDIOLOGY, addressData); + + when(doctorRepository.save(any())).thenReturn(new Doctor(registerData)); + + var response = mockMvc.perform(post("/doctors") + .contentType(MediaType.APPLICATION_JSON) + .content(jacksonTesterRegistration.write(registerData).getJson())) + .andReturn().getResponse(); + + assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED.value()); + + var detailingData = new DoctorDetailingData(null, registerData.name(), registerData.email(), registerData.phone(), registerData.specialty(), new Address(registerData.addressData())); + + var expectedJson = jacksonTesterListing.write( + detailingData + ).getJson(); + + assertThat(response.getContentAsString()).isEqualTo(expectedJson); + } +}
\ No newline at end of file diff --git a/api/src/test/java/med/voll/api/domain/doctor/DoctorRepositoryTest.java b/api/src/test/java/med/voll/api/domain/doctor/DoctorRepositoryTest.java new file mode 100644 index 0000000..1ff80be --- /dev/null +++ b/api/src/test/java/med/voll/api/domain/doctor/DoctorRepositoryTest.java @@ -0,0 +1,107 @@ +package med.voll.api.domain.doctor; + +import med.voll.api.domain.address.AddressData; +import med.voll.api.domain.appointment.Appointment; +import med.voll.api.domain.patient.Patient; +import med.voll.api.domain.patient.PatientRegistrationData; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; +import org.springframework.test.context.ActiveProfiles; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.temporal.TemporalAdjusters; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +@ActiveProfiles("test") +class DoctorRepositoryTest { + + @Autowired + private DoctorRepository doctorRepository; + + @Autowired + private TestEntityManager testEntityManager; + + @Test + @DisplayName("Should return null when there's no doctor available in the date") + void chooseRandomDoctorAvailableTest1() { + var nextMonday10AM = LocalDate.now() + .with(TemporalAdjusters.next(DayOfWeek.MONDAY)) + .atTime(10, 0); + + var doctor = registerDoctor("doctor", "doctor@voll.med", Specialty.CARDIOLOGY); + var patient = registerPatient("patient", "patient@voll.med", "00000000"); + scheduleAppointment(nextMonday10AM, doctor, patient); + + var availableDoctor = doctorRepository.chooseRandomDoctorAvailable(Specialty.CARDIOLOGY, nextMonday10AM); + assertThat(availableDoctor).isNull(); + } + + @Test + @DisplayName("Should return doctor when there's an doctor available in the date") + void chooseRandomDoctorAvailableTest2() { + var nextMonday10AM = LocalDate.now() + .with(TemporalAdjusters.next(DayOfWeek.MONDAY)) + .atTime(10, 0); + + var doctor = registerDoctor("doctor", "doctor@voll.med", Specialty.CARDIOLOGY); + + var availableDoctor = doctorRepository.chooseRandomDoctorAvailable(Specialty.CARDIOLOGY, nextMonday10AM); + assertThat(availableDoctor).isEqualTo(doctor); + } + + private void scheduleAppointment(LocalDateTime date, Doctor doctor, Patient patient) { + testEntityManager.persist(new Appointment(null, date, doctor, patient)); + } + + private Doctor registerDoctor(String name, String email, Specialty specialty) { + var doctor = new Doctor(doctorData(name, email, specialty)); + testEntityManager.persist(doctor); + return doctor; + } + + private Patient registerPatient(String name, String email, String ssn) { + var patient = new Patient(patientData(name, email, ssn)); + testEntityManager.persist(patient); + return patient; + } + + private DoctorRegistrationData doctorData(String name, String email, Specialty specialty) { + return new DoctorRegistrationData( + name, + email, + "999999999", + specialty, + addressData() + ); + } + + private PatientRegistrationData patientData(String name, String email, String ssn) { + return new PatientRegistrationData( + name, + email, + "999999999", + ssn, + addressData() + ); + } + + private AddressData addressData() { + return new AddressData( + "Route 66", + "", + "55555", + "Los Angeles", + "CA" + ); + } +}
\ No newline at end of file |
