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.wicket.model; |
---|
18 | |
---|
19 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
20 | import eu.clarin.cmdi.vlo.service.FacetFieldsService; |
---|
21 | import java.util.Arrays; |
---|
22 | import java.util.List; |
---|
23 | import org.apache.solr.client.solrj.response.FacetField; |
---|
24 | import org.apache.wicket.model.IModel; |
---|
25 | import org.apache.wicket.model.Model; |
---|
26 | import org.hamcrest.Matcher; |
---|
27 | import org.hamcrest.Matchers; |
---|
28 | import org.jmock.Expectations; |
---|
29 | import org.jmock.Mockery; |
---|
30 | import org.jmock.integration.junit4.JUnit4Mockery; |
---|
31 | import org.junit.Test; |
---|
32 | import static org.junit.Assert.*; |
---|
33 | import static org.hamcrest.Matchers.*; |
---|
34 | |
---|
35 | /** |
---|
36 | * |
---|
37 | * @author twagoo |
---|
38 | */ |
---|
39 | public 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 | } |
---|