summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/grouping/GroupingValidatorTestCase.java
blob: 055ea5929dba4afc28b10355f0b3d62873081ee9 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping;

import com.yahoo.vespa.config.search.AttributesConfig;
import com.yahoo.container.QrSearchersConfig;
import com.yahoo.search.Query;
import com.yahoo.search.config.ClusterConfig;
import com.yahoo.search.grouping.request.GroupingOperation;
import com.yahoo.search.searchchain.Execution;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Arrays;
import java.util.Collection;

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

    @Rule
    public ExpectedException thrown = ExpectedException.none();

    @Test
    public void requireThatAvailableAttributesDoNotThrow() {
        validateGrouping("myCluster", Arrays.asList("foo", "bar"),
                "all(group(foo) each(output(max(bar))))");;
    }

    @Test
    public void requireThatUnavailableAttributesThrow() {
        thrown.expect(UnavailableAttributeException.class);
        thrown.expectMessage(createMessage("myCluster", "bar"));
        validateGrouping("myCluster", Arrays.asList("foo"),
                "all(group(foo) each(output(max(bar))))");
    }

    @Test
    public void requireThatEnableFlagPreventsThrow() {
        Query query = createQuery("all(group(foo) each(output(max(bar))))");
        query.properties().set(GroupingValidator.PARAM_ENABLED, "false");
        validateGrouping("myCluster", Arrays.asList("foo"), query);
    }

    @Test
    public void available_primitive_map_attribute_does_not_throw() {
        validateGrouping("myCluster", Arrays.asList("map.key", "map.value"),
                "all(group(map{\"foo\"}) each(output(count())))");
    }

    @Test
    public void unavailable_primitive_map_key_attribute_throws() {
        thrown.expect(UnavailableAttributeException.class);
        thrown.expectMessage(createMessage("myCluster", "map.key"));
        validateGrouping("myCluster", Arrays.asList("null"),
                "all(group(map{\"foo\"}) each(output(count())))");
    }

    @Test
    public void unavailable_primitive_map_value_attribute_throws() {
        thrown.expect(UnavailableAttributeException.class);
        thrown.expectMessage(createMessage("myCluster", "map.value"));
        validateGrouping("myCluster", Arrays.asList("map.key"),
                "all(group(map{\"foo\"}) each(output(count())))");
    }

    @Test
    public void available_struct_map_attribute_does_not_throw() {
        validateGrouping("myCluster", Arrays.asList("map.key", "map.value.name"),
                "all(group(map{\"foo\"}.name) each(output(count())))");
    }

    @Test
    public void unavailable_struct_map_key_attribute_throws() {
        thrown.expect(UnavailableAttributeException.class);
        thrown.expectMessage(createMessage("myCluster", "map.key"));
        validateGrouping("myCluster", Arrays.asList("null"),
                "all(group(map{\"foo\"}.name) each(output(count())))");
    }

    @Test
    public void unavailable_struct_map_value_attribute_throws() {
        thrown.expect(UnavailableAttributeException.class);
        thrown.expectMessage(createMessage("myCluster", "map.value.name"));
        validateGrouping("myCluster", Arrays.asList("map.key"),
                "all(group(map{\"foo\"}.name) each(output(count())))");
    }

    private static String createMessage(String clusterName, String attributeName) {
        return "Grouping request references attribute '" + attributeName + "' which is not available in cluster '" + clusterName + "'.";
    }

    private static Query createQuery(String groupingExpression) {
        Query query = new Query();
        GroupingRequest req = GroupingRequest.newInstance(query);
        req.setRootOperation(GroupingOperation.fromString(groupingExpression));
        return query;
    }

    private static void validateGrouping(String clusterName, Collection<String> attributeNames, String groupingExpression) {
        validateGrouping(clusterName, attributeNames, createQuery(groupingExpression));
    }

    private static void validateGrouping(String clusterName, Collection<String> attributeNames, Query query) {
        QrSearchersConfig.Builder qrsConfig = new QrSearchersConfig.Builder().searchcluster(
                new QrSearchersConfig.Searchcluster.Builder()
                        .indexingmode(QrSearchersConfig.Searchcluster.Indexingmode.Enum.REALTIME)
                        .name(clusterName));
        ClusterConfig.Builder clusterConfig = new ClusterConfig.Builder().
                clusterId(0).
                clusterName("test");
        AttributesConfig.Builder attributesConfig = new AttributesConfig.Builder();
        for (String attributeName : attributeNames) {
            attributesConfig.attribute(new AttributesConfig.Attribute.Builder()
                                            .name(attributeName));
        }
        new Execution(
                new GroupingValidator(new QrSearchersConfig(qrsConfig),
                                      new ClusterConfig(clusterConfig),
                                      new AttributesConfig(attributesConfig)),
                Execution.Context.createContextStub()).search(query);
    }
}