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.solr.impl; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import java.util.Arrays; |
---|
22 | import java.util.Collections; |
---|
23 | import java.util.HashMap; |
---|
24 | import java.util.List; |
---|
25 | import java.util.Map; |
---|
26 | import org.apache.solr.client.solrj.SolrQuery; |
---|
27 | import org.hamcrest.Matchers; |
---|
28 | import org.junit.BeforeClass; |
---|
29 | import org.junit.Test; |
---|
30 | import static org.junit.Assert.*; |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | * @author twagoo |
---|
35 | */ |
---|
36 | public class SolrFacetQueryFactoryImplTest { |
---|
37 | |
---|
38 | public static final List<String> FACET_FIELDS = Arrays.asList("facet1", "facet2", "facet3"); |
---|
39 | |
---|
40 | /** |
---|
41 | * static because it doesn't carry state |
---|
42 | */ |
---|
43 | private static SolrFacetQueryFactoryImpl instance; |
---|
44 | |
---|
45 | @BeforeClass |
---|
46 | public static void setUpClass() { |
---|
47 | instance = new SolrFacetQueryFactoryImpl(); |
---|
48 | } |
---|
49 | |
---|
50 | /** |
---|
51 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
52 | */ |
---|
53 | @Test |
---|
54 | public void testCreateFacetQueryNoFacets() { |
---|
55 | // default constructor -> empty |
---|
56 | QueryFacetsSelection selection = new QueryFacetsSelection(); |
---|
57 | SolrQuery query = instance.createFacetQuery(selection, FACET_FIELDS, 20); |
---|
58 | |
---|
59 | // default: query selects all values |
---|
60 | assertNull(query.getQuery()); |
---|
61 | |
---|
62 | // no selection -> no filter queries |
---|
63 | assertEquals(0, query.getFilterQueries().length); |
---|
64 | |
---|
65 | assertEquals(20, query.getFacetLimit()); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
70 | */ |
---|
71 | @Test |
---|
72 | public void testCreateFacetQueryNoSelection() { |
---|
73 | // Facets are present but no values are selected |
---|
74 | Map<String, FacetSelection> selection = new HashMap<String, FacetSelection>() { |
---|
75 | { |
---|
76 | put("facet1", new FacetSelection(Collections.<String>emptySet())); |
---|
77 | put("facet2", new FacetSelection(Collections.<String>emptyList())); |
---|
78 | } |
---|
79 | }; |
---|
80 | |
---|
81 | SolrQuery query = instance.createFacetQuery(new QueryFacetsSelection(selection), FACET_FIELDS, 20); |
---|
82 | |
---|
83 | // default: query selects all values |
---|
84 | assertNull(query.getQuery()); |
---|
85 | |
---|
86 | // Only empty selections -> no filter queries |
---|
87 | assertEquals(0, query.getFilterQueries().length); |
---|
88 | |
---|
89 | // Facet limit should be adopted |
---|
90 | assertEquals(20, query.getFacetLimit()); |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
95 | */ |
---|
96 | @Test |
---|
97 | public void testCreateFacetQuerySelection() { |
---|
98 | // Some facets have one or more values selected |
---|
99 | Map<String, FacetSelection> selection = new HashMap<String, FacetSelection>() { |
---|
100 | { |
---|
101 | put("facet1", new FacetSelection(Arrays.asList("valueA"))); |
---|
102 | put("facet2", new FacetSelection(Arrays.asList("valueB", "valueC"))); |
---|
103 | put("facet3", new FacetSelection(Collections.<String>emptyList())); |
---|
104 | } |
---|
105 | }; |
---|
106 | SolrQuery query = instance.createFacetQuery(new QueryFacetsSelection(selection), FACET_FIELDS, 20); |
---|
107 | |
---|
108 | // default: query selects all values |
---|
109 | assertNull(query.getQuery()); |
---|
110 | |
---|
111 | // Expecting three filter queries as three values have been selected in total |
---|
112 | assertEquals(3, query.getFilterQueries().length); |
---|
113 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet1:\"valueA\"")); |
---|
114 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet2:\"valueB\"")); |
---|
115 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet2:\"valueC\"")); |
---|
116 | // facet 3 does not occur as there is not selected value! |
---|
117 | |
---|
118 | // Facet limit should be adopted |
---|
119 | assertEquals(20, query.getFacetLimit()); |
---|
120 | } |
---|
121 | |
---|
122 | /** |
---|
123 | * Test of createFacetQuery method, of class SolrQueryFactoryImpl. |
---|
124 | */ |
---|
125 | @Test |
---|
126 | public void testCreateFacetQuerySelectionAndQuery() { |
---|
127 | Map<String, FacetSelection> selection = new HashMap<String, FacetSelection>() { |
---|
128 | { |
---|
129 | put("facet1", new FacetSelection(Arrays.asList("value A"))); |
---|
130 | } |
---|
131 | }; |
---|
132 | SolrQuery query = instance.createFacetQuery(new QueryFacetsSelection("query string", selection), FACET_FIELDS, 20); |
---|
133 | |
---|
134 | assertEquals(1, query.getFilterQueries().length); |
---|
135 | assertEquals("\"query\\ string\"", query.getQuery()); //space should be escaped, and query wrapped! |
---|
136 | |
---|
137 | // Expecting three filter queries as three values have been selected in total |
---|
138 | assertEquals(1, query.getFilterQueries().length); |
---|
139 | assertThat(Arrays.asList(query.getFilterQueries()), Matchers.<String>hasItem("facet1:\"value\\ A\"")); |
---|
140 | |
---|
141 | // Facet limit should be adopted |
---|
142 | assertEquals(20, query.getFacetLimit()); |
---|
143 | } |
---|
144 | |
---|
145 | /** |
---|
146 | * Test of createCountFacetsQuery method, of class SolrQueryFactoryImpl. |
---|
147 | */ |
---|
148 | @Test |
---|
149 | public void testCreateCountFacetsQuery() { |
---|
150 | SolrQuery query = instance.createCountFacetsQuery(FACET_FIELDS); |
---|
151 | assertArrayEquals(FACET_FIELDS.toArray(), query.getFacetFields()); |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|