diff --git a/src/main/java/com/production/ehayvanbackendapi/DTO/request/CreateOrUpdateScheduleDTO.java b/src/main/java/com/production/ehayvanbackendapi/DTO/request/CreateOrUpdateScheduleDTO.java index 1ed742f..770c6b3 100644 --- a/src/main/java/com/production/ehayvanbackendapi/DTO/request/CreateOrUpdateScheduleDTO.java +++ b/src/main/java/com/production/ehayvanbackendapi/DTO/request/CreateOrUpdateScheduleDTO.java @@ -1,6 +1,7 @@ package com.production.ehayvanbackendapi.DTO.request; import com.fasterxml.jackson.annotation.JsonFormat; +import com.production.ehayvanbackendapi.DTO.ScheduleDTO; import org.springframework.cglib.core.Local; import org.springframework.format.annotation.DateTimeFormat; @@ -15,6 +16,16 @@ public class CreateOrUpdateScheduleDTO { private Integer doseFrequency; private Integer doseCount; + public CreateOrUpdateScheduleDTO() { + + } + + public CreateOrUpdateScheduleDTO(ScheduleDTO scheduleDTO) { + this.beginningDate = scheduleDTO.getBeginningDate(); + this.doseFrequency = scheduleDTO.getDoseFrequency(); + this.doseCount = scheduleDTO.getDoseCount(); + } + public LocalDate getBeginningDate() { return beginningDate; } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8d27f7e..4d61498 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,3 +7,4 @@ spring.jpa.hibernate.ddl-auto=update spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect spring.security.user.name=test spring.security.user.password=password +spring.datasource.hikari.maximum-pool-size=2 \ No newline at end of file diff --git a/src/test/java/com/production/ehayvanbackendapi/ServiceTests/AppointmentServiceTest.java b/src/test/java/com/production/ehayvanbackendapi/ServiceTests/AppointmentServiceTest.java index 491cc11..d8a1652 100644 --- a/src/test/java/com/production/ehayvanbackendapi/ServiceTests/AppointmentServiceTest.java +++ b/src/test/java/com/production/ehayvanbackendapi/ServiceTests/AppointmentServiceTest.java @@ -135,7 +135,7 @@ public void testServiceUpdatePet() { @Test @Transactional - public void testServiceDeletePet() { + public void testServiceDeleteSchedule() { // firstly save new Appointment Appointment returnedAppointment = testAppointmentRepository.save(testAppointment); Optional searchedAppointment = testAppointmentRepository.findById(returnedAppointment.getAppointmentID()); diff --git a/src/test/java/com/production/ehayvanbackendapi/ServiceTests/CustomerServiceTest.java b/src/test/java/com/production/ehayvanbackendapi/ServiceTests/CustomerServiceTest.java new file mode 100644 index 0000000..da30753 --- /dev/null +++ b/src/test/java/com/production/ehayvanbackendapi/ServiceTests/CustomerServiceTest.java @@ -0,0 +1,118 @@ +package com.production.ehayvanbackendapi.ServiceTests; + +import com.production.ehayvanbackendapi.DTO.CustomerDTO; +import com.production.ehayvanbackendapi.DTO.PetDTO; +import com.production.ehayvanbackendapi.DTO.ScheduleDTO; +import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateCustomerDTO; +import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdatePetDTO; +import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateScheduleDTO; +import com.production.ehayvanbackendapi.Entities.*; +import com.production.ehayvanbackendapi.Mappers.CustomerMapper; +import com.production.ehayvanbackendapi.Mappers.PetMapper; +import com.production.ehayvanbackendapi.Mappers.ScheduleMapper; +import com.production.ehayvanbackendapi.Repositories.CustomerRepository; +import com.production.ehayvanbackendapi.Repositories.PetRepository; +import com.production.ehayvanbackendapi.Repositories.ScheduleRepository; +import com.production.ehayvanbackendapi.Services.CustomerService; +import com.production.ehayvanbackendapi.Services.PetService; +import com.production.ehayvanbackendapi.Services.ScheduleService; +import com.production.ehayvanbackendapi.TestUtils.DataSeed; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.time.Month; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@AutoConfigureMockMvc +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class CustomerServiceTest { + @SpyBean + @Autowired + private CustomerService testCustomerService; + + @SpyBean + @Autowired + private CustomerRepository testCustomerRepository; + + @Autowired + DataSeed dataSeed; + + private Customer testCustomer; + + private CreateOrUpdateCustomerDTO testCreateOrUpdateCustomerDTO; + + @Autowired + private CustomerMapper testCustomerMapper; + + @BeforeAll + public void setUp() { + dataSeed.loadSeedToDatabase(); + } + + @BeforeEach + @Transactional + public void onEachTestStart() { + testCustomer = new Customer(); + testCustomer.setUserID(1); + testCustomer.setName("Meth"); + testCustomer.setSurname("Fulled"); + testCustomer.setPassword("Finn"); + testCustomer.setEmail("finnn@minnn.com"); + + testCreateOrUpdateCustomerDTO = new CreateOrUpdateCustomerDTO(testCustomerMapper.convertToDto(testCustomer)); + } + + @AfterEach + public void onEachTestEnd() { + testCreateOrUpdateCustomerDTO = null; + testCustomer = null; + } + + @Test + public void testServiceGetByIdWhichNoExists() { + int testCustomerId = 0; + CustomerDTO returnedCustomerDTO = testCustomerService.getCustomerById(testCustomerId); + assertThat(returnedCustomerDTO).isNull(); + } + + @Test + @Transactional + public void testServiceGetByIdWhichInDatabase() { + Customer returnedCustomer = testCustomerRepository.save(testCustomer); + CustomerDTO returnedCustomerDTO = testCustomerService.getCustomerById(returnedCustomer.getUserID()); + + assertThat(returnedCustomerDTO).isNotNull(); + assertThat(returnedCustomerDTO.getUserID()).isEqualTo(returnedCustomer.getUserID()); + assertThat(returnedCustomerDTO.getName()).isEqualTo(returnedCustomer.getName()); + assertThat(returnedCustomerDTO.getSurname()).isEqualTo(returnedCustomer.getSurname()); + assertThat(returnedCustomerDTO.getPassword()).isEqualTo(returnedCustomer.getPassword()); + assertThat(returnedCustomerDTO.getEmail()).isEqualTo(returnedCustomer.getEmail()); + + testCustomerRepository.deleteById(returnedCustomer.getUserID()); + } + + @Test + @Transactional + public void testServiceDeleteCustomer() { + // firstly save new Customer + Customer returnedCustomer = testCustomerRepository.save(testCustomer); + Optional searchedCustomer = testCustomerRepository.findById(returnedCustomer.getUserID()); + assertThat(searchedCustomer.isPresent()).isEqualTo(true); + + CustomerDTO deletedCustomer = testCustomerService.deleteCustomer(returnedCustomer.getUserID()); + + assertThat(deletedCustomer.getUserID()).isEqualTo(returnedCustomer.getUserID()); + + searchedCustomer = testCustomerRepository.findById(returnedCustomer.getUserID()); + assertThat(searchedCustomer.isPresent()).isEqualTo(false); + } +} diff --git a/src/test/java/com/production/ehayvanbackendapi/ServiceTests/ScheduleServiceTest.java b/src/test/java/com/production/ehayvanbackendapi/ServiceTests/ScheduleServiceTest.java new file mode 100644 index 0000000..7c48bff --- /dev/null +++ b/src/test/java/com/production/ehayvanbackendapi/ServiceTests/ScheduleServiceTest.java @@ -0,0 +1,110 @@ +package com.production.ehayvanbackendapi.ServiceTests; + +import com.production.ehayvanbackendapi.DTO.PetDTO; +import com.production.ehayvanbackendapi.DTO.ScheduleDTO; +import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdatePetDTO; +import com.production.ehayvanbackendapi.DTO.request.CreateOrUpdateScheduleDTO; +import com.production.ehayvanbackendapi.Entities.*; +import com.production.ehayvanbackendapi.Mappers.PetMapper; +import com.production.ehayvanbackendapi.Mappers.ScheduleMapper; +import com.production.ehayvanbackendapi.Repositories.PetRepository; +import com.production.ehayvanbackendapi.Repositories.ScheduleRepository; +import com.production.ehayvanbackendapi.Services.PetService; +import com.production.ehayvanbackendapi.Services.ScheduleService; +import com.production.ehayvanbackendapi.TestUtils.DataSeed; +import org.junit.jupiter.api.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.transaction.annotation.Transactional; + +import java.time.LocalDate; +import java.time.Month; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +@AutoConfigureMockMvc +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class ScheduleServiceTest { + @SpyBean + @Autowired + private ScheduleService testScheduleService; + + @SpyBean + @Autowired + private ScheduleRepository testScheduleRepository; + + @Autowired + DataSeed dataSeed; + + private Schedule testSchedule; + + private CreateOrUpdateScheduleDTO testCreateOrUpdateScheduleDTO; + + @Autowired + private ScheduleMapper testScheduleMapper; + + @BeforeAll + public void setUp() { + dataSeed.loadSeedToDatabase(); + } + + @BeforeEach + @Transactional + public void onEachTestStart() { + testSchedule = new Schedule(); + testSchedule.setScheduleID(10); + testSchedule.setDoseCount(12); + testSchedule.setDoseFrequency(1000); + testSchedule.setBeginningDate(LocalDate.of(1995, Month.APRIL, 30)); + + testCreateOrUpdateScheduleDTO = new CreateOrUpdateScheduleDTO(testScheduleMapper.convertToDto(testSchedule)); + } + + @AfterEach + public void onEachTestEnd() { + testCreateOrUpdateScheduleDTO = null; + testSchedule = null; + } + + @Test + public void testServiceGetByIdWhichNoExists() { + int testScheduleId = 0; + ScheduleDTO returnedScheduleDTO = testScheduleService.getScheduleById(testScheduleId); + assertThat(returnedScheduleDTO).isNull(); + } + + @Test + @Transactional + public void testServiceGetByIdWhichInDatabase() { + Schedule returnedSchedule = testScheduleRepository.save(testSchedule); + ScheduleDTO returnedScheduleDTO = testScheduleService.getScheduleById(returnedSchedule.getScheduleID()); + + assertThat(returnedScheduleDTO).isNotNull(); + assertThat(returnedScheduleDTO.getScheduleID()).isEqualTo(returnedSchedule.getScheduleID()); + assertThat(returnedScheduleDTO.getBeginningDate()).isEqualTo(returnedSchedule.getBeginningDate()); + assertThat(returnedScheduleDTO.getDoseCount()).isEqualTo(returnedSchedule.getDoseCount()); + + testScheduleRepository.deleteById(returnedSchedule.getScheduleID()); + } + + @Test + @Transactional + public void testServiceDeleteSchedule() { + // firstly save new Pet + Schedule returnedSchedule = testScheduleRepository.save(testSchedule); + Optional searchedSchedule = testScheduleRepository.findById(returnedSchedule.getScheduleID()); + assertThat(searchedSchedule.isPresent()).isEqualTo(true); + + ScheduleDTO deletedSchedule = testScheduleService.deleteSchedule(returnedSchedule.getScheduleID()); + + assertThat(deletedSchedule.getScheduleID()).isEqualTo(returnedSchedule.getScheduleID()); + + searchedSchedule = testScheduleRepository.findById(returnedSchedule.getScheduleID()); + assertThat(searchedSchedule.isPresent()).isEqualTo(false); + } +}