package com.avg.zaap.crashlibrary.offline.publisher.metadata;

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

import java.io.File;

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

/**
 * A test class for {@link CrashMetadataHelper}.
 * <p/>
 * Created by marek.osvald on 8.9.2016.
 */
public class CrashMetadataHelperTests {

    private static final String FILENAME_WITHOUT_EXTENSION = "file";
    private static final String FILENAME_WITH_EXTENSION = "file.ext";
    private static final String FILENAME_WITH_TWO_EXTENSIONS = "file.ext1.ext2";
    private static final String METADATA_FILENAME = "file.metadata";
    private static final String METADATA_FILENAME_WITH_TWO_EXTENSIONS = "file.ext1.metadata";

    private ICrashMetadataHelper mCrashMetadataHelper;

    @Before
    public void setUp() throws Exception {
        mCrashMetadataHelper = new CrashMetadataHelper();
    }

    @Test
    public void getMetadataFileName_fileNameWithExtension_replacesExtension() throws Exception {
        // arrange
        File file = mock(File.class);
        when(file.getName()).thenReturn(FILENAME_WITH_EXTENSION);

        // act
        final String result = mCrashMetadataHelper.getMetadataFileName(file);

        // assert
        assertThat(result, is(METADATA_FILENAME));
    }

    @Test
    public void getMetadataFileName_fileNameWithoutExtension_replacesExtension() throws Exception {
        // arrange
        File file = mock(File.class);
        when(file.getName()).thenReturn(FILENAME_WITHOUT_EXTENSION);

        // act
        final String result = mCrashMetadataHelper.getMetadataFileName(file);

        // assert
        assertThat(result, is(METADATA_FILENAME));
    }

    @Test
    public void getMetadataFileName_fileNameWithTwoExtensions_replacesExtension() throws Exception {
        // arrange
        File file = mock(File.class);
        when(file.getName()).thenReturn(FILENAME_WITH_TWO_EXTENSIONS);

        // act
        final String result = mCrashMetadataHelper.getMetadataFileName(file);

        // assert
        assertThat(result, is(METADATA_FILENAME_WITH_TWO_EXTENSIONS));
    }

}
