package be.iminds.ilabt.jfed.rspec.model;

import org.testng.annotations.Test;

import java.util.List;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;

/**
 * BasicStringRspecTest
 */
public class BasicStringRspecTest {
    private static final String wall2Manifest =
            "<rspec xmlns=\"http://www.geni.net/resources/rspec/3\" xmlns:client=\"http://www.protogeni.net/resources/rspec/ext/client/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" type=\"manifest\" xsi:schemaLocation=\"http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/manifest.xsd \" expires=\"2014-09-01T06:15:12Z\">\n  " +
                    "   <node client_id=\"PC\" component_manager_id=\"urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm\" exclusive=\"true\" component_id=\"urn:publicid:IDN+wall2.ilabt.iminds.be+node+n091-25\" sliver_id=\"urn:publicid:IDN+wall2.ilabt.iminds.be+sliver+28109\">\n" +
                    "      <sliver_type name=\"raw-pc\"/>\n" +
                    "      <rs:vnode xmlns:rs=\"http://www.protogeni.net/resources/rspec/ext/emulab/1\" name=\"n091-25\"/><host name=\"PC.sTKe1409551752.ch-geni-net.wall2.ilabt.iminds.be\"/>" +
                    "      <services><login authentication=\"ssh-keys\" hostname=\"n091-25.wall2.ilabt.iminds.be\" port=\"22\" username=\"wimvande\"/></services>" +
                    "   </node>\n" +
                    "</rspec>";
    private static final String wall2ManifestWithProxy =
            "<rspec xmlns=\"http://www.geni.net/resources/rspec/3\" xmlns:client=\"http://www.protogeni.net/resources/rspec/ext/client/1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" type=\"manifest\" " +
                    "        xmlns:sshproxy=\"http://jfed.iminds.be/proxy/1.0\" " +
                    "        xsi:schemaLocation=\"http://www.geni.net/resources/rspec/3 http://www.geni.net/resources/rspec/3/manifest.xsd \" expires=\"2014-09-01T06:15:12Z\">\n  " +
                    "   <node client_id=\"PC\" component_manager_id=\"urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm\" exclusive=\"true\" component_id=\"urn:publicid:IDN+wall2.ilabt.iminds.be+node+n091-25\" sliver_id=\"urn:publicid:IDN+wall2.ilabt.iminds.be+sliver+28109\">\n" +
                    "      <sliver_type name=\"raw-pc\"/>\n" +
                    "      <rs:vnode xmlns:rs=\"http://www.protogeni.net/resources/rspec/ext/emulab/1\" name=\"n091-25\"/><host name=\"PC.sTKe1409551752.ch-geni-net.wall2.ilabt.iminds.be\"/>" +
                    "      <services>\n" +
                    "         <sshproxy:proxy proxy=\"wvdemeer@bastion.test.iminds.be:22\" for=\"wimvande@n091-25.wall2.ilabt.iminds.be:22\"/>\n"+
                    "         <login authentication=\"ssh-keys\" hostname=\"n091-25.wall2.ilabt.iminds.be\" port=\"22\" username=\"wimvande\"/>\n" +
                    "      </services>" +
                    "   </node>\n" +
                    "</rspec>";

    public static final String helloGeniRspec = "<!--\n" +
            "Copyright (c) 2010-2015 Raytheon BBN Technologies\n" +
            "\n" +
            "Permission is hereby granted, free of charge, to any person obtaining\n" +
            "a copy of this software and/or hardware specification (the \"Work\") to\n" +
            "deal in the Work without restriction, including without limitation the\n" +
            "rights to use, copy, modify, merge, publish, distribute, sublicense,\n" +
            "and/or sell copies of the Work, and to permit persons to whom the Work\n" +
            "is furnished to do so, subject to the following conditions:\n" +
            "\n" +
            "The above copyright notice and this permission notice shall be\n" +
            "included in all copies or substantial portions of the Work.\n" +
            "\n" +
            "THE WORK IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS\n" +
            "OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\n" +
            "MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\n" +
            "NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT\n" +
            "HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n" +
            "WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n" +
            "OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS\n" +
            "IN THE WORK.\n" +
            "-->\n" +
            "\n" +
            "<rspec xmlns=\"http://www.geni.net/resources/rspec/3\"  \n" +
            "       xmlns:emulab=\"http://www.protogeni.net/resources/rspec/ext/emulab/1\" \n" +
            "       xmlns:tour=\"http://www.protogeni.net/resources/rspec/ext/apt-tour/1\" \n" +
            "       xmlns:jacks=\"http://www.protogeni.net/resources/rspec/ext/jacks/1\"\n" +
            "       xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
            "       xsi:schemaLocation=\"http://www.geni.net/resources/rspec/3    http://www.geni.net/resources/rspec/3/request.xsd\" \n" +
            "       type=\"request\">\n" +
            "\n" +
            " <node xmlns:rs=\"http://www.protogeni.net/resources/rspec/ext/emulab/1\" client_id=\"server\" exclusive=\"false\">\n" +
            "    <sliver_type name=\"default-vm\"/>\n" +
            "    <emulab:routable_control_ip xmlns:emulab=\"http://www.protogeni.net/resources/rspec/ext/emulab/1\"/>\n" +
            "    <services>\n" +
            "      <install install_path=\"/local\" url=\"http://www.gpolab.bbn.com/experiment-support/HelloGENI//hellogeni-install.tar.gz\"/>\n" +
            "      <execute command=\"sudo /local/install-script.sh\" shell=\"sh\"/>\n" +
            "    </services>\n" +
            "    <interface client_id=\"server:if0\">\n" +
            "       <ip xmlns=\"http://www.geni.net/resources/rspec/3\" address=\"10.10.10.1\" type=\"ipv4\" netmask=\"255.255.255.0\"/>\n" +
            "     </interface>\n" +
            "    <site xmlns=\"http://www.protogeni.net/resources/rspec/ext/jacks/1\" id=\"28\"/>\n" +
            "  </node>\n" +
            "  \n" +
            "  <node xmlns:rs=\"http://www.protogeni.net/resources/rspec/ext/emulab/1\" client_id=\"client\" exclusive=\"false\">\n" +
            "    <sliver_type name=\"default-vm\"/>\n" +
            "    <services>  \n" +
            "      <install install_path=\"/local\" url=\"http://www.gpolab.bbn.com/experiment-support/HelloGENI//hellogeni-install.tar.gz\"/>\n" +
            "      <execute command=\"sudo /local/install-script.sh\" shell=\"sh\"/>\n" +
            "    </services>\n" +
            "    <interface client_id=\"client:if0\">\n" +
            "      <ip xmlns=\"http://www.geni.net/resources/rspec/3\" address=\"10.10.10.2\" type=\"ipv4\" netmask=\"255.255.255.0\"/>\n" +
            "    </interface>\n" +
            "    <site xmlns=\"http://www.protogeni.net/resources/rspec/ext/jacks/1\" id=\"28\"/>\n" +
            "  </node>\n" +
            "\n" +
            "  <link client_id=\"lan0\">\n" +
            "    <interface_ref client_id=\"server:if0\"/>\n" +
            "    <interface_ref client_id=\"client:if0\"/>\n" +
            "    <property source_id=\"server:if0\" dest_id=\"client:if0\"/>\n" +
            "    <property source_id=\"client:if0\" dest_id=\"server:if0\"/>\n" +
            "    <link_type name=\"lan\"/>\n" +
            "  </link>\n" +
            "\n" +
            "</rspec>\n";

