aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/schema/document/ComplexAttributeFieldUtilsTestCase.java
blob: 7a89f52268fa8325aa71773e43c831c9fd3057f3 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.document;

import com.yahoo.schema.Schema;
import com.yahoo.schema.ApplicationBuilder;
import com.yahoo.schema.parser.ParseException;
import org.junit.jupiter.api.Test;

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

public class ComplexAttributeFieldUtilsTestCase {

    private static class FixtureBase {

        private final ImmutableSDField field;

        FixtureBase(String fieldName, String sdContent) throws ParseException {
            Schema schema = ApplicationBuilder.createFromString(sdContent).getSchema();
            field = schema.getConcreteField(fieldName);
        }

        public ImmutableSDField field() {
            return field;
        }

        boolean isSupportedComplexField() {
            return ComplexAttributeFieldUtils.isSupportedComplexField(field());
        }

        boolean isArrayOfSimpleStruct() {
            return ComplexAttributeFieldUtils.isArrayOfSimpleStruct(field());
        }

        boolean isMapOfSimpleStruct() {
            return ComplexAttributeFieldUtils.isMapOfSimpleStruct(field());
        }

        boolean isMapOfPrimitiveType() {
            return ComplexAttributeFieldUtils.isMapOfPrimitiveType(field());
        }

        boolean isComplexFieldWithOnlyStructFieldAttributes() {
            return ComplexAttributeFieldUtils.isComplexFieldWithOnlyStructFieldAttributes(field());
        }
    }

    private static class Fixture extends FixtureBase {

        Fixture(String fieldName, String sdFieldContent) throws ParseException {
            super(fieldName, joinLines("search test {",
                    "  document test {",
                    "    struct elem {",
                    "      field name type string {}",
                    "      field weight type int {}",
                    "    }",
                    sdFieldContent,
                    "  }",
                    "}"));
        }
    }

    private static class ComplexFixture extends FixtureBase {

        ComplexFixture(String fieldName, String sdFieldContent) throws ParseException {
            super(fieldName, joinLines("search test {",
                    "  document test {",
                    "    struct elem {",
                    "      field name type string {}",
                    "      field weights type array<int> {}",
                    "    }",
                    sdFieldContent,
                    "  }",
                    "}"));
        }
    }

    @Test
    void array_of_struct_with_only_struct_field_attributes_is_tagged_as_such() throws ParseException {
        Fixture f = new Fixture("elem_array",
                joinLines("field elem_array type array<elem> {",
                        "  indexing: summary",
                        "  struct-field name { indexing: attribute }",
                        "  struct-field weight { indexing: attribute }",
                        "}"));
        assertTrue(f.isSupportedComplexField());
        assertTrue(f.isArrayOfSimpleStruct());
        assertTrue(f.isComplexFieldWithOnlyStructFieldAttributes());
    }

    @Test
    void array_of_struct_with_some_struct_field_attributes_is_tagged_as_such() throws ParseException {
        Fixture f = new Fixture("elem_array",
                joinLines("field elem_array type array<elem> {",
                        "  indexing: summary",
                        "  struct-field weight { indexing: attribute }",
                        "}"));
        assertTrue(f.isSupportedComplexField());
        assertTrue(f.isArrayOfSimpleStruct());
        assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
    }

    @Test
    void map_of_struct_with_only_struct_field_attributes_is_tagged_as_such() throws ParseException {
        Fixture f = new Fixture("elem_map",
                joinLines("field elem_map type map<string, elem> {",
                        "  indexing: summary",
                        "  struct-field key { indexing: attribute }",
                        "  struct-field value.name { indexing: attribute }",
                        "  struct-field value.weight { indexing: attribute }",
                        "}"));
        assertTrue(f.isSupportedComplexField());
        assertTrue(f.isMapOfSimpleStruct());
        assertFalse(f.isMapOfPrimitiveType());
        assertTrue(f.isComplexFieldWithOnlyStructFieldAttributes());
    }

