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.FacetConstants; |
---|
20 | import eu.clarin.cmdi.vlo.config.VloConfig; |
---|
21 | import eu.clarin.cmdi.vlo.importer.CMDIData; |
---|
22 | import java.io.File; |
---|
23 | import java.util.List; |
---|
24 | import org.apache.solr.client.solrj.SolrQuery; |
---|
25 | import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; |
---|
26 | import org.apache.solr.client.solrj.response.FacetField; |
---|
27 | import org.apache.solr.common.SolrInputDocument; |
---|
28 | import org.apache.solr.util.AbstractSolrTestCase; |
---|
29 | import org.hamcrest.Matchers; |
---|
30 | import org.junit.After; |
---|
31 | import org.junit.Before; |
---|
32 | import org.junit.Test; |
---|
33 | import static org.hamcrest.Matchers.*; |
---|
34 | |
---|
35 | /** |
---|
36 | * Example taken from |
---|
37 | * {@link http://blog.synyx.de/2011/01/integration-tests-for-your-solr-config/} |
---|
38 | * |
---|
39 | * @author twagoo |
---|
40 | */ |
---|
41 | public class SearchResultsDaoImplTest extends AbstractSolrTestCase { |
---|
42 | |
---|
43 | private EmbeddedSolrServer server; |
---|
44 | private SearchResultsDaoImpl instance; |
---|
45 | |
---|
46 | @Before |
---|
47 | @Override |
---|
48 | public void setUp() throws Exception { |
---|
49 | super.setUp(); |
---|
50 | initCore(getResourcePath(getConfigString()), getResourcePath(getSchemaString())); |
---|
51 | server = new EmbeddedSolrServer(h.getCoreContainer(), h.getCore().getName()); |
---|
52 | instance = new SearchResultsDaoImpl(server, new VloConfig()); |
---|
53 | } |
---|
54 | |
---|
55 | @After |
---|
56 | @Override |
---|
57 | public void tearDown() throws Exception { |
---|
58 | super.tearDown(); |
---|
59 | if (server != null) { |
---|
60 | server.shutdown(); |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * Test of getFacets method, of class SearchResultsDaoImpl. |
---|
66 | */ |
---|
67 | @Test |
---|
68 | public void testGetFacets() throws Exception { |
---|
69 | int id = 1; |
---|
70 | CMDIData cmdiData = new CMDIData(); |
---|
71 | cmdiData.addDocField(FacetConstants.FIELD_COLLECTION, "Collection1", false); |
---|
72 | cmdiData.addDocField(FacetConstants.FIELD_COUNTRY, "Country1", false); |
---|
73 | SolrInputDocument document = cmdiData.getSolrDocument(); |
---|
74 | document.addField("id", Integer.toString(id++)); |
---|
75 | server.add(document); |
---|
76 | server.commit(); |
---|
77 | |
---|
78 | cmdiData = new CMDIData(); |
---|
79 | cmdiData.addDocField(FacetConstants.FIELD_COLLECTION, "Collection1", false); |
---|
80 | cmdiData.addDocField(FacetConstants.FIELD_COUNTRY, "Country2", false); |
---|
81 | document = cmdiData.getSolrDocument(); |
---|
82 | document.addField("id", Integer.toString(id++)); |
---|
83 | server.add(document); |
---|
84 | server.commit(); |
---|
85 | |
---|
86 | SolrQuery query = new SolrQuery(); |
---|
87 | query.setRows(10); |
---|
88 | query.setStart(0); |
---|
89 | query.setFields(FacetConstants.FIELD_NAME, FacetConstants.FIELD_ID, FacetConstants.FIELD_DESCRIPTION); |
---|
90 | query.setQuery("*:*"); |
---|
91 | |
---|
92 | query.setFacet(true); |
---|
93 | query.setFacetMinCount(1); |
---|
94 | query.addFacetField(FacetConstants.FIELD_COLLECTION, FacetConstants.FIELD_COUNTRY); |
---|
95 | |
---|
96 | List<FacetField> facetFields = instance.getFacets(query); |
---|
97 | assertNotNull(facetFields); |
---|
98 | assertEquals(2, facetFields.size()); |
---|
99 | |
---|
100 | // 1 collection |
---|
101 | assertThat(facetFields, Matchers.<FacetField>hasItem(Matchers.allOf( |
---|
102 | Matchers.<FacetField>hasProperty("name", equalTo(FacetConstants.FIELD_COLLECTION)), |
---|
103 | Matchers.<FacetField>hasProperty("valueCount", equalTo(1))))); |
---|
104 | |
---|
105 | // 2 countries |
---|
106 | assertThat(facetFields, Matchers.<FacetField>hasItem(Matchers.allOf( |
---|
107 | Matchers.<FacetField>hasProperty("name", equalTo(FacetConstants.FIELD_COUNTRY)), |
---|
108 | Matchers.<FacetField>hasProperty("valueCount", equalTo(2))))); |
---|
109 | } |
---|
110 | |
---|
111 | public static String getSchemaString() { |
---|
112 | return "/solr/collection1/conf/schema.xml"; |
---|
113 | } |
---|
114 | |
---|
115 | public static String getConfigString() { |
---|
116 | return "/solr/collection1/conf/solrconfig.xml"; |
---|
117 | } |
---|
118 | |
---|
119 | public static String getResourcePath(String resource) throws Exception { |
---|
120 | return new File(SearchResultsDaoImplTest.class.getResource(resource).toURI()).getAbsolutePath(); |
---|
121 | } |
---|
122 | } |
---|