package au.gov.ga.geodesy.domain.model;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.testng.annotations.Test;

import au.gov.ga.geodesy.domain.model.sitelog.EffectiveDates;
import au.gov.ga.geodesy.domain.model.sitelog.SiteLog;
import au.gov.ga.geodesy.domain.service.CorsSiteLogService;
import au.gov.ga.geodesy.port.adapter.sopac.SopacSiteLogReader;
import au.gov.ga.geodesy.support.TestResources;
import au.gov.ga.geodesy.support.spring.GeodesyServiceTestConfig;
import au.gov.ga.geodesy.support.spring.RepositoryTest;
import au.gov.ga.geodesy.support.spring.GeodesySupportConfig;
import au.gov.ga.geodesy.support.spring.PersistenceJpaConfig;
import au.gov.ga.geodesy.support.utils.GMLDateUtils;

@ContextConfiguration(
        classes = {GeodesyServiceTestConfig.class},
        loader = AnnotationConfigContextLoader.class)
public class SetupRepositoryTest extends RepositoryTest {

    @Autowired
    private SetupRepository setups;

    @Autowired
    private CorsSiteRepository sites;

    @Autowired
    private CorsSiteLogService siteLogService;

    @Test
    @Rollback(false)
    public void upload() throws Exception {
        SiteLog alice = new SopacSiteLogReader(TestResources.sopacSiteLogReader("ALIC")).getSiteLog();
        siteLogService.upload(alice);
    }

    @Test(dependsOnMethods = {"upload"})
    public void testFindByDateRange() {
        Integer siteId = sites.findByFourCharacterId("ALIC").getId();
        Instant effectiveFrom = GMLDateUtils.stringToDate("2001-12-12", "uuuu-MM-dd");
        Instant effectiveTo = GMLDateUtils.stringToDate("2014-05-05", "uuuu-MM-dd");
        List<Setup> result = setups.findBySiteIdAndDateRange(siteId, effectiveFrom, effectiveTo);

        EffectiveDates firstPeriod = result.get(0).getEffectivePeriod();
        assertEquals(GMLDateUtils.dateToString(firstPeriod.getFrom(), "uuuu-MM-dd"), "2000-01-24");

        EffectiveDates lastPeriod = result.get(result.size() - 1).getEffectivePeriod();
        assertNull(lastPeriod.getTo());
    }
}

@@ -465,24 +465,119 @@
 ions.Test;%0A%0A
+import com.google.common.collect.Lists;%0Aimport com.querydsl.core.types.dsl.BooleanExpression;%0A%0A
 import au.go
@@ -622,24 +622,24 @@
 ctiveDates;%0A
-
 import au.go
@@ -979,132 +979,8 @@
 st;%0A
-import au.gov.ga.geodesy.support.spring.GeodesySupportConfig;%0Aimport au.gov.ga.geodesy.support.spring.PersistenceJpaConfig;%0A
 impo
@@ -1609,16 +1609,71 @@
 %0A    %7D%0A%0A
+    // TODO: remove, if can be superseded by query dsl%0A
     @Tes
@@ -1979,32 +1979,32 @@
  %22uuuu-MM-dd%22);%0A
-
         List%3CSet
@@ -2256,24 +2256,1594 @@
 0-01-24%22);%0A%0A
+        EffectiveDates lastPeriod = result.get(result.size() - 1).getEffectivePeriod();%0A        assertNull(lastPeriod.getTo());%0A    %7D%0A%0A    @Test(dependsOnMethods = %7B%22upload%22%7D)%0A    public void testFindByDateRangeUsingQueryDsl() %7B%0A        Integer siteId = sites.findByFourCharacterId(%22ALIC%22).getId();%0A        Instant effectiveFrom = GMLDateUtils.stringToDate(%222001-12-12%22, %22uuuu-MM-dd%22);%0A        Instant effectiveTo = GMLDateUtils.stringToDate(%222014-05-05%22, %22uuuu-MM-dd%22);%0A%0A        QSetup setup = QSetup.setup;%0A        BooleanExpression isValid = setup.invalidated.isFalse();%0A%0A        BooleanExpression isContained =%0A            setup.effectivePeriod.from.goe(effectiveFrom)%0A            .and(setup.effectivePeriod.to.loe(effectiveTo));%0A%0A        BooleanExpression isInterceptedByFromDate =%0A            setup.effectivePeriod.from.loe(effectiveFrom)%0A            .and(setup.effectivePeriod.to.goe(effectiveFrom));%0A%0A        BooleanExpression isInterceptedByToDate =%0A            setup.effectivePeriod.from.loe(effectiveTo)%0A            .and(setup.effectivePeriod.to.goe(effectiveTo).or(setup.effectivePeriod.to.isNull()));%0A%0A        BooleanExpression isIntercepted = isInterceptedByFromDate.or(isInterceptedByToDate);%0A%0A        List%3CSetup%3E result = Lists.newArrayList(setups.findAll(setup%0A            .siteId.eq(siteId)%0A            .and(isValid)%0A            .and(isContained.or(isIntercepted))%0A        ));%0A%0A        EffectiveDates firstPeriod = result.get(0).getEffectivePeriod();%0A        assertEquals(GMLDateUtils.dateToString(firstPeriod.getFrom(), %22uuuu-MM-dd%22), %222000-01-24%22);%0A%0A
         Effe

Reimplement a test method using QueryDSL