package io.specto.hoverfly.junit.core;


import com.google.common.io.Resources;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static io.specto.hoverfly.junit.core.SystemConfigFactory.OsName.OSX;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TempFileManagerTest {

    private TempFileManager tempFileManager;
    private String systemTempDir = System.getProperty("java.io.tmpdir");

    @Before
    public void setUp() {
        tempFileManager = new TempFileManager();
    }

    @Test
    public void shouldLazilyInitializedTempDirectory() {
        assertThat(tempFileManager.getTempDirectory()).isNull();

        tempFileManager.copyHoverflyBinary(new SystemConfigFactory().createSystemConfig());

        Path tempDir = tempFileManager.getTempDirectory();
        assertThat(Files.isDirectory(tempDir)).isTrue();
        assertThat(Files.isWritable(tempDir)).isTrue();
        assertThat(tempDir.getParent()).isEqualTo(Paths.get(systemTempDir));
    }


    @Test
    public void shouldPurgeAllCreatedTempFiles() {
        Path tempResourcePath = tempFileManager.copyClassPathResource("ssl/ca.crt", "ca.crt");

        tempFileManager.purge();

        assertThat(Files.exists(tempResourcePath)).isFalse();
        assertThat(Files.exists(tempResourcePath.getParent())).isFalse();
    }

    @Test
    public void shouldCopyClassPathResourceToCurrentTempDirectory() throws Exception {

        URL sourceFileUrl = Resources.getResource("ssl/ca.crt");
        Path sourceFile = Paths.get(sourceFileUrl.toURI());
        Path targetFile = tempFileManager.copyClassPathResource("ssl/ca.crt", "ca.crt");

        assertThat(Files.exists(targetFile)).isTrue();
        assertThat(Files.isRegularFile(targetFile)).isTrue();
        assertThat(Files.isReadable(targetFile)).isTrue();
        assertThat(targetFile.getParent()).isEqualTo(tempFileManager.getTempDirectory());
        assertThat(FileUtils.contentEquals(sourceFile.toFile(), targetFile.toFile())).isTrue();
    }

    @Test
    public void shouldCopyHoverflyBinary() throws Exception {

        // Given
        URL sourceFileUrl = Resources.getResource("binaries/hoverfly_OSX_amd64");
        Path sourceFile = Paths.get(sourceFileUrl.toURI());
        SystemConfig systemConfig = mock(SystemConfig.class);
        when(systemConfig.getHoverflyBinaryName()).thenReturn("hoverfly_OSX_amd64");
        when(systemConfig.getOsName()).thenReturn(OSX);

        // When
        Path targetFile = tempFileManager.copyHoverflyBinary(systemConfig);

        // Then
        assertThat(Files.exists(targetFile)).isTrue();
        assertThat(Files.isRegularFile(targetFile)).isTrue();
        assertThat(Files.isReadable(targetFile)).isTrue();
        assertThat(Files.isExecutable(targetFile)).isTrue();
        assertThat(targetFile.getParent()).isEqualTo(tempFileManager.getTempDirectory());
        assertThat(FileUtils.contentEquals(sourceFile.toFile(), targetFile.toFile())).isTrue();

    }

    @After
    public void tearDown() {
        tempFileManager.purge();

    }
}
@@ -309,206 +309,50 @@
 tic 
-io.specto.hoverfly.junit.core.SystemConfigFactory.OsName.OSX;%0Aimport static org.assertj.core.api.Assertions.assertThat;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when
+org.assertj.core.api.Assertions.assertThat
 ;%0A%0Ap
@@ -2200,79 +2200,81 @@
     
-URL sourceFileUrl = Resources.getResource(%22binaries/hoverfly_OSX_amd64%22
+SystemConfig systemConfig = new SystemConfigFactory().createSystemConfig(
 );%0A 
@@ -2272,36 +2272,35 @@
 nfig();%0A        
-Path
+URL
  sourceFile = Pa
@@ -2298,245 +2298,146 @@
 File
- = Paths.get(sourceFileUrl.toURI());%0A        SystemConfig systemConfig = mock(SystemConfig.class);%0A        when(systemConfig.getHoverflyBinaryName()).thenReturn(%22hoverfly_OSX_amd64%22);%0A        when(systemConfig.getOsName()).thenReturn(OSX
+Url = Resources.getResource(%22binaries/%22 + systemConfig.getHoverflyBinaryName());%0A        Path sourceFile = Paths.get(sourceFileUrl.toURI()
 );%0A%0A
@@ -2964,25 +2964,24 @@
 ).isTrue();%0A
-%0A
     %7D%0A%0A    @
@@ -3044,20 +3044,19 @@
 er.purge();%0A
-%0A
     %7D%0A%7D

Fix copyHoverflyBinary test which is hardwired to OSX