View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de 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.plugins.childpages.business.portlet;
35  
36  import java.io.IOException;
37  import java.io.PrintWriter;
38  import java.security.SecureRandom;
39  import java.util.Collection;
40  import java.util.List;
41  import java.util.Locale;
42  
43  import javax.servlet.ServletOutputStream;
44  import javax.servlet.http.Cookie;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.junit.After;
48  import org.junit.Before;
49  import org.junit.Test;
50  
51  import fr.paris.lutece.portal.business.page.Page;
52  import fr.paris.lutece.portal.business.page.PageHome;
53  import fr.paris.lutece.portal.business.portlet.Portlet;
54  import fr.paris.lutece.portal.business.portlet.PortletHome;
55  import fr.paris.lutece.portal.business.style.PageTemplateHome;
56  import fr.paris.lutece.portal.service.message.SiteMessageException;
57  import fr.paris.lutece.portal.service.page.IPageService;
58  import fr.paris.lutece.portal.service.portal.PortalService;
59  import fr.paris.lutece.portal.service.spring.SpringContextService;
60  import fr.paris.lutece.portal.web.LocalVariables;
61  import fr.paris.lutece.test.LuteceTestCase;
62  import fr.paris.lutece.test.MokeHttpServletRequest;
63  
64  public class EventListenerTest extends LuteceTestCase
65  {
66      // FIXME : remove when LUTECE-1838 is resolved
67      private static final class MokeHttpServletResponse implements
68              HttpServletResponse
69      {
70          @Override
71          public void setLocale( Locale loc )
72          {
73              // TODO Auto-generated method stub
74  
75          }
76  
77          @Override
78          public void setContentType( String type )
79          {
80              // TODO Auto-generated method stub
81  
82          }
83  
84          @Override
85          public void setContentLength( int len )
86          {
87              // TODO Auto-generated method stub
88  
89          }
90  
91          @Override
92          public void setCharacterEncoding( String charset )
93          {
94              // TODO Auto-generated method stub
95  
96          }
97  
98          @Override
99          public void setBufferSize( int size )
100         {
101             // TODO Auto-generated method stub
102 
103         }
104 
105         @Override
106         public void resetBuffer( )
107         {
108             // TODO Auto-generated method stub
109 
110         }
111 
112         @Override
113         public void reset( )
114         {
115             // TODO Auto-generated method stub
116 
117         }
118 
119         @Override
120         public boolean isCommitted( )
121         {
122             // TODO Auto-generated method stub
123             return false;
124         }
125 
126         @Override
127         public PrintWriter getWriter( ) throws IOException
128         {
129             // TODO Auto-generated method stub
130             return null;
131         }
132 
133         @Override
134         public ServletOutputStream getOutputStream( ) throws IOException
135         {
136             // TODO Auto-generated method stub
137             return null;
138         }
139 
140         @Override
141         public Locale getLocale( )
142         {
143             // TODO Auto-generated method stub
144             return null;
145         }
146 
147         @Override
148         public String getContentType( )
149         {
150             // TODO Auto-generated method stub
151             return null;
152         }
153 
154         @Override
155         public String getCharacterEncoding( )
156         {
157             // TODO Auto-generated method stub
158             return null;
159         }
160 
161         @Override
162         public int getBufferSize( )
163         {
164             // TODO Auto-generated method stub
165             return 0;
166         }
167 
168         @Override
169         public void flushBuffer( ) throws IOException
170         {
171             // TODO Auto-generated method stub
172 
173         }
174 
175         @Override
176         public void setStatus( int sc, String sm )
177         {
178             // TODO Auto-generated method stub
179 
180         }
181 
182         @Override
183         public void setStatus( int sc )
184         {
185             // TODO Auto-generated method stub
186 
187         }
188 
189         @Override
190         public void setIntHeader( String name, int value )
191         {
192             // TODO Auto-generated method stub
193 
194         }
195 
196         @Override
197         public void setHeader( String name, String value )
198         {
199             // TODO Auto-generated method stub
200 
201         }
202 
203         @Override
204         public void setDateHeader( String name, long date )
205         {
206             // TODO Auto-generated method stub
207 
208         }
209 
210         @Override
211         public void sendRedirect( String location ) throws IOException
212         {
213             // TODO Auto-generated method stub
214 
215         }
216 
217         @Override
218         public void sendError( int sc, String msg ) throws IOException
219         {
220             // TODO Auto-generated method stub
221 
222         }
223 
224         @Override
225         public void sendError( int sc ) throws IOException
226         {
227             // TODO Auto-generated method stub
228 
229         }
230 
231         @Override
232         public String encodeUrl( String url )
233         {
234             // TODO Auto-generated method stub
235             return null;
236         }
237 
238         @Override
239         public String encodeURL( String url )
240         {
241             // TODO Auto-generated method stub
242             return null;
243         }
244 
245         @Override
246         public String encodeRedirectUrl( String url )
247         {
248             // TODO Auto-generated method stub
249             return null;
250         }
251 
252         @Override
253         public String encodeRedirectURL( String url )
254         {
255             // TODO Auto-generated method stub
256             return null;
257         }
258 
259         @Override
260         public boolean containsHeader( String name )
261         {
262             // TODO Auto-generated method stub
263             return false;
264         }
265 
266         @Override
267         public void addIntHeader( String name, int value )
268         {
269             // TODO Auto-generated method stub
270 
271         }
272 
273         @Override
274         public void addHeader( String name, String value )
275         {
276             // TODO Auto-generated method stub
277 
278         }
279 
280         @Override
281         public void addDateHeader( String name, long date )
282         {
283             // TODO Auto-generated method stub
284 
285         }
286 
287         @Override
288         public void addCookie( Cookie cookie )
289         {
290             // TODO Auto-generated method stub
291 
292         }
293 
294         @Override
295         public void setContentLengthLong(long len) {
296             // TODO Auto-generated method stub
297             
298         }
299 
300         @Override
301         public int getStatus() {
302             // TODO Auto-generated method stub
303             return 0;
304         }
305 
306         @Override
307         public String getHeader(String name) {
308             // TODO Auto-generated method stub
309             return null;
310         }
311 
312         @Override
313         public Collection<String> getHeaders(String name) {
314             // TODO Auto-generated method stub
315             return null;
316         }
317 
318         @Override
319         public Collection<String> getHeaderNames() {
320             // TODO Auto-generated method stub
321             return null;
322         }
323     }
324 
325     private static final String PORLET_NAME = "ChildPagesPortletTest";
326 
327     @Before
328     public void setUp( ) throws Exception
329     {
330         super.setUp( );
331         ChildPagesPortlet portlet = new ChildPagesPortlet( );
332         portlet.setParentPageId( 1 );
333         portlet.setPageId( 1 );
334         portlet.setStyleId( Integer
335                 .parseInt( PortletHome
336                         .getStylesList(
337                                 ChildPagesPortletHome.getInstance( )
338                                         .getPortletTypeId( ) ).get( 0 )
339                         .getCode( ) ) );
340         portlet.setColumn( 1 );
341         portlet.setOrder( 1 );
342         portlet.setName( PORLET_NAME );
343         portlet.setStatus( Portlet.STATUS_PUBLISHED );
344         ChildPagesPortletHome.getInstance( ).create( portlet );
345     }
346 
347     @After
348     public void tearDown( ) throws Exception
349     {
350         ChildPagesPortlet portlet = findTestPortlet( );
351         if ( portlet != null )
352         {
353             ChildPagesPortletHome.getInstance( ).remove( portlet );
354         }
355         super.tearDown( );
356     }
357 
358     /**
359      * test that page modification are reflected in the portlet
360      * 
361      * @throws SiteMessageException
362      */
363     @Test
364     public void testProcessEvent( ) throws SiteMessageException
365     {
366         // FIXME : rework when LUTECE-1837 is resolved
367         MokeHttpServletRequest request = new MokeHttpServletRequest( )
368         {
369 
370             @Override
371             public Object getAttribute( String string )
372             {
373                 return null;
374             }
375 
376         };
377         HttpServletResponse response = new MokeHttpServletResponse( );
378         LocalVariables.setLocal( null, request, response );
379         // determine a random page name
380         String randomPageName = "page" + new SecureRandom( ).nextLong( );
381         // get the page content
382         IPageService pageService = ( IPageService ) SpringContextService
383                 .getBean( "pageService" );
384         String content = pageService.getPage( "1", 0, request );
385         assertFalse( "page should not contain not yet created page with name "
386                 + randomPageName, content.contains( randomPageName ) );
387         assertFalse(
388                 "page should not contain not yet created page with description "
389                         + randomPageName + "_desc",
390                 content.contains( randomPageName + "_desc" ) );
391         // create the page
392         Page page = new Page( );
393         page.setParentPageId( PortalService.getRootPageId( ) );
394         page.setPageTemplateId( PageTemplateHome.getPageTemplatesList( )
395                 .get( 0 ).getId( ) );
396         page.setName( randomPageName );
397         page.setDescription( randomPageName + "_desc" );
398         pageService.createPage( page );
399         // get the page content
400         content = pageService.getPage( "1", 0, request );
401         assertTrue( "page should contain page with name " + randomPageName,
402                 content.contains( randomPageName ) );
403         assertTrue( "page should contain page with decription "
404                 + randomPageName + "_desc",
405                 content.contains( randomPageName + "_desc" ) );
406         // change the page name
407         randomPageName = randomPageName + "_mod";
408         page.setName( randomPageName );
409         page.setDescription( randomPageName + "_desc" );
410         pageService.updatePage( page );
411         // get the page content
412         content = pageService.getPage( "1", 0, request );
413         assertTrue( "page should contain page with the modified name "
414                 + randomPageName, content.contains( randomPageName ) );
415         assertTrue( "page should contain page with decription "
416                 + randomPageName + "_desc",
417                 content.contains( randomPageName + "_desc" ) );
418         // remove the page
419         pageService.removePage( page.getId( ) );
420         // get the page content
421         content = pageService.getPage( "1", 0, request );
422         assertFalse( "page should not contain page with name " + randomPageName
423                 + " anymore", content.contains( randomPageName ) );
424         assertFalse(
425                 "page should not contain not yet created page with description "
426                         + randomPageName + "_desc anymore",
427                 content.contains( randomPageName + "_desc" ) );
428     }
429 
430     /**
431      * test that changing a page parent id is reflected in the portlet
432      * 
433      * @see <a
434      *      href="https://dev.lutece.paris.fr/jira/browse/CHILDPAGES-7">https://dev.lutece.paris.fr/jira/browse/CHILDPAGES-7</a>
435      * @throws SiteMessageException
436      */
437     @Test
438     public void testProcessEventUpdatePageParent( ) throws SiteMessageException
439     {
440         // FIXME : rework when LUTECE-1837 is resolved
441         MokeHttpServletRequest request = new MokeHttpServletRequest( )
442         {
443 
444             @Override
445             public Object getAttribute( String string )
446             {
447                 return null;
448             }
449 
450         };
451         HttpServletResponse response = new MokeHttpServletResponse( );
452         LocalVariables.setLocal( null, request, response );
453         // determine a random page name
454         String randomPageName = "page" + new SecureRandom( ).nextLong( );
455         // get the page content
456         IPageService pageService = ( IPageService ) SpringContextService
457                 .getBean( "pageService" );
458         String content = pageService.getPage( "1", 0, request );
459         assertFalse( "page should not contain not yet created page with name "
460                 + randomPageName, content.contains( randomPageName ) );
461         assertFalse(
462                 "page should not contain not yet created page with description "
463                         + randomPageName + "_desc",
464                 content.contains( randomPageName + "_desc" ) );
465         // create the page
466         Page page = new Page( );
467         page.setParentPageId( PortalService.getRootPageId( ) );
468         page.setPageTemplateId( PageTemplateHome.getPageTemplatesList( )
469                 .get( 0 ).getId( ) );
470         page.setName( randomPageName );
471         page.setDescription( randomPageName + "_desc" );
472         pageService.createPage( page );
473         // get the page content
474         content = pageService.getPage( "1", 0, request );
475         assertTrue( "page should contain page with name " + randomPageName,
476                 content.contains( randomPageName ) );
477         assertTrue( "page should contain page with decription "
478                 + randomPageName + "_desc",
479                 content.contains( randomPageName + "_desc" ) );
480         // create a sibling page
481         Page siblingPage = new Page( );
482         siblingPage.setParentPageId( PortalService.getRootPageId( ) );
483         siblingPage.setPageTemplateId( PageTemplateHome.getPageTemplatesList( )
484                 .get( 0 ).getId( ) );
485         siblingPage.setName( randomPageName + "_sibling" );
486         siblingPage.setDescription( randomPageName + "_sibling_desc" );
487         pageService.createPage( siblingPage );
488         // get the page content
489         content = pageService.getPage( "1", 0, request );
490         assertTrue( "page should contain page with name " + randomPageName
491                 + "_sibling", content.contains( randomPageName + "_sibling" ) );
492         assertTrue( "page should contain page with decription "
493                 + randomPageName + "_sibling_desc",
494                 content.contains( randomPageName + "_sibling_desc" ) );
495         // move the page
496         siblingPage.setParentPageId( page.getId( ) );
497         pageService.updatePage( siblingPage );
498         // get the page content
499         content = pageService.getPage( "1", 0, request );
500         assertFalse( "page should not contain page with name " + randomPageName
501                 + "_sibling anymore",
502                 content.contains( randomPageName + "_sibling" ) );
503         assertFalse(
504                 "page should not contain not yet created page with description "
505                         + randomPageName + "_sibling_desc anymore",
506                 content.contains( randomPageName + "_sibling_desc" ) );
507         // cleanup
508         pageService.removePage( siblingPage.getId( ) );
509         pageService.removePage( page.getId( ) );
510     }
511 
512     /**
513      * test that there is no event loop
514      */
515     @Test( timeout = 1000 )
516     public void testProcessEventLoopPrevention( )
517     {
518         // determine a random page name
519         String randomPageName = "page" + new SecureRandom( ).nextLong( );
520         // get the page content
521         IPageService pageService = ( IPageService ) SpringContextService
522                 .getBean( "pageService" );
523         // create the page
524         Page page = new Page( );
525         page.setParentPageId( PortalService.getRootPageId( ) );
526         page.setPageTemplateId( PageTemplateHome.getPageTemplatesList( )
527                 .get( 0 ).getId( ) );
528         page.setName( randomPageName );
529         page.setDescription( randomPageName + "_desc" );
530         pageService.createPage( page );
531         ChildPagesPortlet portlet = new ChildPagesPortlet( );
532         portlet.setParentPageId( page.getId( ) );
533         portlet.setPageId( page.getId( ) );
534         portlet.setStyleId( Integer
535                 .parseInt( PortletHome
536                         .getStylesList(
537                                 ChildPagesPortletHome.getInstance( )
538                                         .getPortletTypeId( ) ).get( 0 )
539                         .getCode( ) ) );
540         portlet.setColumn( 1 );
541         portlet.setOrder( 1 );
542         portlet.setName( PORLET_NAME );
543         portlet.setStatus( Portlet.STATUS_PUBLISHED );
544         ChildPagesPortletHome.getInstance( ).create( portlet );
545         Page page2 = new Page( );
546         page2.setParentPageId( page.getId( ) );
547         page2.setPageTemplateId( PageTemplateHome.getPageTemplatesList( )
548                 .get( 0 ).getId( ) );
549         page2.setName( randomPageName + "_child" );
550         page2.setDescription( randomPageName + "_child_desc" );
551         pageService.createPage( page2 );
552         ChildPagesPortlet portlet2 = new ChildPagesPortlet( );
553         portlet2.setParentPageId( PortalService.getRootPageId( ) );
554         portlet2.setPageId( page2.getId( ) );
555         portlet2.setStyleId( Integer
556                 .parseInt( PortletHome
557                         .getStylesList(
558                                 ChildPagesPortletHome.getInstance( )
559                                         .getPortletTypeId( ) ).get( 0 )
560                         .getCode( ) ) );
561         portlet2.setColumn( 1 );
562         portlet2.setOrder( 1 );
563         portlet2.setName( PORLET_NAME );
564         portlet2.setStatus( Portlet.STATUS_PUBLISHED );
565         ChildPagesPortletHome.getInstance( ).create( portlet2 );
566         pageService.removePage( page2.getId( ) );
567         pageService.removePage( page.getId( ) );
568     }
569 
570     private ChildPagesPortlet findTestPortlet( )
571     {
572         Page rootPage = PageHome.findByPrimaryKey( 1 );
573         List<Portlet> portlets = rootPage.getPortlets( );
574         for ( Portlet aPortlet : portlets )
575         {
576             if ( aPortlet.getPortletTypeId( ).equals(
577                     ChildPagesPortletHome.getInstance( ).getPortletTypeId( ) )
578                     && aPortlet.getName( ).equals( PORLET_NAME ) )
579             {
580                 return ( ChildPagesPortlet ) aPortlet;
581             }
582         }
583         return null;
584     }
585 
586 }