1 | /* |
---|
2 | * Copyright (C) 2014 CLARIN |
---|
3 | * |
---|
4 | * This program is free software: you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation, either version 3 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
16 | */ |
---|
17 | package eu.clarin.cmdi.vlo.service.impl; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.service.solr.impl.SolrFacetQueryFactoryImpl; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import java.util.Arrays; |
---|
22 | import java.util.Collection; |
---|
23 | import java.util.Collections; |
---|
24 | import java.util.HashMap; |
---|
25 | import java.util.List; |
---|
26 | import java.util.Map; |
---|
27 | import org.apache.solr.client.solrj.SolrQuery; |
---|
28 | import org.hamcrest.Matchers; |
---|
29 | import org.junit.BeforeClass; |
---|
30 | import org.junit.Test; |
---|
31 | import static org.junit.Assert.*; |
---|
32 | |
---|
33 | /** |
---|
34 | * |
---|
35 | * @author twagoo |
---|
36 | */ |
---|
37 | public class SolrFacetQueryFactoryImplTest { |
---|
38 | |
---|
39 | public static final List<String> FACET_FIELDS = Arrays.asList("facet1", "facet2", "facet3"); |
---|
40 | |
---|
41 | /** |
---|
42 | * static because it doesn't carry state |
---|
43 | */ |
---|
44 | private static SolrFacetQueryFactoryImpl instance; |
---|
45 | |
---|
46 | @BeforeClass |
---|
47 | public static void setUpClass() { |
---|
48 | instance = new SolrFacetQueryFactoryImpl(FACET_FIELDS); |
---|
49 | } |
---|
50 | |
---|
51 | /** |
---|
52 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
53 | */ |
---|
54 | @Test |
---|
55 | public void testCreateFacetQueryNoFacets() { |
---|
56 | // default constructor -> empty |
---|
57 | QueryFacetsSelection selection = new QueryFacetsSelection(); |
---|
58 | SolrQuery query = instance.createFacetQuery(selection); |
---|
59 | |
---|
60 | // default: query selects all values |
---|
61 | assertEquals("*:*", query.getQuery()); |
---|
62 | |
---|
63 | // no selection -> no filter queries |
---|
64 | assertEquals(0, query.getFilterQueries().length); |
---|
65 | } |
---|
66 | |
---|
67 | /** |
---|
68 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
69 | */ |
---|
70 | @Test |
---|
71 | public void testCreateFacetQueryNoSelection() { |
---|
72 | // Facets are present but no values are selected |
---|
73 | Map<String, Collection<String>> selection = new HashMap<String, Collection<String>>() { |
---|
74 | { |
---|
75 | put("facet1", Collections.<String>emptySet()); |
---|
76 | put("facet2", Collections.<String>emptyList()); |
---|
77 | } |
---|
78 | }; |
---|
79 | |
---|
80 | SolrQuery query = instance.createFacetQuery(new QueryFacetsSelection(selection)); |
---|
81 | |
---|
82 | // default: query selects all values |
---|
83 | assertEquals("*:*", query.getQuery()); |
---|
84 | |
---|
85 | // Only empty selections -> no filter queries |
---|
86 | assertEquals(0, query.getFilterQueries().length); |
---|
87 | } |
---|
88 | |
---|
89 | /** |
---|
90 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
91 | */ |
---|
92 | @Test |
---|
93 | public void testCreateFacetQuerySelection() { |
---|
94 | // Some facets have one or more values selected |
---|
95 | Map<String, Collection<String>> selection = new HashMap<String, Collection<String>>() { |
---|
96 | { |
---|
97 | put("facet1", Arrays.asList("valueA")); |
---|
98 | put("facet2", Arrays.asList("valueB", "valueC")); |
---|
99 | put("facet3", Collections.<String>emptyList()); |
---|
100 | } |
---|
101 | }; |
---|
102 | SolrQuery query = instance.createFacetQuery(new QueryFacetsSelection(selection)); |
---|
103 | |
---|
104 | // default: query selects all values |
---|
105 | assertEquals("*:*", query.getQuery()); |
---|
106 | |
---|
107 | // Expecting three filter queries as three values have been selected in total |
---|
108 | assertEquals(3, query.getFilterQueries().length); |
---|
109 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet1:valueA")); |
---|
110 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet2:valueB")); |
---|
111 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet2:valueC")); |
---|
112 | // facet 3 does not occur as there is not selected value! |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
117 | */ |
---|
118 | @Test |
---|
119 | public void testCreateFacetQuerySelectionAndQuery() { |
---|
120 | Map<String, Collection<String>> selection = new HashMap<String, Collection<String>>() { |
---|
121 | { |
---|
122 | put("facet1", Arrays.asList("valueA")); |
---|
123 | } |
---|
124 | }; |
---|
125 | SolrQuery query = instance.createFacetQuery(new QueryFacetsSelection("query string", selection)); |
---|
126 | |
---|
127 | assertEquals(1, query.getFilterQueries().length); |
---|
128 | assertEquals("query\\ string", query.getQuery()); //space should be escaped! |
---|
129 | |
---|
130 | // Expecting three filter queries as three values have been selected in total |
---|
131 | assertEquals(1, query.getFilterQueries().length); |
---|
132 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet1:valueA")); |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Test of createCountFacetsQuery method, of class SolrQueryFactoryImpl. |
---|
137 | */ |
---|
138 | @Test |
---|
139 | public void testCreateCountFacetsQuery() { |
---|
140 | SolrQuery query = instance.createCountFacetsQuery(); |
---|
141 | assertArrayEquals(FACET_FIELDS.toArray(), query.getFacetFields()); |
---|
142 | } |
---|
143 | |
---|
144 | } |
---|