    @Test
    public void testWall2Manifest() {
        BasicStringRspec basicStringRspec = new BasicStringRspec(wall2Manifest);

        List<BasicStringRspec.BasicNodeInfo> basicNodeInfos = basicStringRspec.getBasicNodeInfo();
        assertEquals(basicNodeInfos.size(), 1);
        BasicStringRspec.BasicNodeInfo node0 = basicNodeInfos.get(0);
        assertNotNull(node0.getClientId());
        assertNotNull(node0.getComponentId());
        assertEquals(node0.getClientId(), "PC");
        assertEquals(node0.getComponentId(), "urn:publicid:IDN+wall2.ilabt.iminds.be+node+n091-25");
        assertEquals(node0.getComponentManagerId(), "urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm");
        assertEquals(node0.getUniqueId(), "PC@urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm");

        List<BasicStringRspec.LoginService> loginServiceList = basicStringRspec.findNodeLoginInfoByUniqueId(
                node0.getUniqueId());
        assertEquals(loginServiceList.size(), 1);
        BasicStringRspec.LoginService login = loginServiceList.get(0);
        assertEquals(login.getAuthentication(), "ssh-keys");
        assertEquals(login.getHostname(), "n091-25.wall2.ilabt.iminds.be");
        assertEquals(login.getPort(), 22);
        assertEquals(login.getUsername(), "wimvande");
        assertNull(login.getSshProxy());
    }
    @Test
    public void testWall2ManifestWithProxy() {
        BasicStringRspec basicStringRspec = new BasicStringRspec(wall2ManifestWithProxy);

        List<BasicStringRspec.BasicNodeInfo> basicNodeInfos = basicStringRspec.getBasicNodeInfo();
        assertEquals(basicNodeInfos.size(), 1);
        BasicStringRspec.BasicNodeInfo node0 = basicNodeInfos.get(0);
        assertNotNull(node0.getClientId());
        assertNotNull(node0.getComponentId());
        assertEquals(node0.getClientId(), "PC");
        assertEquals(node0.getComponentId(), "urn:publicid:IDN+wall2.ilabt.iminds.be+node+n091-25");
        assertEquals(node0.getComponentManagerId(), "urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm");
        assertEquals(node0.getUniqueId(), "PC@urn:publicid:IDN+wall2.ilabt.iminds.be+authority+cm");

        List<BasicStringRspec.LoginService> loginServiceList = basicStringRspec.findNodeLoginInfoByUniqueId(
                node0.getUniqueId());
        assertEquals(loginServiceList.size(), 1);
        BasicStringRspec.LoginService login = loginServiceList.get(0);
        assertEquals(login.getAuthentication(), "ssh-keys");
        assertEquals(login.getHostname(), "n091-25.wall2.ilabt.iminds.be");
        assertEquals(login.getPort(), 22);
        assertEquals(login.getUsername(), "wimvande");
        assertNotNull(login.getSshProxy());
        assertEquals(login.getSshProxy().getUsername(), "wvdemeer");
        assertEquals(login.getSshProxy().getHostname(), "bastion.test.iminds.be");
        assertEquals(login.getSshProxy().getPort(), 22);
        assertNull(login.getSshProxy().getHostKey());
        assertNull(login.getSshProxy().getSshKeyInfo());
    }
    @Test
    public void testHelloGeni() {
        BasicStringRspec basicStringRspec = new BasicStringRspec(helloGeniRspec);

        List<BasicStringRspec.BasicNodeInfo> basicNodeInfos = basicStringRspec.getBasicNodeInfo();
        assertEquals(basicNodeInfos.size(), 2);
        BasicStringRspec.BasicNodeInfo node0 = basicNodeInfos.get(0);
        assertNotNull(node0.getClientId());
        assertNull(node0.getComponentId());
        assertNull(node0.getComponentManagerId());

        assertEquals(basicStringRspec.getAllComponentManagerUrns().size(), 0);
    }
}
