// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.builder.xml.dom;

import com.yahoo.config.application.Xml;
import com.yahoo.text.XML;
import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.ConfigPayloadBuilder;
import com.yahoo.vespa.config.GenericConfig;
import com.yahoo.vespa.model.HostResource;
import com.yahoo.vespa.model.HostSystem;
import com.yahoo.vespa.model.VespaModel;
import com.yahoo.vespa.model.test.utils.VespaModelCreatorWithMockPkg;
import org.junit.Test;
import org.w3c.dom.Element;

import java.io.StringReader;

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

/**
 * @author gjoranv
 */
public class VespaDomBuilderTest {

    private final static String hosts = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<hosts>" +
            "  <host name=\"localhost\">" +
            "    <alias>node1</alias>" +
            "    <alias>node2</alias>" +
            "  </host>" +
            "</hosts>";

    private final static String services = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<services>" +
            "  <config name=\"a.standard\">" +
            "    <basicStruct>" +
            "      <stringVal>default</stringVal>" +
            "    </basicStruct>" +
            "  </config> " +
            "  <config name=\"container.core.container-http\"><port><search>6745</search></port></config>" +
            "  <admin version=\"2.0\">" +
            "    <adminserver hostalias=\"node1\" />" +
            "  </admin>" +
            "  <container version=\"1.0\">" +
            "      <config name=\"a.standard\">" +
            "        <basicStruct>" +
            "          <stringVal>qrservers</stringVal>" +
            "        </basicStruct>" +
            "      </config> " +
            "    <nodes>\n" +
            "      <node hostalias=\"node1\"/>\n" +
            "    </nodes>\n" +
            "  </container>\n" +
            "</services>";

    private final static String servicesWithNamespace = "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<services>" +
            "  <config name=\"foo.testnamespace\">" +
            "    <basicStruct>" +
            "      <stringVal>default</stringVal>" +
            "    </basicStruct>" +
            "  </config> " +
            "  <admin version=\"2.0\">" +
            "    <adminserver hostalias=\"node1\" />" +
            "  </admin>" +
            "</services>";


    @Test
    public void testUserConfigsWithNamespace() {
        VespaModel model = createModel(hosts, servicesWithNamespace);

        GenericConfig.GenericConfigBuilder builder = 
                new GenericConfig.GenericConfigBuilder(new ConfigDefinitionKey("testnamespace", "foo"), new ConfigPayloadBuilder());
        model.getConfig(builder, "admin");
        assertEquals(builder.getPayload().toString(), "{\n" + 
        		" \"basicStruct\": {\n" + 
        		"  \"stringVal\": \"default\"\n" + 
        		" }\n" + 
        		"}\n");
    }

    @Test
    public void testGetElement() {
        Element e = Xml.getElement(new StringReader("<searchchain><foo>sdf</foo></searchchain>"));
        assertEquals(e.getTagName(), "searchchain");
        assertEquals(XML.getChild(e, "foo").getTagName(), "foo");
        assertEquals(XML.getValue(XML.getChild(e, "foo")), "sdf");
    }

    @Test
    public void testHostSystem() {
        VespaModel model = createModel(hosts, services);
        HostSystem hostSystem = model.hostSystem();
        assertEquals(1, hostSystem.getHosts().size());
        HostResource host = hostSystem.getHosts().get(0);
        assertEquals(hostSystem.getHostByHostname(host.getHostname()), host);
        assertNotNull(hostSystem.getHost("node1"));
        assertEquals("hosts [" + host.getHostname() + "]", hostSystem.toString());
    }

    private VespaModel createModel(String hosts, String services) {
       VespaModelCreatorWithMockPkg creator = new VespaModelCreatorWithMockPkg(hosts, services);
       return creator.create();
    }

}

@@ -2948,61 +2948,39 @@
 als(
-builder.getPayload().toString(), %22%7B%5Cn%22 + %0A        %09%09%22
+%22%7B%5Cn%22 +%0A                     %22 
  %5C%22b
@@ -2995,34 +2995,39 @@
 ct%5C%22: %7B%5Cn%22 +
- 
 %0A
+      
         
 %09%09%22  %5C%22strin
@@ -3018,13 +3018,20 @@
     
-%09%09%22
+       %22 
   
+ 
 %5C%22st
@@ -3053,25 +3053,24 @@
 fault%5C%22%5Cn%22 +
- 
 %0A        %09%09%22
@@ -3070,19 +3070,30 @@
     
-%09%09%22
+             %22 
  %7D%5Cn%22 +
- 
 %0A   
@@ -3101,15 +3101,59 @@
     
-%09%09
+             
 %22%7D%5Cn%22
+, builder.getPayload().toString()
 );%0A 

Update another test