diff options
author | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-12 08:56:17 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahooinc.com> | 2022-01-12 08:56:17 +0100 |
commit | 24a3202a133636af279931491096f4cdce339ffc (patch) | |
tree | f5328d9b37913128367fe11a66f44bf0851c1218 /node-admin | |
parent | c68d279f66031cb1a599589a8a346200f3514178 (diff) |
Merge TemplateFile into Template
Diffstat (limited to 'node-admin')
4 files changed, 70 insertions, 95 deletions
diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java index 77d50a392c8..da3d4eef03f 100644 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java +++ b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/Template.java @@ -1,8 +1,10 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.hosted.node.admin.task.util.template; +import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixPath; import com.yahoo.vespa.hosted.node.admin.task.util.text.CursorRange; +import java.nio.file.Path; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,7 +31,6 @@ import java.util.Optional; * * <p>To reuse a template, create the template and work on snapshots of that ({@link #snapshot()}).</p> * - * @see TemplateFile * @author hakonhall */ public class Template { @@ -40,8 +41,13 @@ public class Template { private final Map<String, String> values = new HashMap<>(); private final Map<String, ListSection> lists; - public static Template from(String text) { return from(text, new TemplateDescriptor()); } + public static Template at(Path path) { return at(path, new TemplateDescriptor()); } + public static Template at(Path path, TemplateDescriptor descriptor) { + String content = new UnixPath(path).readUtf8File(); + return Template.from(content, descriptor); + } + public static Template from(String text) { return from(text, new TemplateDescriptor()); } public static Template from(String text, TemplateDescriptor descriptor) { return TemplateParser.parse(text, descriptor).template(); } diff --git a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFile.java b/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFile.java deleted file mode 100644 index 0c1a26f4f65..00000000000 --- a/node-admin/src/main/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFile.java +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.node.admin.task.util.template; - -import com.yahoo.vespa.hosted.node.admin.task.util.file.UnixPath; - -import java.nio.file.Path; - -/** - * Parses a template file, see {@link Template} for details. - * - * @author hakonhall - */ -public class TemplateFile { - public static Template read(Path path) { return read(path, new TemplateDescriptor()); } - - public static Template read(Path path, TemplateDescriptor descriptor) { - String content = new UnixPath(path).readUtf8File(); - return Template.from(content, descriptor); - } -} diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java deleted file mode 100644 index 8c276ff0491..00000000000 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateFileTest.java +++ /dev/null @@ -1,73 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.vespa.hosted.node.admin.task.util.template; - -import org.junit.jupiter.api.Test; - -import java.nio.file.Path; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -/** - * @author hakonhall - */ -class TemplateFileTest { - @Test - void verifyVariableSection() { - Template template = getTemplate("template1.tmp"); - template.set("varname", "varvalue"); - assertEquals("variable section 'varvalue'\n" + - "end of text\n", template.render()); - } - - @Test - void verifySimpleListSection() { - Template template = getTemplate("template1.tmp"); - template.set("varname", "varvalue") - .add("listname") - .set("varname", "different varvalue") - .set("varname2", "varvalue2"); - assertEquals("variable section 'varvalue'\n" + - "same variable section 'different varvalue'\n" + - "different variable section 'varvalue2'\n" + - "between ends\n" + - "end of text\n", template.render()); - } - - @Test - void verifyNestedListSection() { - Template template = getTemplate("template2.tmp"); - Template A0 = template.add("listA"); - Template A0B0 = A0.add("listB"); - Template A0B1 = A0.add("listB"); - - Template A1 = template.add("listA"); - Template A1B0 = A1.add("listB"); - assertEquals("body A\n" + - "body B\n" + - "body B\n" + - "body A\n" + - "body B\n", - template.render()); - } - - @Test - void verifyVariableReferences() { - Template template = getTemplate("template3.tmp"); - template.set("varname", "varvalue") - .set("innerVarSetAtTop", "val2"); - template.add("l"); - template.add("l") - .set("varname", "varvalue2"); - assertEquals("varvalue\n" + - "varvalue\n" + - "inner varvalue\n" + - "val2\n" + - "inner varvalue2\n" + - "val2\n", - template.render()); - } - - private Template getTemplate(String filename) { - return TemplateFile.read(Path.of("src/test/resources/" + filename)); - } -}
\ No newline at end of file diff --git a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java index fb5f8e74b73..bd54c4a6a49 100644 --- a/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java +++ b/node-admin/src/test/java/com/yahoo/vespa/hosted/node/admin/task/util/template/TemplateTest.java @@ -3,6 +3,8 @@ package com.yahoo.vespa.hosted.node.admin.task.util.template; import org.junit.jupiter.api.Test; +import java.nio.file.Path; + import static org.junit.jupiter.api.Assertions.assertEquals; /** @@ -88,4 +90,64 @@ public class TemplateTest { .set("area", "Norway"); assertEquals("hello worldhello Norway", snapshot.render()); } + + @Test + void verifyVariableSection() { + Template template = getTemplate("template1.tmp"); + template.set("varname", "varvalue"); + assertEquals("variable section 'varvalue'\n" + + "end of text\n", template.render()); + } + + @Test + void verifySimpleListSection() { + Template template = getTemplate("template1.tmp"); + template.set("varname", "varvalue") + .add("listname") + .set("varname", "different varvalue") + .set("varname2", "varvalue2"); + assertEquals("variable section 'varvalue'\n" + + "same variable section 'different varvalue'\n" + + "different variable section 'varvalue2'\n" + + "between ends\n" + + "end of text\n", template.render()); + } + + @Test + void verifyNestedListSection() { + Template template = getTemplate("template2.tmp"); + Template A0 = template.add("listA"); + Template A0B0 = A0.add("listB"); + Template A0B1 = A0.add("listB"); + + Template A1 = template.add("listA"); + Template A1B0 = A1.add("listB"); + assertEquals("body A\n" + + "body B\n" + + "body B\n" + + "body A\n" + + "body B\n", + template.render()); + } + + @Test + void verifyVariableReferences() { + Template template = getTemplate("template3.tmp"); + template.set("varname", "varvalue") + .set("innerVarSetAtTop", "val2"); + template.add("l"); + template.add("l") + .set("varname", "varvalue2"); + assertEquals("varvalue\n" + + "varvalue\n" + + "inner varvalue\n" + + "val2\n" + + "inner varvalue2\n" + + "val2\n", + template.render()); + } + + private Template getTemplate(String filename) { + return Template.at(Path.of("src/test/resources/" + filename)); + } } |