xref: /OpenGrok/suggester/src/test/java/org/opengrok/suggest/query/SuggesterPhraseQueryTest.java (revision 2f7dccc7cd05ce1957006b093948d5359068ae4f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * See LICENSE.txt included in this distribution for the specific
9  * language governing permissions and limitations under the License.
10  *
11  * When distributing Covered Code, include this CDDL HEADER in each
12  * file and include the License file at LICENSE.txt.
13  * If applicable, add the following below this CDDL HEADER, with the
14  * fields enclosed by brackets "[]" replaced with your own identifying
15  * information: Portions Copyright [yyyy] [name of copyright owner]
16  *
17  * CDDL HEADER END
18  */
19 
20 /*
21  * Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
22  * Portions Copyright (c) 2020, Chris Fraire <cfraire@me.com>.
23  */
24 package org.opengrok.suggest.query;
25 
26 import org.apache.lucene.index.Term;
27 import org.junit.jupiter.api.Test;
28 import org.opengrok.suggest.query.customized.CustomPhraseQuery;
29 
30 import java.lang.reflect.Field;
31 import java.util.Arrays;
32 
33 import static org.junit.jupiter.api.Assertions.assertEquals;
34 
35 public class SuggesterPhraseQueryTest {
36 
37     @Test
prefixQueryTest()38     public void prefixQueryTest() {
39         SuggesterPhraseQuery q = new SuggesterPhraseQuery("test", "ident",
40                 Arrays.asList("one", "two", "tident"), 0);
41 
42         SuggesterPrefixQuery prefixQuery = (SuggesterPrefixQuery) q.getSuggesterQuery();
43 
44         assertEquals("t", prefixQuery.getPrefix().text());
45     }
46 
47     @Test
phraseQueryTest()48     public void phraseQueryTest() throws Exception {
49         SuggesterPhraseQuery q = new SuggesterPhraseQuery("test", "ident",
50                 Arrays.asList("one", "two", "tident"), 0);
51 
52         CustomPhraseQuery query = q.getPhraseQuery();
53 
54         assertEquals(2, query.getOffset());
55 
56         Term[] terms = getTerms(query);
57 
58         assertEquals("one", terms[0].text());
59         assertEquals("two", terms[1].text());
60     }
61 
getTerms(final CustomPhraseQuery query)62     private Term[] getTerms(final CustomPhraseQuery query) throws Exception {
63         Field f = CustomPhraseQuery.class.getDeclaredField("terms");
64         f.setAccessible(true);
65         return (Term[]) f.get(query);
66     }
67 
68 }
69