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
|
// 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.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.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
/**
* Visit the trace and check JSON payload is stored there when requested.
*
* @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
*/
public class JSONDebugSearcherTestCase {
private static final String NODUMPJSON = "?query=1&tracelevel=6";
private static final String DUMPJSON = "?query=1&dumpjson=jsonfield&tracelevel=6";
private Chain<Searcher> searchChain;
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;
}
}
}
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);
}
@Before
public void setUp() throws Exception {
searchChain = makeSearchChain("{1: 2}", new JSONDebugSearcher());
}
@After
public void tearDown() throws Exception {
}
@Test
public final void test() {
Execution e = new Execution(searchChain, Execution.Context.createContextStub());
e.search(new Query(NODUMPJSON));
Trace t = e.trace();
LookForJson visitor = new LookForJson();
t.accept(visitor);
assertEquals(false, visitor.gotJson);
e = new Execution(searchChain, Execution.Context.createContextStub());
e.search(new Query(DUMPJSON));
t = e.trace();
t.accept(visitor);
assertEquals(true, visitor.gotJson);
}
}
|