Fix appointment overlap rules
This commit is contained in:
@@ -0,0 +1,112 @@
|
|||||||
|
package com.petshop.backend.service;
|
||||||
|
|
||||||
|
import com.petshop.backend.entity.Appointment;
|
||||||
|
import com.petshop.backend.entity.Customer;
|
||||||
|
import com.petshop.backend.entity.Service;
|
||||||
|
import com.petshop.backend.entity.StoreLocation;
|
||||||
|
import com.petshop.backend.repository.AppointmentRepository;
|
||||||
|
import com.petshop.backend.repository.CustomerRepository;
|
||||||
|
import com.petshop.backend.repository.ServiceRepository;
|
||||||
|
import com.petshop.backend.repository.StoreRepository;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.InjectMocks;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalTime;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
|
class AppointmentServiceTest {
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private AppointmentRepository appointmentRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private ServiceRepository serviceRepository;
|
||||||
|
|
||||||
|
@Mock
|
||||||
|
private StoreRepository storeRepository;
|
||||||
|
|
||||||
|
@InjectMocks
|
||||||
|
private AppointmentService appointmentService;
|
||||||
|
|
||||||
|
private Customer customer;
|
||||||
|
private StoreLocation store;
|
||||||
|
private Service grooming;
|
||||||
|
private Service nailTrim;
|
||||||
|
private LocalDate date;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
customer = new Customer();
|
||||||
|
customer.setCustomerId(1L);
|
||||||
|
customer.setFirstName("Pat");
|
||||||
|
customer.setLastName("Owner");
|
||||||
|
|
||||||
|
store = new StoreLocation();
|
||||||
|
store.setStoreId(1L);
|
||||||
|
store.setStoreName("Main Store");
|
||||||
|
|
||||||
|
grooming = new Service();
|
||||||
|
grooming.setServiceId(1L);
|
||||||
|
grooming.setServiceName("Grooming");
|
||||||
|
grooming.setServiceDuration(30);
|
||||||
|
|
||||||
|
nailTrim = new Service();
|
||||||
|
nailTrim.setServiceId(2L);
|
||||||
|
nailTrim.setServiceName("Nail Trim");
|
||||||
|
nailTrim.setServiceDuration(30);
|
||||||
|
|
||||||
|
date = LocalDate.now().plusDays(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void checkAvailabilityAllowsDifferentServicesAtSameTime() {
|
||||||
|
Appointment existing = appointment(1L, date, LocalTime.of(10, 0), grooming, store);
|
||||||
|
when(storeRepository.findById(1L)).thenReturn(Optional.of(store));
|
||||||
|
when(serviceRepository.findById(2L)).thenReturn(Optional.of(nailTrim));
|
||||||
|
when(appointmentRepository.findByStoreAndDate(1L, date)).thenReturn(List.of(existing));
|
||||||
|
|
||||||
|
List<String> slots = appointmentService.checkAvailability(1L, 2L, date);
|
||||||
|
|
||||||
|
assertTrue(slots.contains("10:00"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void checkAvailabilityBlocksSameServiceAtSameTime() {
|
||||||
|
Appointment existing = appointment(1L, date, LocalTime.of(10, 0), grooming, store);
|
||||||
|
when(storeRepository.findById(1L)).thenReturn(Optional.of(store));
|
||||||
|
when(serviceRepository.findById(1L)).thenReturn(Optional.of(grooming));
|
||||||
|
when(appointmentRepository.findByStoreAndDate(1L, date)).thenReturn(List.of(existing));
|
||||||
|
|
||||||
|
List<String> slots = appointmentService.checkAvailability(1L, 1L, date);
|
||||||
|
|
||||||
|
assertFalse(slots.contains("10:00"));
|
||||||
|
}
|
||||||
|
|
||||||
|
private Appointment appointment(Long id, LocalDate date, LocalTime time, Service service, StoreLocation storeLocation) {
|
||||||
|
Appointment appointment = new Appointment();
|
||||||
|
appointment.setAppointmentId(id);
|
||||||
|
appointment.setAppointmentDate(date);
|
||||||
|
appointment.setAppointmentTime(time);
|
||||||
|
appointment.setAppointmentStatus("Booked");
|
||||||
|
appointment.setService(service);
|
||||||
|
appointment.setStore(storeLocation);
|
||||||
|
appointment.setCustomer(customer);
|
||||||
|
appointment.setPets(Set.of());
|
||||||
|
return appointment;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user