package io.dshell.core.artifacts;

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

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.Future;

import static org.junit.Assert.assertEquals;

public class LocalArtifactStoreTest {
    @Rule
    public final TemporaryFolder temporaryFolder = new TemporaryFolder();


    @Test
    public void move_artifacts_to_target_directory() throws Exception {
        final Path storePath = temporaryFolder.newFolder("artifacts_store").toPath();
        final LocalArtifactStore store = new LocalArtifactStore(storePath);
        final Future<URI> artifact = store.save(temporaryFolder.newFile("artifact").toPath());
        assertEquals(storePath, Paths.get(artifact.get().getPath()).getParent().getParent());
    }

    @Test(expected = IllegalArgumentException.class)
    public void throws_exception_if_store_path_is_not_a_directory() throws IOException {
        new LocalArtifactStore(temporaryFolder.newFile().toPath());
    }

    @Test(expected = IllegalArgumentException.class)
    public void throws_exception_if_store_path_is_not_writable() throws IOException {
        final File file = temporaryFolder.newFolder();
        file.setWritable(false);
        new LocalArtifactStore(file.toPath().resolve("artifacts_store"));
    }

    @Test(expected = IllegalArgumentException.class)
    public void throws_exception_if_given_artifact_is_not_a_file() throws Exception {
        final Path storePath = temporaryFolder.newFolder().toPath();
        final LocalArtifactStore store = new LocalArtifactStore(storePath);
        store.save(storePath);
    }
}