add tests for save, for OpeningHours
@@ -6,14 +6,18 @@ import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.when;
import greencity.GreenCityApplication;
-import greencity.entity.Category;
+import greencity.entity.BreakTime;
import greencity.entity.OpeningHours;
+import greencity.exception.BadRequestException;
import greencity.exception.NotFoundException;
import greencity.repository.OpenHoursRepo;
+import greencity.service.BreakTimeService;
+import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
+import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
@@ -26,16 +30,66 @@ import org.springframework.boot.test.context.SpringBootTest;
public class OpenHoursServiceImplTest {
@Mock
private OpenHoursRepo openHoursRepo;
+ @Mock
+ private BreakTimeService breakTimeService;
@InjectMocks
private OpenHoursServiceImpl openHoursService;
@Test
- public void saveTest() {
- OpeningHours genericEntity = new OpeningHours();
+ public void saveTestWOBreakTime() {
+ OpeningHours openingHours = OpeningHours.builder()
+ .openTime(LocalTime.of(9, 0))
+ .closeTime(LocalTime.of(20, 0))
+ .build();
+
+ when(openHoursRepo.save(openingHours)).thenReturn(openingHours);
+
+ assertEquals(openingHours, openHoursService.save(openingHours));
+ }
+
+ @Test
+ public void saveTestWithBreakTime() {
+ BreakTime breakTime = BreakTime.builder()
+ .startTime(LocalTime.of(13, 0))
+ .endTime(LocalTime.of(14, 0))
+ .build();
+
+ OpeningHours openingHours = OpeningHours.builder()
+ .openTime(LocalTime.of(9, 0))
+ .closeTime(LocalTime.of(20, 0))
+ .breakTime(breakTime)
+ .build();
+
+ when(openHoursRepo.save(openingHours)).thenReturn(openingHours);
+ when(breakTimeService.save(any(BreakTime.class))).thenReturn(breakTime);
+
+ assertEquals(openingHours, openHoursService.save(openingHours));
+ }
+
+ @Test(expected = BadRequestException.class)
+ public void saveTestWithOpeningHoursBiggerThanEndOpeningHours_ThrowException() {
+ OpeningHours openingHours = OpeningHours.builder()
+ .openTime(LocalTime.of(20, 0))
+ .closeTime(LocalTime.of(9, 0))
+ .build();
+
+ openHoursService.save(openingHours);
+ }
+
+ @Test(expected = BadRequestException.class)
+ public void saveTestWithStartBreakTimeBiggerThanEndBreakTime_ThrowException() {
+ BreakTime breakTime = BreakTime.builder()
+ .startTime(LocalTime.of(13, 0))
+ .endTime(LocalTime.of(21, 0))
+ .build();
- when(openHoursRepo.save(genericEntity)).thenReturn(genericEntity);
+ OpeningHours openingHours = OpeningHours.builder()
+ .openTime(LocalTime.of(9, 0))
+ .closeTime(LocalTime.of(20, 0))
+ .breakTime(breakTime)
+ .build();
- assertEquals(genericEntity, openHoursService.save(genericEntity));
+ openHoursService.save(openingHours);
}
@Test
