aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/storage/searcher/VisitorSearcherTestCase.java
blob: e49d2ca4db3bb7002b2bf156203ce4bda2170e64 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.storage.searcher;

import com.yahoo.component.chain.Chain;
import com.yahoo.cloud.config.ClusterListConfig;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.container.protect.Error;
import com.yahoo.documentapi.VisitorSession;
import com.yahoo.feedhandler.NullFeedMetric;
import com.yahoo.vespa.config.content.LoadTypeConfig;
import com.yahoo.document.DataType;
import com.yahoo.document.DocumentType;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.documentapi.VisitorParameters;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.feedapi.FeedContext;
import com.yahoo.feedapi.MessagePropertyProcessor;
import com.yahoo.messagebus.StaticThrottlePolicy;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.vdslib.VisitorOrdering;
import com.yahoo.vespaclient.ClusterList;
import com.yahoo.vespaclient.config.FeederConfig;

import org.junit.Test;

import java.util.Arrays;

import static org.junit.Assert.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@SuppressWarnings("deprecation")
public class VisitorSearcherTestCase {

    private DocumentTypeManager docMan = null;
    private DocumentType docType;
    DocumentSessionFactory factory;

    @org.junit.Before
    public void setUp() {
        docMan = new DocumentTypeManager();
        docType = new DocumentType("kittens");
        docType.addHeaderField("name", DataType.STRING);
        docType.addField("description", DataType.STRING);
        docType.addField("image", DataType.RAW);
        docType.addField("fluffiness", DataType.INT);
        docType.addField("foo", DataType.RAW);
        docMan.registerDocumentType(docType);
        factory = new DocumentSessionFactory(docType);
    }

    public VisitSearcher create() throws Exception {
        ClusterListConfig.Storage.Builder storageCluster = new ClusterListConfig.Storage.Builder().configid("storage/cluster.foobar").name("foobar");
        ClusterListConfig clusterListCfg = new ClusterListConfig(new ClusterListConfig.Builder().storage(storageCluster));
        ClusterList clusterList = new ClusterList(clusterListCfg);
        return new VisitSearcher(new FeedContext(
                new MessagePropertyProcessor(new FeederConfig(new FeederConfig.Builder().timeout(458).route("riksveg18").retryenabled(true)),
                        new LoadTypeConfig(new LoadTypeConfig.Builder())),
                factory, docMan, clusterList, new NullFeedMetric(true)));
    }

    @Test
    public void testQueryParameters() throws Exception {
        VisitSearcher searcher = create();
        VisitorParameters params = searcher.getVisitorParameters(
                newQuery("visit?visit.selection=id.user=1234&visit.cluster=foobar" +
                         "&visit.dataHandler=othercluster&visit.fieldSet=[header]&visit.fromTimestamp=112&visit.toTimestamp=224" +
                         "&visit.maxBucketsPerVisitor=2&visit.maxPendingMessagesPerVisitor=7&visit.maxPendingVisitors=14" +
                         "&visit.ordering=ASCENDING&priority=NORMAL_1&tracelevel=7&visit.visitInconsistentBuckets&visit.visitRemoves"), null);

        assertEquals("id.user=1234", params.getDocumentSelection());
        assertEquals(7, params.getMaxPending());
        assertEquals(2, params.getMaxBucketsPerVisitor());
        assertEquals(14, ((StaticThrottlePolicy)params.getThrottlePolicy()).getMaxPendingCount());
        assertEquals("[Storage:cluster=foobar;clusterconfigid=storage/cluster.foobar]", params.getRoute().toString());
        assertEquals("othercluster", params.getRemoteDataHandler());
        assertEquals("[header]", params.fieldSet());
        assertEquals(112, params.getFromTimestamp());
        assertEquals(224, params.getToTimestamp());
        assertEquals(VisitorOrdering.ASCENDING, params.getVisitorOrdering());
        assertEquals(DocumentProtocol.Priority.NORMAL_1, params.getPriority());
        assertEquals(7, params.getTraceLevel());
        assertEquals(true, params.visitInconsistentBuckets());
        assertEquals(true, params.visitRemoves());
    }

    @Test
    public void timestampQueryParametersAreParsedAsLongs() throws Exception {
        VisitorParameters params = create().getVisitorParameters(
                newQuery("visit?visit.selection=id.user=1234&" +
                         "visit.fromTimestamp=1419021596000000&" +
                         "visit.toTimestamp=1419021597000000"), null);
        assertEquals(1419021596000000L, params.getFromTimestamp());
        assertEquals(1419021597000000L, params.getToTimestamp());
    }

    @Test
    public void testQueryParametersDefaults() throws Exception {
        VisitSearcher searcher = create();
        VisitorParameters params = searcher.getVisitorParameters(
                newQuery("visit?visit.selection=id.user=1234&hits=100"), null);

        assertEquals("id.user=1234", params.getDocumentSelection());
        assertEquals(1, params.getMaxBucketsPerVisitor());
        assertEquals(1, ((StaticThrottlePolicy)params.getThrottlePolicy()).getMaxPendingCount());
        assertEquals(1, params.getMaxFirstPassHits());
        assertEquals(1, params.getMaxTotalHits());
        assertEquals(32, params.getMaxPending());
        assertEquals(false, params.visitInconsistentBuckets());
    }

