package com.kii.thingif.query;

import android.os.Parcel;

import com.kii.thingif.exception.InternalServerErrorException;
import com.kii.thingif.states.AirConditionerState;

import junit.framework.Assert;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RunWith(RobolectricTestRunner.class)
public class AggregatedResultTest {

    @Test
    public void equals_hashCodeTest() {
        TimeRange range = new TimeRange(new Date(1), new Date(100));
        AirConditionerState air1 = new AirConditionerState();
        air1.currentTemperature = 25;
        air1.power = false;
        HistoryState<AirConditionerState> airHS = new HistoryState<>(air1, new Date());
        List<HistoryState<AirConditionerState>> list1 = new ArrayList<>();
        list1.add(airHS);
        List<HistoryState<AirConditionerState>> list2 = new ArrayList<>();

        AggregatedResult<Integer, AirConditionerState> target =
                new AggregatedResult<>(range, 25, list1);
        AggregatedResult<Integer, AirConditionerState> sameOne =
                new AggregatedResult<>(range, 25, list1);
        AggregatedResult<Integer, AirConditionerState> different1 =
                new AggregatedResult<>(range, 10, list1);
        AggregatedResult<Integer, AirConditionerState> different2 =
                new AggregatedResult<>(range, 25, list2);

        Assert.assertTrue(target.equals(sameOne));
        Assert.assertEquals(target.hashCode(), sameOne.hashCode());

        Assert.assertFalse(target.equals(different1));
        Assert.assertNotSame(target.hashCode(), different1.hashCode());
        Assert.assertFalse(target.equals(different2));
        Assert.assertNotSame(target.hashCode(), different2.hashCode());

        Assert.assertFalse(target.equals(null));
        Assert.assertFalse(target.equals((Object)airHS));
    }

    @Test
    public void parcelableTest() {
        TimeRange range = new TimeRange(new Date(1), new Date(100));
        Integer value = 25;
        AirConditionerState air1 = new AirConditionerState();
        air1.currentTemperature = 25;
        air1.power = false;
        HistoryState<AirConditionerState> airHS = new HistoryState<>(air1, new Date());
        List<HistoryState<AirConditionerState>> list = new ArrayList<>();
        list.add(airHS);

        AggregatedResult<Integer, AirConditionerState> src =
                new AggregatedResult<>(range, value, list);

        Assert.assertNotNull(src);
        Assert.assertEquals(range, src.getTimeRange());
        Assert.assertEquals(value, src.getValue());
        Assert.assertTrue(Arrays.equals(list.toArray(), src.getAggregatedObjects().toArray()));

        Parcel parcel = Parcel.obtain();
        src.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        AggregatedResult<Integer, AirConditionerState> dest =
                AggregatedResult.CREATOR.createFromParcel(parcel);

        Assert.assertNotNull(dest);
        Assert.assertEquals(range, dest.getTimeRange());
        Assert.assertEquals(value, dest.getValue());
        Assert.assertTrue(Arrays.equals(list.toArray(), dest.getAggregatedObjects().toArray()));
    }
}

@@ -1525,16 +1525,143 @@
 list2);%0D
+%0A        AggregatedResult%3CInteger, AirConditionerState%3E different3 =%0D%0A                new AggregatedResult%3C%3E(range, 25, null);%0D
 %0A%0D%0A     
@@ -2027,32 +2027,161 @@
 t2.hashCode());%0D
+%0A        Assert.assertFalse(target.equals(different3));%0D%0A        Assert.assertNotSame(target.hashCode(), different3.hashCode());%0D
 %0A%0D%0A        Asser
@@ -3017,32 +3017,91 @@
 c.getValue());%0D%0A
+        Assert.assertNotNull(src.getAggregatedObjects());%0D%0A
         Assert.a
@@ -3574,32 +3574,92 @@
 t.getValue());%0D%0A
+        Assert.assertNotNull(dest.getAggregatedObjects());%0D%0A
         Assert.a
@@ -3746,12 +3746,1092 @@
 %0D%0A    %7D%0D
+%0A%0D%0A    @Test%0D%0A    public void parcelableNullableTest() %7B%0D%0A        TimeRange range = new TimeRange(new Date(1), new Date(100));%0D%0A        Integer value = 30;%0D%0A        AirConditionerState air1 = new AirConditionerState();%0D%0A        air1.currentTemperature = 10;%0D%0A        air1.power = true;%0D%0A%0D%0A        AggregatedResult%3CInteger, AirConditionerState%3E src =%0D%0A                new AggregatedResult%3C%3E(range, value, null);%0D%0A%0D%0A        Assert.assertNotNull(src);%0D%0A        Assert.assertEquals(range, src.getTimeRange());%0D%0A        Assert.assertEquals(value, src.getValue());%0D%0A        Assert.assertNull(src.getAggregatedObjects());%0D%0A%0D%0A        Parcel parcel = Parcel.obtain();%0D%0A        src.writeToParcel(parcel, 0);%0D%0A        parcel.setDataPosition(0);%0D%0A        AggregatedResult%3CInteger, AirConditionerState%3E dest =%0D%0A                AggregatedResult.CREATOR.createFromParcel(parcel);%0D%0A%0D%0A        Assert.assertNotNull(dest);%0D%0A        Assert.assertEquals(range, dest.getTimeRange());%0D%0A        Assert.assertEquals(value, dest.getValue());%0D%0A        Assert.assertNull(dest.getAggregatedObjects());%0D%0A    %7D%0D
 %0A%7D%0D%0A

add null check test.