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

import com.google.common.util.concurrent.MoreExecutors;
import com.yahoo.component.chain.Chain;
import com.yahoo.language.simple.SimpleLinguistics;
import com.yahoo.search.rendering.RendererRegistry;
import com.yahoo.search.result.Hit;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.testutil.DocumentSourceSearcher;
import com.yahoo.prelude.searcher.QueryValidatingSearcher;
import org.junit.Test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
 * Tests correct denial of query.
 *
 * @author Steinar Knutsen
 */
public class QueryValidatingSearcherTestCase {

    @Test
    public void testBasic() {
        // Setup
        Map<Searcher, Searcher> chained = new HashMap<>();
        Query query = new Query("?query=test");

        Result result = new Result(query);
        result.hits().add(new Hit("ymail://1111111111/AQAAAP7JgwEAj6XjQQAAAO/+ggA=",950));

        Searcher validator = new QueryValidatingSearcher();
        DocumentSourceSearcher source = new DocumentSourceSearcher();
        chained.put(validator, source);
        source.addResult(query, result);

        // Exercise
        Result returnedResult = doSearch(validator, query, 0, 10, chained);

        // Validate
        assertEquals(1, returnedResult.getHitCount());
        assertNull(returnedResult.hits().getError());

        returnedResult = doSearch(validator, query, 0, 1001, chained);
        assertEquals(0, returnedResult.getConcreteHitCount());
        assertEquals(4, returnedResult.hits().getError().getCode());

        returnedResult = doSearch(validator, query, 1001, 10, chained);
        assertEquals(0, returnedResult.getConcreteHitCount());
        assertEquals(4, returnedResult.hits().getError().getCode());
    }

    private Result doSearch(Searcher searcher, Query query, int offset, int hits, Map<Searcher, Searcher> chained) {
        query.setOffset(offset);
        query.setHits(hits);
        return createExecution(searcher, chained).search(query);
    }

    private Chain<Searcher> chainedAsSearchChain(Searcher topOfChain, Map<Searcher, Searcher> chained) {
        List<Searcher> searchers = new ArrayList<>();
        for (Searcher current = topOfChain; current != null; current = chained.get(current)) {
            searchers.add(current);
        }
        return new Chain<>(searchers);
    }

    private Execution createExecution(Searcher searcher, Map<Searcher, Searcher> chained) {
        Execution.Context context = new Execution.Context(null, null, null, new RendererRegistry(MoreExecutors.directExecutor()), new SimpleLinguistics());
        return new Execution(chainedAsSearchChain(searcher, chained), context);
    }

}