// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition;

import com.yahoo.document.DataType;
import com.yahoo.document.Field;
import com.yahoo.document.annotation.AnnotationReferenceDataType;
import com.yahoo.searchdefinition.Schema;
import com.yahoo.searchdefinition.document.SDDocumentType;
import com.yahoo.searchdefinition.document.SDField;
import com.yahoo.config.model.deploy.TestProperties;
import org.junit.Test;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.Assert.assertFalse;

/**
 * @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
    public void noAnnotationReferenceInDocument() throws Exception {
        var builder = new ApplicationBuilder(new TestProperties().setExperimentalSdParsing(true));
        builder.addSchema(sd);
        builder.build(true);
        checkForAnnRef(builder.getSchema().getDocument());
    }

    void checkForAnnRef(SDDocumentType doc) {
        for (var child : doc.getTypes()) {
            System.err.println("Check child ["+child+"] of parent "+doc);
            checkForAnnRef(child);
        }
        for (Field field : doc.fieldSet()) {
            DataType fieldType = field.getDataType();
            System.err.println("datatype "+fieldType+" in field  "+field+" in doc "+doc);
            assertFalse(fieldType instanceof AnnotationReferenceDataType);
        }
    }
        
}

@@ -163,32 +163,74 @@
 ument.DataType;%0A
+import com.yahoo.document.StructDataType;%0A
 import com.yahoo
@@ -647,16 +647,59 @@
 rtFalse;
+%0Aimport static org.junit.Assert.assertTrue;
 %0A%0A/**%0A *
@@ -1953,56 +1953,548 @@
     
-checkForAnnRef(builder.getSchema().getDocument()
+var doc = builder.getSchema().getDocument();%0A        checkForAnnRef(doc);%0A        var complex = doc.findAnnotation(%22complex%22);%0A        System.err.println(%22annotation: %22+complex);%0A        var dt = complex.getDataType();%0A        System.err.println(%22associated datatype: %22+dt);%0A        assertTrue(dt instanceof StructDataType);%0A        var struct = (StructDataType)dt;%0A        var field = struct.getField(%22owner%22);%0A        System.err.println(%22owner field: %22+field);%0A        assertTrue(field.getDataType() instanceof AnnotationReferenceDataType
 );%0A 

check some more details for sanity