/*
 * This source file was generated by the Gradle 'init' task
 */
package utb.fai;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

import java.util.concurrent.TimeUnit;

import org.junit.Test;

import utb.fai.Core.MessageBuffer;
import utb.fai.Core.NATTContext;
import utb.fai.Core.MessageBuffer.SearchType;
import utb.fai.Core.ExternalProgramRunner;
import utb.fai.Exception.InternalErrorException;
import utb.fai.Exception.NonUniqueModuleNamesException;
import utb.fai.Exception.TestedAppFailedToRunException;

public class ExternalProgramRunnerTest {

    @Test
    public void externalProgramRunnerTest()
            throws TestedAppFailedToRunException, InterruptedException, NonUniqueModuleNamesException,
            InternalErrorException {
        ExternalProgramRunner runner = new ExternalProgramRunner();
        runner.runExternalProgram("java -jar ../echo_test.jar");

        TimeUnit.SECONDS.sleep(1);

        boolean status = runner.sendMessageToExternalProgram("Zprava odeslana z testovaci aplikace", true);

        assertEquals(true, status);

        TimeUnit.SECONDS.sleep(1);

        MessageBuffer msgBuff = NATTContext.instance().getMessageBuffer();

        assertEquals(3, msgBuff.getMessages("app-std-out").size());

        assertEquals("Přijatý text: Zprava odeslana z testovaci aplikace",
                msgBuff.searchMessages("app-std-out", "", "", SearchType.NONE, false).getLast().getMessage());

        runner.stopExternalProgram();
        TimeUnit.SECONDS.sleep(1);
    }

    @Test
    public void testCommandStringToCommandList() {
        String commandString = "java -jar app.jar localhost 9999 sender@email.com recipient@email.com Subject1 'Text Message'";
        String[] expected = { "java", "-jar", "app.jar", "localhost", "9999", "sender@email.com", "recipient@email.com",
                "Subject1", "Text Message" };
        assertArrayEquals(expected, ExternalProgramRunner.commandStringToCommandList(commandString));
    }

    @Test
    public void testCommandStringToCommandListWithSpaces() {
        String commandString = "java -jar app.jar localhost 9999 'sender@email.com' 'recipient@email.com' 'Subject1' 'Text Message with spaces'";
        String[] expected = { "java", "-jar", "app.jar", "localhost", "9999", "sender@email.com", "recipient@email.com",
                "Subject1", "Text Message with spaces" };
        assertArrayEquals(expected, ExternalProgramRunner.commandStringToCommandList(commandString));
    }

    @Test
    public void testCommandStringToCommandListWithNoQuotes() {
        String commandString = "java -jar app.jar localhost 9999 sender@email.com recipient@email.com Subject1 TextMessage";
        String[] expected = { "java", "-jar", "app.jar", "localhost", "9999", "sender@email.com", "recipient@email.com",
                "Subject1", "TextMessage" };
        assertArrayEquals(expected, ExternalProgramRunner.commandStringToCommandList(commandString));
    }

    @Test
    public void testCommandStringToCommandListWithURL() {
        String commandString = "java -jar app/build/libs/app-all.jar https://docs.oracle.com/en/ 0 0";
        String[] expected = { "java", "-jar", "app/build/libs/app-all.jar", "https://docs.oracle.com/en/", "0", "0" };
        assertArrayEquals(expected, ExternalProgramRunner.commandStringToCommandList(commandString));
    }

    @Test
    public void testCommandStringToCommandListWithEmptyString() {
        String commandString = "";
        String[] expected = {};
        assertArrayEquals(expected, ExternalProgramRunner.commandStringToCommandList(commandString));
    }

}
