package de.techdev.trackr.domain.employee.vacation;

import de.techdev.trackr.TransactionalIntegrationTest;
import de.techdev.trackr.util.LocalDateUtil;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.util.List;

import static org.echocat.jomon.testing.BaseMatchers.isNotEmpty;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * @author Moritz Schulze
 */
public class VacationRequestRepositoryTest extends TransactionalIntegrationTest {
    
    @Autowired
    private VacationRequestDataOnDemand vacationRequestDataOnDemand;

    @Autowired
    private VacationRequestRepository vacationRequestRepository;

    @Before
    public void setUp() throws Exception {
        vacationRequestDataOnDemand.init();
    }

    @Test
    public void one() throws Exception {
        VacationRequest vacationRequest = vacationRequestDataOnDemand.getRandomObject();
        VacationRequest one = vacationRequestRepository.findOne(vacationRequest.getId());
        assertThat(one.getId(), is(vacationRequest.getId()));
    }

    @Test
    public void all() throws Exception {
        Iterable<VacationRequest> all = vacationRequestRepository.findAll();
        assertThat(all, isNotEmpty());
    }

    @Test
    public void findByEmployeeOrderByStartDateAsc() throws Exception {
        VacationRequest vacationRequest = vacationRequestDataOnDemand.getRandomObject();
        List<VacationRequest> all = vacationRequestRepository.findByEmployeeOrderByStartDateAsc(vacationRequest.getEmployee());
        assertThat(all, isNotEmpty());
    }

    @Test
    public void findByApprovedOrderBySubmissionTimeAsc() throws Exception {
        VacationRequest vacationRequest = vacationRequestDataOnDemand.getRandomObject();
        List<VacationRequest> all = vacationRequestRepository.findByStatusOrderBySubmissionTimeAsc(vacationRequest.getStatus());
        assertThat(all, isNotEmpty());
    }

    @Test
    public void findBySubmissionTimeBefore() throws Exception {
        VacationRequest vacationRequest = vacationRequestDataOnDemand.getRandomObject();
        vacationRequest.setSubmissionTime(LocalDateUtil.fromLocalDate(LocalDate.now().minusDays(8)));
        vacationRequest.setStatus(VacationRequest.VacationRequestStatus.PENDING);
        vacationRequestRepository.save(vacationRequest);
        List<VacationRequest> all = vacationRequestRepository.findBySubmissionTimeBeforeAndStatus(LocalDateUtil.fromLocalDate(LocalDate.now().minusDays(7)), VacationRequest.VacationRequestStatus.PENDING);
        assertThat(all, isNotEmpty());
    }
}

@@ -97,24 +97,138 @@
 rationTest;%0A
+import de.techdev.trackr.domain.employee.Employee;%0Aimport de.techdev.trackr.domain.employee.EmployeeDataOnDemand;%0A
 import de.te
@@ -260,16 +260,16 @@
 teUtil;%0A
-
 import o
@@ -372,16 +372,113 @@
 wired;%0A%0A
+import java.text.DateFormat;%0Aimport java.text.ParseException;%0Aimport java.text.SimpleDateFormat;%0A
 import j
@@ -497,16 +497,39 @@
 alDate;%0A
+import java.util.Date;%0A
 import j
@@ -544,16 +544,78 @@
 .List;%0A%0A
+import static org.echocat.jomon.testing.BaseMatchers.hasSize;%0A
 import s
@@ -897,32 +897,32 @@
 %0A    @Autowired%0A
-
     private Vaca
@@ -975,16 +975,87 @@
 emand;%0A%0A
+    @Autowired%0A    private EmployeeDataOnDemand employeeDataOnDemand;%0A%0A
     @Aut
@@ -3053,11 +3053,1083 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void findByApprovedBetween() throws ParseException %7B%0A        Employee employee = employeeDataOnDemand.getRandomObject();%0A        DateFormat df = new SimpleDateFormat(%22dd.MM.yyyy%22);%0A%0A        Date start = df.parse(%2201.10.2014%22);%0A        Date end   = df.parse(%2208.12.2014%22);%0A%0A        VacationRequest vr1 = new VacationRequest();%0A        vr1.setEmployee(employee);%0A        vr1.setStartDate(start);%0A        vr1.setEndDate(end);%0A        vr1.setStatus(VacationRequest.VacationRequestStatus.APPROVED);%0A        vacationRequestRepository.save(vr1);%0A%0A        VacationRequest vr2 = new VacationRequest();%0A        vr2.setEmployee(employee);%0A        vr2.setStartDate(start);%0A        vr2.setEndDate(end);%0A        vr2.setStatus(VacationRequest.VacationRequestStatus.REJECTED);%0A        vacationRequestRepository.save(vr2);%0A%0A        List%3CVacationRequest%3E all = vacationRequestRepository%0A                .findByStartDateBetweenOrEndDateBetweenAndStatus(start, end, start, end, VacationRequest.VacationRequestStatus.APPROVED);%0A        assertThat(all, hasSize(1));%0A    %7D
 %0A%7D%0A

test query for approved vacation requests