1 | package eu.clarin.cmdi.vlo.wicket.pages; |
---|
2 | |
---|
3 | import eu.clarin.cmdi.vlo.VloApplicationTestConfig; |
---|
4 | import eu.clarin.cmdi.vlo.VloWicketApplication; |
---|
5 | import eu.clarin.cmdi.vlo.config.VloServicesSpringConfig; |
---|
6 | import eu.clarin.cmdi.vlo.config.VloSolrSpringConfig; |
---|
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 java.util.List; |
---|
12 | import javax.inject.Inject; |
---|
13 | import org.apache.solr.client.solrj.response.FacetField; |
---|
14 | import org.apache.solr.common.SolrDocument; |
---|
15 | import org.apache.wicket.util.tester.WicketTester; |
---|
16 | import org.jmock.Expectations; |
---|
17 | import org.jmock.Mockery; |
---|
18 | import org.jmock.integration.junit4.JUnit4Mockery; |
---|
19 | import org.junit.Before; |
---|
20 | import org.junit.Test; |
---|
21 | import org.junit.runner.RunWith; |
---|
22 | import org.springframework.context.annotation.Bean; |
---|
23 | import org.springframework.context.annotation.Configuration; |
---|
24 | import org.springframework.context.annotation.Import; |
---|
25 | import org.springframework.test.context.ContextConfiguration; |
---|
26 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
27 | import 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) |
---|
35 | public 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 | } |
---|