    @Test
    void map_of_struct_with_some_struct_field_attributes_is_tagged_as_such() throws ParseException {
        {
            Fixture f = new Fixture("elem_map",
                    joinLines("field elem_map type map<int, elem> {",
                            "  indexing: summary",
                            "  struct-field value.name { indexing: attribute }",
                            "  struct-field value.weight { indexing: attribute }",
                            "}"));
            assertTrue(f.isSupportedComplexField());
            assertTrue(f.isMapOfSimpleStruct());
            assertFalse(f.isMapOfPrimitiveType());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
        {
            Fixture f = new Fixture("elem_map",
                    joinLines("field elem_map type map<int, elem> {",
                            "  indexing: summary",
                            "  struct-field key { indexing: attribute }",
                            "  struct-field value.weight { indexing: attribute }",
                            "}"));
            assertTrue(f.isSupportedComplexField());
            assertTrue(f.isMapOfSimpleStruct());
            assertFalse(f.isMapOfPrimitiveType());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
    }

    @Test
    void map_of_primitive_type_with_only_struct_field_attributes_is_tagged_as_such() throws ParseException {
        Fixture f = new Fixture("str_map",
                joinLines("field str_map type map<string, string> {",
                        "  indexing: summary",
                        "  struct-field key { indexing: attribute }",
                        "  struct-field value { indexing: attribute }",
                        "}"));
        assertTrue(f.isSupportedComplexField());
        assertTrue(f.isMapOfPrimitiveType());
        assertFalse(f.isMapOfSimpleStruct());
        assertTrue(f.isComplexFieldWithOnlyStructFieldAttributes());
    }

    @Test
    void map_of_primitive_type_with_some_struct_field_attributes_is_tagged_as_such() throws ParseException {
        {
            Fixture f = new Fixture("int_map",
                    joinLines("field int_map type map<int, int> {",
                            "  indexing: summary",
                            "  struct-field key { indexing: attribute }",
                            "}"));
            assertTrue(f.isSupportedComplexField());
            assertTrue(f.isMapOfPrimitiveType());
            assertFalse(f.isMapOfSimpleStruct());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
        {
            Fixture f = new Fixture("int_map",
                    joinLines("field int_map type map<int, int> {",
                            "  indexing: summary",
                            "  struct-field value { indexing: attribute }",
                            "}"));
            assertTrue(f.isSupportedComplexField());
            assertTrue(f.isMapOfPrimitiveType());
            assertFalse(f.isMapOfSimpleStruct());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
    }

    @Test
    void unsupported_complex_field_is_tagged_as_such() throws ParseException {
        {
            ComplexFixture f = new ComplexFixture("elem_array",
                    joinLines("field elem_array type array<elem> {",
                            "  struct-field name { indexing: attribute }",
                            "  struct-field weights { indexing: attribute }",
                            "}"));
            assertFalse(f.isSupportedComplexField());
            assertFalse(f.isArrayOfSimpleStruct());
            assertFalse(f.isMapOfSimpleStruct());
            assertFalse(f.isMapOfPrimitiveType());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
        {
            ComplexFixture f = new ComplexFixture("elem_map",
                    joinLines("field elem_map type map<int, elem> {",
                            "  indexing: summary",
                            "  struct-field key { indexing: attribute }",
                            "  struct-field value.weights { indexing: attribute }",
                            "}"));
            assertFalse(f.isSupportedComplexField());
            assertFalse(f.isArrayOfSimpleStruct());
            assertFalse(f.isMapOfSimpleStruct());
            assertFalse(f.isMapOfPrimitiveType());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
    }

    @Test
    void only_struct_field_attributes_are_considered_when_tagging_a_complex_field() throws ParseException {
        {
            ComplexFixture f = new ComplexFixture("elem_array",
                    joinLines("field elem_array type array<elem> {",
                            "  struct-field name { indexing: attribute }",
                            "}"));
            assertTrue(f.isSupportedComplexField());
            assertTrue(f.isArrayOfSimpleStruct());
            assertFalse(f.isMapOfSimpleStruct());
            assertFalse(f.isMapOfPrimitiveType());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
        {
            ComplexFixture f = new ComplexFixture("elem_map",
                    joinLines("field elem_map type map<int, elem> {",
                            "  indexing: summary",
                            "  struct-field key { indexing: attribute }",
                            "  struct-field value.name { indexing: attribute }",
                            "}"));
            assertTrue(f.isSupportedComplexField());
            assertFalse(f.isArrayOfSimpleStruct());
            assertTrue(f.isMapOfSimpleStruct());
            assertFalse(f.isMapOfPrimitiveType());
            assertFalse(f.isComplexFieldWithOnlyStructFieldAttributes());
        }
    }

}