summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/searcher/test/ValidatePredicateSearcherTestCase.java
blob: 3c3c6c921e37f95a35cd621b3fab963a559edcb5 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.searcher.test;

import com.yahoo.language.Linguistics;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.prelude.Index;
import com.yahoo.prelude.IndexFacts;
import com.yahoo.prelude.IndexModel;
import com.yahoo.prelude.SearchDefinition;
import com.yahoo.prelude.searcher.ValidatePredicateSearcher;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.query.QueryTree;
import com.yahoo.search.query.parser.Parsable;
import com.yahoo.search.query.parser.ParserEnvironment;
import com.yahoo.search.rendering.RendererRegistry;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.yql.YqlParser;
import org.junit.Test;

import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * @author <a href="mailto:magnarn@yahoo-inc.com">Magnar Nedland</a>
 */
public class ValidatePredicateSearcherTestCase {

    @Test
    public void testValidQuery() {
        ValidatePredicateSearcher searcher = new ValidatePredicateSearcher();
        String q = "select * from sources * where predicate(predicate_field,0,{\"age\":20L});";
        Result r = doSearch(searcher, q, "predicate-bounds [0..99]");
        assertNull(r.hits().getError());
    }

    @Test
    public void testQueryOutOfBounds() {
        ValidatePredicateSearcher searcher = new ValidatePredicateSearcher();
        String q = "select * from sources * where predicate(predicate_field,0,{\"age\":200L});";
        Result r = doSearch(searcher, q, "predicate-bounds [0..99]");
        assertEquals(ErrorMessage.createIllegalQuery("age=200 outside configured predicate bounds."), r.hits().getError());
    }

    private static Result doSearch(ValidatePredicateSearcher searcher, String yqlQuery, String command) {
        QueryTree queryTree = new YqlParser(new ParserEnvironment()).parse(new Parsable().setQuery(yqlQuery));
        Query query = new Query();
        query.getModel().getQueryTree().setRoot(queryTree.getRoot());

        TreeMap<String, List<String>> masterClusters = new TreeMap<>();
        masterClusters.put("cluster", Arrays.asList("document"));
        SearchDefinition searchDefinition = new SearchDefinition("document");
        Index index = new Index("predicate_field");
        index.addCommand(command);
        searchDefinition.addIndex(index);
        Map<String, SearchDefinition> searchDefinitionMap = new HashMap<>();
        searchDefinitionMap.put("document", searchDefinition);
        IndexFacts indexFacts = new IndexFacts(new IndexModel(masterClusters, searchDefinitionMap, searchDefinition));
        Execution.Context context = new Execution.Context(null, indexFacts, null, new RendererRegistry(), new SimpleLinguistics());
        return new Execution(searcher, context).search(query);
    }

}