package org.photon.tests.databaseintegrations.h2.fieldtest;

import org.junit.Before;
import org.junit.Test;
import org.photon.Photon;
import org.photon.PhotonTransaction;
import org.photon.tests.entities.fieldtest.FieldTest;
import org.photon.tests.entities.fieldtest.TestEnum;

import java.time.*;
import java.util.Date;

import static org.junit.Assert.*;

public class FieldTestTests
{
    private Photon photon;

    @Before
    public void setupDatabase()
    {
        photon = FieldTestDbSetup.setupDatabase();
    }

    @Test
    public void queryAggregate_withDates_fetchesAggregateWithCorrectValues()
    {
        FieldTestDbSetup.registerAggregate(photon);

        try(PhotonTransaction transaction = photon.beginTransaction())
        {
            FieldTest fieldTest = transaction
                .query(FieldTest.class)
                .fetchById(1);

            assertNotNull(fieldTest);
            assertEquals(1, fieldTest.getId());

            assertEquals(fieldTest.getDate(), new Date(1489933697000L));
            assertEquals(fieldTest.getZonedDateTime(), ZonedDateTime.ofInstant(Instant.ofEpochMilli(1489933698000L), ZoneId.systemDefault()));
            assertEquals(fieldTest.getLocalDate(), LocalDate.ofEpochDay(17244));
            assertEquals(fieldTest.getLocalDateTime(), LocalDateTime.ofInstant(Instant.ofEpochMilli(1489933700000L), ZoneId.systemDefault()));
            assertEquals(fieldTest.getInstant(), Instant.ofEpochMilli(1489933701000L));

            assertEquals(fieldTest.getTestEnumNumber(), TestEnum.VALUE_ZERO);
            assertEquals(fieldTest.getTestEnumString(), TestEnum.VALUE_ONE);
        }
    }

    @Test
    public void queryAggregate_nullValues_fetchesAggregateWithAllNullValues()
    {
        FieldTestDbSetup.registerAggregate(photon);

        try(PhotonTransaction transaction = photon.beginTransaction())
        {
            FieldTest fieldTest = transaction
                .query(FieldTest.class)
                .fetchById(2);

            assertNotNull(fieldTest);
            assertEquals(2, fieldTest.getId());

            assertNull(fieldTest.getDate());
            assertNull(fieldTest.getZonedDateTime());
            assertNull(fieldTest.getLocalDate());
            assertNull(fieldTest.getLocalDateTime());
            assertNull(fieldTest.getInstant());

            assertNull(fieldTest.getTestEnumNumber());
            assertNull(fieldTest.getTestEnumString());
        }
    }

    @Test
    public void createAggregate_withDates_createsAggregateWithCorrectValues()
    {
        FieldTestDbSetup.registerAggregate(photon);

        Date date = new Date(1481122691000L);
        ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(Instant.ofEpochMilli(1481122692000L), ZoneId.systemDefault());
        LocalDate localDate = LocalDate.ofEpochDay(17184);
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(1481122693000L), ZoneId.systemDefault());
        Instant instant = Instant.ofEpochMilli(1481122694000L);

        try(PhotonTransaction transaction = photon.beginTransaction())
        {
            FieldTest fieldTest = new FieldTest(3, date, zonedDateTime, localDate, localDateTime, instant, TestEnum.VALUE_ONE, TestEnum.VALUE_TWO);
            transaction.save(fieldTest);
            transaction.commit();
        }

        try(PhotonTransaction transaction = photon.beginTransaction())
        {
            FieldTest fieldTest = transaction
                .query(FieldTest.class)
                .fetchById(3);

            assertNotNull(fieldTest);
            assertEquals(3, fieldTest.getId());

            assertEquals(fieldTest.getDate(), date);
            assertEquals(fieldTest.getZonedDateTime(), zonedDateTime);
            assertEquals(fieldTest.getLocalDate(), localDate);
            assertEquals(fieldTest.getLocalDateTime(), localDateTime);
            assertEquals(fieldTest.getInstant(), instant);

            assertEquals(fieldTest.getTestEnumNumber(), TestEnum.VALUE_ONE);
            assertEquals(fieldTest.getTestEnumString(), TestEnum.VALUE_TWO);

            transaction.commit();
        }
    }
}

@@ -975,37 +975,16 @@
 tEquals(
-fieldTest.getDate(), 
 new Date
@@ -999,16 +999,37 @@
 697000L)
+, fieldTest.getDate()
 );%0A     
@@ -1052,38 +1052,8 @@
 als(
-fieldTest.getZonedDateTime(), 
 Zone
@@ -1113,39 +1113,75 @@
 0L), ZoneId.
-systemDefault()
+of(%22America/Chicago%22)), fieldTest.getZonedDateTime(
 ));%0A        
@@ -1201,34 +1201,8 @@
 als(
-fieldTest.getLocalDate(), 
 Loca
@@ -1224,16 +1224,42 @@
 y(17244)
+, fieldTest.getLocalDate()
 );%0A     
@@ -1282,38 +1282,8 @@
 als(
-fieldTest.getLocalDateTime(), 
 Loca
@@ -1347,31 +1347,67 @@
  ZoneId.
-systemDefault()
+of(%22America/Chicago%22)), fieldTest.getLocalDateTime(
 ));%0A    
@@ -1431,32 +1431,8 @@
 als(
-fieldTest.getInstant(), 
 Inst
@@ -1463,16 +1463,40 @@
 701000L)
+, fieldTest.getInstant()
 );%0A%0A    
@@ -1508,32 +1508,53 @@
    assertEquals(
+TestEnum.VALUE_ZERO, 
 fieldTest.getTes
@@ -1570,29 +1570,8 @@
 er()
-, TestEnum.VALUE_ZERO
 );%0A 
@@ -1586,32 +1586,52 @@
    assertEquals(
+TestEnum.VALUE_ONE, 
 fieldTest.getTes
@@ -1643,36 +1643,16 @@
 String()
-, TestEnum.VALUE_ONE
 );%0A     
@@ -3660,32 +3660,38 @@
    assertEquals(
+date, 
 fieldTest.getDat
@@ -3693,22 +3693,16 @@
 etDate()
-, date
 );%0A     
@@ -3713,32 +3713,47 @@
    assertEquals(
+zonedDateTime, 
 fieldTest.getZon
@@ -3764,31 +3764,16 @@
 teTime()
-, zonedDateTime
 );%0A     
@@ -3784,32 +3784,43 @@
    assertEquals(
+localDate, 
 fieldTest.getLoc
@@ -3827,27 +3827,16 @@
 alDate()
-, localDate
 );%0A     
@@ -3859,39 +3859,37 @@
 als(
-fieldTest.getLocalDateTime(), l
+localDateTime, fieldTest.getL
 ocal
@@ -3896,16 +3896,18 @@
 DateTime
+()
 );%0A     
@@ -3918,32 +3918,41 @@
    assertEquals(
+instant, 
 fieldTest.getIns
@@ -3957,25 +3957,16 @@
 nstant()
-, instant
 );%0A%0A    
@@ -3978,32 +3978,52 @@
    assertEquals(
+TestEnum.VALUE_ONE, 
 fieldTest.getTes
@@ -4035,36 +4035,16 @@
 Number()
-, TestEnum.VALUE_ONE
 );%0A     
@@ -4063,16 +4063,36 @@
 tEquals(
+TestEnum.VALUE_TWO, 
 fieldTes
@@ -4112,36 +4112,16 @@
 String()
-, TestEnum.VALUE_TWO
 );%0A%0A    

Fix issue with date tests not working on machines outside central time.