use of com.yahoo.data.access.Inspector in project vespa by vespa-engine.
the class InspectorConformanceTestBase method testArrayValue.
@Test
public void testArrayValue() throws Exception {
final Inspector value = getData();
List<Inspector> expected_entries = new Entries().add(value.entry(0)).add(value.entry(1)).add(value.entry(2)).add(value.entry(3)).add(value.entry(4)).entries;
assertThat(value.valid(), is(true));
assertThat(value.type(), is(Type.ARRAY));
assertThat(value.entryCount(), is(expected_entries.size()));
assertThat(value.fieldCount(), is(0));
assertThat(new Try() {
void f() {
value.asBool();
}
}.call(), instanceOf(IllegalStateException.class));
assertThat(new Try() {
void f() {
value.asLong();
}
}.call(), instanceOf(IllegalStateException.class));
assertThat(new Try() {
void f() {
value.asDouble();
}
}.call(), instanceOf(IllegalStateException.class));
assertThat(new Try() {
void f() {
value.asString();
}
}.call(), instanceOf(IllegalStateException.class));
assertThat(new Try() {
void f() {
value.asUtf8();
}
}.call(), instanceOf(IllegalStateException.class));
assertThat(new Try() {
void f() {
value.asData();
}
}.call(), instanceOf(IllegalStateException.class));
assertThat(value.asBool(true), is(true));
assertThat(value.asLong(50), is(50L));
assertThat(value.asDouble(20.25), is(20.25));
assertThat(value.asString("default"), is("default"));
assertThat(value.asUtf8("utf8".getBytes("UTF-8")), is("utf8".getBytes("UTF-8")));
assertThat(value.asData("data".getBytes("UTF-8")), is("data".getBytes("UTF-8")));
assertThat(new Entries().traverse(value).entries, is(expected_entries));
assertThat(new Fields().traverse(value).fields.size(), is(0));
assertThat(value.entry(10).valid(), is(false));
assertThat(value.field("foo").valid(), is(false));
assertThat(new Entries().iterate(value).entries, is(expected_entries));
assertThat(new Fields().iterate(value).fields.size(), is(0));
}
use of com.yahoo.data.access.Inspector in project vespa by vespa-engine.
the class InspectorConformanceTestBase method testNesting.
@Test
public void testNesting() throws Exception {
Inspector value1 = getData().entry(4).field("nested");
assertThat(value1.type(), is(Type.ARRAY));
Inspector value2 = value1.entry(0);
assertThat(value2.type(), is(Type.OBJECT));
Inspector value3 = value2.field("hidden");
assertThat(value3.type(), is(Type.STRING));
assertThat(value3.asString(), is("treasure"));
}
use of com.yahoo.data.access.Inspector in project vespa by vespa-engine.
the class JSONStringTestCase method testArrayOfStruct.
@Test
public void testArrayOfStruct() {
String json = "[{\"as1\":[\"per\",\"paal\"]," + "\"l1\":1122334455667788994,\"d1\":74.790001," + "\"i1\":14,\"al1\":[11223344556677881,11223344556677883],\"s1\":\"string\\n" + "espa\u00F1a\\n" + "asf1[0].s1\"},{\"as1\":[\"per\",\"paal\"],\"l1\":1122334455667788995,\"d1\":75.790001,\"i1\":15,\"al1\":[11223344556677881,11223344556677883],\"s1\":\"string\\n" + "espa\u00F1a asf1[1].s1\"}]";
JSONString js = new JSONString(json);
String[] o1Fields = { "\n <item>\n", " <struct-field name=\"l1\">1122334455667788994</struct-field>\n", " <struct-field name=\"al1\">\n" + " <item>11223344556677881</item>\n" + " <item>11223344556677883</item>\n" + " </struct-field>\n", " <struct-field name=\"i1\">14</struct-field>\n", " <struct-field name=\"d1\">74.790001</struct-field>\n", " <struct-field name=\"as1\">\n" + " <item>per</item>\n" + " <item>paal</item>\n" + " </struct-field>\n", " <struct-field name=\"s1\">string\n" + "españa\n" + "asf1[0].s1</struct-field>\n" };
String separator = " </item>\n" + " <item>\n";
String[] o2Fields = { " <struct-field name=\"l1\">1122334455667788995</struct-field>\n", " <struct-field name=\"al1\">\n" + " <item>11223344556677881</item>\n" + " <item>11223344556677883</item>\n" + " </struct-field>\n", " <struct-field name=\"i1\">15</struct-field>\n", " <struct-field name=\"d1\">75.790001</struct-field>\n", " <struct-field name=\"as1\">\n" + " <item>per</item>\n" + " <item>paal</item>\n" + " </struct-field>\n", " <struct-field name=\"s1\">string\n" + "españa asf1[1].s1</struct-field>\n" };
String rendered = js.toString();
int o2Offset = rendered.indexOf(separator);
assertTrue(-1 < o2Offset);
checkSubstrings(o1Fields, rendered);
checkSubstrings(o2Fields, rendered, o2Offset);
Inspector top = new Value.ArrayValue().add(new Value.ObjectValue().put("d1", 74.790001).put("s1", "string\n" + "espa\u00F1a\n" + "asf1[0].s1").put("al1", new Value.ArrayValue().add(11223344556677881L).add(11223344556677883L)).put("l1", 1122334455667788994L).put("as1", new Value.ArrayValue().add("per").add("paal")).put("i1", 14)).add(new Value.ObjectValue().put("d1", 75.790001).put("s1", "string\n" + "espa\u00F1a asf1[1].s1").put("al1", new Value.ArrayValue().add(11223344556677881L).add(11223344556677883L)).put("l1", 1122334455667788995L).put("as1", new Value.ArrayValue().add(new Value.StringValue("per")).add(new Value.StringValue("paal"))).put("i1", 15));
js = new JSONString(top);
rendered = js.renderFromInspector();
o2Offset = rendered.indexOf(separator);
assertTrue(-1 < o2Offset);
checkSubstrings(o1Fields, rendered.substring(0, o2Offset));
checkSubstrings(o2Fields, rendered, o2Offset);
}
use of com.yahoo.data.access.Inspector in project vespa by vespa-engine.
the class JSONStringTestCase method testArrayOfStructWithMap.
/**
* here is some json for you
*
* [{"asf":"here is 1st simple string field",
* "map":[{"key":"one key string","value":["one value string","embedded array"]},
* {"key":"two key string","value":["two value string","embedded array"]}],
* "sf2":"here is 2nd simple string field"},
* {"asf":"here is 3rd simple string field",
* "map":[{"key":"three key string","value":["three value string","embedded array"]},
* {"key":"four key string","value":["four value string","embedded array"]}],
* "sf2":"here is 4th simple string field"},
* ]
*
**
*/
/**
* and here is some corresponding XML
*
* <item>
* <struct-field name="asf">here is 1st simple string field</struct-field>
* <struct-field name="map">
* <item><key>one key string</key><value>
* <item>one value string</item>
* <item>embedded array</item>
* </value></item>
* <item><key>two key string</key><value>
* <item>two value string</item>
* <item>embedded array</item>
* </value></item>
* </struct-field>
* <struct-field name="sf2">here is 2nd simple string field</struct-field>
* </item>
* <item>
* <struct-field name="asf">here is 3rd simple string field</struct-field>
* <struct-field name="map">
* <item><key>three key string</key><value>
* <item>three value string</item>
* <item>embedded array</item>
* </value></item>
* <item><key>four key string</key><value>
* <item>four value string</item>
* <item>embedded array</item>
* </value></item>
* </struct-field>
* <struct-field name="sf2">here is 4th simple string field</struct-field>
* </item>
*
**
*/
@Test
public void testArrayOfStructWithMap() {
String json = "[{\"asf\":\"here is 1st simple string field\",\"map\":[{\"key\":\"one key string\",\"value\":[\"one value string\",\"embedded array\"]},{\"key\":\"two key string\",\"value\":[\"two value string\",\"embedded array\"]}],\"sf2\":\"here is 2nd simple string field\"},{\"asf\":\"here is 3rd simple string field\",\"map\":[{\"key\":\"three key string\",\"value\":[\"three value string\",\"embedded array\"]},{\"key\":\"four key string\",\"value\":[\"four value string\",\"embedded array\"]}],\"sf2\":\"here is 4th simple string field\"}]";
JSONString js = new JSONString(json);
String correct = "\n" + " <item>\n" + " <struct-field name=\"asf\">here is 1st simple string field</struct-field>\n" + " <struct-field name=\"map\">\n" + " <item><key>one key string</key><value>\n" + " <item>one value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " <item><key>two key string</key><value>\n" + " <item>two value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " </struct-field>\n" + " <struct-field name=\"sf2\">here is 2nd simple string field</struct-field>\n" + " </item>\n" + " <item>\n" + " <struct-field name=\"asf\">here is 3rd simple string field</struct-field>\n" + " <struct-field name=\"map\">\n" + " <item><key>three key string</key><value>\n" + " <item>three value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " <item><key>four key string</key><value>\n" + " <item>four value string</item>\n" + " <item>embedded array</item>\n" + " </value></item>\n" + " </struct-field>\n" + " <struct-field name=\"sf2\">here is 4th simple string field</struct-field>\n" + " </item>\n" + " ";
assertEquals(correct, js.toString());
Inspector top = new Value.ArrayValue().add(new Value.ObjectValue().put("asf", "here is 1st simple string field").put("map", new Value.ArrayValue().add(new Value.ObjectValue().put("key", "one key string").put("value", new Value.ArrayValue().add("one value string").add("embedded array"))).add(new Value.ObjectValue().put("key", "two key string").put("value", new Value.ArrayValue().add("two value string").add("embedded array")))).put("sf2", "here is 2nd simple string field")).add(new Value.ObjectValue().put("asf", "here is 3rd simple string field").put("map", new Value.ArrayValue().add(new Value.ObjectValue().put("key", "three key string").put("value", new Value.ArrayValue().add("three value string").add("embedded array"))).add(new Value.ObjectValue().put("key", "four key string").put("value", new Value.ArrayValue().add("four value string").add("embedded array")))).put("sf2", "here is 4th simple string field"));
js = new JSONString(top);
assertEquals(correct, js.renderFromInspector());
}
use of com.yahoo.data.access.Inspector in project vespa by vespa-engine.
the class JSONStringTestCase method testSimpleArrays.
@Test
public void testSimpleArrays() {
String json = "[1, 2, 3]";
JSONString js = new JSONString(json);
String correct = "\n" + " <item>1</item>\n" + " <item>2</item>\n" + " <item>3</item>\n ";
assertEquals(correct, js.toString());
Inspector top = new Value.ArrayValue().add(1).add(2).add(3);
js = new JSONString(top);
assertEquals(correct, js.renderFromInspector());
json = "[1.0, 2.0, 3.0]";
js = new JSONString(json);
correct = "\n" + " <item>1.0</item>\n" + " <item>2.0</item>\n" + " <item>3.0</item>\n ";
assertEquals(correct, js.toString());
top = new Value.ArrayValue().add(1.0).add(2.0).add(3.0);
js = new JSONString(top);
assertEquals(correct, js.renderFromInspector());
json = "[\"a\", \"b\", \"c\"]";
correct = "\n" + " <item>a</item>\n" + " <item>b</item>\n" + " <item>c</item>\n ";
js = new JSONString(json);
assertEquals(correct, js.toString());
top = new Value.ArrayValue().add("a").add("b").add("c");
js = new JSONString(top);
assertEquals(correct, js.renderFromInspector());
}
Aggregations