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.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
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
74 _top = getPage( null );
75 _middle = getPage( _top.getId( ) );
76 _bottom = getPage( _middle.getId( ) );
77
78 _top.setParentPageId( _bottom.getId( ) );
79 _pageService.updatePage( _top );
80
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 }