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.xpages;
35  
36  import java.io.File;
37  import java.io.FileInputStream;
38  import java.io.FileOutputStream;
39  import java.io.IOException;
40  import java.io.InputStream;
41  import java.io.OutputStream;
42  import java.security.SecureRandom;
43  import java.util.Properties;
44  
45  import org.springframework.mock.web.MockHttpServletRequest;
46  
47  import fr.paris.lutece.portal.business.page.Page;
48  import fr.paris.lutece.portal.business.style.PageTemplateHome;
49  import fr.paris.lutece.portal.service.page.IPageService;
50  import fr.paris.lutece.portal.service.portal.PortalService;
51  import fr.paris.lutece.portal.service.spring.SpringContextService;
52  import fr.paris.lutece.portal.service.util.AppLogService;
53  import fr.paris.lutece.portal.service.util.AppPropertiesService;
54  import fr.paris.lutece.test.LuteceTestCase;
55  
56  /**
57   * Tests that a cycle in the page hierarchy is handled by the site map
58   */
59  public class SiteMapAppCycleTest extends LuteceTestCase
60  {
61  
62      private Page _top;
63      private Page _middle;
64      private Page _bottom;
65      private IPageService _pageService;
66      private int _nInitialRootId;
67  
68      @Override
69      protected void setUp( ) throws Exception
70      {
71          super.setUp( );
72          _pageService = (IPageService) SpringContextService.getBean( "pageService" );
73          // create pages
74          _top = getPage( null );
75          _middle = getPage( _top.getId( ) );
76          _bottom = getPage( _middle.getId( ) );
77          // set up the cyle
78          _top.setParentPageId( _bottom.getId( ) );
79          _pageService.updatePage( _top );
80          // change root id
81          _nInitialRootId = PortalService.getRootPageId( );
82          setRootPageId( _top.getId( ) );
83      }
84  
85      private Page getPage( Integer parentId )
86      {
87          Page page = new Page( );
88          if ( parentId != null )
89          {
90              page.setParentPageId( parentId );
91          }
92          page.setPageTemplateId( PageTemplateHome.getPageTemplatesList( ).get( 0 ).getId( ) );
93          page.setName( "page" + new SecureRandom( ).nextLong( ) );
94  
95          _pageService.createPage( page );
96  
97          return page;
98      }
99  
100     @Override
101     protected void tearDown( ) throws Exception
102     {
103         removePageQuietly( _bottom.getId( ) );
104         removePageQuietly( _middle.getId( ) );
105         removePageQuietly( _top.getId( ) );
106         setRootPageId( _nInitialRootId );
107         super.tearDown( );
108     }
109 
110     private void setRootPageId( int nRootPageId ) throws IOException
111     {
112         AppLogService.info( "Setting root page id to {}", nRootPageId );
113         File luteceProperties = new File( getResourcesDir( ), "WEB-INF/conf/lutece.properties" );
114         Properties props = new Properties( );
115         try ( InputStream is = new FileInputStream( luteceProperties ) )
116         {
117             props.load( is );
118         }
119 
120         props.setProperty( "lutece.page.root", Integer.toString( nRootPageId ) );
121 
122         try ( OutputStream os = new FileOutputStream( luteceProperties ) )
123         {
124             props.store( os, "saved for junit " + this.getClass( ).getCanonicalName( ) );
125         }
126         AppPropertiesService.reloadAll( );
127     }
128 
129     private void removePageQuietly( int nPageId )
130     {
131         try
132         {
133             _pageService.removePage( nPageId );
134         }
135         catch( Throwable t )
136         {
137             AppLogService.error( "Could not remove page {}", nPageId, t );
138         }
139     }
140 
141     public void testGetPage( )
142     {
143         SiteMapApp app = new SiteMapApp( );
144 
145         XPage res = app.getPage( new MockHttpServletRequest( ), 0, null );
146 
147         assertNotNull( res );
148         assertTrue( "SiteMap should contain reference to page", res.getContent( ).contains( _middle.getName( ) ) );
149         assertTrue( "SiteMap should contain reference to page", res.getContent( ).contains( _bottom.getName( ) ) );
150     }
151 
152 }