View Javadoc
1   /*
2    * Copyright (c) 2002-2022, 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.service.cache;
35  
36  import java.math.BigInteger;
37  import java.security.SecureRandom;
38  import java.util.HashSet;
39  import java.util.List;
40  import java.util.Locale;
41  import java.util.Random;
42  import java.util.Set;
43  
44  import org.springframework.mock.web.MockHttpServletRequest;
45  
46  import fr.paris.lutece.portal.business.page.Page;
47  import fr.paris.lutece.portal.service.page.PageEvent;
48  import fr.paris.lutece.portal.service.page.PageService;
49  import fr.paris.lutece.portal.service.portal.PortalService;
50  import fr.paris.lutece.portal.service.spring.SpringContextService;
51  import fr.paris.lutece.portal.web.constants.Parameters;
52  import fr.paris.lutece.test.LuteceTestCase;
53  
54  public class PathCacheServiceEnabledTest extends LuteceTestCase
55  {
56  
57      private static final String BEAN_PAGE_SERVICE = "pageService";
58  
59      IPathCacheService service;
60      boolean bEnabled;
61  
62      @Override
63      protected void setUp( ) throws Exception
64      {
65          super.setUp( );
66          service = null;
67          List<CacheableService> serviceList = CacheService.getCacheableServicesList( );
68          for ( CacheableService aService : serviceList )
69          {
70              if ( aService instanceof IPathCacheService )
71              {
72                  service = (IPathCacheService) aService;
73                  bEnabled = aService.isCacheEnable( );
74                  aService.enableCache( true );
75                  aService.resetCache( );
76                  break;
77              }
78          }
79          assertNotNull( service );
80      }
81  
82      @Override
83      protected void tearDown( ) throws Exception
84      {
85          List<CacheableService> serviceList = CacheService.getCacheableServicesList( );
86          for ( CacheableService aService : serviceList )
87          {
88              if ( aService == service )
89              {
90                  aService.resetCache( );
91                  aService.enableCache( bEnabled );
92                  break;
93              }
94          }
95          service = null;
96          super.tearDown( );
97      }
98  
99      public void testGetKey( )
100     {
101         Set<String> keys = new HashSet<>( );
102         for ( String xpageName : new String [ ] {
103                 "name", "name2"
104         } )
105         {
106             for ( int mode : new int [ ] {
107                     0, 1, 2
108             } )
109             {
110                 String key = service.getKey( xpageName, mode, null );
111                 checkKey( keys, key );
112                 MockHttpServletRequest request = new MockHttpServletRequest( );
113                 request.addPreferredLocale( Locale.FRANCE );
114                 key = service.getKey( xpageName, mode, request );
115                 checkKey( keys, key );
116                 request = new MockHttpServletRequest( );
117                 request.addPreferredLocale( Locale.ITALY );
118                 key = service.getKey( xpageName, mode, request );
119                 checkKey( keys, key );
120                 request = new MockHttpServletRequest( );
121                 request.addParameter( Parameters.PAGE_ID, "1" );
122                 key = service.getKey( xpageName, mode, request );
123                 checkKey( keys, key );
124                 request = new MockHttpServletRequest( );
125                 request.addParameter( Parameters.PAGE_ID, "2" );
126                 key = service.getKey( xpageName, mode, request );
127                 checkKey( keys, key );
128                 request = new MockHttpServletRequest( );
129                 request.addParameter( Parameters.PORTLET_ID, "1" );
130                 key = service.getKey( xpageName, mode, request );
131                 checkKey( keys, key );
132                 request = new MockHttpServletRequest( );
133                 request.addParameter( Parameters.PORTLET_ID, "2" );
134                 key = service.getKey( xpageName, mode, request );
135                 checkKey( keys, key );
136             }
137         }
138     }
139 
140     public void testGetKeyWithTitleUrls( )
141     {
142         Set<String> keys = new HashSet<>( );
143         for ( String xpageName : new String [ ] {
144                 "name", "name2"
145         } )
146         {
147             for ( int mode : new int [ ] {
148                     0, 1, 2
149             } )
150             {
151                 for ( String titleUrls : new String [ ] {
152                         null, "title1", "title2"
153                 } )
154                 {
155                     String key = service.getKey( xpageName, mode, titleUrls, null );
156                     checkKey( keys, key );
157                     MockHttpServletRequest request = new MockHttpServletRequest( );
158                     request.addPreferredLocale( Locale.FRANCE );
159                     key = service.getKey( xpageName, mode, titleUrls, request );
160                     checkKey( keys, key );
161                     request = new MockHttpServletRequest( );
162                     request.addPreferredLocale( Locale.ITALY );
163                     key = service.getKey( xpageName, mode, titleUrls, request );
164                     checkKey( keys, key );
165                     request = new MockHttpServletRequest( );
166                     request.addParameter( Parameters.PAGE_ID, "1" );
167                     key = service.getKey( xpageName, mode, titleUrls, request );
168                     checkKey( keys, key );
169                     request = new MockHttpServletRequest( );
170                     request.addParameter( Parameters.PAGE_ID, "2" );
171                     key = service.getKey( xpageName, mode, titleUrls, request );
172                     checkKey( keys, key );
173                     request = new MockHttpServletRequest( );
174                     request.addParameter( Parameters.PORTLET_ID, "1" );
175                     key = service.getKey( xpageName, mode, titleUrls, request );
176                     checkKey( keys, key );
177                     request = new MockHttpServletRequest( );
178                     request.addParameter( Parameters.PORTLET_ID, "2" );
179                     key = service.getKey( xpageName, mode, titleUrls, request );
180                     checkKey( keys, key );
181                 }
182             }
183         }
184     }
185 
186     public void testPutAndGetFromCache( )
187     {
188         service.putInCache( null, "junit" );
189         String key = service.getKey( "junit", 0, null );
190         service.putInCache( key, "junit" );
191         assertEquals( "junit", service.getFromCache( key ) );
192         assertNull( service.getFromCache( null ) );
193         assertNull( service.getFromCache( "NotInCache" ) );
194     }
195 
196     public void testProcessPageEvent( )
197     {
198         String key = service.getKey( "junit", 0, null );
199         service.putInCache( key, "junit" );
200         PageEvent event = new PageEvent( new Page( ), PageEvent.PAGE_CREATED );
201         ( (PathCacheService) service ).processPageEvent( event );
202         assertEquals( "junit", service.getFromCache( key ) );
203         for ( int nEventType : new int [ ] {
204                 PageEvent.PAGE_CONTENT_MODIFIED, PageEvent.PAGE_DELETED, PageEvent.PAGE_MOVED, PageEvent.PAGE_STATE_CHANGED
205         } )
206         {
207             service.putInCache( key, "junit" );
208             event = new PageEvent( new Page( ), nEventType );
209             ( (PathCacheService) service ).processPageEvent( event );
210             assertNull( service.getFromCache( key ) );
211         }
212     }
213 
214     public void testRegisteredPageEventListener( )
215     {
216         String key = service.getKey( "junit", 0, null );
217         service.putInCache( key, "junit" );
218         PageService pageService = SpringContextService.getBean( BEAN_PAGE_SERVICE );
219         Page page = new Page( );
220         page.setName( getRandomName( ) );
221         page.setDescription( page.getName( ) );
222         page.setParentPageId( PortalService.getRootPageId( ) );
223         pageService.createPage( page );
224         try
225         {
226             // FIXME : change when LUTECE-1834 Adding or removing a page blows up all caches
227             // is fixed
228             // assertEquals( "junit", service.getFromCache( key ) );
229             assertNull( service.getFromCache( key ) );
230             service.putInCache( key, "junit" );
231             page.setDescription( page.getName( ) + page.getName( ) );
232             pageService.updatePage( page );
233             assertNull( service.getFromCache( key ) );
234             service.putInCache( key, "junit" );
235         }
236         finally
237         {
238             pageService.removePage( page.getId( ) );
239             assertNull( service.getFromCache( key ) );
240         }
241     }
242 
243     private String getRandomName( )
244     {
245         Random rand = new SecureRandom( );
246         BigInteger bigInt = new BigInteger( 128, rand );
247         return "junit" + bigInt.toString( 36 );
248     }
249 
250     private void checkKey( Set<String> keys, String key )
251     {
252         assertNotNull( key );
253         assertFalse( keys.contains( key ) );
254         keys.add( key );
255     }
256 }