summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/fs4/test/GetDocSumsPacketTestCase.java
blob: a5c538d235aaffb39330652a92247470b2e6f1b2 (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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4.test;

import com.yahoo.fs4.BufferTooSmallException;
import com.yahoo.fs4.GetDocSumsPacket;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.result.Hit;
import org.junit.Test;

import java.nio.ByteBuffer;
import java.util.Arrays;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
 * Tests the GetDocsumsPacket
 *
 * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a>
 */
public class GetDocSumsPacketTestCase {

    private static final byte IGNORE = 69;

    @Test
    public void testDefaultDocsumClass() {
        Query query = new Query("/?query=chain");
        assertNull(query.getPresentation().getSummary());
    }

    @Test
    public void testEncodingWithQuery() throws BufferTooSmallException {
        FastHit hit = new FastHit();
        hit.setIgnoreRowBits(true);
        assertPacket(true, hit, new byte[] { 0, 0, 0, 57, 0, 0, 0, -37, 0, 0, 40, 21, 0, 0, 0, 0, IGNORE, IGNORE, IGNORE,
                IGNORE, 7, 100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 0x01, 0, 0, 0, 7,
                100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 1, 0, 0, 0, 6, 4, 0, 3, 102, 111, 111, 0, 0, 0, 1 });

        hit = new FastHit();
        hit.setIgnoreRowBits(false);
        assertPacket(true, hit, new byte[] {0, 0, 0, 53, 0, 0, 0, -37, 0, 0, 8, 21, 0, 0, 0, 0, IGNORE, IGNORE, IGNORE,
                IGNORE, 7, 100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 0x01, 0, 0, 0, 7,
                100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 1, 0, 0, 0, 6, 4, 0, 3, 102, 111, 111});
    }

    @Test
    public void testEncodingWithoutQuery() throws BufferTooSmallException {
        FastHit hit = new FastHit();
        hit.setIgnoreRowBits(true);
        assertPacket(false, hit, new byte[] { 0, 0, 0, 43, 0, 0, 0, -37, 0, 0, 40, 17, 0, 0, 0, 0, IGNORE, IGNORE, IGNORE,
                     IGNORE, 7, 100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 0x01, 0, 0, 0, 7,
                     100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 1
        });

        hit = new FastHit();
        hit.setIgnoreRowBits(false);
        assertPacket(false, hit, new byte[] { 0, 0, 0, 39, 0, 0, 0, -37, 0, 0, 8, 17, 0, 0, 0, 0, IGNORE, IGNORE, IGNORE,
                IGNORE, 7, 100, 101, 102, 97, 117, 108, 116, 0, 0, 0, 0x01, 0, 0, 0, 7, 100, 101, 102, 97, 117, 108, 116
        });
    }

    @Test
    public void requireThatSessionIdIsEncodedAsPropertyWhenUsingSearchSession() throws BufferTooSmallException {
        Result result = new Result(new Query("?query=foo"));
        result.getQuery().getSessionId(true);  // create session id.
        result.getQuery().getRanking().setQueryCache(true);
        FastHit hit = new FastHit();
        hit.setIgnoreRowBits(true);
        result.hits().add(hit);
        assertPacket(false, result, new byte[] { 0, 0, 0, -123, 0, 0, 0, -37, 0, 0, 56, 17, 0, 0, 0, 0,
                // query timeout
                IGNORE, IGNORE, IGNORE, IGNORE,
                // "default" - rank profile
                7, 'd', 'e', 'f', 'a', 'u', 'l', 't', 0, 0, 0, 0x01,
                // "default" - summaryclass
                0, 0, 0, 7, 'd', 'e', 'f', 'a', 'u', 'l', 't',
                // 2 property entries
                0, 0, 0, 2,
                // rank: sessionId => qrserver.0.XXXXXXXXXXXXX.0
                0, 0, 0, 4, 'r', 'a', 'n', 'k', 0, 0, 0, 1, 0, 0, 0, 9, 's', 'e', 's', 's', 'i', 'o', 'n', 'I', 'd', 0, 0, 0, 26, 'q', 'r', 's', 'e', 'r', 'v', 'e', 'r', '.',
                IGNORE, '.', IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, IGNORE, '.', IGNORE,
                // caches: features => true
                0, 0, 0, 6, 'c', 'a', 'c', 'h', 'e', 's', 0, 0, 0, 1, 0, 0, 0, 5, 'q', 'u', 'e', 'r', 'y', 0, 0, 0, 4, 't', 'r', 'u', 'e',
                // flags
                0, 0, 0, 1
        });
    }

    private static void assertPacket(boolean sendQuery, Hit hit, byte[] expected) throws BufferTooSmallException {
        Result result = new Result(new Query("?query=foo"));
        result.hits().add(hit);
        assertPacket(sendQuery, result, expected);
    }

    private static void assertPacket(boolean sendQuery, Result result, byte[] expected) throws BufferTooSmallException {
        GetDocSumsPacket packet = GetDocSumsPacket.create(result, "default", sendQuery);
        ByteBuffer buf = ByteBuffer.allocate(1024);
        packet.encode(buf);
        buf.flip();

        byte[] actual = new byte[buf.remaining()];
        buf.get(actual);
        // assertEquals(Arrays.toString(expected), Arrays.toString(actual));

        assertEquals("Equal length", expected.length, actual.length);
        for (int i = 0; i < expected.length; ++i) {
            if (expected[i] == IGNORE) {
                actual[i] = IGNORE;
            }
        }

        assertArrayEquals(expected, actual);
    }
}