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

import com.yahoo.document.DataType;
import com.yahoo.document.StructDataType;
import com.yahoo.document.Field;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.schema.document.SDDocumentType;
import com.yahoo.config.model.deploy.TestProperties;
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;

/**
 * @author arnej
 */
public class AnnotationReferenceTestCase {

    static final String sd =
        joinLines("search test {",
                  "    document test { ",
                  "        struct mystruct {",
                  "          field x type int {}",
                  "        }",
                  "        field a type string {}",
                  "        field b type mystruct {}",
                  "        annotation marker {}",
                  "        annotation person {",
                  "            field name type string {}",
                  "            field age type int {}",
                  "        }",
                  "        annotation complex {",
                  "            field title type string {}",
                  "            field tag type annotationreference<marker> {}",
                  "            field owner type annotationreference<person> {}",
                  "        }",
                  "    }",
                  "}");

    @Test
    void noAnnotationReferenceInDocument() throws Exception {
        var builder = new ApplicationBuilder(new TestProperties());
        builder.addSchema(sd);
        builder.build(true);
        var doc = builder.getSchema().getDocument();
        checkForAnnRef(doc);
        var complex = doc.findAnnotation("complex");
        var dt = complex.getDataType();
        assertTrue(dt instanceof StructDataType);
        var struct = (StructDataType) dt;
        var field = struct.getField("owner");
        assertTrue(field.getDataType() instanceof AnnotationReferenceDataType);
    }

    void checkForAnnRef(SDDocumentType doc) {
        for (var child : doc.getTypes()) {
            checkForAnnRef(child);
        }
        for (Field field : doc.fieldSet()) {
            DataType fieldType = field.getDataType();
            assertFalse(fieldType instanceof AnnotationReferenceDataType);
        }
    }
        
}