aboutsummaryrefslogtreecommitdiffstats
path: root/vespaclient-container-plugin/src/test/java/com/yahoo/storage/searcher/GetSearcherTestCase.java
blob: 9d6c8c2feac30f0141fc6b568b111c09fa743f63 (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
// 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.container.jdisc.HttpRequest;
import com.yahoo.feedhandler.NullFeedMetric;
import com.yahoo.jdisc.HeaderFields;
import com.yahoo.vespa.config.content.LoadTypeConfig;
import com.yahoo.document.*;
import com.yahoo.document.datatypes.IntegerFieldValue;
import com.yahoo.document.datatypes.Raw;
import com.yahoo.document.datatypes.StringFieldValue;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.documentapi.messagebus.protocol.GetDocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.GetDocumentReply;
import com.yahoo.feedapi.FeedContext;
import com.yahoo.feedapi.MessagePropertyProcessor;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.routing.Route;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.vespaclient.ClusterList;
import com.yahoo.vespaclient.config.FeederConfig;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.zip.GZIPOutputStream;

import static org.junit.Assert.*;

@SuppressWarnings("deprecation")
public class GetSearcherTestCase {

    private DocumentTypeManager docMan = null;
    private DocumentType docType;
    private FeederConfig defFeedCfg = new FeederConfig(new FeederConfig.Builder());
    private LoadTypeConfig defLoadTypeCfg = new LoadTypeConfig(new LoadTypeConfig.Builder());

    @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.STRING);
        docType.addField("fluffiness", DataType.INT);
        docType.addField("foo", DataType.RAW);
        docMan.registerDocumentType(docType);
    }

    @org.junit.After
    public void tearDown() {
        docMan = null;
        docType = null;
    }

    private void assertHits(HitGroup hits, String... wantedHits) {
        assertEquals(wantedHits.length, hits.size());
        for (int i = 0; i < wantedHits.length; ++i) {
            assertTrue(hits.get(i) instanceof DocumentHit);
            DocumentHit hit = (DocumentHit)hits.get(i);
            assertEquals(wantedHits[i], hit.getDocument().getId().toString());
        }
    }

    @Test
    public void testGetSingleDocumentQuery() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType); // Needs auto-reply
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?id=userdoc:kittens:1:2"));
        System.out.println("HTTP request is " + result.getQuery().getHttpRequest());

        assertEquals(1, factory.messages.size());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:1:2", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
        }
        assertEquals(1, result.hits().size());
        assertHits(result.hits(), "userdoc:kittens:1:2");
        // By default, document hit should not have its hit fields set
        DocumentHit hit = (DocumentHit)result.hits().get(0);
        assertEquals(0, hit.fieldKeys().size());
    }

    @Test
    public void testGetMultipleDocumentsQuery() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Query query = newQuery("?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4");
        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);

        assertEquals(2, factory.messages.size());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:1:2", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
        }

        {
            Message m = factory.messages.get(1);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:3:4", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
        }
        assertEquals(2, result.hits().size());
        assertNull(result.hits().getErrorHit());
        assertHits(result.hits(), "userdoc:kittens:1:2", "userdoc:kittens:3:4");
        assertEquals(2, query.getHits());
    }

    // Test that you can use both query string and POSTed IDs
    @Test
    public void testGetMultipleDocumentsQueryAndPOST() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        String data = "userdoc:kittens:5:6\nuserdoc:kittens:7:8\nuserdoc:kittens:9:10";
        MockHttpRequest request = new MockHttpRequest(data.getBytes("utf-8"), "/get/?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4");
        Query query = new Query(request.toRequest());
        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);

        assertEquals(5, factory.messages.size());
        assertEquals(5, result.hits().size());
        assertNull(result.hits().getErrorHit());
        assertHits(result.hits(), "userdoc:kittens:1:2", "userdoc:kittens:3:4",
                "userdoc:kittens:5:6", "userdoc:kittens:7:8", "userdoc:kittens:9:10");
    }

    @Test
    public void testGetMultipleDocumentsQueryAndGZippedPOST() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        String data = "userdoc:kittens:5:6\nuserdoc:kittens:7:8\nuserdoc:kittens:9:10";

        // Create with automatic GZIP encoding
        MockHttpRequest request = new MockHttpRequest(data.getBytes("utf-8"), "/get/?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4", true);
        Query query = new Query(request.toRequest());
        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);

        assertEquals(5, factory.messages.size());
        assertEquals(5, result.hits().size());
        assertNull(result.hits().getErrorHit());
        assertHits(result.hits(), "userdoc:kittens:1:2", "userdoc:kittens:3:4",
                "userdoc:kittens:5:6", "userdoc:kittens:7:8", "userdoc:kittens:9:10");
    }

    /* Test that a query without any ids is passed through to the next chain */
    @Test
    public void testQueryPassThrough() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        HitGroup hits = new HitGroup("mock");
        hits.add(new Hit("blernsball"));
        Chain<Searcher> searchChain = new Chain<>(searcher, new MockBackend(hits));

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?flarn=blern"));

        assertEquals(0, factory.messages.size());
        assertEquals(1, result.hits().size());
        assertNotNull(result.hits().get("blernsball"));
    }

    /* Test that a query will contain both document hits and hits from a searcher
     * further down the chain, iff the searcher returns a DocumentHit.
     */
    @Test
    public void testQueryPassThroughAndGet() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:1234:foo"));
        doc1.setFieldValue("name", new StringFieldValue("megacat"));
        doc1.setFieldValue("description", new StringFieldValue("supercat"));
        doc1.setFieldValue("fluffiness", new IntegerFieldValue(10000));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1)
        };

        DocumentSessionFactory factory = new DocumentSessionFactory(docType, null, false, replies);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        DocumentHit backendHit = new DocumentHit(new Document(docType, new DocumentId("userdoc:kittens:5678:bar")), 5);
        Chain<Searcher> searchChain = new Chain<>(searcher, new MockBackend(backendHit));

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?query=flarn&id=userdoc:kittens:1234:foo"));

        assertEquals(1, factory.messages.size());
        assertEquals(2, result.hits().size());
        assertNotNull(result.hits().get("userdoc:kittens:5678:bar"));
        assertNotNull(result.hits().get("userdoc:kittens:1234:foo"));
    }

    @Test
    public void testQueryPassThroughAndGetUnknownBackendHit() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        HitGroup hits = new HitGroup("mock");
        hits.add(new Hit("blernsball"));
        Chain<Searcher> searchChain = new Chain<>(searcher, new MockBackend(hits));

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?flarn=blern&id=userdoc:kittens:9:aaa"));

        assertEquals(0, factory.messages.size());
        assertNotNull(result.hits().getErrorHit());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"18\" message=\"Internal server error.: " +
                "A backend searcher to com.yahoo.storage.searcher.GetSearcher returned a " +
                "hit that was not an instance of com.yahoo.storage.searcher.DocumentHit. " +
                "Only DocumentHit instances are supported in the backend hit result set " +
                "when doing queries that contain document identifier sets recognised by the " +
                "Get Searcher.\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testConfig() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(new FeederConfig(new FeederConfig.Builder().timeout(58).route("route66").retryenabled(false)), defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?id=doc:batman:dahnahnahnah"));

        assertEquals(1, factory.messages.size());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("doc:batman:dahnahnahnah", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(Route.parse("route66"), gdm.getRoute());
            assertFalse(gdm.getRetryEnabled());
            assertTrue(58000 >= gdm.getTimeRemaining());
        }
    }

    @Test
    public void testConfigChanges() throws Exception {
        String config = "raw:timeout 37\nroute \"riksveg18\"\nretryenabled true";
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(new FeederConfig(new FeederConfig.Builder().timeout(58).route("riksveg18").retryenabled(true)),
                            defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?id=doc:batman:dahnahnahnah"));

        assertEquals(1, factory.messages.size());
        assertEquals(1, factory.getSessionsCreated());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("doc:batman:dahnahnahnah", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(Route.parse("riksveg18"), gdm.getRoute());
            assertTrue(gdm.getRetryEnabled());
            assertTrue(58000 >= gdm.getTimeRemaining());
        }

        factory.messages.clear();

        FeederConfig newConfig = new FeederConfig(new FeederConfig.Builder()
                .timeout(123)
                .route("e6")
                .retryenabled(false)
        );
        searcher.getMessagePropertyProcessor().configure(newConfig, defLoadTypeCfg);

        new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=doc:spiderman:does_whatever_a_spider_can"));

        // riksveg18 is created again, and e6 is created as well.
        assertEquals(3, factory.getSessionsCreated());

        assertEquals(1, factory.messages.size());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("doc:spiderman:does_whatever_a_spider_can", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(Route.parse("e6"), gdm.getRoute());
            assertFalse(gdm.getRetryEnabled());
            assertTrue(123000 >= gdm.getTimeRemaining());
        }
    }

    @Test
    public void testQueryOverridesDefaults() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4&priority=LOW_2&route=highwaytohell&timeout=58"));

        assertEquals(2, factory.messages.size());
        long lastTimeout = 58000;
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:1:2", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(DocumentProtocol.Priority.LOW_2, gdm.getPriority());
            assertEquals(Route.parse("highwaytohell"), gdm.getRoute());
            assertTrue(lastTimeout >= gdm.getTimeRemaining());
            lastTimeout = gdm.getTimeRemaining();
        }

        {
            Message m = factory.messages.get(1);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:3:4", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(DocumentProtocol.Priority.LOW_2, gdm.getPriority());
            assertEquals(Route.parse("highwaytohell"), gdm.getRoute());
            assertTrue(lastTimeout >= gdm.getTimeRemaining());
        }
    }

    @Test
    public void testQueryOverridesConfig() throws Exception {
        String config = "raw:timeout 458\nroute \"route66\"";
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4&priority=LOW_2&route=highwaytohell&timeout=123"));

        long lastTimeout = 123000;
        assertEquals(2, factory.messages.size());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:1:2", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(DocumentProtocol.Priority.LOW_2, gdm.getPriority());
            assertEquals(Route.parse("highwaytohell"), gdm.getRoute());
            assertTrue(lastTimeout >= gdm.getTimeRemaining());
            lastTimeout = gdm.getTimeRemaining();
        }

        {
            Message m = factory.messages.get(1);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:3:4", d.toString());
            assertEquals("[all]", gdm.getFieldSet());
            assertEquals(DocumentProtocol.Priority.LOW_2, gdm.getPriority());
            assertEquals(Route.parse("highwaytohell"), gdm.getRoute());
            assertTrue(lastTimeout >= gdm.getTimeRemaining());
        }
    }

    @Test
    public void testBadPriorityValue() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:1:2&priority=onkel_jubalon"));

        assertNotNull(result.hits().getErrorHit());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"3\" message=\"Illegal query: " +
                "java.lang.IllegalArgumentException: No enum const" +
                "ant " +
                "com.yahoo.documentapi.messagebus.protocol.DocumentProtocol" +
                "."  +
                "Priority.onkel_jubalon\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testMultiIdBadArrayIndex() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        {
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                    newQuery("?id[1]=userdoc:kittens:1:2"));

            assertNotNull(result.hits().getErrorHit());

            assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<result>\n" +
                    "<errors>\n" +
                    "<error type=\"searcher\" code=\"3\" message=\"Illegal query: " +
                    "java.lang.IllegalArgumentException: query contains document ID " +
                    "array that is not zero-based and/or linearly increasing\"/>\n" +
                    "</errors>\n" +
                    "</result>\n", result);
        }

        {
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                    newQuery("?id[0]=userdoc:kittens:1:2&id[2]=userdoc:kittens:2:3"));

            assertNotNull(result.hits().getErrorHit());

            assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<result>\n" +
                    "<errors>\n" +
                    "<error type=\"searcher\" code=\"3\" message=\"Illegal query: " +
                    "java.lang.IllegalArgumentException: query contains document ID " +
                    "array that is not zero-based and/or linearly increasing\"/>\n" +
                    "</errors>\n" +
                    "</result>\n", result);
        }

        {
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                    newQuery("?id[1]=userdoc:kittens:2:3"));

            assertNotNull(result.hits().getErrorHit());

            assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<result>\n" +
                    "<errors>\n" +
                    "<error type=\"searcher\" code=\"3\" message=\"Illegal query: " +
                    "java.lang.IllegalArgumentException: query contains document ID " +
                    "array that is not zero-based and/or linearly increasing\"/>\n" +
                    "</errors>\n" +
                    "</result>\n", result);
        }

        {
            Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                    newQuery("?id[0=userdoc:kittens:1:2"));

            assertNotNull(result.hits().getErrorHit());

            assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                    "<result>\n" +
                    "<errors>\n" +
                    "<error type=\"searcher\" code=\"3\" message=\"Illegal query: " +
                    "java.lang.IllegalArgumentException: Malformed document ID array parameter\"/>\n" +
                    "</errors>\n" +
                    "</result>\n", result);
        }
    }

    @Test
    public void testLegacyHeadersOnly() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType); // Needs auto-reply
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:1:2&headersonly=true"));

        assertEquals(1, factory.messages.size());
        {
            Message m = factory.messages.get(0);
            assertEquals(DocumentProtocol.MESSAGE_GETDOCUMENT, m.getType());
            GetDocumentMessage gdm = (GetDocumentMessage)m;
            DocumentId d = gdm.getDocumentId();
            assertEquals("userdoc:kittens:1:2", d.toString());
            assertEquals("[header]", gdm.getFieldSet());
        }
        assertEquals(1, result.hits().size());
        assertHits(result.hits(), "userdoc:kittens:1:2");
    }

    @Test
    public void testFieldSet() throws Exception {
    }

    @Test
    public void testConsistentResultOrdering() throws Exception {
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(new Document(docType, new DocumentId("userdoc:kittens:1:2"))),
                new GetDocumentReply(new Document(docType, new DocumentId("userdoc:kittens:7:8"))),
                new GetDocumentReply(new Document(docType, new DocumentId("userdoc:kittens:555:123")))
        };

        // Use a predefined reply list to ensure messages are answered out of order
        DocumentSessionFactory factory = new DocumentSessionFactory(docType, null, false, replies);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:555:123&id[1]=userdoc:kittens:1:2&id[2]=userdoc:kittens:7:8"));

        assertEquals(3, factory.messages.size());
        assertEquals(3, result.hits().size());
        // Hits must be in the same order as their document IDs in the query
        assertHits(result.hits(), "userdoc:kittens:555:123", "userdoc:kittens:1:2", "userdoc:kittens:7:8");

        assertEquals(0, ((DocumentHit)result.hits().get(0)).getIndex());
        assertEquals(1, ((DocumentHit)result.hits().get(1)).getIndex());
        assertEquals(2, ((DocumentHit)result.hits().get(2)).getIndex());
    }

    @Test
    public void testResultWithSingleError() throws Exception {
        com.yahoo.messagebus.Error err = new com.yahoo.messagebus.Error(32, "Alas, it went poorly");
        DocumentSessionFactory factory = new DocumentSessionFactory(docType, err, true);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4"));
        assertNotNull(result.hits().getErrorHit());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"messagebus\" code=\"32\" message=\"Alas, it went poorly\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testResultWithMultipleErrors() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:77:88"));
        Document doc2 = new Document(docType, new DocumentId("userdoc:kittens:99:111"));
        GetDocumentReply errorReply1 = new GetDocumentReply(doc1);
        errorReply1.addError(new com.yahoo.messagebus.Error(123, "userdoc:kittens:77:88 had fleas."));
        GetDocumentReply errorReply2 = new GetDocumentReply(doc2);
        errorReply2.addError(new com.yahoo.messagebus.Error(456, "userdoc:kittens:99:111 shredded the curtains."));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                errorReply1,
                errorReply2
        };

        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:77:88&id[1]=userdoc:kittens:99:111"));

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"messagebus\" code=\"123\" message=\"userdoc:kittens:77:88 had fleas.\"/>\n" +
                "<error type=\"messagebus\" code=\"456\" message=\"userdoc:kittens:99:111 shredded the curtains.\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testResultWithNullDocument() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType, null, true);
        factory.setNullReply(true);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:55:bad_document_id"));
        // Document not found does not produce any hit at all, error or otherwise
        assertNull(result.hits().getErrorHit());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "</result>\n", result);
    }

    @Test
    public void testDefaultDocumentHitRendering() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:3:4"));
        doc1.setFieldValue("name", new StringFieldValue("mittens"));
        doc1.setFieldValue("description", new StringFieldValue("it's a cat"));
        doc1.setFieldValue("fluffiness", new IntegerFieldValue(8));
        Document doc2 = new Document(docType, new DocumentId("userdoc:kittens:1:2"));
        doc2.setFieldValue("name", new StringFieldValue("garfield"));
        doc2.setFieldValue("description",
                new StringFieldValue("preliminary research indicates <em>hatred</em> of mondays. caution advised"));
        doc2.setFieldValue("fluffiness", new IntegerFieldValue(2));
        Document doc3 = new Document(docType, new DocumentId("userdoc:kittens:77:88"));
        GetDocumentReply errorReply = new GetDocumentReply(doc3);
        errorReply.addError(new com.yahoo.messagebus.Error(123, "userdoc:kittens:77:88 had fleas."));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1),
                new GetDocumentReply(doc2),
                errorReply
        };

        // Use a predefined reply list to ensure messages are answered out of order
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result xmlResult = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:77:88&id[1]=userdoc:kittens:1:2&id[2]=userdoc:kittens:3:4"));

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                       "<result>\n" +
                       "<errors>\n" +
                       "<error type=\"messagebus\" code=\"123\" message=\"userdoc:kittens:77:88 had fleas.\"/>\n" +
                       "</errors>\n" +
                       "<document documenttype=\"kittens\" documentid=\"userdoc:kittens:1:2\">\n" +
                       "  <name>garfield</name>\n" +
                       "  <description>preliminary research indicates &lt;em&gt;hatred&lt;/em&gt; of mondays. caution advised</description>\n" +
                       "  <fluffiness>2</fluffiness>\n" +
                       "</document>\n" +
                       "<document documenttype=\"kittens\" documentid=\"userdoc:kittens:3:4\">\n" +
                       "  <name>mittens</name>\n" +
                       "  <description>it's a cat</description>\n" +
                       "  <fluffiness>8</fluffiness>\n" +
                       "</document>\n" +
                       "</result>\n", xmlResult);
    }

    @Test
    public void testDocumentFieldNoContentType() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:5:1"));
        doc1.setFieldValue("name", "derrick");
        doc1.setFieldValue("description", "kommisar katze");
        doc1.setFieldValue("fluffiness", 0);
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1),
        };
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:5:1&field=description"));

        assertNull(result.hits().getErrorHit());
        assertEquals("text/xml", result.getTemplating().getTemplates().getMimeType());
        assertEquals("UTF-8", result.getTemplating().getTemplates().getEncoding());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>kommisar katze</result>\n", result);
    }

    @Test
    public void testDocumentFieldEscapeXML() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:5:1"));
        doc1.setFieldValue("name", "asfd");
        doc1.setFieldValue("description", "<script type=\"evil/madness\">horror & screams</script>");
        doc1.setFieldValue("fluffiness", 0);
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1),
        };
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:5:1&field=description"));

        assertNull(result.hits().getErrorHit());
        assertEquals("text/xml", result.getTemplating().getTemplates().getMimeType());
        assertEquals("UTF-8", result.getTemplating().getTemplates().getEncoding());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>&lt;script type=\"evil/madness\"&gt;horror &amp; screams&lt;/script&gt;</result>\n", result);
    }

    @Test
    public void testDocumentFieldRawContent() throws Exception {
        byte[] contentBytes = new byte[] { 0, -128, 127 };

        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:123:456"));
        doc1.setFieldValue("foo", new Raw(ByteBuffer.wrap(contentBytes)));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1)
        };

        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:123:456&field=foo"));

        assertNull(result.hits().getErrorHit());
        assertEquals("application/octet-stream", result.getTemplating().getTemplates().getMimeType());

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        com.yahoo.prelude.templates.SearchRendererAdaptor.callRender(stream, result);
        stream.flush();

        byte[] resultBytes = stream.toByteArray();
        assertEquals(contentBytes.length, resultBytes.length);
        for (int i = 0; i < resultBytes.length; ++i) {
            assertEquals(contentBytes[i], resultBytes[i]);
        }
    }

    @Test
    public void testDocumentFieldRawWithContentOverride() throws Exception {
        byte[] contentBytes = new byte[] { 0, -128, 127 };

        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:123:456"));
        doc1.setFieldValue("foo", new Raw(ByteBuffer.wrap(contentBytes)));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1)
        };

        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:123:456&field=foo&contenttype=text/fancy"));

        assertNull(result.hits().getErrorHit());
        assertEquals("text/fancy", result.getTemplating().getTemplates().getMimeType());

        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        com.yahoo.prelude.templates.SearchRendererAdaptor.callRender(stream, result);
        stream.flush();

        byte[] resultBytes = stream.toByteArray();
        assertEquals(contentBytes.length, resultBytes.length);
        for (int i = 0; i < resultBytes.length; ++i) {
            assertEquals(contentBytes[i], resultBytes[i]);
        }
    }

    @Test
    public void testDocumentFieldWithMultipleIDs() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:1:2&id[1]=userdoc:kittens:3:4&field=name"));
        assertNotNull(result.hits().getErrorHit());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"3\" message=\"Illegal query: " +
                "java.lang.IllegalArgumentException: Field only valid for single document id query\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testDocumentFieldNotSet() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:5:1"));
        doc1.setFieldValue("name", "asdf");
        doc1.setFieldValue("description", "fdsafsdf");
        doc1.setFieldValue("fluffiness", 10);
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1),
        };
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:5:1&field=image"));

        assertNotNull(result.hits().getErrorHit());
        assertEquals(1, result.hits().size());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"16\" message=\"Resource not found.: " +
                "Field 'image' found in document type, but had no content in userdoc:kittens:5:1\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }


    @Test
    public void testDocumentFieldWithDocumentNotFound() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType, null, true);
        factory.setNullReply(true);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:1:2&field=name"));
        assertNotNull(result.hits().getErrorHit());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"16\" message=\"Resource not found.: " +
                "Document not found, could not return field 'name'\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testDocumentFieldNotReachableWithHeadersOnly() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:5:1"));
        doc1.setFieldValue("name", "asdf");
        // don't set body fields
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1),
        };
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:5:1&field=description&headersonly=true"));

        assertNotNull(result.hits().getErrorHit());
        assertEquals(1, result.hits().size());

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"4\" message=\"Invalid query parameter: " +
                "Field 'description' is located in document body, but headersonly " +
                "prevents it from being retrieved in userdoc:kittens:5:1\"/>\n" +
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testVespaXMLTemplate() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:3:4"));
        doc1.setFieldValue("name", "mittens");
        doc1.setFieldValue("description", "it's a cat");
        doc1.setFieldValue("fluffiness", 8);
        Document doc2 = new Document(docType, new DocumentId("userdoc:kittens:1:2"));
        doc2.setFieldValue("name", "garfield");
        doc2.setFieldValue("description", "preliminary research indicates <em>hatred</em> of mondays. caution advised");
        doc2.setFieldValue("fluffiness", 2);
        Document doc3 = new Document(docType, new DocumentId("userdoc:kittens:77:88"));
        GetDocumentReply errorReply = new GetDocumentReply(doc3);
        errorReply.addError(new com.yahoo.messagebus.Error(123, "userdoc:kittens:77:88 lost in a <ni!>\"shrubbery\"</ni!>"));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1),
                new GetDocumentReply(doc2),
                errorReply
        };

        // Use a predefined reply list to ensure messages are answered out of order
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id[0]=userdoc:kittens:77:88&id[1]=userdoc:kittens:1:2&id[2]=userdoc:kittens:3:4")); // TODO!

        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"messagebus\" code=\"123\" message=\"userdoc:kittens:77:88 lost in a &lt;ni!&gt;&quot;shrubbery&quot;&lt;/ni!&gt;\"/>\n"+
                "</errors>\n" +
                "<document documenttype=\"kittens\" documentid=\"userdoc:kittens:1:2\">\n" +
                "  <name>garfield</name>\n" +
                "  <description>preliminary research indicates &lt;em&gt;hatred&lt;/em&gt; of mondays. caution advised</description>\n" +
                "  <fluffiness>2</fluffiness>\n" +
                "</document>\n" +
                "<document documenttype=\"kittens\" documentid=\"userdoc:kittens:3:4\">\n" +
                "  <name>mittens</name>\n" +
                "  <description>it's a cat</description>\n" +
                "  <fluffiness>8</fluffiness>\n" +
                "</document>\n" +
                "</result>\n", result);
    }

    @Test
    public void testDocumentHitWithPopulatedHitFields() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:1234:foo"));
        doc1.setFieldValue("name", new StringFieldValue("megacat"));
        doc1.setFieldValue("description", new StringFieldValue("supercat"));
        doc1.setFieldValue("fluffiness", new IntegerFieldValue(10000));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new GetDocumentReply(doc1)
        };

        // Use a predefined reply list to ensure messages are answered out of order
        Chain<Searcher> searchChain = createSearcherChain(replies);

        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(
                newQuery("?id=userdoc:kittens:1234:foo&populatehitfields=true"));
        assertEquals(1, result.hits().size());
        assertHits(result.hits(), "userdoc:kittens:1234:foo");

        DocumentHit hit = (DocumentHit)result.hits().get(0);
        Iterator<Map.Entry<String, Object>> iter = hit.fieldIterator();
        Set<String> fieldSet = new TreeSet<>();
        while (iter.hasNext()) {
            Map.Entry<String, Object> kv = iter.next();
            StringBuilder field = new StringBuilder();
            field.append(kv.getKey()).append(" -> ").append(kv.getValue());
            fieldSet.add(field.toString());
        }
        StringBuilder fields = new StringBuilder();
        for (String s : fieldSet) {
            fields.append(s).append("\n");
        }
        assertEquals(
                "description -> supercat\n" +
                "documentid -> userdoc:kittens:1234:foo\n" +
                "fluffiness -> 10000\n" +
                "name -> megacat\n",
                fields.toString());
    }

    @Test
    public void deserializationExceptionsAreHandledGracefully() throws Exception {
        Document doc1 = new Document(docType, new DocumentId("userdoc:kittens:5:1"));
        GetDocumentReply[] replies = new GetDocumentReply[] {
                new MockFailingGetDocumentReply(doc1),
        };
        Chain<Searcher> searchChain = createSearcherChain(replies);
        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(newQuery("?id=userdoc:kittens:5:1"));
        assertRendered("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<result>\n" +
                "<errors>\n" +
                "<error type=\"searcher\" code=\"18\" message=\"Internal server error.: " +
                "Got exception of type java.lang.RuntimeException during document " +
                "deserialization: epic dragon attack\"/>\n"+
                "</errors>\n" +
                "</result>\n", result);
    }

    @Test
    public void testJsonRendererSetting() throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType); // Needs auto-reply
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        Chain<Searcher> searchChain = new Chain<>(searcher);

        Query query = newQuery("?id=userdoc:kittens:1:2&format=json");
        Result result = new Execution(searchChain, Execution.Context.createContextStub()).search(query);
        assertFalse(result.getTemplating().getTemplates() instanceof DocumentXMLTemplate);
    }

    private Query newQuery(String queryString) {
        return new Query(HttpRequest.createTestRequest(queryString, com.yahoo.jdisc.http.HttpRequest.Method.GET));
    }
    
    private Chain<Searcher> createSearcherChain(GetDocumentReply[] replies) throws Exception {
        DocumentSessionFactory factory = new DocumentSessionFactory(docType, null, false, replies);
        GetSearcher searcher = new GetSearcher(new FeedContext(
                new MessagePropertyProcessor(defFeedCfg, defLoadTypeCfg),
                factory, docMan, new ClusterList(), new NullFeedMetric()));
        return new Chain<>(searcher);
    }

    private static class MockFailingGetDocumentReply extends GetDocumentReply {
        private int countdown = 2;

        private MockFailingGetDocumentReply(Document doc) {
            super(doc);
        }

        @Override
        public Document getDocument() {
            // Reason for countdown is that the test DocumentSessionFactory calls
            // getDocument once internally before code can ever reach handleReply.
            if (--countdown == 0) {
                throw new RuntimeException("epic dragon attack");
            }
            return super.getDocument();
        }
    }

    private static class MockBackend extends Searcher {
        private Hit hitToReturn;

        public MockBackend(Hit hitToReturn) {
            this.hitToReturn = hitToReturn;
        }

        @Override
        public Result search(Query query, Execution execution) {
            Result result = new Result(query);
            result.hits().add(hitToReturn);
            return result;
        }
    }

    private class MockHttpRequest {

        private final String req;
        private byte[] data;
        private boolean gzip = false;

        MockHttpRequest(byte[] data, String req) {
            this.req = req;
            this.data = data;
        }

        MockHttpRequest(byte[] data, String req, boolean gzip) {
            this.data = data;
            this.req = req;
            this.gzip = gzip;
        }

        public InputStream getData() {
            if (gzip) {
                try {
                    ByteArrayOutputStream rawOut = new ByteArrayOutputStream();
                    GZIPOutputStream compressed = new GZIPOutputStream(rawOut);
                    compressed.write(data, 0, data.length);
                    compressed.finish();
                    compressed.flush();
                    rawOut.flush();
                    return new ByteArrayInputStream(rawOut.toByteArray());
                } catch (Exception e) {
                    return null;
                }
            }
            return new ByteArrayInputStream(data);
        }

        public void addHeaders(HeaderFields headers) {
            headers.add("Content-Type", "text/plain;encoding=UTF-8");
            if (gzip)
                headers.add("Content-Encoding", "gzip");
        }

        public com.yahoo.container.jdisc.HttpRequest toRequest() {
            com.yahoo.container.jdisc.HttpRequest request = com.yahoo.container.jdisc.HttpRequest.createTestRequest(req, com.yahoo.jdisc.http.HttpRequest.Method.GET, getData());
            addHeaders(request.getJDiscRequest().headers());
            return request;
        }

    }

    public static void assertRendered(String expected,Result result) throws Exception {
        assertRendered(expected,result,true);
    }

    public static void assertRendered(String expected,Result result,boolean checkFullEquality) throws Exception {
        if (checkFullEquality)
            assertEquals(expected, ResultRenderingUtil.getRendered(result));
        else
            assertTrue(ResultRenderingUtil.getRendered(result).startsWith(expected));
    }

}