source: vlo/trunk/vlo-web-app/src/test/java/eu/clarin/cmdi/vlo/wicket/pages/TestFacetedSearchPage.java @ 6813

Last change on this file since 6813 was 6813, checked in by davor.ostojic@oeaw.ac.at, 9 years ago

#795 VLO web-app generates unnecessary requests. The idea behind is to fire SOLR query only when selection is changed.

File size: 4.5 KB
Line 
1package eu.clarin.cmdi.vlo.wicket.pages;
2
3import eu.clarin.cmdi.vlo.VloApplicationTestConfig;
4import eu.clarin.cmdi.vlo.VloWicketApplication;
5import eu.clarin.cmdi.vlo.config.VloServicesSpringConfig;
6import eu.clarin.cmdi.vlo.config.VloSolrSpringConfig;
7import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
8import eu.clarin.cmdi.vlo.service.solr.FacetFieldsService;
9import eu.clarin.cmdi.vlo.service.solr.SolrDocumentService;
10import java.util.Arrays;
11import java.util.List;
12import javax.inject.Inject;
13import org.apache.solr.client.solrj.response.FacetField;
14import org.apache.solr.common.SolrDocument;
15import org.apache.wicket.util.tester.WicketTester;
16import org.jmock.Expectations;
17import org.jmock.Mockery;
18import org.jmock.integration.junit4.JUnit4Mockery;
19import org.junit.Before;
20import org.junit.Test;
21import org.junit.runner.RunWith;
22import org.springframework.context.annotation.Bean;
23import org.springframework.context.annotation.Configuration;
24import org.springframework.context.annotation.Import;
25import org.springframework.test.context.ContextConfiguration;
26import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
27import org.springframework.test.context.support.AnnotationConfigContextLoader;
28
29/**
30 * Mock injection based on blog post by Petri Kainulainen found at
31 * {@link http://www.petrikainulainen.net/programming/tips-and-tricks/mocking-spring-beans-with-apache-wicket-and-mockito/}
32 */
33@RunWith(SpringJUnit4ClassRunner.class)
34@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
35public class TestFacetedSearchPage {
36
37    @Inject
38    private VloWicketApplication application;
39    @Inject
40    private Mockery mockery;
41    @Inject
42    private FacetFieldsService facetFieldsService;
43    @Inject
44    private SolrDocumentService documentService;
45
46    private WicketTester tester;
47
48    @Before
49    public void setUp() {
50        tester = new WicketTester(application);
51    }
52
53    @Test
54    public void homepageRendersSuccessfully() {
55        // mock behaviour of facet fields service
56        mockery.checking(new Expectations() {
57            {
58                // mock facets
59                atLeast(1).of(facetFieldsService).getFacetFieldCount(with(any(List.class)));
60                will(returnValue(2L));
61                atLeast(1).of(facetFieldsService).getFacetFields(with(any(QueryFacetsSelection.class)), with(any(List.class)), with(any(Integer.class)));
62                will(returnValue(Arrays.asList(
63                                new FacetField("languageCode"),
64                                new FacetField("collection"),
65                                new FacetField("resourceClass"),
66                                new FacetField("country"),
67                                new FacetField("modality"),
68                                new FacetField("genre"), 
69                                new FacetField("subject"),
70                                new FacetField("format"),
71                                new FacetField("organisation"),
72                                new FacetField("availability"),
73                                new FacetField("nationalProject"),
74                                new FacetField("keywords"),
75                                new FacetField("dataProvider")
76                )));
77
78                // mock search results
79                atLeast(1).of(documentService).getDocumentCount(with(any(QueryFacetsSelection.class)));
80                will(returnValue(1000L));
81                oneOf(documentService).getDocuments(with(any(QueryFacetsSelection.class)), with(equal(0)), with(equal(10)));
82                will(returnValue(Arrays.asList(new SolrDocument(), new SolrDocument())));
83            }
84        });
85
86        //start and render the test page
87        tester.startPage(FacetedSearchPage.class);
88
89        //assert rendered page class
90        tester.assertRenderedPage(FacetedSearchPage.class);
91    }
92
93    /**
94     * Custom configuration injected into web app for testing
95     */
96    @Configuration
97    @Import({
98        VloSolrTestConfig.class,
99        VloApplicationTestConfig.class,
100        VloServicesSpringConfig.class})
101    static class ContextConfiguration {
102
103        @Bean
104        public Mockery mockery() {
105            // shared mockery context
106            return new JUnit4Mockery();
107        }
108    }
109
110    /**
111     * Provides some mock Solr services
112     */
113    @Configuration
114    static class VloSolrTestConfig extends VloSolrSpringConfig {
115
116        @Inject
117        private Mockery mockery;
118
119        @Override
120        public SolrDocumentService documentService() {
121            return mockery.mock(SolrDocumentService.class);
122        }
123
124        @Override
125        public FacetFieldsService facetFieldsService() {
126            return mockery.mock(FacetFieldsService.class, "facetFieldsService");
127        }
128    }
129}
Note: See TracBrowser for help on using the repository browser.