1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.portal.web.search;
35
36 import java.io.IOException;
37 import java.util.List;
38
39 import org.apache.lucene.document.Document;
40 import org.springframework.mock.web.MockHttpServletRequest;
41
42 import fr.paris.lutece.portal.business.user.AdminUser;
43 import fr.paris.lutece.portal.service.admin.AccessDeniedException;
44 import fr.paris.lutece.portal.service.daemon.AppDaemonService;
45 import fr.paris.lutece.portal.service.daemon.DaemonEntry;
46 import fr.paris.lutece.portal.service.message.SiteMessageException;
47 import fr.paris.lutece.portal.service.search.IndexationService;
48 import fr.paris.lutece.portal.service.search.SearchIndexer;
49 import fr.paris.lutece.portal.service.security.SecurityTokenService;
50 import fr.paris.lutece.test.LuteceTestCase;
51 import fr.paris.lutece.test.Utils;
52
53
54
55
56
57 public class SearchIndexationJspBeanTest extends LuteceTestCase
58 {
59 private TestSearchIndexer testIndexer;
60 private Boolean bIndexDaemonInitialState;
61
62 private static final class TestSearchIndexer implements SearchIndexer
63 {
64 private boolean _bIndexDocumentsCalled;
65
66 @Override
67 public boolean isEnable( )
68 {
69 return true;
70 }
71
72 @Override
73 public void indexDocuments( ) throws IOException, InterruptedException, SiteMessageException
74 {
75 _bIndexDocumentsCalled = true;
76 }
77
78 @Override
79 public String getVersion( )
80 {
81 return "1.0.0";
82 }
83
84 @Override
85 public String getSpecificSearchAppUrl( )
86 {
87 return null;
88 }
89
90 @Override
91 public String getName( )
92 {
93 return this.getClass( ).getCanonicalName( );
94 }
95
96 @Override
97 public List<String> getListType( )
98 {
99 return null;
100 }
101
102 @Override
103 public List<Document> getDocuments( String strIdDocument ) throws IOException, InterruptedException, SiteMessageException
104 {
105 return null;
106 }
107
108 @Override
109 public String getDescription( )
110 {
111 return "junit test indexer";
112 }
113 }
114
115 @Override
116 protected void setUp( ) throws Exception
117 {
118 super.setUp( );
119
120
121 for ( DaemonEntry daemonEntry : AppDaemonService.getDaemonEntries( ) )
122 {
123 if ( daemonEntry.getId( ).equals( "indexer" ) )
124 {
125 bIndexDaemonInitialState = daemonEntry.isRunning( );
126 break;
127 }
128 }
129 assertNotNull( "Did not find indexer daemon", bIndexDaemonInitialState );
130 AppDaemonService.stopDaemon( "indexer" );
131 testIndexer = new TestSearchIndexer( );
132 IndexationService.registerIndexer( testIndexer );
133
134 }
135
136 @Override
137 protected void tearDown( ) throws Exception
138 {
139 IndexationService.unregisterIndexer( testIndexer );
140 if ( bIndexDaemonInitialState.booleanValue( ) )
141 {
142 AppDaemonService.startDaemon( "indexer" );
143 }
144 super.tearDown( );
145 }
146
147
148
149
150 public void testGetIndexingProperties( ) throws AccessDeniedException
151 {
152 MockHttpServletRequest request = new MockHttpServletRequest( );
153 Utils.registerAdminUserWithRigth( request, new AdminUser( ), SearchIndexationJspBean.RIGHT_INDEXER );
154
155 SearchIndexationJspBean instance = new SearchIndexationJspBean( );
156 instance.init( request, SearchIndexationJspBean.RIGHT_INDEXER );
157 assertNotNull( instance.getIndexingProperties( request ) );
158 }
159
160
161
162
163 public void testDoIndexing( ) throws AccessDeniedException
164 {
165 MockHttpServletRequest request = new MockHttpServletRequest( );
166 SearchIndexationJspBean instance = new SearchIndexationJspBean( );
167 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
168 SecurityTokenService.getInstance( ).getToken( request, "admin/search/manage_search_indexation.html" ) );
169
170 Utils.registerAdminUserWithRigth( request, new AdminUser( ), SearchIndexationJspBean.RIGHT_INDEXER );
171 instance.init( request, SearchIndexationJspBean.RIGHT_INDEXER );
172 instance.doIndexing( request );
173 assertTrue( testIndexer._bIndexDocumentsCalled );
174 }
175
176 public void testDoIndexingInvalidToken( ) throws AccessDeniedException
177 {
178 MockHttpServletRequest request = new MockHttpServletRequest( );
179 SearchIndexationJspBean instance = new SearchIndexationJspBean( );
180 request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
181 SecurityTokenService.getInstance( ).getToken( request, "admin/search/manage_search_indexation.html" ) + "b" );
182
183 Utils.registerAdminUserWithRigth( request, new AdminUser( ), SearchIndexationJspBean.RIGHT_INDEXER );
184 instance.init( request, SearchIndexationJspBean.RIGHT_INDEXER );
185 try
186 {
187 instance.doIndexing( request );
188 fail( "Should have thrown" );
189 }
190 catch( AccessDeniedException e )
191 {
192 assertFalse( testIndexer._bIndexDocumentsCalled );
193 }
194 }
195
196 public void testDoIndexingNoToken( ) throws AccessDeniedException
197 {
198 MockHttpServletRequest request = new MockHttpServletRequest( );
199 SearchIndexationJspBean instance = new SearchIndexationJspBean( );
200
201 Utils.registerAdminUserWithRigth( request, new AdminUser( ), SearchIndexationJspBean.RIGHT_INDEXER );
202 instance.init( request, SearchIndexationJspBean.RIGHT_INDEXER );
203 try
204 {
205 instance.doIndexing( request );
206 fail( "Should have thrown" );
207 }
208 catch( AccessDeniedException e )
209 {
210 assertFalse( testIndexer._bIndexDocumentsCalled );
211 }
212 }
213 }