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 com.google.common.collect.ImmutableList; |
---|
20 | import com.google.common.collect.Maps; |
---|
21 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_COLLECTION; |
---|
22 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_COUNTRY; |
---|
23 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
24 | import eu.clarin.cmdi.vlo.pojo.FacetSelection; |
---|
25 | import eu.clarin.cmdi.vlo.pojo.FacetSelectionType; |
---|
26 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
27 | import java.util.Arrays; |
---|
28 | import java.util.Collections; |
---|
29 | import java.util.List; |
---|
30 | import java.util.Map; |
---|
31 | import org.apache.commons.lang3.SerializationUtils; |
---|
32 | import org.apache.wicket.request.mapper.parameter.PageParameters; |
---|
33 | import org.apache.wicket.util.string.StringValue; |
---|
34 | import static org.hamcrest.Matchers.equalTo; |
---|
35 | import static org.hamcrest.Matchers.hasItem; |
---|
36 | import org.junit.Before; |
---|
37 | import org.junit.Test; |
---|
38 | import static org.junit.Assert.*; |
---|
39 | |
---|
40 | /** |
---|
41 | * |
---|
42 | * @author twagoo |
---|
43 | */ |
---|
44 | public class QueryFacetsSelectionParametersConverterTest { |
---|
45 | |
---|
46 | private QueryFacetsSelectionParametersConverter instance; |
---|
47 | |
---|
48 | @Before |
---|
49 | public void setUp() { |
---|
50 | VloConfig config = new VloConfig() { |
---|
51 | |
---|
52 | @Override |
---|
53 | public List<String> getAllFacetFields() { |
---|
54 | return ImmutableList.of("facet1", "facet2", "facet3", "facet4"); |
---|
55 | } |
---|
56 | |
---|
57 | }; |
---|
58 | instance = new QueryFacetsSelectionParametersConverter(config); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Test of fromParameters method, of class |
---|
63 | * QueryFacetsSelectionParametersConverter. |
---|
64 | */ |
---|
65 | @Test |
---|
66 | public void testFromParameters() { |
---|
67 | final PageParameters params = new PageParameters(); |
---|
68 | params.add("q", "query"); |
---|
69 | params.add("q", "ignored"); // only one query param is selected |
---|
70 | params.add("fq", "facet1:valueA"); |
---|
71 | params.add("fq", "facet1:valueB"); |
---|
72 | params.add("fq", "facet2:value:C"); // has a colon in value |
---|
73 | params.add("fq", "illegal-no-colon"); //should get ignored |
---|
74 | params.add("fq", "facet5:valueD"); //not in list, should get ignored |
---|
75 | params.add("fq", ""); // not a valid facet selection |
---|
76 | params.add("fq", "invalid"); // not a valid facet selection |
---|
77 | params.add("fqType", "facet1:or"); |
---|
78 | params.add("fqType", "facet3:not_empty"); |
---|
79 | params.add("fqType", "facet4:illegaltype"); //should get ignored |
---|
80 | params.add("fqType", "illegal-no-colon"); //should get ignored |
---|
81 | |
---|
82 | final QueryFacetsSelection result = instance.fromParameters(params); |
---|
83 | assertEquals("query", result.getQuery()); |
---|
84 | assertEquals(3, result.getFacets().size()); |
---|
85 | assertThat(result.getFacets(), hasItem("facet1")); |
---|
86 | assertThat(result.getFacets(), hasItem("facet2")); |
---|
87 | assertThat(result.getSelectionValues("facet1").getValues(), hasItem("valueA")); |
---|
88 | assertThat(result.getSelectionValues("facet1").getValues(), hasItem("valueB")); |
---|
89 | assertThat(result.getSelectionValues("facet2").getValues(), hasItem("value:C")); |
---|
90 | // OR explicitly set |
---|
91 | assertEquals(FacetSelectionType.OR, result.getSelectionValues("facet1").getSelectionType()); |
---|
92 | // AND is default |
---|
93 | assertEquals(FacetSelectionType.AND, result.getSelectionValues("facet2").getSelectionType()); |
---|
94 | // NOT_EMPTY explicitly set |
---|
95 | assertEquals(FacetSelectionType.NOT_EMPTY, result.getSelectionValues("facet3").getSelectionType()); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * Test of fromParameters method, of class |
---|
100 | * QueryFacetsSelectionParametersConverter. |
---|
101 | */ |
---|
102 | @Test |
---|
103 | public void testFromParametersSerializable() { |
---|
104 | final PageParameters params = new PageParameters(); |
---|
105 | params.add("q", "query"); |
---|
106 | params.add("q", "ignored"); // only one query param is selected |
---|
107 | params.add("fq", "facet1:valueA"); |
---|
108 | params.add("fq", "facet1:valueB"); |
---|
109 | params.add("fq", "facet2:valueC"); |
---|
110 | params.add("fq", ""); // not a valid facet selection |
---|
111 | params.add("fq", "invalid"); // not a valid facet selection |
---|
112 | |
---|
113 | SerializationUtils.roundtrip(instance.fromParameters(params)); |
---|
114 | } |
---|
115 | |
---|
116 | /** |
---|
117 | * Test of toParameters method, of class |
---|
118 | * QueryFacetsSelectionParametersConverter. |
---|
119 | */ |
---|
120 | @Test |
---|
121 | public void testToParameters() { |
---|
122 | final String query = "query"; |
---|
123 | final Map<String, FacetSelection> map = Maps.newHashMapWithExpectedSize(3); |
---|
124 | map.put("facet1", new FacetSelection(FacetSelectionType.OR, Arrays.asList("valueA", "valueB"))); |
---|
125 | map.put("facet2", new FacetSelection(FacetSelectionType.AND, Collections.singleton("value:C"))); |
---|
126 | map.put("facet3", new FacetSelection(FacetSelectionType.NOT_EMPTY)); |
---|
127 | |
---|
128 | QueryFacetsSelection selection = new QueryFacetsSelection(query, map); |
---|
129 | PageParameters result = instance.toParameters(selection); |
---|
130 | |
---|
131 | assertThat(result.get("q"), equalTo(StringValue.valueOf("query"))); |
---|
132 | |
---|
133 | final List<StringValue> fq = result.getValues("fq"); |
---|
134 | assertNotNull(fq); |
---|
135 | assertThat(fq, hasItem(StringValue.valueOf("facet1:valueA"))); |
---|
136 | assertThat(fq, hasItem(StringValue.valueOf("facet1:valueB"))); |
---|
137 | assertThat(fq, hasItem(StringValue.valueOf("facet2:value:C"))); |
---|
138 | |
---|
139 | final List<StringValue> fqType = result.getValues("fqType"); |
---|
140 | assertNotNull(fqType); |
---|
141 | assertThat(fqType, hasItem(StringValue.valueOf("facet1:or"))); |
---|
142 | //facet 2 is AND, which is default and should not be encoded |
---|
143 | assertThat(fqType, hasItem(StringValue.valueOf("facet3:not_empty"))); |
---|
144 | } |
---|
145 | |
---|
146 | } |
---|