package com.avg.zaap.crashlibrary.publish.cap.http.boundary;

import com.avg.zaap.crashlibrary.publish.cap.http.boundary.BoundaryGenerator;
import com.avg.zaap.crashlibrary.publish.cap.http.boundary.IBoundaryGenerator;
import com.avg.zaap.crashlibrary.publish.cap.http.boundary.IBoundaryGeneratorFactory;

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.Random;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * A test class for {@link BoundaryGenerator}.
 * <p>
 * Created by marek.osvald on 9.6.2016.
 */
@RunWith(PowerMockRunner.class)
@PrepareForTest({BoundaryGenerator.class, System.class})
public class BoundaryGeneratorTests {

    private static final int RANDOM_NUMBER = -990;
    private static final long MOCK_CURRENT_TIME_IN_MILLIS = 123456789;

    private IBoundaryGenerator mBoundaryGenerator;
    private Random mRandom;

    @Before
    public void setUp() {
        PowerMockito.mockStatic(System.class);
        PowerMockito.when(System.currentTimeMillis()).thenReturn(MOCK_CURRENT_TIME_IN_MILLIS);

        mRandom = mock(Random.class);
        when(mRandom.nextInt()).thenReturn(RANDOM_NUMBER);

        mBoundaryGenerator = new BoundaryGenerator(new IBoundaryGeneratorFactory() {
            @Override
            public Random getRandom() {
                return mRandom;
            }
        });
    }

    @Test
    public void generateBoundary_nothing_returnsBoundary() throws Exception {
        // act
        final String result = mBoundaryGenerator.generateBoundary();

        // assert
        assertThat(result, is(MOCK_CURRENT_TIME_IN_MILLIS + "" + (-RANDOM_NUMBER)));
    }

}
