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.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
67
68 public class FileServiceTest extends LuteceTestCase
69 {
70
71
72
73
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
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
103
104
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
127
128
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
153
154
155
156
157
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
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
198
199
200
201
202
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
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
242
243
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
265
266
267
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
284
285
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
299
300
301
302
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 }