aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/result/TemplatingTestCase.java
blob: 13d0c64a0c279e5530e34be4dee07fc9719ee47f (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.Writer;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import com.yahoo.search.rendering.Renderer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.common.base.Splitter;
import com.yahoo.prelude.fastsearch.FastHit;
import com.yahoo.prelude.templates.UserTemplate;
import com.yahoo.prelude.templates.test.BoomTemplate;
import com.yahoo.search.Query;
import com.yahoo.search.Result;

/**
 * Control helper method for result rendering/result templates.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class TemplatingTestCase {
    Result result;

    @Before
    public void setUp() throws Exception {
        Query q = new Query("/?query=a&presentation.format=nalle&offset=1&hits=5");
        result = new Result(q);
        result.setTotalHitCount(1000L);
        result.hits().add(new FastHit("http://localhost/1", .95));
        result.hits().add(new FastHit("http://localhost/2", .90));
        result.hits().add(new FastHit("http://localhost/3", .85));
        result.hits().add(new FastHit("http://localhost/4", .80));
        result.hits().add(new FastHit("http://localhost/5", .75));
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public final void testGetFirstHitNo() {
        assertEquals(2, result.getTemplating().getFirstHitNo());
    }

    @Test
    public final void testGetNextFirstHitNo() {
        assertEquals(7, result.getTemplating().getNextFirstHitNo());
        result.getQuery().setHits(6);
        assertEquals(0, result.getTemplating().getNextFirstHitNo());
    }

    @Test
    public final void testGetNextLastHitNo() {
        assertEquals(11, result.getTemplating().getNextLastHitNo());
        result.getQuery().setHits(6);
        assertEquals(0, result.getTemplating().getNextLastHitNo());
    }

    @Test
    public final void testGetLastHitNo() {
        assertEquals(6, result.getTemplating().getLastHitNo());
    }

    @Test
    public final void testGetPrevFirstHitNo() {
        assertEquals(1, result.getTemplating().getPrevFirstHitNo());
    }

    @Test
    public final void testGetPrevLastHitNo() {
        assertEquals(1, result.getTemplating().getPrevLastHitNo());
    }

    @Test
    public final void testGetNextResultURL() {
        String next = result.getTemplating().getNextResultURL();
        Set<String> expectedParameters = new HashSet<>(Arrays.asList(new String[] {
                "hits=5",
                "query=a",
                "presentation.format=nalle",
                "offset=6"
        }));
        Set<String> actualParameters = new HashSet<>();
        Splitter s = Splitter.on("&");
        for (String parameter : s.split(next.substring(next.indexOf('?') + 1))) {
            actualParameters.add(parameter);
        }
        assertEquals(expectedParameters, actualParameters);
    }

    @Test
    public final void testGetPreviousResultURL() {
        String previous = result.getTemplating().getPreviousResultURL();
        Set<String> expectedParameters = new HashSet<>(Arrays.asList(new String[] {
                "hits=5",
                "query=a",
                "presentation.format=nalle",
                "offset=0"
        }));
        Set<String> actualParameters = new HashSet<>();
        Splitter s = Splitter.on("&");
        for (String parameter : s.split(previous.substring(previous.indexOf('?') + 1))) {
            actualParameters.add(parameter);
        }
        assertEquals(expectedParameters, actualParameters);
    }

    @Test
    public final void testGetCurrentResultURL() {
        String previous = result.getTemplating().getCurrentResultURL();
        Set<String> expectedParameters = new HashSet<>(Arrays.asList(new String[] {
                "hits=5",
                "query=a",
                "presentation.format=nalle",
                "offset=1"
        }));
        Set<String> actualParameters = new HashSet<>();
        Splitter s = Splitter.on("&");
        for (String parameter : s.split(previous.substring(previous.indexOf('?') + 1))) {
            actualParameters.add(parameter);
        }
        assertEquals(expectedParameters, actualParameters);
    }

    @Test
    public final void testGetTemplates() {
        @SuppressWarnings({ "unchecked", "deprecation" })
        UserTemplate<Writer> t = result.getTemplating().getTemplates();
        assertEquals("default", t.getName());
    }

    @SuppressWarnings("deprecation")
    @Test
    public final void testSetTemplates() {
        result.getTemplating().setTemplates(new BoomTemplate("gnuff", "text/plain", "ISO-8859-15"));
        @SuppressWarnings("unchecked")
        UserTemplate<Writer> t = result.getTemplating().getTemplates();
        assertEquals("gnuff", t.getName());
    }

    private static class TestRenderer extends Renderer {

        @Override
        public void render(Writer writer, Result result) throws IOException {
        }

        @Override
        public String getEncoding() {
            return null;
        }

        @Override
        public String getMimeType() {
            return null;
        }
    }

    @SuppressWarnings("deprecation")
    @Test
    public final void testUsesDefaultTemplate() {
        assertTrue(result.getTemplating().usesDefaultTemplate());
        result.getTemplating().setRenderer(new TestRenderer());
        assertFalse(result.getTemplating().usesDefaultTemplate());
    }

}