package com.atlassian.bitbucket.installer.validation;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.TemporaryFolder;

public class HomeDirectoryValidationStrategyTest {

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

    private HomeDirectoryValidationStrategy validationStrategy;

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

    @Test
    public void testValidation() throws Exception {
        for (HomeDirectoryStructure version : HomeDirectoryStructure.values()) {
            testFolder.delete();
            testFolder.create();

            createAll(version.getContents());

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

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

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

    @Test
    public void testUnknownHomeDirectoryStructure() throws Exception {
        expectedException.expect(HardValidationException.class);
        expectedException.expectMessage(HardValidationException.ERROR_DIRECTORY_STRUCTURE);
        testFolder.newFile("garbage");

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

    private void createAll(Iterable<String> paths) throws Exception {
        for (String path : paths) {
            testFolder.newFolder(path.split("/"));
        }
    }
}
