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 com.google.common.collect.ImmutableList; |
---|
20 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_COLLECTION; |
---|
21 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_COUNTRY; |
---|
22 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_DESCRIPTION; |
---|
23 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_ID; |
---|
24 | import static eu.clarin.cmdi.vlo.FacetConstants.FIELD_NAME; |
---|
25 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
26 | import eu.clarin.cmdi.vlo.importer.CMDIData; |
---|
27 | import java.io.File; |
---|
28 | import java.util.List; |
---|
29 | import org.apache.solr.client.solrj.SolrQuery; |
---|
30 | import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; |
---|
31 | import org.apache.solr.client.solrj.response.FacetField; |
---|
32 | import org.apache.solr.common.SolrDocumentList; |
---|
33 | import org.apache.solr.common.SolrInputDocument; |
---|
34 | import org.apache.solr.util.AbstractSolrTestCase; |
---|
35 | import org.hamcrest.Matchers; |
---|
36 | import org.junit.After; |
---|
37 | import org.junit.Before; |
---|
38 | import org.junit.Test; |
---|
39 | import static org.hamcrest.Matchers.*; |
---|
40 | |
---|
41 | /** |
---|
42 | * Example taken from |
---|
43 | * {@link http://blog.synyx.de/2011/01/integration-tests-for-your-solr-config/} |
---|
44 | * |
---|
45 | * @author twagoo |
---|
46 | */ |
---|
47 | public class SearchResultsDaoImplTest extends AbstractSolrTestCase { |
---|
48 | |
---|
49 | private EmbeddedSolrServer server; |
---|
50 | private SearchResultsDaoImpl instance; |
---|
51 | |
---|
52 | @Before |
---|
53 | @Override |
---|
54 | public void setUp() throws Exception { |
---|
55 | // set up an embedded solr server |
---|
56 | super.setUp(); |
---|
57 | initCore(getResourcePath(getConfigString()), getResourcePath(getSchemaString())); |
---|
58 | server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName()); |
---|
59 | instance = new SearchResultsDaoImpl(server, new VloConfig() { |
---|
60 | |
---|
61 | @Override |
---|
62 | public List<String> getAllFacetFields() { |
---|
63 | return ImmutableList.of(FIELD_COLLECTION, FIELD_COUNTRY); |
---|
64 | } |
---|
65 | |
---|
66 | }); |
---|
67 | |
---|
68 | // add some documents |
---|
69 | int id = 1; |
---|
70 | CMDIData cmdiData = new CMDIData(); |
---|
71 | cmdiData.addDocField(FIELD_COLLECTION, "Collection1", false); |
---|
72 | cmdiData.addDocField(FIELD_COUNTRY, "Country1", false); |
---|
73 | SolrInputDocument document = cmdiData.getSolrDocument(); |
---|
74 | document.addField("id", Integer.toString(id++)); |
---|
75 | server.add(document); |
---|
76 | |
---|
77 | cmdiData = new CMDIData(); |
---|
78 | cmdiData.addDocField(FIELD_COLLECTION, "Collection1", false); |
---|
79 | cmdiData.addDocField(FIELD_COUNTRY, "Country2", false); |
---|
80 | document = cmdiData.getSolrDocument(); |
---|
81 | document.addField("id", Integer.toString(id++)); |
---|
82 | server.add(document); |
---|
83 | |
---|
84 | server.commit(); |
---|
85 | } |
---|
86 | |
---|
87 | @After |
---|
88 | @Override |
---|
89 | public void tearDown() throws Exception { |
---|
90 | super.tearDown(); |
---|
91 | if (server != null) { |
---|
92 | server.shutdown(); |
---|
93 | } |
---|
94 | } |
---|
95 | |
---|
96 | /** |
---|
97 | * Test of getFacets method, of class SearchResultsDaoImpl. |
---|
98 | * |
---|
99 | * @throws java.lang.Exception |
---|
100 | */ |
---|
101 | @Test |
---|
102 | public void testGetFacets() throws Exception { |
---|
103 | final SolrQuery query = new SolrQuery(); |
---|
104 | query.setRows(10); |
---|
105 | query.setStart(0); |
---|
106 | query.setFields(FIELD_NAME, FIELD_ID, FIELD_DESCRIPTION); |
---|
107 | query.setQuery(null); |
---|
108 | |
---|
109 | query.setFacet(true); |
---|
110 | query.setFacetMinCount(1); |
---|
111 | query.addFacetField(FIELD_COLLECTION, FIELD_COUNTRY); |
---|
112 | |
---|
113 | List<FacetField> facetFields = instance.getFacets(query); |
---|
114 | assertNotNull(facetFields); |
---|
115 | assertEquals(2, facetFields.size()); |
---|
116 | |
---|
117 | // 1 collection |
---|
118 | assertThat(facetFields, Matchers.<FacetField>hasItem(Matchers.allOf( |
---|
119 | Matchers.<FacetField>hasProperty("name", equalTo(FIELD_COLLECTION)), |
---|
120 | Matchers.<FacetField>hasProperty("valueCount", equalTo(1))))); |
---|
121 | |
---|
122 | // 2 countries |
---|
123 | assertThat(facetFields, Matchers.<FacetField>hasItem(Matchers.allOf( |
---|
124 | Matchers.<FacetField>hasProperty("name", equalTo(FIELD_COUNTRY)), |
---|
125 | Matchers.<FacetField>hasProperty("valueCount", equalTo(2))))); |
---|
126 | } |
---|
127 | |
---|
128 | @Test |
---|
129 | public void testGetDocuments() { |
---|
130 | // get all documents |
---|
131 | SolrQuery query = new SolrQuery(); |
---|
132 | query.setRows(10); |
---|
133 | query.setStart(0); |
---|
134 | query.setFields(FIELD_NAME, FIELD_ID, FIELD_DESCRIPTION); |
---|
135 | { |
---|
136 | // all documents should match this |
---|
137 | SolrDocumentList documents = instance.getDocuments(query); |
---|
138 | assertEquals(2, documents.getNumFound()); |
---|
139 | } |
---|
140 | |
---|
141 | // get document with specific field value |
---|
142 | query.setQuery(FIELD_COUNTRY + ":Country1"); |
---|
143 | { |
---|
144 | // only document with id "1" should match this |
---|
145 | SolrDocumentList documents = instance.getDocuments(query); |
---|
146 | assertEquals(1, documents.getNumFound()); |
---|
147 | assertEquals("1", documents.get(0).getFieldValue(FIELD_ID)); |
---|
148 | } |
---|
149 | |
---|
150 | query.setFilterQueries(FIELD_COLLECTION + ":Collection2"); |
---|
151 | { |
---|
152 | // no matches |
---|
153 | SolrDocumentList documents = instance.getDocuments(query); |
---|
154 | assertEquals(0, documents.getNumFound()); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | public static String getSchemaString() { |
---|
159 | return "/solr/collection1/conf/schema.xml"; |
---|
160 | } |
---|
161 | |
---|
162 | public static String getConfigString() { |
---|
163 | return "/solr/collection1/conf/solrconfig.xml"; |
---|
164 | } |
---|
165 | |
---|
166 | public static String getResourcePath(String resource) throws Exception { |
---|
167 | return new File(SearchResultsDaoImplTest.class.getResource(resource).toURI()).getAbsolutePath(); |
---|
168 | } |
---|
169 | } |
---|