package info.nightscout.androidaps.plugins.general.automation.triggers;

import com.squareup.otto.Bus;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import java.util.GregorianCalendar;

import info.AAPSMocker;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.utils.DateUtil;
import info.nightscout.androidaps.utils.T;

import static org.powermock.api.mockito.PowerMockito.when;

@RunWith(PowerMockRunner.class)
@PrepareForTest({MainApp.class, Bus.class, DateUtil.class, GregorianCalendar.class})
public class TriggerTimeTest {

    long now = 1514766900000L;

    @Test
    public void shouldRunTest() {

        // scheduled 1 min before
        TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs());
        Assert.assertTrue(t.shouldRun());

        // scheduled 1 min in the future
        t = new TriggerTime().runAt(now + T.mins(1).msecs());
        Assert.assertFalse(t.shouldRun());

        // already run
        t = new TriggerTime().runAt(now - T.mins(1).msecs()).lastRun(now - 1);
        Assert.assertFalse(t.shouldRun());

    }

    String timeJson = "{\"data\":{\"runAt\":1514766840000,\"lastRun\":0},\"type\":\"info.nightscout.androidaps.plugins.general.automation.triggers.TriggerTime\"}";

    @Test
    public void toJSONTest() {
        TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs());
        Assert.assertEquals(timeJson, t.toJSON());
    }

    @Test
    public void fromJSONTest() throws JSONException {
        TriggerTime t = new TriggerTime().runAt(now - T.mins(1).msecs());

        TriggerTime t2 = (TriggerTime) Trigger.instantiate(new JSONObject(t.toJSON()));
        Assert.assertEquals(now - T.mins(1).msecs(), t2.getRunAt());
    }

    @Before
    public void mock() {
        AAPSMocker.mockMainApp();
        AAPSMocker.mockBus();

        PowerMockito.mockStatic(DateUtil.class);
        when(DateUtil.now()).thenReturn(now);

        GregorianCalendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(now);
        when(DateUtil.gregorianCalendar()).thenReturn(calendar);
    }
}

@@ -62,24 +62,64 @@
 .triggers;%0A%0A
+import com.google.common.base.Optional;%0A
 import com.s
@@ -573,16 +573,53 @@
 ainApp;%0A
+import info.nightscout.androidaps.R;%0A
 import i
@@ -2078,24 +2078,692 @@
 ());%0A    %7D%0A%0A
+    @Test%0A    public void copyConstructorTest() %7B%0A        TriggerTime t = new TriggerTime();%0A        t.runAt(now);%0A%0A        TriggerTime t1 = (TriggerTime) t.duplicate();%0A        Assert.assertEquals(now, t1.getRunAt(), 0.01d);%0A    %7D%0A%0A    @Test%0A    public void friendlyNameTest() %7B%0A        Assert.assertEquals(R.string.time, new TriggerTime().friendlyName());%0A    %7D%0A%0A    @Test%0A    public void friendlyDescriptionTest() %7B%0A        Assert.assertEquals(null, new TriggerTime().friendlyDescription()); //not mocked    %7D%0A    %7D%0A%0A    @Test%0A    public void iconTest() %7B%0A        Assert.assertEquals(Optional.of(R.drawable.ic_access_alarm_24dp), new TriggerTime().icon());%0A%0A    %7D%0A%0A
     @Before%0A

increase coverage in TriggerTime