source: vlo/branches/vlo-3.0/vlo-web-app/src/test/java/eu/clarin/cmdi/vlo/service/impl/SearchResultsDaoImplTest.java @ 4661

Last change on this file since 4661 was 4661, checked in by Twan Goosen, 10 years ago

Package structure: separated solr services from other services and moved all panels into new wicket.panels package

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