package com.atlassian.bitbucket.installer.validation;

import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;
import org.mockito.Mock;

import java.io.File;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class EmptyDirectoryValidationStrategyTest {

    @Rule
    public final ExpectedException expectedException = ExpectedException.none();
    @Rule
    public final TemporaryFolder testFolder = new TemporaryFolder();

    @Mock
    public final File testFolderThatWillFail = mock(File.class);

    private EmptyDirectoryValidationStrategy validationStrategy;

    @Before
    public void setup() {
        validationStrategy = directoryEmpty();
    }

    @Test
    public void testValidation() {
        validationStrategy.validate(testFolder.getRoot());
    }

    @Test
    public void testIOErrorGettingContents() throws Exception {
        expectedException.expect(HardValidationException.class);
        expectedException.expectMessage(HardValidationException.ERROR_DIRECTORY_ACCESS);

        when(testFolderThatWillFail.list()).thenReturn(null);
        when(testFolderThatWillFail.getAbsolutePath()).thenReturn("folder-that-will-fail");
        when(testFolderThatWillFail.exists()).thenReturn(true);
        validationStrategy.validate(testFolderThatWillFail);
    }

    @Test
    public void testDirectoryIsFile() throws Exception {
        expectedException.expect(HardValidationException.class);
        expectedException.expectMessage(HardValidationException.ERROR_DIRECTORY_FILE);
        File newFile = testFolder.newFile("a-file");

        validationStrategy.validate(newFile);
    }

    @Test
    public void testNonEmptyFolder() throws Exception {
        expectedException.expect(SoftValidationException.class);
        expectedException.expectMessage(SoftValidationException.ERROR_DIRECTORY_USED);
        testFolder.newFile("garbage");

        validationStrategy.validate(testFolder.getRoot());
    }

    @Test
    public void testDirectoryWithElementsInTheWhitelist() throws Exception {
        validationStrategy = directoryEmpty("shared");

        testFolder.newFolder("shared");

        validationStrategy.validate(testFolder.getRoot());
    }

    @Test
    public void testDirectoryWithFilesystemMetadata() throws Exception {
        testFolder.newFolder(".snapshot");
        testFolder.newFolder(".DS_Store");
        testFolder.newFolder("lost+found");

        validationStrategy.validate(testFolder.getRoot());
    }

    @Test
     public void testDirectoryWithFileElementsInTheWhitelist() throws Exception {
        expectedException.expect(HardValidationException.class);
        expectedException.expectMessage(HardValidationException.ERROR_INVALID_CONTENTS);
        validationStrategy = directoryEmpty("shared");

        testFolder.newFile("shared");

        validationStrategy.validate(testFolder.getRoot());
    }

    private static EmptyDirectoryValidationStrategy directoryEmpty(String... exclude) {
        return new EmptyDirectoryValidationStrategy(ImmutableSet.copyOf(exclude),
                file -> new SoftValidationException(SoftValidationException.ERROR_DIRECTORY_USED));
    }
}
