package org.opentosca.toscana.plugins.scripts;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.opentosca.toscana.core.BaseUnitTest;
import org.opentosca.toscana.core.plugin.PluginFileAccess;
import org.opentosca.toscana.core.transformation.logging.Log;

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

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.opentosca.toscana.plugins.lifecycle.AbstractLifecycle.SCRIPTS_DIR_PATH;
import static org.opentosca.toscana.plugins.lifecycle.AbstractLifecycle.UTIL_DIR_NAME;
import static org.opentosca.toscana.plugins.scripts.BashScript.SHEBANG;

public class BashScriptTest extends BaseUnitTest {

    private BashScript bashScript;
    private PluginFileAccess access;
    private File targetScriptFolder;
    private String fileName;

    @Before
    public void setUp() {
        bashScript = null;
        fileName = UUID.randomUUID().toString();
        access = new PluginFileAccess(tmpdir, tmpdir, mock(Log.class));
        try {
            bashScript = new BashScript(access, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        targetScriptFolder = new File(tmpdir, SCRIPTS_DIR_PATH);
    }

    @Test
    public void createScriptTest() throws IOException {

        File expectedGeneratedScript = new File(targetScriptFolder, fileName + ".sh");
        assertTrue(expectedGeneratedScript.exists());
        List<String> result = readFile(expectedGeneratedScript);
        assertEquals(SHEBANG, result.get(0));
        assertEquals("source " + UTIL_DIR_NAME + "*", result.get(1));
    }

    @Test
    public void appendTest() throws IOException {
        String string = UUID.randomUUID().toString();
        bashScript.append(string);
        File expectedGeneratedScript = new File(targetScriptFolder, fileName + ".sh");
        List<String> result = readFile(expectedGeneratedScript);
        assertEquals(string, result.get(result.size() - 1));
    }

    public List<String> readFile(File file) throws IOException {
        List<String> result = new ArrayList<>();
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String current;
        while ((current = reader.readLine()) != null) {
            result.add(current);
        }
        reader.close();
        return result;
    }
}

@@ -45,39 +45,8 @@
 s;%0A%0A
-import java.io.BufferedReader;%0A
 impo
@@ -81,22 +81,27 @@
 .io.File
-Reader
+InputStream
 ;%0Aimport
@@ -126,36 +126,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -339,16 +339,54 @@
 g.Log;%0A%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -1477,17 +1477,16 @@
 ption %7B%0A
-%0A
         
@@ -1644,32 +1644,61 @@
 g%3E result = 
-readFile
+IOUtils.readLines(new FileInputStream
 (expectedGen
@@ -1702,32 +1702,33 @@
 GeneratedScript)
+)
 ;%0A        assert
@@ -2102,24 +2102,53 @@
 esult = 
-readFile
+IOUtils.readLines(new FileInputStream
 (expecte
@@ -2164,16 +2164,17 @@
 dScript)
+)
 ;%0A      
@@ -2237,372 +2237,7 @@
    %7D
-%0A%0A    public List%3CString%3E readFile(File file) throws IOException %7B%0A        List%3CString%3E result = new ArrayList%3C%3E();%0A        BufferedReader reader = new BufferedReader(new FileReader(file));%0A        String current;%0A        while ((current = reader.readLine()) != null) %7B%0A            result.add(current);%0A        %7D%0A        reader.close();%0A        return result;%0A    %7D
 %0A%7D%0A

Remove unnecessary BashScriptTest::readFile (#318)