package nl.esciencecenter.xenon.adaptors.ssh;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import nl.esciencecenter.xenon.InvalidLocationException;

import org.junit.Test;

import com.jcraft.jsch.ConfigRepository;

public class SSHLocationTest {

    public static final int DEFAULT_PORT = 22;

    @Test
    public void test_parse_hostOnly() throws Exception {
        SshLocation tmp = SshLocation.parse("host", ConfigRepository.nullConfig);
        assertNull(tmp.getUser());
        assertEquals(tmp.getHost(), "host");
        assertTrue(tmp.getPort() == DEFAULT_PORT);
    }

    @Test
    public void test_parse_userHost() throws Exception {
        SshLocation tmp = SshLocation.parse("user@host", ConfigRepository.nullConfig);
        assertEquals(tmp.getUser(), "user");
        assertEquals(tmp.getHost(), "host");
        assertTrue(tmp.getPort() == DEFAULT_PORT);
    }

    @Test
    public void test_parse_hostPort() throws Exception {
        SshLocation tmp = SshLocation.parse("host:33", ConfigRepository.nullConfig);
        assertNull(tmp.getUser());
        assertEquals(tmp.getHost(), "host");
        assertTrue(tmp.getPort() == 33);
    }

    @Test
    public void test_parse_userHostPort() throws Exception {
        SshLocation tmp = SshLocation.parse("user@host:33", ConfigRepository.nullConfig);
        assertEquals(tmp.getUser(), "user");
        assertEquals(tmp.getHost(), "host");
        assertTrue(tmp.getPort() == 33);
    }

    @Test
    public void test_parse_withScheme_correctScheme() throws InvalidLocationException {
        SshLocation tmp = SshLocation.parse("ssh://host", ConfigRepository.nullConfig);
        assertEquals("ssh", tmp.getScheme());
    }

    @Test
    public void test_parse_withScheme_correctHost() throws InvalidLocationException {
        SshLocation tmp = SshLocation.parse("ssh://host", ConfigRepository.nullConfig);
        assertEquals("host", tmp.getHost());
    }

    @Test
    public void test_parse_withScheme_correctPort() throws InvalidLocationException {
        SshLocation tmp = SshLocation.parse("ssh://host:777", ConfigRepository.nullConfig);
        assertEquals(777, tmp.getPort());
    }

    @Test
    public void test_parseToString_withOutScheme() throws InvalidLocationException {
        String url = "user@host:777";
        SshLocation tmp = SshLocation.parse(url, ConfigRepository.nullConfig);
        assertEquals("ssh://" + url, tmp.toString());
    }

    @Test
    public void test_parseToString_withScheme() throws InvalidLocationException {
        String url = "ssh://user@host:777";
        SshLocation tmp = SshLocation.parse(url, ConfigRepository.nullConfig);
        assertEquals(url, tmp.toString());
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_negativePort() throws Exception {
        SshLocation.parse("user@host:-42", ConfigRepository.nullConfig);
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_zeroPort() throws Exception {
        SshLocation.parse("user@host:0", ConfigRepository.nullConfig);
    }

    @Test
    public void test_parse_missingUser() throws Exception {
        SshLocation tmp = SshLocation.parse("@host:33", ConfigRepository.nullConfig);
        assertNull(tmp.getUser());
    }

    public void test_parse_missingPort() throws Exception {
        SshLocation tmp = SshLocation.parse("host:", ConfigRepository.nullConfig);
        assertTrue(tmp.usesDefaultPort());
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_missingPort2() throws Exception {
        SshLocation.parse("host:  ", ConfigRepository.nullConfig);
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_invalidPort() throws Exception {
        SshLocation.parse("host:aap", ConfigRepository.nullConfig);
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_missingHost1() throws Exception {
        SshLocation.parse(":33", ConfigRepository.nullConfig);
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_missingHost2() throws Exception {
        SshLocation.parse("user@", ConfigRepository.nullConfig);
    }

    @Test(expected = InvalidLocationException.class)
    public void test_parse_missingHost3() throws Exception {
        SshLocation.parse("user@:33", ConfigRepository.nullConfig);
    }

}
@@ -654,32 +654,532 @@
 T_PORT);%0A    %7D%0A%0A
+    /** Test if empty values are updated with SSH config settings. */%0A    @Test%0A    public void test_parse_hostOnlyWithConfig() throws Exception %7B%0A        String confString = %22Host host%5CnHostName host.fqdn%5CnUser myuser%5CnPort 42%22;%0A        OpenSSHConfig config = OpenSSHConfig.parse(confString);%0A        SshLocation tmp = SshLocation.parse(%22host%22, config);%0A        assertEquals(%22myuser%22, tmp.getUser());%0A        assertEquals(%22host.fqdn%22, tmp.getHost());%0A        assertEquals(42, tmp.getPort());%0A    %7D%0A%0A
     @Test%0A    pu
@@ -2035,32 +2035,545 @@
  == 33);%0A    %7D%0A%0A
+    /** Test if filled values are not overwritten by SSH config settings. */%0A    @Test%0A    public void test_parse_userHostPortConfig() throws Exception %7B%0A        String confString = %22Host host%5CnHostName host.fqdn%5CnUser myuser%5CnPort 42%22;%0A        OpenSSHConfig config = OpenSSHConfig.parse(confString);%0A        SshLocation tmp = SshLocation.parse(%22user@host:33%22, config);%0A        assertEquals(%22user%22, tmp.getUser());%0A        assertEquals(%22host.fqdn%22, tmp.getHost());%0A        assertEquals(33, tmp.getPort());%0A    %7D%0A%0A
     @Test%0A    pu

Test whether OpenSSHConfig is correctly read