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.service.file;
35  
36  import java.io.FileWriter;
37  import java.io.IOException;
38  import java.io.PrintWriter;
39  import java.io.UnsupportedEncodingException;
40  import java.nio.charset.Charset;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Locale;
44  import java.util.Map;
45  
46  import javax.servlet.http.HttpServletRequest;
47  
48  import org.apache.commons.fileupload.FileItem;
49  import org.apache.commons.fileupload.disk.DiskFileItemFactory;
50  import org.apache.commons.io.FileUtils;
51  import org.apache.http.NameValuePair;
52  import org.apache.http.client.utils.URLEncodedUtils;
53  import org.springframework.mock.web.MockHttpServletRequest;
54  
55  import fr.paris.lutece.portal.business.file.File;
56  import fr.paris.lutece.portal.business.physicalfile.PhysicalFile;
57  import fr.paris.lutece.portal.business.user.AdminUser;
58  import fr.paris.lutece.portal.service.admin.AccessDeniedException;
59  import fr.paris.lutece.portal.service.admin.AdminAuthenticationService;
60  import fr.paris.lutece.portal.service.security.UserNotSignedException;
61  import fr.paris.lutece.test.LuteceTestCase;
62  import fr.paris.lutece.util.date.DateUtil;
63  
64  /**
65   *
66   * @author seboo
67   */
68  public class FileServiceTest extends LuteceTestCase
69  {
70      /**
71       * test store Lutece File
72       * 
73       * @throws java.io.UnsupportedEncodingException
74       */
75      public void testStoreFile( ) throws UnsupportedEncodingException
76      {
77          File file = getOneLuteceFile( );
78  
79          try 
80          {
81  	        String strFileId = FileService.getInstance( ).getFileStoreServiceProvider( ).storeFile( file );
82  	
83  	        File storedFile = FileService.getInstance( ).getFileStoreServiceProvider( ).getFile( strFileId );
84  	
85  	        assertEquals( file.getTitle( ), storedFile.getTitle( ) );
86  	
87  	        // test delete
88  	        FileService.getInstance( ).getFileStoreServiceProvider( ).delete( strFileId );
89  	        storedFile = FileService.getInstance( ).getFileStoreServiceProvider( ).getFile( strFileId );
90  	
91  	        assertNull( storedFile );
92          
93          }
94          catch( FileServiceException e )
95          {
96          	fail( e.getMessage( ) );
97          }
98  
99      }
100 
101     /**
102      * test store
103      * 
104      * @throws IOException
105      */
106     public void testStoreBytes( ) throws IOException
107     {
108     	try
109     	{
110 	        java.io.File file = getOneFile( );
111 	        byte [ ] fileInBytes = FileUtils.readFileToByteArray( file );
112 	
113 	        String strFileId = FileService.getInstance( ).getFileStoreServiceProvider( ).storeBytes( fileInBytes );
114 	
115 	        File storedFile = FileService.getInstance( ).getFileStoreServiceProvider( ).getFile( strFileId );
116 	
117 	        assertEquals( fileInBytes.length, storedFile.getPhysicalFile( ).getValue( ).length );
118     	}
119         catch( FileServiceException e )
120         {
121         	fail( e.getMessage( ) );
122         }
123     }
124 
125     /**
126      * test store fileitem
127      * 
128      * @throws IOException
129      */
130     public void testStoreFileItem( ) throws IOException
131     {
132     	try
133     	{
134 	        java.io.File file = getOneFile( );
135 	        FileItem fileItem = getOneFileItem( file );
136 	
137 	        byte [ ] fileInBytes = FileUtils.readFileToByteArray( file );
138 	
139 	        String strFileId = FileService.getInstance( ).getFileStoreServiceProvider( ).storeFileItem( fileItem );
140 	
141 	        File storedFile = FileService.getInstance( ).getFileStoreServiceProvider( ).getFile( strFileId );
142 	
143 	        assertEquals( fileInBytes.length, storedFile.getPhysicalFile( ).getValue( ).length );
144     	}
145         catch( FileServiceException e )
146         {
147         	fail( e.getMessage( ) );
148         }
149     }
150 
151     /**
152      * test store fileitem
153      * 
154      * @throws IOException
155      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
156      * @throws fr.paris.lutece.portal.service.file.ExpiredLinkException
157      * @throws fr.paris.lutece.portal.service.security.UserNotSignedException
158      */
159     public void testDownloadFileBO( ) throws IOException, AccessDeniedException, ExpiredLinkException, UserNotSignedException
160     {
161         File file = getOneLuteceFile( );
162 
163         try
164         {
165 	        String strFileId = FileService.getInstance( ).getFileStoreServiceProvider( ).storeFile( file );
166 	
167 	        Map<String, String> data = new HashMap<>( );
168 	        data.put( FileService.PARAMETER_RESOURCE_ID, "123" );
169 	        data.put( FileService.PARAMETER_RESOURCE_TYPE, "TEST" );
170 	
171 	        String strUrl = FileService.getInstance( ).getFileStoreServiceProvider( ).getFileDownloadUrlBO( strFileId, data );
172 	        assertNotNull( strUrl );
173 	
174 	        List<NameValuePair> params = URLEncodedUtils.parse( strUrl, Charset.forName( "UTF-8" ) );
175 	
176 	        MockHttpServletRequest request = new MockHttpServletRequest( );
177 	        for ( NameValuePair param : params )
178 	        {
179 	            request.addParameter( param.getName( ), param.getValue( ) );
180 	        }
181 	
182 	        // add mock BO authentication
183 	        registerAdminUserAdmin( request );
184 	
185 	        File storedFile = FileService.getInstance( ).getFileStoreServiceProvider( ).getFileFromRequestBO( request );
186 	
187 	        assertEquals( file.getPhysicalFile( ).getValue( ).length, storedFile.getPhysicalFile( ).getValue( ).length );
188         }
189         catch( FileServiceException e )
190         {
191         	fail( e.getMessage( ) );
192         }
193 
194     }
195 
196     /**
197      * test store fileitem
198      * 
199      * @throws IOException
200      * @throws fr.paris.lutece.portal.service.admin.AccessDeniedException
201      * @throws fr.paris.lutece.portal.service.file.ExpiredLinkException
202      * @throws fr.paris.lutece.portal.service.security.UserNotSignedException
203      */
204     public void testDownloadFileFO( ) throws IOException, AccessDeniedException, ExpiredLinkException, UserNotSignedException
205     {
206         File file = getOneLuteceFile( );
207 
208         try
209         {
210 	        String strFileId = FileService.getInstance( ).getFileStoreServiceProvider( ).storeFile( file );
211 	
212 	        Map<String, String> data = new HashMap<>( );
213 	        data.put( FileService.PARAMETER_RESOURCE_ID, "123" );
214 	        data.put( FileService.PARAMETER_RESOURCE_TYPE, "TEST" );
215 	
216 	        String strUrl = FileService.getInstance( ).getFileStoreServiceProvider( ).getFileDownloadUrlFO( strFileId, data );
217 	        assertNotNull( strUrl );
218 	
219 	        List<NameValuePair> params = URLEncodedUtils.parse( strUrl, Charset.forName( "UTF-8" ) );
220 	
221 	        MockHttpServletRequest request = new MockHttpServletRequest( );
222 	        for ( NameValuePair param : params )
223 	        {
224 	            request.addParameter( param.getName( ), param.getValue( ) );
225 	        }
226 	
227 	        // no authentication
228 	
229 	        File storedFile = FileService.getInstance( ).getFileStoreServiceProvider( ).getFileFromRequestFO( request );
230 	
231 	        assertEquals( file.getPhysicalFile( ).getValue( ).length, storedFile.getPhysicalFile( ).getValue( ).length );
232         }
233         catch( FileServiceException e )
234         {
235         	fail( e.getMessage( ) );
236         }
237 
238     }
239 
240     /**
241      * get lutece test file
242      * 
243      * @return a file
244      */
245     private File getOneLuteceFile( ) throws UnsupportedEncodingException
246     {
247         File file = new File( );
248         file.setTitle( "test" );
249         file.setDateCreation( DateUtil.formatTimestamp( "1/1/2000", Locale.FRANCE ) );
250         file.setExtension( "txt" );
251         file.setMimeType( "text/plain" );
252         file.setSize( 12 );
253 
254         PhysicalFile physicalFile = new PhysicalFile( );
255         physicalFile.setIdPhysicalFile( 1 );
256         physicalFile.setValue( "some content".getBytes( "UTF-8" ) );
257 
258         file.setPhysicalFile( physicalFile );
259 
260         return file;
261     }
262 
263     /**
264      * get java.io.file
265      * 
266      * @return the file
267      * @throws IOException
268      */
269     private java.io.File getOneFile( ) throws IOException
270     {
271         java.io.File file = java.io.File.createTempFile( "test", "txt" );
272 
273         FileWriter fileWriter = new FileWriter( file.getPath( ), true );
274         PrintWriter printWriter = new PrintWriter( fileWriter );
275 
276         printWriter.print( "some content" );
277         printWriter.close( );
278 
279         return file;
280     }
281 
282     /**
283      * get test FileItem
284      * 
285      * @return the file
286      */
287     private FileItem getOneFileItem( java.io.File file ) throws IOException
288     {
289         DiskFileItemFactory factory = new DiskFileItemFactory( );
290         FileItem fileItem = factory.createItem( file.getName( ), "text/plain", false, file.getPath( ) );
291 
292         fileItem.getOutputStream( ).write( FileUtils.readFileToByteArray( file ) );
293 
294         return fileItem;
295     }
296 
297     /**
298      * register admin adminUser for tests
299      * 
300      * @param request
301      * @throws AccessDeniedException
302      * @throws UserNotSignedException
303      */
304     private void registerAdminUserAdmin( HttpServletRequest request ) throws AccessDeniedException, UserNotSignedException
305     {
306         AdminUser adminUser = new AdminUser( );
307         adminUser.setAccessCode( "admin" );
308         adminUser.setLastName( "test" );
309         adminUser.setStatus( 0 );
310         adminUser.setUserLevel( 0 );
311 
312         AdminAuthenticationService.getInstance( ).registerUser( request, adminUser );
313     }
314 }