aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/application/validation/change/search/DocumentTypeChangeValidatorTest.java
blob: 639ca820613c91845483685fd0df16bfcf366596 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation.change.search;

import com.yahoo.config.application.api.ValidationId;
import com.yahoo.config.provision.ClusterSpec;
import com.yahoo.document.DocumentType;
import com.yahoo.document.Field;
import com.yahoo.documentmodel.NewDocumentReferenceDataType;
import com.yahoo.document.StructDataType;
import com.yahoo.documentmodel.NewDocumentType;
import com.yahoo.schema.FieldSets;
import com.yahoo.vespa.model.application.validation.change.VespaConfigChangeAction;
import com.yahoo.vespa.model.application.validation.change.VespaRefeedAction;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

import static com.yahoo.vespa.model.application.validation.change.ConfigChangeTestUtils.newRefeedAction;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Test validation of changes between a current and next document type used in a document database.
 *
 * @author toregge
 */
public class DocumentTypeChangeValidatorTest {

    private static class Fixture extends ContentClusterFixture {

        DocumentTypeChangeValidator validator;

        public Fixture(String currentSd, String nextSd) throws Exception {
            super(currentSd, nextSd);
            validator = new DocumentTypeChangeValidator(ClusterSpec.Id.from("test"),
                                                        currentDocType(),
                                                        nextDocType());
        }

        @Override
        public List<VespaConfigChangeAction> validate() {
            return validator.validate();
        }

    }

    @Test
    void requireThatFieldRemovalIsOK() throws Exception {
        Fixture f = new Fixture("field f1 type string { indexing: summary }",
                "field f2 type string { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatSameDataTypeIsOK() throws Exception {
        Fixture f = new Fixture("field f1 type string { indexing: summary }",
                "field f1 type string { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatDataTypeChangeIsNotOK() throws Exception {
        Fixture f = new Fixture("field f1 type string { indexing: summary }",
                "field f1 type int { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'string' -> 'int'"));
    }

    @Test
    void requireThatAddingCollectionTypeIsNotOK() throws Exception {
        Fixture f = new Fixture("field f1 type string { indexing: summary }",
                "field f1 type array<string> { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'string' -> 'Array<string>'"));
    }


    @Test
    void requireThatSameNestedDataTypeIsOK() throws Exception {
        Fixture f = new Fixture("field f1 type array<string> { indexing: summary }",
                "field f1 type array<string> { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatNestedDataTypeChangeIsNotOK() throws Exception {
        Fixture f = new Fixture("field f1 type array<string> { indexing: summary }",
                "field f1 type array<int> { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'Array<string>' -> 'Array<int>'"));
    }

