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.plugins.componentdashboard.web;
35  
36  import org.springframework.mock.web.MockHttpServletRequest;
37  import org.springframework.mock.web.MockHttpServletResponse;
38  import org.springframework.mock.web.MockServletConfig;
39  
40  import fr.paris.lutece.plugins.publicdashboard.business.PublicDashboard;
41  import fr.paris.lutece.plugins.publicdashboard.business.PublicDashboardHome;
42  import fr.paris.lutece.plugins.publicdashboard.web.PublicDashboardJspBean;
43  import fr.paris.lutece.portal.business.user.AdminUser;
44  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
45  import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
46  import fr.paris.lutece.portal.service.security.UserNotSignedException;
47  import java.util.List;
48  import java.io.IOException;
49  import fr.paris.lutece.test.LuteceTestCase;
50  import fr.paris.lutece.portal.service.security.SecurityTokenService;
51  import fr.paris.lutece.portal.web.LocalVariables;
52  
53  /**
54   * This is the business class test for the object Dashboard
55   */
56  public class DashboardJspBeanTest extends LuteceTestCase
57  {
58      private static final int IDBEAN1 = 1;
59      private static final int IDBEAN2 = 2;
60      private static final int ZONE1 = 1;
61      private static final int ZONE2 = 2;
62      private static final int ORDER1 = 1;
63      private static final int ORDER2 = 2;
64  
65      public void testJspBeans( ) throws AccessDeniedException, IOException
66      {
67          MockHttpServletRequest request = new MockHttpServletRequest( );
68          MockHttpServletResponse response = new MockHttpServletResponse( );
69          MockServletConfig config = new MockServletConfig( );
70  
71          // display admin Dashboard management JSP
72          PublicDashboardJspBean jspbean = new PublicDashboardJspBean( );
73          String html = jspbean.getManageDashboards( request );
74          assertNotNull( html );
75  
76          // display admin Dashboard creation JSP
77          html = jspbean.getCreateDashboard( request );
78          assertNotNull( html );
79  
80          // action create Dashboard
81          request = new MockHttpServletRequest( );
82  
83          response = new MockHttpServletResponse( );
84          AdminUser adminUser = new AdminUser( );
85          adminUser.setAccessCode( "admin" );
86  
87          request.addParameter( "id_bean", String.valueOf( IDBEAN1 ) );
88          request.addParameter( "zone", String.valueOf( ZONE1 ) );
89          request.addParameter( "order", String.valueOf( ORDER1 ) );
90          request.addParameter( "action", "createDashboard" );
91          request.addParameter( "token", SecurityTokenService.getInstance( ).getToken( request, "createDashboard" ) );
92          request.setMethod( "POST" );
93  
94          try
95          {
96              AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
97              html = jspbean.processController( request, response );
98  
99              // MockResponse object does not redirect, result is always null
100             assertNull( html );
101         }
102         catch( AccessDeniedException e )
103         {
104             fail( "access denied" );
105         }
106         catch( UserNotSignedException e )
107         {
108             fail( "user not signed in" );
109         }
110 
111         // display modify Dashboard JSP
112         request = new MockHttpServletRequest( );
113         request.addParameter( "id_bean", String.valueOf( IDBEAN1 ) );
114         request.addParameter( "zone", String.valueOf( ZONE1 ) );
115         request.addParameter( "order", String.valueOf( ORDER1 ) );
116         List<Integer> listIds = PublicDashboardHome.getIdDashboardsList( );
117         assertTrue( !listIds.isEmpty( ) );
118         request.addParameter( "id", String.valueOf( listIds.get( 0 ) ) );
119         jspbean = new PublicDashboardJspBean( );
120 
121         assertNotNull( jspbean.getModifyDashboard( request ) );
122 
123         // action modify Dashboard
124         request = new MockHttpServletRequest( );
125         response = new MockHttpServletResponse( );
126 
127         adminUser = new AdminUser( );
128         adminUser.setAccessCode( "admin" );
129 
130         request.addParameter( "id_bean", String.valueOf( IDBEAN2 ) );
131         request.addParameter( "zone", String.valueOf( ZONE2 ) );
132         request.addParameter( "order", String.valueOf( ORDER2 ) );
133         request.setRequestURI( "jsp/admin/plugins/example/ManageDashboards.jsp" );
134         // important pour que MVCController sache quelle action effectuer, sinon, il redirigera vers createDashboard, qui est l'action par défaut
135         request.addParameter( "action", "modifyDashboard" );
136         request.addParameter( "token", SecurityTokenService.getInstance( ).getToken( request, "modifyDashboard" ) );
137 
138         try
139         {
140             AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
141             html = jspbean.processController( request, response );
142 
143             // MockResponse object does not redirect, result is always null
144             assertNull( html );
145         }
146         catch( AccessDeniedException e )
147         {
148             fail( "access denied" );
149         }
150         catch( UserNotSignedException e )
151         {
152             fail( "user not signed in" );
153         }
154 
155         // get remove Dashboard
156         request = new MockHttpServletRequest( );
157         // request.setRequestURI("jsp/admin/plugins/example/ManageDashboards.jsp");
158         request.addParameter( "id", String.valueOf( listIds.get( 0 ) ) );
159         jspbean = new PublicDashboardJspBean( );
160         request.addParameter( "action", "confirmRemoveDashboard" );
161         assertNotNull( jspbean.getModifyDashboard( request ) );
162 
163         // do remove Dashboard
164         request = new MockHttpServletRequest( );
165         response = new MockHttpServletResponse( );
166         request.setRequestURI( "jsp/admin/plugins/example/ManageDashboardts.jsp" );
167         // important pour que MVCController sache quelle action effectuer, sinon, il redirigera vers createDashboard, qui est l'action par défaut
168         request.addParameter( "action", "removeDashboard" );
169         request.addParameter( "token", SecurityTokenService.getInstance( ).getToken( request, "removeDashboard" ) );
170         request.addParameter( "id", String.valueOf( listIds.get( 0 ) ) );
171         request.setMethod( "POST" );
172         adminUser = new AdminUser( );
173         adminUser.setAccessCode( "admin" );
174 
175         try
176         {
177             AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
178             html = jspbean.processController( request, response );
179 
180             // MockResponse object does not redirect, result is always null
181             assertNull( html );
182         }
183         catch( AccessDeniedException e )
184         {
185             fail( "access denied" );
186         }
187         catch( UserNotSignedException e )
188         {
189             fail( "user not signed in" );
190         }
191 
192     }
193 }