aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/test/java/com/yahoo/document/annotation/AnnotationTestCase.java
blob: 202768fddf378c8b11d21ad69b17b5b2622014f9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.annotation;

import com.yahoo.document.DataType;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.document.datatypes.Struct;
import com.yahoo.document.serialization.*;
import com.yahoo.io.GrowableByteBuffer;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;

/**
 * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
 */
public class AnnotationTestCase extends AbstractTypesTest {

    @Test
    public void testBasic() {
        AnnotationType begTagType = new AnnotationType("begin_tag");
        Annotation a = new Annotation(begTagType);
        Annotation b = new Annotation(begTagType);

        assertEquals(a, b);
        assertEquals(a.hashCode(), b.hashCode());
        assertNotSame(a, b);

        Annotation c = new Annotation(new AnnotationType("determiner"));

        assertFalse(a.equals(c));
        assertFalse(c.equals(a));
        assertFalse(b.equals(c));
        assertFalse(c.equals(b));

        assertFalse(a.hashCode() == c.hashCode());
        assertFalse(c.hashCode() == a.hashCode());
        assertFalse(b.hashCode() == c.hashCode());
        assertFalse(c.hashCode() == b.hashCode());
    }

    @Test
    public void testFieldValues() {
        AnnotationType atype = new AnnotationType("foobar", DataType.STRING);
        StringFieldValue sfv = new StringFieldValue("balloo");

        Annotation a = new Annotation(atype);
        a.setFieldValue(sfv);
    }

    @Test
    public void testSerializeDeserialize() {
        {
            Annotation annotation = new Annotation(dummy);
            serializeAndAssert(annotation);
        }
        {
            Annotation annotation = new Annotation(number, new IntegerFieldValue(56));
            serializeAndAssert(annotation);
        }
        {
            Struct value = new Struct(person);
            value.setFieldValue("firstname", "Barack");
            value.setFieldValue("lastname", "Obama");
            value.setFieldValue("birthyear", 1909);
            Annotation annotation = new Annotation(personA, value);
            serializeAndAssert(annotation);
        }
    }

    /**
     * A test case taken from real use to verify the API ease of use
     */
    @Test
    public void testApi() {
        // Prepare
        AnnotationType type1 = new AnnotationType("type1", DataType.STRING);
        AnnotationType type2 = new AnnotationType("type2", DataType.INT);
        StringFieldValue output = new StringFieldValue("foo bar");
        SpanTree tree;

        // no shortcuts
        {
            SpanList root = new SpanList();
            tree = new SpanTree("SpanTree1", root);
            SpanNode node = new Span(0, 3);
            tree.annotate(node, new Annotation(type1, new StringFieldValue("text")));
            tree.annotate(node, new Annotation(type2, new IntegerFieldValue(1)));
            root.add(node);
            output.setSpanTree(tree);
        }

        // short
        {
            SpanList root = new SpanList();
            output.setSpanTree(new SpanTree("SpanTree2", root));
            SpanNode node = root.add(new Span(0, 3));
            node.annotate(type1, "text").annotate(type2, 1);
        }

        // shorter
        {
            SpanList root = output.setSpanTree(new SpanTree("SpanTree3")).spanList();
            root.span(0, 3).annotate(type1, "text").annotate(type2, 1);
        }

        // shortest
        {
            output.setSpanTree(new SpanTree("SpanTree4")).spanList().span(0, 3).annotate(type1, "text")
                  .annotate(type2, 1);
        }
    }

    private void serializeAndAssert(Annotation annotation) {
        GrowableByteBuffer buffer = new GrowableByteBuffer(1024);
        DocumentSerializer serializer = DocumentSerializerFactory.create6(buffer);
        serializer.write(annotation);
        buffer.flip();

        DocumentDeserializer deserializer = DocumentDeserializerFactory.create6(man, buffer);
        Annotation annotation2 = new Annotation();
        deserializer.read(annotation2);

        assertEquals(annotation, annotation2);
        assertNotSame(annotation, annotation2);
    }
}