aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/query/profile/config/test/QueryProfileIntegrationTestCase.java
blob: c2e78108cf899bfbf67c2e8893698483a2bda208 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.config.test;

import com.yahoo.jdisc.http.HttpRequest.Method;
import com.yahoo.container.jdisc.HttpRequest;
import com.yahoo.component.chain.dependencies.After;
import com.yahoo.component.chain.dependencies.Provides;
import com.yahoo.container.Container;
import com.yahoo.container.core.config.testutil.HandlersConfigurerTestWrapper;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.handler.HttpSearchResponse;
import com.yahoo.search.handler.SearchHandler;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * Tests using query profiles in searches
 *
 * @author bratseth
 */
public class QueryProfileIntegrationTestCase {

    @org.junit.jupiter.api.AfterEach
    public void tearDown() {
        System.getProperties().remove("config.id");
    }

    @Test
    void testUntyped() {
        String configId = "dir:src/test/java/com/yahoo/search/query/profile/config/test/untyped";
        System.setProperty("config.id", configId);
        Container container = new Container();
        HandlersConfigurerTestWrapper configurer = new HandlersConfigurerTestWrapper(container, configId);
        SearchHandler searchHandler = (SearchHandler) configurer.getRequestHandlerRegistry().getComponent(SearchHandler.class.getName());

        // Should get "default" query profile containing the "test" search chain containing the "test" searcher
        HttpRequest request = HttpRequest.createTestRequest("search", Method.GET);
        HttpSearchResponse response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().get("from:test"));

        // Should get the "test' query profile containing the "default" search chain containing the "default" searcher
        request = HttpRequest.createTestRequest("search?queryProfile=test", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().get("from:default"));

        // Should get "default" query profile, but override the search chain to default
        request = HttpRequest.createTestRequest("search?searchChain=default", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().get("from:default"));

        // Tests a profile setting hits and offset
        request = HttpRequest.createTestRequest("search?queryProfile=hitsoffset", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertEquals(20, response.getQuery().getHits());
        assertEquals(80, response.getQuery().getOffset());

        // Tests a non-resolved profile request
        request = HttpRequest.createTestRequest("search?queryProfile=none", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().getError(), "Got an error");
        assertEquals("Could not resolve query profile 'none'", response.getResult().hits().getError().getDetailedMessage());

        // Tests that properties in objects owned by query is handled correctly
        request = HttpRequest.createTestRequest("search?query=word&queryProfile=test", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertEquals("index", response.getQuery().getModel().getDefaultIndex());
        assertEquals("WEAKAND(100) index:word", response.getQuery().getModel().getQueryTree().toString());
        configurer.shutdown();
    }

    @Test
    void testTyped() {
        String configId = "dir:src/test/java/com/yahoo/search/query/profile/config/test/typed";
        System.setProperty("config.id", configId);
        Container container = new Container();
        HandlersConfigurerTestWrapper configurer = new HandlersConfigurerTestWrapper(container, configId);
        SearchHandler searchHandler = (SearchHandler) configurer.getRequestHandlerRegistry().getComponent(SearchHandler.class.getName());

        // Should get "default" query profile containing the "test" search chain containing the "test" searcher
        HttpRequest request = HttpRequest.createTestRequest("search", Method.GET);
        HttpSearchResponse response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().get("from:test"));

        // Should get the "test' query profile containing the "default" search chain containing the "default" searcher
        request = HttpRequest.createTestRequest("search?queryProfile=test", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().get("from:default"));

        // Should get "default" query profile, but override the search chain to default
        request = HttpRequest.createTestRequest("search?searchChain=default", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().get("from:default"));

        // Tests a profile setting hits and offset
        request = HttpRequest.createTestRequest("search?queryProfile=hitsoffset", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertEquals(22, response.getQuery().getHits());
        assertEquals(80, response.getQuery().getOffset());

        // Tests a non-resolved profile request
        request = HttpRequest.createTestRequest("search?queryProfile=none", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertNotNull(response.getResult().hits().getError(), "Got an error");
        assertEquals("Could not resolve query profile 'none'", response.getResult().hits().getError().getDetailedMessage());

        // Test overriding a sub-profile in the request
        request = HttpRequest.createTestRequest("search?queryProfile=root&sub=newsub", Method.GET);
        response = (HttpSearchResponse) searchHandler.handle(request); // Cast to access content directly
        assertEquals("newsubvalue1", response.getQuery().properties().get("sub.value1"));
        assertEquals("newsubvalue2", response.getQuery().properties().get("sub.value2"));
        configurer.shutdown();
    }

    public static class DefaultSearcher extends Searcher {

        @Override
        public Result search(Query query,Execution execution) {
            Result result=execution.search(query);
            result.hits().add(new Hit("from:default"));
            return result;
        }

    }

    public static class TestSearcher extends Searcher {

        @Override
        public Result search(Query query,Execution execution) {
            Result result=execution.search(query);
            result.hits().add(new Hit("from:test"));
            return result;
        }

    }

    /** Tests searcher communication - setting */
    @Provides("SomeObject")
    public static class SettingSearcher extends Searcher {

        @Override
        public Result search(Query query,Execution execution) {
            SomeObject.setTo(query,new SomeObject());
            return execution.search(query);
        }

    }

    /** Tests searcher communication - receiving */
    @After("SomeObject")
    public static class ReceivingSearcher extends Searcher {

        @Override
        public Result search(Query query,Execution execution) {
            assertNotNull(SomeObject.getFrom(query));
            assertEquals(SomeObject.class,SomeObject.getFrom(query).getClass());
            return execution.search(query);
        }

    }

    /** An example of a model object */
    private static class SomeObject {

        public static void setTo(Query query,SomeObject someObject) {
            query.properties().set("SomeObject",someObject);
        }

        public static SomeObject getFrom(Query query) {
            // In some cases we want to create if this does not exist here
            return (SomeObject)query.properties().get("SomeObject");
        }

    }

}