    @Test
    void requireThatChangedCollectionTypeIsNotOK() throws Exception {
        Fixture f = new Fixture("field f1 type array<string> { indexing: summary }",
                "field f1 type weightedset<string> { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'Array<string>' -> 'WeightedSet<string>'"));
    }

    @Test
    void requireThatMultipleDataTypeChangesIsNotOK() throws Exception {
        Fixture f = new Fixture("field f1 type string { indexing: summary } field f2 type int { indexing: summary }",
                "field f2 type string { indexing: summary } field f1 type int { indexing: summary }");
        Instant.now();
        Instant.now();
        f.assertValidation(Arrays.asList(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'string' -> 'int'"),
                newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f2' changed: data type: 'int' -> 'string'")));
    }

    @Test
    void requireThatSameDataTypeInStructFieldIsOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type string {} } field f2 type s1 { indexing: summary }",
                "struct s1 { field f1 type string {} } field f2 type s1 { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatSameNestedDataTypeChangeInStructFieldIsOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type array<string> {} } field f2 type s1 { indexing: summary }",
                "struct s1 { field f1 type array<string> {} } field f2 type s1 { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatAddingFieldInStructFieldIsOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type string {} } field f3 type s1 { indexing: summary }",
                "struct s1 { field f1 type string {} field f2 type int {} } field f3 type s1 { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatRemovingFieldInStructFieldIsOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type string {} field f2 type int {} } field f3 type s1 { indexing: summary }",
                "struct s1 { field f1 type string {} } field f3 type s1 { indexing: summary }");
        f.assertValidation();
    }

    @Test
    void requireThatDataTypeChangeInStructFieldIsNotOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type string {} } field f2 type s1 { indexing: summary }",
                "struct s1 { field f1 type int {} } field f2 type s1 { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f2' changed: data type: 's1:{f1:string}' -> 's1:{f1:int}'"));
    }

    @Test
    void requireThatNestedDataTypeChangeInStructFieldIsNotOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type array<string> {} } field f2 type s1 { indexing: summary }",
                "struct s1 { field f1 type array<int> {} } field f2 type s1 { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f2' changed: data type: 's1:{f1:Array<string>}' -> 's1:{f1:Array<int>}'"));
    }

    @Test
    void requireThatDataTypeChangeInNestedStructFieldIsNotOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type string {} } struct s2 { field f2 type s1 {} } field f3 type s2 { indexing: summary }",
                "struct s1 { field f1 type int {} }    struct s2 { field f2 type s1 {} } field f3 type s2 { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f3' changed: data type: 's2:{s1:{f1:string}}' -> 's2:{s1:{f1:int}}'"));
    }

    @Test
    void requireThatMultipleDataTypeChangesInStructFieldIsNotOK() throws Exception {
        Fixture f = new Fixture("struct s1 { field f1 type string {} field f2 type int {} } field f3 type s1 { indexing: summary }",
                "struct s1 { field f1 type int {} field f2 type string {} } field f3 type s1 { indexing: summary }");
        Instant.now();
        f.assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f3' changed: data type: 's1:{f1:string,f2:int}' -> 's1:{f1:int,f2:string}'"));
    }

    @Test
    void requireThatChangingTargetTypeOfReferenceFieldIsNotOK() {
        var validator = new DocumentTypeChangeValidator(ClusterSpec.Id.from("test"),
                createDocumentTypeWithReferenceField("oldDoc"),
                createDocumentTypeWithReferenceField("newDoc"));
        List<VespaConfigChangeAction> result = validator.validate();
        assertEquals(1, result.size());
        VespaConfigChangeAction action = result.get(0);
        assertTrue(action instanceof VespaRefeedAction);
        assertEquals(
                "type='refeed', " +
                        "message='Field 'ref' changed: data type: 'Reference<oldDoc>' -> 'Reference<newDoc>'', " +
                        "services=[], documentType=''",
                action.toString());
    }

    @Test
    void changing_tensor_type_of_tensor_field_requires_refeed() throws Exception {
        Instant.now();
        new Fixture(
                "field f1 type tensor(x[2]) { indexing: attribute }",
                "field f1 type tensor(x[3]) { indexing: attribute }")
                .assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'tensor(x[2])' -> 'tensor(x[3])'"));

        Instant.now();
        new Fixture(
                "field f1 type tensor(x[5]) { indexing: attribute }",
                "field f1 type tensor(x[3]) { indexing: attribute }")
                .assertValidation(newRefeedAction(ClusterSpec.Id.from("test"), ValidationId.fieldTypeChange, "Field 'f1' changed: data type: 'tensor(x[5])' -> 'tensor(x[3])'"));
    }

    private static NewDocumentType createDocumentTypeWithReferenceField(String nameReferencedDocumentType) {
        StructDataType headerfields = new StructDataType("headerfields");
        headerfields.addField(new Field("ref", new NewDocumentReferenceDataType(new DocumentType(nameReferencedDocumentType))));
        return new NewDocumentType(
                new NewDocumentType.Name("mydoc"),
                headerfields,
                new FieldSets(Optional.empty()),
                Collections.emptySet(),
                Collections.emptySet());
    }

}