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

import com.yahoo.component.ComponentId;
import com.yahoo.component.chain.Chain;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.federation.FederationSearcher;
import com.yahoo.search.federation.sourceref.SearchChainResolver;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.SearchChainRegistry;
import com.yahoo.search.searchchain.model.federation.FederationOptions;

import java.util.Collections;

/**
* @author tonytv
*/
class FederationTester {

    SearchChainResolver.Builder builder = new SearchChainResolver.Builder();
    SearchChainRegistry registry = new SearchChainRegistry();

    Execution execution;

    void addSearchChain(String id, Searcher... searchers) {
        addSearchChain(id, federationOptions(), searchers);
    }

    void addSearchChain(String id, FederationOptions federationOptions, Searcher... searchers) {
        ComponentId searchChainId = ComponentId.fromString(id);

        builder.addSearchChain(searchChainId, federationOptions, Collections.<String>emptyList());

        Chain<Searcher> chain = new Chain<>(searchChainId, searchers);
        registry.register(chain);
    }

    public void addOptionalSearchChain(String id, Searcher... searchers) {
        addSearchChain(id, federationOptions().setOptional(true), searchers);
    }

    private FederationOptions federationOptions() {
        int preventTimeout = 24 * 60 * 60 * 1000;
        return new FederationOptions().setUseByDefault(true).setTimeoutInMilliseconds(preventTimeout);
    }

    FederationSearcher buildFederationSearcher() {
        return new FederationSearcher(ComponentId.fromString("federation"), builder.build());
    }

    public Result search() {
        return search(new Query());
    }

    public Result search(Query query) {
        execution = createExecution();
        return execution.search(query);
    }

    public Result searchAndFill() {
        Result result = search();
        fill(result);
        return result;
    }

    private Execution createExecution() {
        registry.freeze();
        return new Execution(new Chain<Searcher>(buildFederationSearcher()), Execution.Context.createContextStub(registry, null));
    }

    public void fill(Result result) {
        execution.fill(result, "default");
    }
}