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

import android.os.Build;

import org.junit.Before;
import org.junit.Test;
import org.powermock.core.classloader.annotations.PrepareForTest;

import java.util.Map;

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

/**
 * A test class for {@link ExceptionHeaderGenerator}.
 * <p/>
 * Created by marek.osvald on 10.6.2016.
 */
@PrepareForTest({Build.VERSION.class})
public class ExceptionHeaderGeneratorTests {

    private static final int EXCEPTION_HEADER_SIZE = 7;
    //    private static final int EXCEPTION_HEADER_SIZE = 8;
    private static final String MOCK_EXCEPTION_CODE = "mockExceptionCode";
    private static final String MOCK_EXCEPTION_FAULTING_OFFSET = "mockExceptionFaultingOffset";
    private static final String MOCK_CRASHING_MODULE_NAME = "mockCrashingModuleName";
    private static final String MOCK_CRASHING_MODULE_VERSION = "mockCrashingModuleVersion";
    private static final String MOCK_CRASHING_PROCESS_NAME = "mockCrashingProcessName";
    private static final String MOCK_CRASHING_PROCESS_VERSION = "mockCrashingProcessVersion";
    private static final String MOCK_BUCKET_ID = "mockBucketID";
//    private static final String MOCK_OS_VERSION = "1.2";

    private static final String HEADER_KEY_EXCEPTION_CODE = "EXC";
    private static final String HEADER_KEY_EXCEPTION_FAULTING_OFFSET = "FSO";
    private static final String HEADER_KEY_CRASHING_MODULE_NAME = "MDN";
    private static final String HEADER_KEY_CRASHING_MODULE_VERSION = "MDV";
    private static final String HEADER_KEY_CRASHING_PROCESS_NAME = "PFN";
    private static final String HEADER_KEY_CRASHING_PROCESS_VERSION = "PFV";
    private static final String HEADER_KEY_BUCKET_ID = "BCK";
//    private static final String HEADER_KEY_OS_VERSION = "OSV";

    private IExceptionHeaderGenerator mExceptionHeaderGenerator;

    @Before
    public void setUp() throws Exception {
//        initializeBuild();
        mExceptionHeaderGenerator = new ExceptionHeaderGenerator();
    }

    @Test
    public void generateHeader_nothing_valuesSet() throws Exception {
        // act
        final Map<String, String> result = mExceptionHeaderGenerator.generateHeader(
                MOCK_EXCEPTION_CODE,
                MOCK_EXCEPTION_FAULTING_OFFSET,
                MOCK_CRASHING_MODULE_NAME,
                MOCK_CRASHING_MODULE_VERSION,
                MOCK_CRASHING_PROCESS_NAME,
                MOCK_CRASHING_PROCESS_VERSION,
                MOCK_BUCKET_ID
        );

        // assert
        assertThat(result.size(), is(EXCEPTION_HEADER_SIZE));
        //@formatter:off
        assertThat(result.get(HEADER_KEY_EXCEPTION_CODE),            is(MOCK_EXCEPTION_CODE));
        assertThat(result.get(HEADER_KEY_EXCEPTION_FAULTING_OFFSET), is(MOCK_EXCEPTION_FAULTING_OFFSET));
        assertThat(result.get(HEADER_KEY_CRASHING_MODULE_NAME),      is(MOCK_CRASHING_MODULE_NAME));
        assertThat(result.get(HEADER_KEY_CRASHING_MODULE_VERSION),   is(MOCK_CRASHING_MODULE_VERSION));
        assertThat(result.get(HEADER_KEY_CRASHING_PROCESS_NAME),     is(MOCK_CRASHING_PROCESS_NAME));
        assertThat(result.get(HEADER_KEY_CRASHING_PROCESS_VERSION),  is(MOCK_CRASHING_PROCESS_VERSION));
        assertThat(result.get(HEADER_KEY_BUCKET_ID),                 is(MOCK_BUCKET_ID));
//        assertThat(result.get(HEADER_KEY_OS_VERSION),                is(MOCK_OS_VERSION));
        //@formatter:on
    }

//    private void initializeBuild() throws Exception {
//        Field buildVersionRelease = Build.VERSION.class.getField("RELEASE");
//        buildVersionRelease.setAccessible(true);
//        Field modifiersField = Field.class.getDeclaredField("modifiers");
//        modifiersField.setAccessible(true);
//        modifiersField.setInt(buildVersionRelease, buildVersionRelease.getModifiers() & ~Modifier.FINAL);
//        buildVersionRelease.set(null, MOCK_OS_VERSION);
//    }

}
