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 com.google.common.collect.ImmutableList; |
---|
20 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
21 | import eu.clarin.cmdi.vlo.service.solr.FacetFieldsService; |
---|
22 | import java.util.Arrays; |
---|
23 | import java.util.Collections; |
---|
24 | import java.util.List; |
---|
25 | import org.apache.solr.client.solrj.response.FacetField; |
---|
26 | import org.apache.wicket.model.IModel; |
---|
27 | import org.apache.wicket.model.Model; |
---|
28 | import org.jmock.Expectations; |
---|
29 | import static org.jmock.Expectations.returnValue; |
---|
30 | import org.jmock.Mockery; |
---|
31 | import org.jmock.integration.junit4.JUnit4Mockery; |
---|
32 | import static org.junit.Assert.*; |
---|
33 | import org.junit.Before; |
---|
34 | import org.junit.Test; |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | * @author twagoo |
---|
39 | */ |
---|
40 | public class FacetFieldModelTest { |
---|
41 | |
---|
42 | private final Mockery context = new JUnit4Mockery(); |
---|
43 | |
---|
44 | private IModel<QueryFacetsSelection> selectionModel; |
---|
45 | private QueryFacetsSelection selection; |
---|
46 | private FacetFieldsService service; |
---|
47 | |
---|
48 | @Before |
---|
49 | public void setUp() { |
---|
50 | service = context.mock(FacetFieldsService.class); |
---|
51 | selection = new QueryFacetsSelection(); |
---|
52 | selectionModel = new Model(selection); |
---|
53 | } |
---|
54 | |
---|
55 | /** |
---|
56 | * Test of load method, of class FacetFieldsModel. |
---|
57 | */ |
---|
58 | @Test |
---|
59 | public void testGetObject() { |
---|
60 | final FacetFieldModel instance = new FacetFieldModel("facet4", new FacetFieldsModel(service, ImmutableList.of("facet4"), selectionModel, 20)); |
---|
61 | |
---|
62 | context.checking(new Expectations() { |
---|
63 | { |
---|
64 | oneOf(service).getFacetFields(selection, ImmutableList.of("facet4"), 20); |
---|
65 | will(returnValue(Arrays.asList( |
---|
66 | new FacetField("facet4") |
---|
67 | ))); |
---|
68 | } |
---|
69 | }); |
---|
70 | |
---|
71 | final FacetField result = instance.getObject(); |
---|
72 | // selection should be returned |
---|
73 | assertEquals("facet4", result.getName()); |
---|
74 | |
---|
75 | // make another call, should not trigger a call to service because model is loadabledetachable |
---|
76 | final FacetField result2 = instance.getObject(); |
---|
77 | assertEquals(result, result2); |
---|
78 | } |
---|
79 | |
---|
80 | /** |
---|
81 | * Test of load method, of class FacetFieldsModel. |
---|
82 | */ |
---|
83 | @Test |
---|
84 | public void testGetObjectNotIncluded() { |
---|
85 | final FacetFieldModel instance = new FacetFieldModel("facet4", new FacetFieldsModel(service, ImmutableList.of("facet4"), selectionModel, 20)); |
---|
86 | |
---|
87 | context.checking(new Expectations() { |
---|
88 | { |
---|
89 | oneOf(service).getFacetFields(selection, ImmutableList.of("facet4"), 20); |
---|
90 | will(returnValue(Collections.emptyList())); |
---|
91 | } |
---|
92 | }); |
---|
93 | |
---|
94 | final FacetField result = instance.getObject(); |
---|
95 | // selection should be returned |
---|
96 | assertNull(result); |
---|
97 | } |
---|
98 | |
---|
99 | } |
---|