aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/document/test/SDDocumentTypeTestCase.java
blob: f484a92e341c2b729bdba2acab40d850f4b9a5cb (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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.test;

import com.yahoo.document.DataType;
import com.yahoo.document.DataTypeName;
import com.yahoo.documentmodel.VespaDocumentType;
import com.yahoo.schema.AbstractSchemaTestCase;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.document.SDDocumentType;
import com.yahoo.schema.document.SDField;
import com.yahoo.schema.parser.ParseException;
import com.yahoo.vespa.model.test.utils.DeployLoggerStub;
import org.junit.jupiter.api.Test;

import java.util.Iterator;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.jupiter.api.Assertions.*;

/**
 * @author Thomas Gundersen
 * @author bratseth
 */
public class SDDocumentTypeTestCase extends AbstractSchemaTestCase {

    // Verify that we can register and retrieve fields.
    @Test
    void testSetGet() {
        SDDocumentType docType = new SDDocumentType("testdoc");
        docType.addField("Bongle", DataType.STRING);
        docType.addField("nalle", DataType.INT);

        assertNotNull(docType.getField("Bongle").getName());
        assertNull(docType.getField("bongle"));

    }

    @Test
    void testInheritance() {
        SDDocumentType child = new SDDocumentType("child");
        Iterator<SDDocumentType> inherited = child.getInheritedTypes().iterator();
        assertTrue(inherited.hasNext());
        assertEquals(inherited.next().getDocumentName(), VespaDocumentType.NAME);
        assertFalse(inherited.hasNext());

        child.addField("childfield", DataType.INT);
        SDField overridden = child.addField("overridden", DataType.STRING);

        SDDocumentType parent1 = new SDDocumentType("parent1");
        SDField overridden2 = parent1.addField("overridden", DataType.STRING);
        parent1.addField("parent1field", DataType.STRING);
        child.inherit(parent1);

        SDDocumentType parent2 = new SDDocumentType("parent2");
        parent2.addField("parent2field", DataType.STRING);
        child.inherit(parent2);

        SDDocumentType root = new SDDocumentType("root");
        root.addField("rootfield", DataType.STRING);
        parent1.inherit(root);
        parent2.inherit(root);

        inherited = child.getInheritedTypes().iterator();
        assertEquals(VespaDocumentType.NAME, inherited.next().getDocumentName());
        assertEquals(new DataTypeName("parent1"), inherited.next().getDocumentName());
        assertEquals(new DataTypeName("parent2"), inherited.next().getDocumentName());
        assertFalse(inherited.hasNext());

        inherited = parent1.getInheritedTypes().iterator();
        assertEquals(VespaDocumentType.NAME, inherited.next().getDocumentName());
        assertEquals(new DataTypeName("root"), inherited.next().getDocumentName());
        assertFalse(inherited.hasNext());

        inherited = parent2.getInheritedTypes().iterator();
        assertEquals(VespaDocumentType.NAME, inherited.next().getDocumentName());
        assertEquals(new DataTypeName("root"), inherited.next().getDocumentName());
        assertFalse(inherited.hasNext());

        inherited = root.getInheritedTypes().iterator();
        assertTrue(inherited.hasNext());
        assertEquals(inherited.next().getDocumentName(), VespaDocumentType.NAME);
        assertFalse(inherited.hasNext());


        Iterator fields = child.fieldSet().iterator();
        SDField field;

        field = (SDField) fields.next();
        assertEquals("rootfield", field.getName());

        field = (SDField) fields.next();
        assertEquals("overridden", field.getName());

        field = (SDField) fields.next();
        assertEquals("parent1field", field.getName());

        field = (SDField) fields.next();
        assertEquals("parent2field", field.getName());

        field = (SDField) fields.next();
        assertEquals("childfield", field.getName());
    }

    @Test
    void testStructInheritance() throws ParseException {
        String schemaLines = joinLines(
                "schema test {" +
                        "  document test {" +
                        "    struct parent_struct {" +
                        "      field parent_struct_field_1 type string {}" +
                        "    }" +
                        "    struct child_struct inherits parent_struct {" +
                        "      field child_struct_field_1 type string {}" +
                        "    }" +
                        "    field child_array type array<child_struct> {" +
                        "      indexing: summary\n" +
                        "      struct-field child_struct_field_1 { indexing: attribute }" +
                        "      struct-field parent_struct_field_1  { indexing: attribute }" +
                        "    }" +
                        "  }" +
                        "}");

        ApplicationBuilder builder = new ApplicationBuilder(new DeployLoggerStub());
        builder.addSchema(schemaLines);
        builder.build(true);
        var application = builder.application();

        SDDocumentType type = application.schemas().get("test").getDocument();

        SDDocumentType parent_struct = type.getOwnedType("parent_struct");
        assertEquals(1, parent_struct.fieldSet().size());
        assertNotNull(parent_struct.getField("parent_struct_field_1"));

        SDDocumentType child_struct = type.getOwnedType("child_struct");
        assertTrue(child_struct.inheritedTypes().containsKey(parent_struct.getDocumentName()));
        assertEquals(2, child_struct.fieldSet().size());
        assertNotNull(child_struct.getField("child_struct_field_1"));
        assertNotNull(child_struct.getField("parent_struct_field_1"));
    }

}