aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/searcher/test/JSONDebugSearcherTestCase.java
blob: a10ecfb99e08e21c73b128710e09af37dad16d1c (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 Yahoo. 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.component.chain.Chain;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.hitfield.JSONString;
import com.yahoo.prelude.searcher.JSONDebugSearcher;
import com.yahoo.processing.execution.Execution.Trace;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.result.Relevance;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.search.searchchain.testutil.DocumentSourceSearcher;
import com.yahoo.yolean.trace.TraceNode;
import com.yahoo.yolean.trace.TraceVisitor;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Visit the trace and check JSON payload is stored there when requested.
 *
 * @author Steinar Knutsen
 */
public class JSONDebugSearcherTestCase {

    private static final String NODUMPJSON = "?query=1&tracelevel=6";
    private static final String DUMPJSON = "?query=1&dumpjson=jsonfield&tracelevel=6";

    @Test
    void test() {
        Chain<Searcher> searchChain = makeSearchChain("{1: 2}", new JSONDebugSearcher());
        Execution e = new Execution(searchChain, Execution.Context.createContextStub());
        e.search(new Query(NODUMPJSON));
        Trace t = e.trace();
        LookForJson visitor = new LookForJson();
        t.accept(visitor);
        assertFalse(visitor.gotJson);
        e = new Execution(searchChain, Execution.Context.createContextStub());
        e.search(new Query(DUMPJSON));
        t = e.trace();
        t.accept(visitor);
        assertTrue(visitor.gotJson);
    }

    private Chain<Searcher> makeSearchChain(String content, Searcher dumper) {
        DocumentSourceSearcher docsource = new DocumentSourceSearcher();
        addResult(new Query(DUMPJSON), content, docsource);
        addResult(new Query(NODUMPJSON), content, docsource);
        return new Chain<>(dumper, docsource);
    }

    private void addResult(Query q, String content, DocumentSourceSearcher docsource) {
        Result r = new Result(q);
        FastHit hit = new FastHit();
        hit.setId("http://abc.html");
        hit.setRelevance(new Relevance(1));
        hit.setField("jsonfield", new JSONString(content));
        r.hits().add(hit);
        docsource.addResult(q, r);
    }

    private static class LookForJson extends TraceVisitor {

        private static final String JSON_PAYLOAD = "{1: 2}";
        public boolean gotJson = false;

        @Override
        public void visit(TraceNode node) {
            if (node.payload() == null || node.payload().getClass() != String.class) {
                return;
            }
            if (node.payload().toString().equals(JSONDebugSearcher.JSON_FIELD + JSON_PAYLOAD)) {
                gotJson = true;
            }
        }
    }

}