1 | package eu.clarin.cmdi.vlo.wicket.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.VloWicketApplication; |
---|
4 | import eu.clarin.cmdi.vlo.config.DefaultVloConfigFactory; |
---|
5 | import eu.clarin.cmdi.vlo.config.VloConfigFactory; |
---|
6 | import eu.clarin.cmdi.vlo.config.VloSpringConfig; |
---|
7 | import eu.clarin.cmdi.vlo.pojo.QueryFacetsSelection; |
---|
8 | import eu.clarin.cmdi.vlo.service.solr.FacetFieldsService; |
---|
9 | import eu.clarin.cmdi.vlo.service.solr.SolrDocumentService; |
---|
10 | import java.util.Arrays; |
---|
11 | import org.apache.solr.client.solrj.response.FacetField; |
---|
12 | import org.apache.solr.common.SolrDocument; |
---|
13 | import org.apache.wicket.util.tester.WicketTester; |
---|
14 | import org.jmock.Expectations; |
---|
15 | import org.jmock.Mockery; |
---|
16 | import org.jmock.integration.junit4.JUnit4Mockery; |
---|
17 | import org.junit.Before; |
---|
18 | import org.junit.Test; |
---|
19 | import org.junit.runner.RunWith; |
---|
20 | import org.springframework.beans.factory.annotation.Autowired; |
---|
21 | import org.springframework.context.annotation.Bean; |
---|
22 | import org.springframework.context.annotation.Configuration; |
---|
23 | import org.springframework.test.context.ContextConfiguration; |
---|
24 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
25 | import 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) |
---|
33 | public 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 | } |
---|