use of com.yahoo.config.codegen.DefParser in project vespa by vespa-engine.
the class ConfigDefinitionBuilderTest method testCreateConfigDefinition.
@Test
public // TODO Test ranges
void testCreateConfigDefinition() throws IOException, InterruptedException {
File defFile = new File(DEF_NAME);
DefParser defParser = new DefParser(defFile.getName(), new FileReader(defFile));
CNode root = defParser.getTree();
ConfigDefinition def = ConfigDefinitionBuilder.createConfigDefinition(root);
assertNotNull(def);
assertThat(def.getBoolDefs().size(), is(2));
assertNull(def.getBoolDefs().get("bool_val").getDefVal());
assertThat(def.getBoolDefs().get("bool_with_def").getDefVal(), is(false));
assertThat(def.getIntDefs().size(), is(2));
assertNull(def.getIntDefs().get("int_val").getDefVal());
assertThat(def.getIntDefs().get("int_with_def").getDefVal(), is(-545));
assertThat(def.getLongDefs().size(), is(2));
assertNull(def.getLongDefs().get("long_val").getDefVal());
assertThat(def.getLongDefs().get("long_with_def").getDefVal(), is(-50000000000L));
assertThat(def.getDoubleDefs().size(), is(2));
assertNull(def.getDoubleDefs().get("double_val").getDefVal());
assertThat(def.getDoubleDefs().get("double_with_def").getDefVal(), is(-6.43));
assertThat(def.getEnumDefs().size(), is(3));
assertTrue(def.getEnumDefs().containsKey("enum_val"));
assertThat(def.getEnumDefs().get("enum_val").getVals().size(), is(3));
assertThat(def.getEnumDefs().get("enum_val").getVals().get(0), is("FOO"));
assertThat(def.getEnumDefs().get("enum_val").getVals().get(1), is("BAR"));
assertThat(def.getEnumDefs().get("enum_val").getVals().get(2), is("FOOBAR"));
assertTrue(def.getEnumDefs().containsKey("enumwithdef"));
assertThat(def.getEnumDefs().get("enumwithdef").getDefVal(), is("BAR2"));
assertTrue(def.getEnumDefs().containsKey("onechoice"));
assertThat(def.getEnumDefs().get("onechoice").getDefVal(), is("ONLYFOO"));
assertThat(def.getStringDefs().size(), is(2));
// The return value is a String, so null if no default value
assertNull(def.getStringDefs().get("string_val").getDefVal());
assertThat(def.getStringDefs().get("stringwithdef").getDefVal(), is("foobar"));
assertThat(def.getReferenceDefs().size(), is(2));
assertNotNull(def.getReferenceDefs().get("refval"));
assertThat(def.getReferenceDefs().get("refwithdef").getDefVal(), is(":parent:"));
assertThat(def.getFileDefs().size(), is(1));
assertNotNull(def.getFileDefs().get("fileVal"));
assertThat(def.getArrayDefs().size(), is(9));
assertNotNull(def.getArrayDefs().get("boolarr"));
assertThat(def.getArrayDefs().get("boolarr").getTypeSpec().getType(), is("bool"));
assertNotNull(def.getArrayDefs().get("enumarr"));
assertThat(def.getArrayDefs().get("enumarr").getTypeSpec().getType(), is("enum"));
assertThat(def.getArrayDefs().get("enumarr").getTypeSpec().getEnumVals().toString(), is("[ARRAY, VALUES]"));
assertNotNull(def.getArrayDefs().get("refarr"));
assertThat(def.getArrayDefs().get("refarr").getTypeSpec().getType(), is("reference"));
assertNotNull(def.getArrayDefs().get("fileArr"));
assertThat(def.getArrayDefs().get("fileArr").getTypeSpec().getType(), is("file"));
assertThat(def.getStructDefs().size(), is(2));
assertNotNull(def.getStructDefs().get("basicStruct"));
assertThat(def.getStructDefs().get("basicStruct").getStringDefs().size(), is(1));
assertThat(def.getStructDefs().get("basicStruct").getStringDefs().get("foo").getDefVal(), is("basic"));
assertThat(def.getStructDefs().get("basicStruct").getIntDefs().size(), is(1));
assertNull(def.getStructDefs().get("basicStruct").getIntDefs().get("bar").getDefVal());
assertThat(def.getStructDefs().get("basicStruct").getArrayDefs().size(), is(1));
assertThat(def.getStructDefs().get("basicStruct").getArrayDefs().get("intArr").getTypeSpec().getType(), is("int"));
assertNotNull(def.getStructDefs().get("rootStruct"));
assertNotNull(def.getStructDefs().get("rootStruct").getStructDefs().get("inner0"));
assertNotNull(def.getStructDefs().get("rootStruct").getStructDefs().get("inner1"));
assertThat(def.getStructDefs().get("rootStruct").getInnerArrayDefs().size(), is(1));
assertNotNull(def.getStructDefs().get("rootStruct").getInnerArrayDefs().get("innerArr"));
assertThat(def.getStructDefs().get("rootStruct").getInnerArrayDefs().get("innerArr").getStringDefs().size(), is(1));
assertThat(def.getInnerArrayDefs().size(), is(1));
assertNotNull(def.getInnerArrayDefs().get("myarray"));
assertThat(def.getInnerArrayDefs().get("myarray").getIntDefs().get("intval").getDefVal(), is(14));
assertThat(def.getInnerArrayDefs().get("myarray").getArrayDefs().size(), is(1));
assertNotNull(def.getInnerArrayDefs().get("myarray").getArrayDefs().get("stringval"));
assertThat(def.getInnerArrayDefs().get("myarray").getArrayDefs().get("stringval").getTypeSpec().getType(), is("string"));
assertThat(def.getInnerArrayDefs().get("myarray").getEnumDefs().get("enumval").getDefVal(), is("TYPE"));
assertNull(def.getInnerArrayDefs().get("myarray").getReferenceDefs().get("refval").getDefVal());
assertThat(def.getInnerArrayDefs().get("myarray").getInnerArrayDefs().size(), is(1));
assertThat(def.getInnerArrayDefs().get("myarray").getInnerArrayDefs().get("anotherarray").getIntDefs().get("foo").getDefVal(), is(-4));
assertNull(def.getInnerArrayDefs().get("myarray").getStructDefs().get("myStruct").getIntDefs().get("a").getDefVal());
assertThat(def.getInnerArrayDefs().get("myarray").getStructDefs().get("myStruct").getIntDefs().get("b").getDefVal(), is(2));
// Maps
assertEquals(def.getLeafMapDefs().size(), 4);
assertEquals(def.getLeafMapDefs().get("intMap").getTypeSpec().getType(), "int");
assertEquals(def.getLeafMapDefs().get("stringMap").getTypeSpec().getType(), "string");
assertEquals(def.getStructMapDefs().size(), 1);
assertEquals(def.getStructMapDefs().get("myStructMap").getIntDefs().get("myInt").getDefVal(), null);
assertEquals(def.getStructMapDefs().get("myStructMap").getStringDefs().get("myString").getDefVal(), null);
assertEquals(def.getStructMapDefs().get("myStructMap").getIntDefs().get("myIntDef").getDefVal(), (Integer) 56);
assertEquals(def.getStructMapDefs().get("myStructMap").getStringDefs().get("myStringDef").getDefVal(), "g");
// Ranges
}
use of com.yahoo.config.codegen.DefParser in project vespa by vespa-engine.
the class ConfigFileFormatterTest method require_that_complex_struct_formatting_is_correct.
@Test
public void require_that_complex_struct_formatting_is_correct() throws IOException {
Slime slime = new Slime();
Cursor root = slime.setObject();
Cursor nested = root.setObject("nested");
Cursor nested_inner = nested.setObject("inner");
nested_inner.setString("name", "baz");
nested_inner.setString("gender", "FEMALE");
Cursor nested_inner_arr = nested_inner.setArray("emails");
nested_inner_arr.addString("foo");
nested_inner_arr.addString("bar");
Cursor nestedarr = root.setArray("nestedarr");
Cursor nestedarr1 = nestedarr.addObject();
Cursor inner1 = nestedarr1.setObject("inner");
inner1.setString("name", "foo");
inner1.setString("gender", "FEMALE");
Cursor inner1arr = inner1.setArray("emails");
inner1arr.addString("foo@bar");
inner1arr.addString("bar@foo");
Cursor complexarr = root.setArray("complexarr");
Cursor complexarr1 = complexarr.addObject();
Cursor innerarr1 = complexarr1.setArray("innerarr");
Cursor innerarr11 = innerarr1.addObject();
innerarr11.setString("name", "bar");
innerarr11.setString("gender", "MALE");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InnerCNode def = new DefParser("structtypes", new StringReader(StringUtilities.implode(StructtypesConfig.CONFIG_DEF_SCHEMA, "\n"))).getTree();
new ConfigFileFormat(def).encode(baos, slime);
assertThat(baos.toString(), is("nested.inner.name \"baz\"\n" + "nested.inner.gender FEMALE\n" + "nested.inner.emails[0] \"foo\"\n" + "nested.inner.emails[1] \"bar\"\n" + "nestedarr[0].inner.name \"foo\"\n" + "nestedarr[0].inner.gender FEMALE\n" + "nestedarr[0].inner.emails[0] \"foo@bar\"\n" + "nestedarr[0].inner.emails[1] \"bar@foo\"\n" + "complexarr[0].innerarr[0].name \"bar\"\n" + "complexarr[0].innerarr[0].gender MALE\n"));
}
use of com.yahoo.config.codegen.DefParser in project vespa by vespa-engine.
the class ConfigFileFormatterTest method require_that_illegal_enum_throws_exception.
// TODO: Reenable this when we can reenable typechecking.
@Ignore
@Test(expected = IllegalArgumentException.class)
public void require_that_illegal_enum_throws_exception() throws IOException {
Slime slime = new Slime();
Cursor root = slime.setObject();
root.setString("enumval", "invalid");
InnerCNode def = new DefParser("simpletypes", new StringReader(StringUtilities.implode(SimpletypesConfig.CONFIG_DEF_SCHEMA, "\n"))).getTree();
new ConfigFileFormat(def).encode(new ByteArrayOutputStream(), slime);
}
use of com.yahoo.config.codegen.DefParser in project vespa by vespa-engine.
the class ConfigFileFormatterTest method require_that_struct_formatting_is_correct.
@Test
public void require_that_struct_formatting_is_correct() throws IOException {
Slime slime = new Slime();
Cursor root = slime.setObject();
Cursor simple = root.setObject("simple");
simple.setString("name", "myname");
simple.setString("gender", "FEMALE");
Cursor array = simple.setArray("emails");
array.addString("foo@bar.com");
array.addString("bar@baz.net");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InnerCNode def = new DefParser("structtypes", new StringReader(StringUtilities.implode(StructtypesConfig.CONFIG_DEF_SCHEMA, "\n"))).getTree();
new ConfigFileFormat(def).encode(baos, slime);
assertThat(baos.toString(), is("simple.name \"myname\"\n" + "simple.gender FEMALE\n" + "simple.emails[0] \"foo@bar.com\"\n" + "simple.emails[1] \"bar@baz.net\"\n"));
}
use of com.yahoo.config.codegen.DefParser in project vespa by vespa-engine.
the class ConfigFileFormatterTest method require_that_strings_are_encoded.
@Test
public void require_that_strings_are_encoded() throws IOException {
Slime slime = new Slime();
Cursor root = slime.setObject();
String value = "\u7d22";
root.setString("stringval", value);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InnerCNode def = new DefParser("simpletypes", new StringReader(StringUtilities.implode(SimpletypesConfig.CONFIG_DEF_SCHEMA, "\n"))).getTree();
new ConfigFileFormat(def).encode(baos, slime);
assertThat(baos.toString("UTF-8"), is("stringval \"" + value + "\"\n"));
}
Aggregations