source: vlo/branches/vlo-3.0/vlo-web-app/src/test/java/eu/clarin/cmdi/vlo/wicket/pages/TestFacetedSearchPage.java @ 4652

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

added processing of page parameter to record page (tests needed to be adapted)

File size: 3.7 KB
Line 
1package eu.clarin.cmdi.vlo.wicket.pages;
2
3import eu.clarin.cmdi.vlo.VloWicketApplication;
4import eu.clarin.cmdi.vlo.config.DefaultVloConfigFactory;
5import eu.clarin.cmdi.vlo.config.VloConfigFactory;
6import eu.clarin.cmdi.vlo.config.VloSpringConfig;
7import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection;
8import eu.clarin.cmdi.vlo.service.FacetFieldsService;
9import eu.clarin.cmdi.vlo.service.SolrDocumentService;
10import java.util.Arrays;
11import org.apache.solr.client.solrj.response.FacetField;
12import org.apache.solr.common.SolrDocument;
13import org.apache.wicket.util.tester.WicketTester;
14import org.jmock.Expectations;
15import org.jmock.Mockery;
16import org.jmock.integration.junit4.JUnit4Mockery;
17import org.junit.Before;
18import org.junit.Test;
19import org.junit.runner.RunWith;
20import org.springframework.beans.factory.annotation.Autowired;
21import org.springframework.context.annotation.Bean;
22import org.springframework.context.annotation.Configuration;
23import org.springframework.test.context.ContextConfiguration;
24import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
25import org.springframework.test.context.support.AnnotationConfigContextLoader;
26
27/**
28 * Mock injection based on blog post by Petri Kainulainen found at
29 * {@link http://www.petrikainulainen.net/programming/tips-and-tricks/mocking-spring-beans-with-apache-wicket-and-mockito/}
30 */
31@RunWith(SpringJUnit4ClassRunner.class)
32@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
33public class TestFacetedSearchPage {
34
35    @Configuration
36    static class ContextConfiguration extends VloSpringConfig {
37
38        @Bean
39        public Mockery mockery() {
40            return new JUnit4Mockery();
41        }
42
43        @Override
44        public FacetFieldsService facetFieldsService() {
45            return mockery().mock(FacetFieldsService.class, "facetFieldsService");
46        }
47
48        @Override
49        public SolrDocumentService documentService() {
50            return mockery().mock(SolrDocumentService.class);
51        }
52
53        @Override
54        public VloConfigFactory vloConfigFactory() {
55            //TODO: Separate test config? -> override vloConfig() instead
56            return new DefaultVloConfigFactory();
57        }
58
59    }
60
61    private WicketTester tester;
62    @Autowired(required = true)
63    private VloWicketApplication application;
64    @Autowired(required = true)
65    private Mockery mockery;
66
67    @Autowired(required = true)
68    private FacetFieldsService facetFieldsService;
69
70    @Autowired(required = true)
71    private SolrDocumentService documentService;
72
73    @Before
74    public void setUp() {
75        tester = new WicketTester(application);
76    }
77
78    @Test
79    public void homepageRendersSuccessfully() {
80        // mock behaviour of facet fields service
81        mockery.checking(new Expectations() {
82            {
83                // mock facets
84                atLeast(1).of(facetFieldsService).getFacetFieldCount();
85                will(returnValue(2L));
86                atLeast(1).of(facetFieldsService).getFacetFields(with(any(QueryFacetsSelection.class)));
87                will(returnValue(Arrays.asList(new FacetField("collection"), new FacetField("language"), new FacetField("resource class"))));
88
89                // mock search results
90                atLeast(1).of(documentService).getDocumentCount(with(any(QueryFacetsSelection.class)));
91                will(returnValue(1000L));
92                oneOf(documentService).getDocuments(with(any(QueryFacetsSelection.class)), with(equal(0)), with(equal(10)));
93                will(returnValue(Arrays.asList(new SolrDocument(), new SolrDocument())));
94            }
95        });
96
97        //start and render the test page
98        tester.startPage(FacetedSearchPage.class);
99
100        //assert rendered page class
101        tester.assertRenderedPage(FacetedSearchPage.class);
102    }
103}
Note: See TracBrowser for help on using the repository browser.