aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/processing/ImportedFieldsResolverTestCase.java
blob: 5baa64d06d4099ecdc0855a593ef5be424646124 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.processing;

import com.yahoo.document.DataType;
import com.yahoo.document.TensorDataType;
import com.yahoo.schema.Schema;
import com.yahoo.schema.document.ImmutableImportedSDField;
import com.yahoo.schema.document.ImmutableSDField;
import com.yahoo.schema.document.ImportedField;
import com.yahoo.schema.document.ImportedFields;
import com.yahoo.schema.document.SDField;
import com.yahoo.schema.document.TemporarySDField;
import com.yahoo.tensor.TensorType;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;

/**
 * @author geirst
 */
public class ImportedFieldsResolverTestCase {

    @SuppressWarnings("deprecation")
    @Rule
    public final ExpectedException exceptionRule = ExpectedException.none();

    private void resolve_imported_field(String fieldName, String targetFieldName) {
        SearchModel model = new SearchModel();
        model.addImportedField(fieldName, "ref", targetFieldName).resolve();

        assertEquals(1, model.importedFields.fields().size());
        ImportedField myField = model.importedFields.fields().get(fieldName);
        assertNotNull(myField);
        assertEquals(fieldName, myField.fieldName());
        assertSame(model.childSchema.getConcreteField("ref"), myField.reference().referenceField());
        assertSame(model.parentSchema, myField.reference().targetSearch());
        ImmutableSDField targetField = model.parentSchema.getField(targetFieldName);
        if (targetField instanceof SDField) {
            assertSame(targetField, myField.targetField());
        } else {
            assertSame(getImportedField(targetField), getImportedField(myField.targetField()));
        }
    }

    private static ImportedField getImportedField(ImmutableSDField field) {
        return ((ImmutableImportedSDField) field).getImportedField();
    }

    @Test
    public void valid_imported_fields_are_resolved() {
        resolve_imported_field("my_attribute_field", "attribute_field");
        resolve_imported_field("my_tensor_field", "tensor_field");
        resolve_imported_field("my_ancient_field", "ancient_field");
    }

    @Test
    public void resolver_fails_if_document_reference_is_not_found() {
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage("For schema 'child', import field 'my_attribute_field': "
                + "Reference field 'not_ref' not found");
        new SearchModel().addImportedField("my_attribute_field", "not_ref", "budget").resolve();
    }

    @Test
    public void resolver_fails_if_referenced_field_is_not_found() {
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage("For schema 'child', import field 'my_attribute_field': "
                + "Field 'not_existing' via reference field 'ref': Not found");
        new SearchModel().addImportedField("my_attribute_field", "ref", "not_existing").resolve();
    }

    @Test
    public void resolver_fails_if_imported_field_is_not_an_attribute() {
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage("For schema 'child', import field 'my_not_attribute': "
                + "Field 'not_attribute' via reference field 'ref': Is not an attribute field. Only attribute fields supported");
        new SearchModel().addImportedField("my_not_attribute", "ref", "not_attribute").resolve();
    }

    @Test
    public void resolver_fails_if_imported_field_is_indexing() {
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage(
                "For schema 'child', import field 'my_attribute_and_index': " +
                        "Field 'attribute_and_index' via reference field 'ref': Is an index field. Not supported");
        new SearchModel()
                .addImportedField("my_attribute_and_index", "ref", "attribute_and_index")
                .resolve();
    }

    @Test
    public void resolver_fails_if_imported_field_is_of_type_predicate() {
        exceptionRule.expect(IllegalArgumentException.class);
        exceptionRule.expectMessage(
                "For schema 'child', import field 'my_predicate_field': " +
                        "Field 'predicate_field' via reference field 'ref': Is of type 'predicate'. Not supported");
        new SearchModel().addImportedField("my_predicate_field", "ref", "predicate_field").resolve();
    }

    static class SearchModel extends ParentChildSearchModel {

        public final Schema grandParentSchema;
        public ImportedFields importedFields;

        public SearchModel() {
            super();
            grandParentSchema = createSearch("grandparent");
            var grandParentDoc = grandParentSchema.getDocument();
            grandParentDoc.addField(createField(grandParentDoc, "ancient_field", DataType.INT, "{ attribute }"));
            var parentDoc = parentSchema.getDocument();
            parentDoc.addField(createField(parentDoc, "attribute_field", DataType.INT, "{ attribute }"));
            parentDoc.addField(createField(parentDoc, "attribute_and_index", DataType.INT, "{ attribute | index }"));
            parentDoc.addField(new TemporarySDField(parentDoc, "not_attribute", DataType.INT));
            parentDoc.addField(createField(parentDoc, "tensor_field", new TensorDataType(TensorType.fromSpec("tensor(x[5])")), "{ attribute }"));
            parentDoc.addField(createField(parentDoc, "predicate_field", DataType.PREDICATE, "{ attribute }"));
            addRefField(parentSchema, grandParentSchema, "ref");
            addImportedField(parentSchema, "ancient_field", "ref", "ancient_field");

            addRefField(childSchema, parentSchema, "ref");
        }


        protected SearchModel addImportedField(String fieldName, String referenceFieldName, String targetFieldName) {
            return addImportedField(childSchema, fieldName, referenceFieldName, targetFieldName);
        }

        protected SearchModel addImportedField(Schema schema, String fieldName, String referenceFieldName, String targetFieldName) {
            super.addImportedField(schema, fieldName, referenceFieldName, targetFieldName);
            return this;
        }

        public void resolve() {
            resolve(grandParentSchema);
            resolve(parentSchema);
            importedFields = resolve(childSchema);
        }

        private static ImportedFields resolve(Schema schema) {
            assertNotNull(schema.temporaryImportedFields().get());
            assertFalse(schema.importedFields().isPresent());
            new ImportedFieldsResolver(schema, null, null, null).process(true, false);
            assertNotNull(schema.importedFields().get());
            return schema.importedFields().get();
        }
    }

}