summaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/searchdefinition/derived/InheritanceTestCase.java
blob: 3bd1e568400f653203e57e6c65f69b43d7eb6a5f (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.derived;

import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.document.DataType;
import com.yahoo.document.config.DocumentmanagerConfig;
import com.yahoo.searchdefinition.Index;
import com.yahoo.searchdefinition.Schema;
import com.yahoo.searchdefinition.ApplicationBuilder;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.searchdefinition.parser.ParseException;
import com.yahoo.vespa.configmodel.producers.DocumentManager;
import org.junit.Rule;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import org.junit.rules.TemporaryFolder;

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

/**
 * Tests inheritance
 *
 * @author bratseth
 */
public class InheritanceTestCase extends AbstractExportingTestCase {

    @Override
    boolean useV8DocManagerCfg() { return true; }

    @Rule
    public TemporaryFolder tmpDir = new TemporaryFolder();

    @Test
    public void requireThatIndexedStructFieldCanBeInherited() throws IOException, ParseException {
        String dir = "src/test/derived/inheritstruct/";
        ApplicationBuilder builder = new ApplicationBuilder();
        builder.addSchemaFile(dir + "parent.sd");
        builder.addSchemaFile(dir + "child.sd");
        builder.build(true);
        derive("inheritstruct", builder, builder.getSchema("child"));
        assertCorrectConfigFiles("inheritstruct");
    }

    @Test
    public void requireThatInheritFromNullIsCaught() throws IOException, ParseException {
        try {
            assertCorrectDeriving("inheritfromnull");
        } catch (IllegalArgumentException e) {
            assertEquals("document inheritfromnull inherits from unavailable document foo", e.getMessage());
        }
    }

    @Test
    public void requireThatStructTypesAreInheritedThroughDiamond() throws IOException, ParseException {
        String dir = "src/test/derived/inheritdiamond/";
        List<String> files = Arrays.asList("grandparent.sd", "mother.sd", "father.sd", "child.sd");
        File outDir = tmpDir.newFolder("out");
        for (int startIdx = 0; startIdx < files.size(); ++startIdx) {
            ApplicationBuilder builder = new ApplicationBuilder();
            for (int fileIdx = startIdx; fileIdx < startIdx + files.size(); ++fileIdx) {
                String fileName = files.get(fileIdx % files.size());
                builder.addSchemaFile(dir + fileName);
            }
            builder.build(true);
            DocumentmanagerConfig.Builder b = new DocumentmanagerConfig.Builder();
            DerivedConfiguration.exportDocuments(new DocumentManager().produce(builder.getModel(), b), outDir.getPath());
            DocumentmanagerConfig dc = b.build();
            assertEquals(13, dc.datatype().size());
            assertNull(structType("child.body", dc));
            DocumentmanagerConfig.Datatype.Structtype childHeader = structType("child.header", dc);
            assertEquals(childHeader.field(0).name(), "foo");
            assertEquals(childHeader.field(1).name(), "bar");
            assertEquals(childHeader.field(2).name(), "baz");
            assertEquals(childHeader.field(3).name(), "cox");
            DocumentmanagerConfig.Datatype.Documenttype child = documentType("child", dc);
            assertEquals(child.inherits(0).name(), "document");
            assertEquals(child.inherits(1).name(), "father");
            assertEquals(child.inherits(2).name(), "mother");
            DocumentmanagerConfig.Datatype.Documenttype mother = documentType("mother", dc);
            assertEquals(mother.inherits(0).name(), "grandparent");
            assertEquals(mother.inherits(1).name(), "document");
        }
    }

    private DocumentmanagerConfig.Datatype.Structtype structType(String name, DocumentmanagerConfig dc) {
        for (DocumentmanagerConfig.Datatype dt : dc.datatype()) {
            for (DocumentmanagerConfig.Datatype.Structtype st : dt.structtype()) {
                if (name.equals(st.name())) return st;
            }
        }
        return null;
    }

    private DocumentmanagerConfig.Datatype.Documenttype documentType(String name, DocumentmanagerConfig dc) {
        for (DocumentmanagerConfig.Datatype dt : dc.datatype()) {
            for (DocumentmanagerConfig.Datatype.Documenttype dot : dt.documenttype()) {
                if (name.equals(dot.name())) return dot;
            }
        }
        return null;
    }
    
    @Test
    public void requireThatStructTypesAreInheritedFromParent() throws IOException, ParseException {
        String dir = "src/test/derived/inheritfromparent/";
        ApplicationBuilder builder = new ApplicationBuilder();
        builder.addSchemaFile(dir + "parent.sd");
        builder.addSchemaFile(dir + "child.sd");
        builder.build(true);
        derive("inheritfromparent", builder, builder.getSchema("child"));
        assertCorrectConfigFiles("inheritfromparent");
    }

    @Test
    public void requireThatStructTypesAreInheritedFromGrandParent() throws IOException, ParseException {
        String dir = "src/test/derived/inheritfromgrandparent/";
        ApplicationBuilder builder = new ApplicationBuilder();
        builder.addSchemaFile(dir + "grandparent.sd");
        builder.addSchemaFile(dir + "parent.sd");
        builder.addSchemaFile(dir + "child.sd");
        builder.build(true);
        derive("inheritfromgrandparent", builder, builder.getSchema("child"));
        assertCorrectConfigFiles("inheritfromgrandparent");
    }

    @Test
    public void testInheritance() throws IOException, ParseException {
        String dir = "src/test/derived/inheritance/";
        ApplicationBuilder builder = new ApplicationBuilder();
        builder.addSchemaFile(dir + "grandparent.sd");
        builder.addSchemaFile(dir + "father.sd");
        builder.addSchemaFile(dir + "mother.sd");
        builder.addSchemaFile(dir + "child.sd");
        builder.build(true);
        derive("inheritance", builder, builder.getSchema("child"));
        assertCorrectConfigFiles("inheritance");
    }

    @Test
    public void testIndexSettingInheritance() {
        SDDocumentType parent = new SDDocumentType("parent");
        Schema parentSchema = new Schema("parent", MockApplicationPackage.createEmpty());
        parentSchema.addDocument(parent);
        SDField prefixed = parent.addField("prefixed", DataType.STRING);
        prefixed.parseIndexingScript("{ index }");
        prefixed.addIndex(new Index("prefixed", true));

        SDDocumentType child = new SDDocumentType("child");
        child.inherit(parent);
        Schema childSchema = new Schema("child", MockApplicationPackage.createEmpty());
        childSchema.addDocument(child);

        prefixed = (SDField)child.getField("prefixed");
        assertNotNull(prefixed);
        assertEquals(new Index("prefixed", true), childSchema.getIndex("prefixed"));
    }

}