    @Test
    public void testWrongCluster() throws Exception {
        VisitSearcher searcher = create();

        try {
            searcher.getVisitorParameters(
                    newQuery("visit?visit.selection=id.user=1234&visit.cluster=unknown"), null);

            assertTrue(false);
        } catch (Exception e) {
            // e.printStackTrace();
        }
    }


    @Test(expected = IllegalArgumentException.class)
    public void testNoClusterParamWhenSeveralClusters() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        ClusterListConfig.Storage.Builder storageCluster1 = new ClusterListConfig.Storage.Builder().configid("storage/cluster.foo").name("foo");
        ClusterListConfig.Storage.Builder storageCluster2 = new ClusterListConfig.Storage.Builder().configid("storage/cluster.bar").name("bar");
        ClusterListConfig clusterListCfg = new ClusterListConfig(new ClusterListConfig.Builder().storage(Arrays.asList(storageCluster1, storageCluster2)));
        ClusterList clusterList = new ClusterList(clusterListCfg);
        VisitSearcher searcher = new VisitSearcher(new FeedContext(
                new MessagePropertyProcessor(new FeederConfig(new FeederConfig.Builder().timeout(100).route("whatever").retryenabled(true)),
                        new LoadTypeConfig(new LoadTypeConfig.Builder())),
                factory, docMan, clusterList, new NullFeedMetric(true)));

            searcher.getVisitorParameters(newQuery("visit?visit.selection=id.user=1234"), null);
    }

    @Test
    public void testSimple() throws Exception {
        Chain<Searcher> searchChain = new Chain<>(create());
        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("visit?visit.selection=id.user=1234&hits=100"));
        assertEquals(1, result.hits().size());
        assertRendered(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<document documenttype=\"kittens\" documentid=\"userdoc:foo:1234:bar\"/>\n" +
                "</result>\n", result);
    }

    private Result invokeVisitRemovesSearchChain() throws Exception {
        Chain<Searcher> searchChain = new Chain<>(create());
        return new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("visit?visit.selection=id.user=1234&hits=100&visit.visitRemoves=true"));
    }

    @Test
    public void visitRemovesIncludesRemoveEntriesInResultXml() throws Exception {
        Result result = invokeVisitRemovesSearchChain();
        assertEquals(2, result.hits().size());
        assertRendered(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<document documenttype=\"kittens\" documentid=\"userdoc:foo:1234:bar\"/>\n" +
                "<remove documentid=\"userdoc:foo:1234:removed\"/>\n" +
                "</result>\n", result);
    }

    @Test
    public void removedDocumentIdsAreXmlEscaped() throws Exception {
        factory = mock(DocumentSessionFactory.class);
        when(factory.createVisitorSession(any(VisitorParameters.class))).thenAnswer((p) -> {
            VisitorParameters params = (VisitorParameters)p.getArguments()[0];
            DummyVisitorSession session = new DummyVisitorSession(params, docType);
            session.clearAutoReplyMessages();
            session.addRemoveReply("userdoc:foo:1234:<rem\"o\"ved&stuff>");
            return session;
        });
        Result result = invokeVisitRemovesSearchChain();
        assertEquals(1, result.hits().size());
        assertRendered(
                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<remove documentid=\"userdoc:foo:1234:&lt;rem&quot;o&quot;ved&amp;stuff&gt;\"/>\n" +
                "</result>\n", result);
    }

    private Result invokeSearcherWithUserQuery() throws Exception {
        Chain<Searcher> searchChain = new Chain<>(create());
        return new Execution(searchChain, Execution.Context.createContextStub())
                .search(new Query("visit?visit.selection=id.user=1234&hits=100"));
    }

    @Test
    public void waitUntilDoneFailureReturnsTimeoutErrorHit() throws Exception {
        VisitorSession session = mock(VisitorSession.class);
        when(session.waitUntilDone(anyLong())).thenReturn(false);
        factory = mock(DocumentSessionFactory.class);
        when(factory.createVisitorSession(any(VisitorParameters.class))).thenReturn(session);

        Result result = invokeSearcherWithUserQuery();
        assertNotNull(result.hits().getErrorHit());
        assertEquals(Error.TIMEOUT.code, result.hits().getErrorHit().errors().iterator().next().getCode());
    }

    @Test
    @SuppressWarnings("deprecation")
    public void testRendererWiring() throws Exception {
        Chain<Searcher> searchChain = new Chain<>(create());
        {
            Query query = newQuery("visit?visit.selection=id.user=1234&hits=100&format=json");
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);
            assertEquals(com.yahoo.prelude.templates.DefaultTemplateSet.class, result.getTemplating().getTemplates().getClass());
        }
        {
            Query query = newQuery("visit?visit.selection=id.user=1234&hits=100&format=JsonRenderer");
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);
            assertEquals(com.yahoo.prelude.templates.DefaultTemplateSet.class, result.getTemplating().getTemplates().getClass());
        }
        {
            Query query = newQuery("visit?visit.selection=id.user=1234&hits=100");
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);
            assertEquals(DocumentXMLTemplate.class, result.getTemplating().getTemplates().getClass());
        }
    }

    public static void assertRendered(String expected, Result result) throws Exception {
        assertEquals(expected, ResultRenderingUtil.getRendered(result));
    }

    private Query newQuery(String queryString) {
        return new Query(HttpRequest.createTestRequest(queryString, com.yahoo.jdisc.http.HttpRequest.Method.GET));
    }

}