source: vlo/branches/vlo-3.0/vlo-web-app/src/test/java/eu/clarin/cmdi/vlo/wicket/model/FacetFieldsModelTest.java @ 4590

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

added a subcase to FacetFieldsModel? test

File size: 3.2 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.wicket.model;
18
19import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
20import eu.clarin.cmdi.vlo.service.FacetFieldsService;
21import java.util.Arrays;
22import java.util.List;
23import org.apache.solr.client.solrj.response.FacetField;
24import org.apache.wicket.model.IModel;
25import org.apache.wicket.model.Model;
26import org.hamcrest.Matcher;
27import org.hamcrest.Matchers;
28import org.jmock.Expectations;
29import org.jmock.Mockery;
30import org.jmock.integration.junit4.JUnit4Mockery;
31import org.junit.Test;
32import static org.junit.Assert.*;
33import static org.hamcrest.Matchers.*;
34
35/**
36 *
37 * @author twagoo
38 */
39public class FacetFieldsModelTest {
40
41    private final Mockery context = new JUnit4Mockery();
42
43    /**
44     * Test of load method, of class FacetFieldsModel.
45     */
46    @Test
47    public void testGetObject() {
48        final FacetFieldsService service = context.mock(FacetFieldsService.class);
49        final QueryFacetsSelection selection = new QueryFacetsSelection();
50        final IModel<QueryFacetsSelection> selectionModel = new Model(selection);
51        final List<String> facets = Arrays.asList("facet1", "facet2", "facetX");
52        final FacetFieldsModel instance = new FacetFieldsModel(service, facets, selectionModel);
53
54        context.checking(new Expectations() {
55            {
56                oneOf(service).getFacetFields(selection);
57                will(returnValue(Arrays.asList(
58                        new FacetField("facet1"),
59                        new FacetField("facet2"),
60                        new FacetField("facet3"),
61                        new FacetField("facet4")
62                )));
63            }
64        });
65
66        final List<FacetField> result = instance.getObject();
67        // included facets
68        assertThat(result, hasFacetField("facet1")); // in selection
69        assertThat(result, hasFacetField("facet2")); // in selection
70        // excluded facets
71        assertThat(result, not(hasFacetField("facet3"))); // not in selection
72        assertThat(result, not(hasFacetField("facet4"))); // not in selection
73        assertThat(result, not(hasFacetField("facetX"))); // not in result
74
75        // make another call, should not trigger a call to service because model is loadabledetachable
76        final List<FacetField> result2 = instance.getObject();
77        assertEquals(result, result2);
78    }
79
80    private static Matcher<Iterable<FacetField>> hasFacetField(String facetName) {
81        return Matchers.<FacetField>hasItem(Matchers.<FacetField>hasProperty("name", equalTo(facetName)));
82    }
83
84}
Note: See TracBrowser for help on using the repository browser.