package com.avg.zaap.crashlibrary.offline.uploader;

import com.avg.zaap.crashlibrary.CrashLib;
import com.avg.zaap.crashlibrary.config.CrashLibConfig;

import org.junit.Before;
import org.junit.Test;

import java.util.Calendar;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;

/**
 * A test class for {@link DelayHelper}
 * <p/>
 * Created by marek.osvald on 6.9.2016.
 */
public class DelayHelperTests {

    private IDelayHelper mDelayHelper;

    @Before
    public void setUp() throws Exception {
        initializeCrashLibConfig();
        mDelayHelper = new DelayHelper();
    }

    @Test
    public void isAttemptValid_beforeDelay_returnsFalse() throws Exception {
        // arrange
        long now = Calendar.getInstance().getTimeInMillis();
        long lastUpdateTimestamp = now - 1;

        // act
        final boolean result = mDelayHelper.isAttemptValid(lastUpdateTimestamp);

        // assert
        assertThat(now - lastUpdateTimestamp < CrashLib.getConfig().getDelay(), is(true));
        assertThat(result, is(false));
    }

    @Test
    public void isAttemptValid_afterDelay_returnsTrue() throws Exception {
        // arrange
        long now = Calendar.getInstance().getTimeInMillis();
        long lastUpdateTimestamp = now - 10 * CrashLib.getConfig().getDelay();

        // act
        final boolean result = mDelayHelper.isAttemptValid(lastUpdateTimestamp);

        // assert
        assertThat(now - lastUpdateTimestamp > CrashLib.getConfig().getDelay(), is(true));
        assertThat(result, is(true));
    }

    private void initializeCrashLibConfig() {
        CrashLib.setConfig(new CrashLibConfig.Builder()
                .build());
    }

}
