aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/ItemLabelTestCase.java
blob: 17a4ce400ff94b53a5c96fd7d19dedffa67f67ee (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

import java.lang.reflect.Modifier;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import com.yahoo.search.Query;
import com.yahoo.prelude.query.textualrepresentation.Discloser;

public class ItemLabelTestCase {

    private static final class LabelCatcher implements Discloser {
        public String label = null;
        public void addProperty(String key, Object value) {
            if (key.equals("label")) {
                if (value == null) {
                    label = "null";
                } else {
                    label = (String) value;
                }
            }
        }
        public void setValue(Object value) {}
        public void addChild(Item item) {}
    }

    @Test
    final void testLabelVisibility() throws Exception {
        assertTrue(Modifier.isPublic(Item.class.getMethod("setLabel", String.class).getModifiers()));
        assertTrue(Modifier.isPublic(Item.class.getMethod("getLabel").getModifiers()));
    }

    @Test
    final void testLabelAccess() {
        Item item = new WordItem("word");
        assertFalse(item.hasUniqueID());
        assertNull(item.getLabel());
        item.setLabel("my_label");
        assertTrue(item.hasUniqueID());
        assertEquals("my_label", item.getLabel());
    }

    @Test
    final void testLabelDisclose() {
        LabelCatcher catcher = new LabelCatcher();
        Item item = new WordItem("word");
        item.disclose(catcher);
        assertNull(catcher.label);
        item.setLabel("my_other_label");
        item.disclose(catcher);
        assertEquals("my_other_label", item.getLabel());
    }

    @Test
    final void testLabelEncode() {
        Item w1 = new WordItem("w1");
        Item w2 = new WordItem("w2");
        Item w3 = new WordItem("w3");
        AndItem and = new AndItem();
        Query query = new Query();

        w1.setLabel("bar");
        w3.setLabel("foo");
        and.addItem(w1);
        and.addItem(w2);
        and.addItem(w3);
        and.setLabel("missing");
        query.getModel().getQueryTree().setRoot(and);
        query.prepare();
        assertEquals("3", query.getRanking().getProperties().get("vespa.label.foo.id").get(0));
        assertEquals("1", query.getRanking().getProperties().get("vespa.label.bar.id").get(0));

        // Conceptually, any node can have a label. However, only
        // taggable nodes are allowed to have a unique id. Taggable
        // nodes act as leaf nodes, but labels should be possible for
        // any combination of nodes in the query tree. Thus, generic
        // labeling is appropriate, but only those items that are also
        // taggable will propagate their labels to the bank-end. We
        // can live with this weakness for now, as the nodes we
        // typically need to label in the back-end are leaf-ish nodes.
        assertNull(query.getRanking().getProperties().get("vespa.label.missing.id"));
    }

}