// Copyright Vespa.ai. 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.jupiter.api.Test;
import org.w3c.dom.Element;
import java.io.StringReader;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author gjoranv
*/
public class VespaDomBuilderTest {
private final static String hosts = "" +
"" +
" " +
" node1" +
" node2" +
" " +
"";
private final static String services = "" +
"" +
" " +
" " +
" default" +
" " +
" " +
" 6745" +
" " +
" " +
" " +
" " +
" " +
" " +
" foo" +
" " +
" " +
" \n" +
" \n" +
" \n" +
" \n" +
"";
private final static String servicesWithNamespace = "" +
"" +
" " +
" " +
" default" +
" " +
" " +
" " +
" " +
" " +
"";
@Test
void testUserConfigsWithNamespace() {
VespaModel model = createModel(hosts, servicesWithNamespace);
GenericConfig.GenericConfigBuilder builder =
new GenericConfig.GenericConfigBuilder(new ConfigDefinitionKey("testnamespace", "foo"), new ConfigPayloadBuilder());
model.getConfig(builder, "admin");
assertEquals("{\n" +
" \"basicStruct\": {\n" +
" \"stringVal\": \"default\"\n" +
" }\n" +
"}\n", builder.getPayload().toString());
}
@Test
void testGetElement() {
Element e = Xml.getElement(new StringReader("sdf"));
assertEquals(e.getTagName(), "chain");
assertEquals(XML.getChild(e, "foo").getTagName(), "foo");
assertEquals(XML.getValue(XML.getChild(e, "foo")), "sdf");
}
@Test
void testHostSystem() {
VespaModel model = createModel(hosts, services);
HostSystem hostSystem = model.hostSystem();
assertEquals(1, hostSystem.getHosts().size());
HostResource host = hostSystem.getHosts().get(0);
assertNotNull(hostSystem.getHost("node1"));
assertEquals("hosts [" + host.getHostname() + "]", hostSystem.toString());
}
@Test
void testMinimumRequiredVespaVersion() {
var exception = assertThrows(IllegalArgumentException.class,
() -> createModel(hosts, """
"""));
assertEquals("Cannot deploy application, minimum required Vespa version is specified as 1.0.1 in services.xml, this Vespa version is 1.0.0.",
exception.getMessage());
createModel(hosts, """
""");
}
private VespaModel createModel(String hosts, String services) {
VespaModelCreatorWithMockPkg creator = new VespaModelCreatorWithMockPkg(hosts, services);
return creator.create();
}
}