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

import com.yahoo.document.DataType;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.DocumentPut;
import com.yahoo.document.DocumentType;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.StringFieldValue;
import org.junit.Test;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Simon Thoresen Hult
 */
public class AccessesAnnotationTestCase {

    @Test
    public void requireThatFieldsAreRestricted() {
        DocumentType type = new DocumentType("album");
        type.addField("title", DataType.STRING);
        type.addField("artist", DataType.STRING);
        type.addField("year", DataType.INT);
        Document doc = new Document(type, new DocumentId("id:map:album::1"));

        MyDocProc docProc = new MyDocProc();
        DocumentPut put = new DocumentPut(doc);
        Document proxy = new Call(docProc).configDoc(docProc, put).getDocument();
        proxy.setFieldValue("title", new StringFieldValue("foo"));
        try {
            proxy.setFieldValue("year", new IntegerFieldValue(69));
            fail("Should have failed");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            assertTrue(e.getMessage().matches(".*not allowed.*"));
        }

        proxy.getFieldValue("title");
        try {
            proxy.getFieldValue("year");
            fail("Should have failed");
        } catch (Exception e) {
            System.out.println(e.getMessage());
            assertTrue(e.getMessage().matches(".*not allowed.*"));
        }
    }

    @Accesses({
            @Accesses.Field(name = "title", dataType = "string", description = "What is done on field title",
                   annotations = @Accesses.Field.Tree(produces = { "sentences" })),
            @Accesses.Field(name = "artist", dataType = "string", description = "What is done on field artist",
                   annotations = {
                           @Accesses.Field.Tree(name = "root", produces = { "sentences" }),
                           @Accesses.Field.Tree(name = "root2", consumes = { "places" })
                   }),
            @Accesses.Field(name = "track", dataType = "string", description = "What is done on field track")
    })
    class MyDocProc extends DocumentProcessor {

        @Override
        public Progress process(Processing processing) {
            return null;
        }
    }
}