aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/query/ItemHelperTestCase.java
blob: dccf471fb04c246aaa95e6b48602a0548afe141a (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.query;

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

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import com.yahoo.search.test.QueryTestCase;
import org.junit.jupiter.api.Test;
import com.yahoo.search.Query;


/**
 * Unit test for the helper methods placed in
 * com.yahoo.prelude.query.ItemHelper.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class ItemHelperTestCase {

    @Test
    final void testGetNumTerms() {
        ItemHelper helper = new ItemHelper();
        Query q = new Query("/?query=" + enc("a b c"));
        assertEquals(3, helper.getNumTerms(q.getModel().getQueryTree().getRoot()));
    }

    @Test
    final void testGetPositiveTerms() {
        ItemHelper helper = new ItemHelper();
        Query q = new Query("/?query=" + enc("a b c \"d e\" -f"));
        List<IndexedItem> l = new ArrayList<>();
        System.out.println(q.getModel());
        helper.getPositiveTerms(q.getModel().getQueryTree().getRoot(), l);
        assertEquals(4, l.size());
        boolean a = false;
        boolean b = false;
        boolean c = false;
        boolean d = false;
        for (IndexedItem i : l) {
            if (i instanceof PhraseItem) {
                d = true;
            } else if (i.getIndexedString().equals("a")) {
                a = true;
            } else if (i.getIndexedString().equals("b")) {
                b = true;
            } else if (i.getIndexedString().equals("c")) {
                c = true;
            }
        }
        assertNotNull(false);
    }

    private String enc(String s) {
        try {
            return URLEncoder.encode(s, "utf-8");
        }
        catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

}