View Javadoc
1   /*
2    * Copyright (c) 2002-2025, City of Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
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   * SearchIndexationJspBean Test Class
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         // we stop the indexer daemon so that it does not interfere with the
120         // tests
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      * Test of getIndexingProperties method, of class fr.paris.lutece.portal.web.search.SearchIndexationJspBean.
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      * Test of doIndexing method, of class fr.paris.lutece.portal.web.search.SearchIndexationJspBean.
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 }