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.user.attribute;
35  
36  import java.math.BigInteger;
37  import java.security.SecureRandom;
38  import java.util.ArrayList;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Locale;
42  import java.util.Map;
43  import java.util.Random;
44  
45  import org.springframework.mock.web.MockHttpServletRequest;
46  
47  import fr.paris.lutece.portal.business.user.AdminUser;
48  import fr.paris.lutece.portal.business.user.attribute.AttributeField;
49  import fr.paris.lutece.portal.business.user.attribute.AttributeType;
50  import fr.paris.lutece.portal.business.user.attribute.IAttribute;
51  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
52  import fr.paris.lutece.portal.service.admin.PasswordResetException;
53  import fr.paris.lutece.portal.service.message.AdminMessage;
54  import fr.paris.lutece.portal.service.message.AdminMessageService;
55  import fr.paris.lutece.portal.service.security.SecurityTokenService;
56  import fr.paris.lutece.portal.service.user.attribute.AttributeService;
57  import fr.paris.lutece.portal.service.user.attribute.AttributeTypeService;
58  import fr.paris.lutece.test.LuteceTestCase;
59  import fr.paris.lutece.test.Utils;
60  
61  public class AttributeFieldJspBeanTest extends LuteceTestCase
62  {
63      private AttributeFieldJspBean instance;
64      private Map<AttributeType, IAttribute> _attributes;
65  
66      @Override
67      protected void setUp( ) throws Exception
68      {
69          super.setUp( );
70          instance = new AttributeFieldJspBean( );
71          _attributes = new HashMap<>( );
72          List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
73          for ( AttributeType type : types )
74          {
75              IAttribute attribute = (IAttribute) Class.forName( type.getClassName( ) ).newInstance( );
76              String strName = getRandomName( );
77              attribute.setTitle( strName );
78              attribute.setHelpMessage( strName );
79              List<AttributeField> listAttributeFields = new ArrayList<>( );
80              for ( int i = 0; i < 3; i++ )
81              {
82                  AttributeField attributeField = new AttributeField( );
83                  attributeField.setTitle( strName + "_" + i );
84                  attributeField.setValue( strName + "_" + i );
85                  listAttributeFields.add( attributeField );
86              }
87              attribute.setListAttributeFields( listAttributeFields );
88              AttributeService.getInstance( ).createAttribute( attribute );
89              _attributes.put( type, attribute );
90          }
91      }
92  
93      @Override
94      protected void tearDown( ) throws Exception
95      {
96          for ( IAttribute attribute : _attributes.values( ) )
97          {
98              AttributeService.getInstance( ).removeAttribute( attribute.getIdAttribute( ) );
99          }
100         super.tearDown( );
101     }
102 
103     private String getRandomName( )
104     {
105         Random rand = new SecureRandom( );
106         BigInteger bigInt = new BigInteger( 128, rand );
107         return "junit" + bigInt.toString( 36 );
108     }
109 
110     public void testDoConfirmRemoveAttributeField( )
111     {
112         MockHttpServletRequest request = new MockHttpServletRequest( );
113         request.addParameter( "id_attribute", "1" );
114         request.addParameter( "id_field", "1" );
115 
116         instance.doConfirmRemoveAttributeField( request );
117 
118         AdminMessage message = AdminMessageService.getMessage( request );
119         assertNotNull( message );
120         assertTrue( message.getRequestParameters( ).containsKey( SecurityTokenService.PARAMETER_TOKEN ) );
121     }
122 
123     public void testDoRemoveAttributeField( ) throws AccessDeniedException
124     {
125         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
126         for ( AttributeType type : types )
127         {
128             testDoRemoveAttributeField( _attributes.get( type ) );
129         }
130     }
131 
132     private void testDoRemoveAttributeField( IAttribute attribute ) throws AccessDeniedException
133     {
134         MockHttpServletRequest request = new MockHttpServletRequest( );
135         request.addParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
136         request.addParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
137         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
138                 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/user/attribute/DoRemoveAttributeField.jsp" ) );
139 
140         instance.doRemoveAttributeField( request );
141 
142         IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
143         assertNotNull( stored );
144         assertEquals( 2, stored.getListAttributeFields( ).size( ) );
145     }
146 
147     public void testDoRemoveAttributeFieldInvalidToken( ) throws AccessDeniedException
148     {
149         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
150         for ( AttributeType type : types )
151         {
152             testDoRemoveAttributeFieldInvalidToken( _attributes.get( type ) );
153         }
154     }
155 
156     private void testDoRemoveAttributeFieldInvalidToken( IAttribute attribute ) throws AccessDeniedException
157     {
158         MockHttpServletRequest request = new MockHttpServletRequest( );
159         request.addParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
160         request.addParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
161         request.addParameter( SecurityTokenService.PARAMETER_TOKEN,
162                 SecurityTokenService.getInstance( ).getToken( request, "jsp/admin/user/attribute/DoRemoveAttributeField.jsp" ) + "b" );
163 
164         try
165         {
166             instance.doRemoveAttributeField( request );
167             fail( "Should have thrown" );
168         }
169         catch( AccessDeniedException e )
170         {
171             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
172             assertNotNull( stored );
173             assertEquals( 3, stored.getListAttributeFields( ).size( ) );
174         }
175     }
176 
177     public void testDoRemoveAttributeFieldNoToken( ) throws AccessDeniedException
178     {
179         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
180         for ( AttributeType type : types )
181         {
182             testDoRemoveAttributeFieldNoToken( _attributes.get( type ) );
183         }
184     }
185 
186     private void testDoRemoveAttributeFieldNoToken( IAttribute attribute ) throws AccessDeniedException
187     {
188         MockHttpServletRequest request = new MockHttpServletRequest( );
189         request.addParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
190         request.addParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
191 
192         try
193         {
194             instance.doRemoveAttributeField( request );
195             fail( "Should have thrown" );
196         }
197         catch( AccessDeniedException e )
198         {
199             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
200             assertNotNull( stored );
201             assertEquals( 3, stored.getListAttributeFields( ).size( ) );
202         }
203     }
204 
205     public void testGetCreateAttributeField( ) throws AccessDeniedException
206     {
207         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
208         for ( AttributeType type : types )
209         {
210             testGetCreateAttributeField( _attributes.get( type ) );
211         }
212     }
213 
214     private void testGetCreateAttributeField( IAttribute attribute ) throws PasswordResetException, AccessDeniedException
215     {
216         MockHttpServletRequest request = new MockHttpServletRequest( );
217         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
218         Utils.registerAdminUserWithRigth( request, new AdminUser( ), "CORE_USERS_MANAGEMENT" );
219         instance.init( request, "CORE_USERS_MANAGEMENT" );
220 
221         assertNotNull( instance.getCreateAttributeField( request ) );
222     }
223 
224     public void testDoCreateAttributeField( ) throws AccessDeniedException
225     {
226         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
227         for ( AttributeType type : types )
228         {
229             testDoCreateAttributeField( _attributes.get( type ) );
230         }
231     }
232 
233     private void testDoCreateAttributeField( IAttribute attribute ) throws AccessDeniedException
234     {
235         MockHttpServletRequest request = new MockHttpServletRequest( );
236         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
237         String strName = getRandomName( );
238         request.setParameter( "title", strName );
239         request.setParameter( "value", strName );
240         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
241                 SecurityTokenService.getInstance( ).getToken( request, "admin/user/attribute/create_attribute_field.html" ) );
242 
243         instance.doCreateAttributeField( request );
244 
245         IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
246         assertNotNull( stored );
247         assertEquals( 4, stored.getListAttributeFields( ).size( ) );
248         assertEquals( 1,
249                 stored.getListAttributeFields( ).stream( ).filter( f -> strName.equals( f.getTitle( ) ) && strName.equals( f.getValue( ) ) ).count( ) );
250     }
251 
252     public void testDoCreateAttributeFieldInvalidToken( ) throws AccessDeniedException
253     {
254         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
255         for ( AttributeType type : types )
256         {
257             testDoCreateAttributeFieldInvalidToken( _attributes.get( type ) );
258         }
259     }
260 
261     private void testDoCreateAttributeFieldInvalidToken( IAttribute attribute ) throws AccessDeniedException
262     {
263         MockHttpServletRequest request = new MockHttpServletRequest( );
264         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
265         String strName = getRandomName( );
266         request.setParameter( "title", strName );
267         request.setParameter( "value", strName );
268         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
269                 SecurityTokenService.getInstance( ).getToken( request, "admin/user/attribute/create_attribute_field.html" ) + "b" );
270 
271         try
272         {
273             instance.doCreateAttributeField( request );
274             fail( "Should have thrown" );
275         }
276         catch( AccessDeniedException e )
277         {
278             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
279             assertNotNull( stored );
280             assertEquals( 3, stored.getListAttributeFields( ).size( ) );
281             assertEquals( 0,
282                     stored.getListAttributeFields( ).stream( ).filter( f -> strName.equals( f.getTitle( ) ) && strName.equals( f.getValue( ) ) ).count( ) );
283         }
284     }
285 
286     public void testDoCreateAttributeFieldNoToken( ) throws AccessDeniedException
287     {
288         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
289         for ( AttributeType type : types )
290         {
291             testDoCreateAttributeFieldNoToken( _attributes.get( type ) );
292         }
293     }
294 
295     private void testDoCreateAttributeFieldNoToken( IAttribute attribute ) throws AccessDeniedException
296     {
297         MockHttpServletRequest request = new MockHttpServletRequest( );
298         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
299         String strName = getRandomName( );
300         request.setParameter( "title", strName );
301         request.setParameter( "value", strName );
302 
303         try
304         {
305             instance.doCreateAttributeField( request );
306             fail( "Should have thrown" );
307         }
308         catch( AccessDeniedException e )
309         {
310             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
311             assertNotNull( stored );
312             assertEquals( 3, stored.getListAttributeFields( ).size( ) );
313             assertEquals( 0,
314                     stored.getListAttributeFields( ).stream( ).filter( f -> strName.equals( f.getTitle( ) ) && strName.equals( f.getValue( ) ) ).count( ) );
315         }
316     }
317 
318     public void testGetModifyAttributeField( ) throws AccessDeniedException
319     {
320         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
321         for ( AttributeType type : types )
322         {
323             testGetModifyAttributeField( _attributes.get( type ) );
324         }
325     }
326 
327     private void testGetModifyAttributeField( IAttribute attribute ) throws PasswordResetException, AccessDeniedException
328     {
329         MockHttpServletRequest request = new MockHttpServletRequest( );
330         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
331         request.setParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
332         Utils.registerAdminUserWithRigth( request, new AdminUser( ), "CORE_USERS_MANAGEMENT" );
333         instance.init( request, "CORE_USERS_MANAGEMENT" );
334 
335         assertNotNull( instance.getModifyAttributeField( request ) );
336     }
337 
338     public void testDoModifyAttributeField( ) throws AccessDeniedException
339     {
340         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
341         for ( AttributeType type : types )
342         {
343             testDoModifyAttributeField( _attributes.get( type ) );
344         }
345     }
346 
347     private void testDoModifyAttributeField( IAttribute attribute ) throws AccessDeniedException
348     {
349         MockHttpServletRequest request = new MockHttpServletRequest( );
350         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
351         request.setParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
352         String strName = getRandomName( );
353         request.setParameter( "title", strName );
354         request.setParameter( "value", strName );
355         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
356                 SecurityTokenService.getInstance( ).getToken( request, "admin/user/attribute/modify_attribute_field.html" ) );
357 
358         instance.doModifyAttributeField( request );
359 
360         IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
361         assertNotNull( stored );
362         assertEquals( 3, stored.getListAttributeFields( ).size( ) );
363         assertEquals( 1,
364                 stored.getListAttributeFields( ).stream( ).filter( f -> strName.equals( f.getTitle( ) ) && strName.equals( f.getValue( ) ) ).count( ) );
365     }
366 
367     public void testDoModifyAttributeFieldInvalidToken( ) throws AccessDeniedException
368     {
369         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
370         for ( AttributeType type : types )
371         {
372             testDoModifyAttributeFieldInvalidToken( _attributes.get( type ) );
373         }
374     }
375 
376     private void testDoModifyAttributeFieldInvalidToken( IAttribute attribute ) throws AccessDeniedException
377     {
378         MockHttpServletRequest request = new MockHttpServletRequest( );
379         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
380         request.setParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
381         String strName = getRandomName( );
382         request.setParameter( "title", strName );
383         request.setParameter( "value", strName );
384         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
385                 SecurityTokenService.getInstance( ).getToken( request, "admin/user/attribute/modify_attribute_field.html" ) + "b" );
386 
387         try
388         {
389             instance.doModifyAttributeField( request );
390             fail( "Should have thrown" );
391         }
392         catch( AccessDeniedException e )
393         {
394             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
395             assertNotNull( stored );
396             assertEquals( 3, stored.getListAttributeFields( ).size( ) );
397             assertEquals( 0,
398                     stored.getListAttributeFields( ).stream( ).filter( f -> strName.equals( f.getTitle( ) ) && strName.equals( f.getValue( ) ) ).count( ) );
399         }
400     }
401 
402     public void testDoModifyAttributeFieldNoToken( ) throws AccessDeniedException
403     {
404         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
405         for ( AttributeType type : types )
406         {
407             testDoModifyAttributeFieldNoToken( _attributes.get( type ) );
408         }
409     }
410 
411     private void testDoModifyAttributeFieldNoToken( IAttribute attribute ) throws AccessDeniedException
412     {
413         MockHttpServletRequest request = new MockHttpServletRequest( );
414         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
415         request.setParameter( "id_field", Integer.toString( attribute.getListAttributeFields( ).get( 0 ).getIdField( ) ) );
416         String strName = getRandomName( );
417         request.setParameter( "title", strName );
418         request.setParameter( "value", strName );
419 
420         try
421         {
422             instance.doModifyAttributeField( request );
423             fail( "Should have thrown" );
424         }
425         catch( AccessDeniedException e )
426         {
427             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
428             assertNotNull( stored );
429             assertEquals( 3, stored.getListAttributeFields( ).size( ) );
430             assertEquals( 0,
431                     stored.getListAttributeFields( ).stream( ).filter( f -> strName.equals( f.getTitle( ) ) && strName.equals( f.getValue( ) ) ).count( ) );
432         }
433     }
434 
435     public void testdoMoveDownAttributeField( ) throws AccessDeniedException
436     {
437         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
438         for ( AttributeType type : types )
439         {
440             // we load the attribute from db to get all fields including
441             // position
442             IAttribute attribute = AttributeService.getInstance( ).getAttributeWithFields( _attributes.get( type ).getIdAttribute( ), Locale.FRANCE );
443             testdoMoveDownAttributeField( attribute );
444         }
445     }
446 
447     private void testdoMoveDownAttributeField( IAttribute attribute ) throws AccessDeniedException
448     {
449         assertTrue( attribute.getListAttributeFields( ).size( ) > 1 );
450         AttributeField attributeField = attribute.getListAttributeFields( ).get( 0 );
451         int nOrigPosition = attributeField.getPosition( );
452         MockHttpServletRequest request = new MockHttpServletRequest( );
453         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
454         request.setParameter( "id_field", Integer.toString( attributeField.getIdField( ) ) );
455         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
456                 SecurityTokenService.getInstance( ).getToken( request, attribute.getTemplateModifyAttribute( ) ) );
457 
458         instance.doMoveDownAttributeField( request );
459 
460         IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
461         assertNotNull( stored );
462         AttributeField field = stored.getListAttributeFields( ).stream( ).filter( f -> f.getIdField( ) == attributeField.getIdField( ) ).findFirst( )
463                 .orElseThrow( AssertionError::new );
464         assertEquals( "Orig position was " + nOrigPosition + "; expected higher position but got " + field.getPosition( ), nOrigPosition + 1,
465                 field.getPosition( ) );
466     }
467 
468     public void testdoMoveDownAttributeFieldInvalidToken( ) throws AccessDeniedException
469     {
470         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
471         for ( AttributeType type : types )
472         {
473             // we load the attribute from db to get all fields including
474             // position
475             IAttribute attribute = AttributeService.getInstance( ).getAttributeWithFields( _attributes.get( type ).getIdAttribute( ), Locale.FRANCE );
476             testdoMoveDownAttributeFieldInvalidToken( attribute );
477         }
478     }
479 
480     private void testdoMoveDownAttributeFieldInvalidToken( IAttribute attribute ) throws AccessDeniedException
481     {
482         assertTrue( attribute.getListAttributeFields( ).size( ) > 1 );
483         AttributeField attributeField = attribute.getListAttributeFields( ).get( 0 );
484         int nOrigPosition = attributeField.getPosition( );
485         MockHttpServletRequest request = new MockHttpServletRequest( );
486         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
487         request.setParameter( "id_field", Integer.toString( attributeField.getIdField( ) ) );
488         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
489                 SecurityTokenService.getInstance( ).getToken( request, attribute.getTemplateModifyAttribute( ) ) + "b" );
490 
491         try
492         {
493             instance.doMoveDownAttributeField( request );
494             fail( "Should have thrown" );
495         }
496         catch( AccessDeniedException e )
497         {
498             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
499             assertNotNull( stored );
500             AttributeField field = stored.getListAttributeFields( ).stream( ).filter( f -> f.getIdField( ) == attributeField.getIdField( ) ).findFirst( )
501                     .orElseThrow( AssertionError::new );
502             assertEquals( nOrigPosition, field.getPosition( ) );
503         }
504     }
505 
506     public void testdoMoveDownAttributeFieldNoToken( ) throws AccessDeniedException
507     {
508         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
509         for ( AttributeType type : types )
510         {
511             // we load the attribute from db to get all fields including
512             // position
513             IAttribute attribute = AttributeService.getInstance( ).getAttributeWithFields( _attributes.get( type ).getIdAttribute( ), Locale.FRANCE );
514             testdoMoveDownAttributeFieldNoToken( attribute );
515         }
516     }
517 
518     private void testdoMoveDownAttributeFieldNoToken( IAttribute attribute ) throws AccessDeniedException
519     {
520         assertTrue( attribute.getListAttributeFields( ).size( ) > 1 );
521         AttributeField attributeField = attribute.getListAttributeFields( ).get( 0 );
522         int nOrigPosition = attributeField.getPosition( );
523         MockHttpServletRequest request = new MockHttpServletRequest( );
524         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
525         request.setParameter( "id_field", Integer.toString( attributeField.getIdField( ) ) );
526 
527         try
528         {
529             instance.doMoveDownAttributeField( request );
530             fail( "Should have thrown" );
531         }
532         catch( AccessDeniedException e )
533         {
534             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
535             assertNotNull( stored );
536             AttributeField field = stored.getListAttributeFields( ).stream( ).filter( f -> f.getIdField( ) == attributeField.getIdField( ) ).findFirst( )
537                     .orElseThrow( AssertionError::new );
538             assertEquals( nOrigPosition, field.getPosition( ) );
539         }
540     }
541 
542     public void testdoMoveUpAttributeField( ) throws AccessDeniedException
543     {
544         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
545         for ( AttributeType type : types )
546         {
547             // we load the attribute from db to get all fields including
548             // position
549             IAttribute attribute = AttributeService.getInstance( ).getAttributeWithFields( _attributes.get( type ).getIdAttribute( ), Locale.FRANCE );
550             testdoMoveUpAttributeField( attribute );
551         }
552     }
553 
554     private void testdoMoveUpAttributeField( IAttribute attribute ) throws AccessDeniedException
555     {
556         assertTrue( attribute.getListAttributeFields( ).size( ) > 1 );
557         AttributeField attributeField = attribute.getListAttributeFields( ).get( attribute.getListAttributeFields( ).size( ) - 1 );
558         int nOrigPosition = attributeField.getPosition( );
559         MockHttpServletRequest request = new MockHttpServletRequest( );
560         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
561         request.setParameter( "id_field", Integer.toString( attributeField.getIdField( ) ) );
562         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
563                 SecurityTokenService.getInstance( ).getToken( request, attribute.getTemplateModifyAttribute( ) ) );
564 
565         instance.doMoveUpAttributeField( request );
566 
567         IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
568         assertNotNull( stored );
569         AttributeField field = stored.getListAttributeFields( ).stream( ).filter( f -> f.getIdField( ) == attributeField.getIdField( ) ).findFirst( )
570                 .orElseThrow( AssertionError::new );
571         assertEquals( "Orig position was " + nOrigPosition + "; expected lower position but got " + field.getPosition( ), nOrigPosition - 1,
572                 field.getPosition( ) );
573     }
574 
575     public void testdoMoveUpAttributeFieldInvalidToken( ) throws AccessDeniedException
576     {
577         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
578         for ( AttributeType type : types )
579         {
580             // we load the attribute from db to get all fields including
581             // position
582             IAttribute attribute = AttributeService.getInstance( ).getAttributeWithFields( _attributes.get( type ).getIdAttribute( ), Locale.FRANCE );
583             testdoMoveUpAttributeFieldInvalidToken( attribute );
584         }
585     }
586 
587     private void testdoMoveUpAttributeFieldInvalidToken( IAttribute attribute ) throws AccessDeniedException
588     {
589         assertTrue( attribute.getListAttributeFields( ).size( ) > 1 );
590         AttributeField attributeField = attribute.getListAttributeFields( ).get( 0 );
591         int nOrigPosition = attributeField.getPosition( );
592         MockHttpServletRequest request = new MockHttpServletRequest( );
593         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
594         request.setParameter( "id_field", Integer.toString( attributeField.getIdField( ) ) );
595         request.setParameter( SecurityTokenService.PARAMETER_TOKEN,
596                 SecurityTokenService.getInstance( ).getToken( request, attribute.getTemplateModifyAttribute( ) ) + "b" );
597 
598         try
599         {
600             instance.doMoveUpAttributeField( request );
601             fail( "Should have thrown" );
602         }
603         catch( AccessDeniedException e )
604         {
605             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
606             assertNotNull( stored );
607             AttributeField field = stored.getListAttributeFields( ).stream( ).filter( f -> f.getIdField( ) == attributeField.getIdField( ) ).findFirst( )
608                     .orElseThrow( AssertionError::new );
609             assertEquals( nOrigPosition, field.getPosition( ) );
610         }
611     }
612 
613     public void testdoMoveUpAttributeFieldNoToken( ) throws AccessDeniedException
614     {
615         List<AttributeType> types = AttributeTypeService.getInstance( ).getAttributeTypes( Locale.FRANCE );
616         for ( AttributeType type : types )
617         {
618             // we load the attribute from db to get all fields including
619             // position
620             IAttribute attribute = AttributeService.getInstance( ).getAttributeWithFields( _attributes.get( type ).getIdAttribute( ), Locale.FRANCE );
621             testdoMoveUpAttributeFieldNoToken( attribute );
622         }
623     }
624 
625     private void testdoMoveUpAttributeFieldNoToken( IAttribute attribute ) throws AccessDeniedException
626     {
627         assertTrue( attribute.getListAttributeFields( ).size( ) > 1 );
628         AttributeField attributeField = attribute.getListAttributeFields( ).get( 0 );
629         int nOrigPosition = attributeField.getPosition( );
630         MockHttpServletRequest request = new MockHttpServletRequest( );
631         request.setParameter( "id_attribute", Integer.toString( attribute.getIdAttribute( ) ) );
632         request.setParameter( "id_field", Integer.toString( attributeField.getIdField( ) ) );
633 
634         try
635         {
636             instance.doMoveUpAttributeField( request );
637             fail( "Should have thrown" );
638         }
639         catch( AccessDeniedException e )
640         {
641             IAttribute stored = AttributeService.getInstance( ).getAttributeWithFields( attribute.getIdAttribute( ), Locale.FRANCE );
642             assertNotNull( stored );
643             AttributeField field = stored.getListAttributeFields( ).stream( ).filter( f -> f.getIdField( ) == attributeField.getIdField( ) ).findFirst( )
644                     .orElseThrow( AssertionError::new );
645             assertEquals( nOrigPosition, field.getPosition( ) );
646         }
647     }
648 }