aboutsummaryrefslogtreecommitdiffstats
path: root/indexinglanguage/src/test/java/com/yahoo/vespa/indexinglanguage/SimpleDocumentAdapterTestCase.java
blob: 5a85d8ef5c310f8b74a8b01bd6225af38d83f217 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.indexinglanguage;

import com.yahoo.document.*;
import com.yahoo.document.datatypes.FieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.vespa.indexinglanguage.expressions.VerificationException;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

/**
 * @author Simon Thoresen Hult
 */
public class SimpleDocumentAdapterTestCase {

    @Test
    public void requireThatStructFieldsCanBeAccessed() {
        DataType barType = DataType.STRING;
        FieldValue bar = barType.createFieldValue("bar");

        StructDataType fooType = new StructDataType("my_struct");
        fooType.addField(new Field("bar", barType));
        Struct foo = new Struct(fooType);
        foo.setFieldValue("bar", bar);

        DocumentType docType = new DocumentType("my_doc");
        docType.addField("foo", fooType);
        Document doc = new Document(docType, "id:scheme:my_doc::");
        doc.setFieldValue("foo", foo);

        DocumentAdapter adapter = new SimpleDocumentAdapter(doc);
        assertEquals(fooType, adapter.getInputType(null, "foo"));
        assertEquals(foo, adapter.getInputValue("foo"));
        assertEquals(barType, adapter.getInputType(null, "foo.bar"));
        assertEquals(bar, adapter.getInputValue("foo.bar"));
    }

    @Test
    public void requireThatUnknownFieldsReturnNull() {
        DocumentType docType = new DocumentType("my_doc");
        Document doc = new Document(docType, "id:scheme:my_doc::");

        DocumentAdapter adapter = new SimpleDocumentAdapter(doc);
        try {
            adapter.getInputType(null, "foo");
            fail();
        } catch (VerificationException e) {
            assertEquals("Input field 'foo' not found", e.getMessage());
        }
        assertNull(adapter.getInputValue("foo"));
    }
}