View Javadoc
1   package org.exoplatform.commons.file.services;
2   
3   import org.exoplatform.commons.file.resource.FileSystemResourceProvider;
4   import org.exoplatform.commons.file.resource.BinaryProvider;
5   import org.exoplatform.commons.file.model.FileInfo;
6   import org.exoplatform.commons.file.model.FileItem;
7   import org.junit.Before;
8   import org.junit.Rule;
9   import org.junit.Test;
10  import org.junit.rules.ExpectedException;
11  import org.junit.rules.TemporaryFolder;
12  
13  import java.io.ByteArrayInputStream;
14  import java.io.FileNotFoundException;
15  
16  import static org.junit.Assert.*;
17  import static org.mockito.Matchers.any;
18  import static org.mockito.Mockito.verify;
19  
20  /**
21   *
22   */
23  public class FileSystemBinaryProviderTest {
24  
25    @Rule
26    public TemporaryFolder         folder    = new TemporaryFolder();
27  
28    @Rule
29    public final ExpectedException exception = ExpectedException.none();
30  
31    @Before
32    public void setup() throws Exception {
33    }
34  
35    @Test
36    public void shouldReadBinary() throws Exception {
37  
38    }
39  
40    @Test
41    public void shouldWriteBinary() throws Exception {
42      // Given
43      FileSystemResourceProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
44      // When
45      FileItem file = new FileItem(1L, "file1", "", null, 1, null, "", false, new ByteArrayInputStream(new byte[] {}));
46      fileResourceProvider.put(file.getFileInfo().getChecksum(), file.getAsStream());
47  
48      // Then
49      java.io.File createdFile = fileResourceProvider.getFile(file.getFileInfo().getChecksum());
50      assertTrue(createdFile.exists());
51    }
52  
53    @Test
54    public void shouldWriteBinaryWhenFileAlreadyExistsAndBinaryHasChanged() throws Exception {
55      // Given
56      FileSystemResourceProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
57  
58      // When
59      FileItem file = new FileItem(1L, "file1", "", null, 1, null, "", false, new ByteArrayInputStream("test".getBytes()));
60      fileResourceProvider.put(file.getFileInfo().getChecksum(), file.getAsStream());
61      java.io.File createdFile = fileResourceProvider.getFile(file.getFileInfo().getChecksum());
62      assertTrue(createdFile.exists());
63      file.setChecksum(new ByteArrayInputStream("test-updated".getBytes()));
64      fileResourceProvider.put(file.getFileInfo().getChecksum(), file.getAsStream());
65  
66      // Then
67      java.io.File updatedFile = fileResourceProvider.getFile(file.getFileInfo().getChecksum());
68      assertNotEquals(updatedFile.getAbsolutePath(), createdFile.getAbsolutePath());
69    }
70  
71    @Test
72    public void shouldNotWriteBinaryWhenFileAlreadyExistsAndBinaryHasNotChanged() throws Exception {
73      // Given
74      FileSystemResourceProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
75  
76      // When
77      FileItem file = new FileItem(1L, "file1", "", null, 1, null, "", false, new ByteArrayInputStream("test".getBytes()));
78      fileResourceProvider.put(file);
79      java.io.File createdFile = new java.io.File(fileResourceProvider.getFilePath(file.getFileInfo()));
80      assertTrue(createdFile.exists());
81      fileResourceProvider.put(file);
82  
83      // Then
84      java.io.File updatedFile = new java.io.File(fileResourceProvider.getFilePath(file.getFileInfo()));
85      assertEquals(updatedFile.getAbsolutePath(), createdFile.getAbsolutePath());
86      // TODO need to verify also that it does not effectively write
87      // verify(fileInfoDataStorage, times(1)).update(any(FileInfoEntity.class));
88    }
89  
90    @Test
91    public void shouldDeleteBinary() throws Exception {
92      // Given
93      FileSystemResourceProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
94  
95      // When
96      FileItem file = new FileItem(1L, "file1", "", null, 1, null, "", false, new ByteArrayInputStream("test".getBytes()));
97      fileResourceProvider.put(file);
98      java.io.File createdFile = new java.io.File(fileResourceProvider.getFilePath(file.getFileInfo()));
99      assertTrue(createdFile.exists());
100     fileResourceProvider.remove(file.getFileInfo());
101 
102     // Then
103     java.io.File deletedFile = new java.io.File(fileResourceProvider.getFilePath(file.getFileInfo()));
104     assertFalse(deletedFile.exists());
105   }
106 
107   @Test
108   public void shouldThrowExceptionWhenDeletingABinaryWhichDoesNotExist() throws Exception {
109     // Given
110     FileSystemResourceProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
111 
112     // When
113     FileItem file = new FileItem(1L, "file1", "", null, 1, null, "", false, new ByteArrayInputStream("test".getBytes()));
114     exception.expect(FileNotFoundException.class);
115     fileResourceProvider.remove(file.getFileInfo());
116   }
117 
118   @Test
119   public void shouldReturnPathWhenChecksumIsValid() throws Exception {
120     // Given
121     FileSystemResourceProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
122 
123     // When
124     FileInfo fileInfo = new FileInfo(1L, "file1", "", null, 1, null, "", "d41d8cd98f00b204e9800998ecf8427e", false);
125     String path = fileResourceProvider.getFilePath(fileInfo);
126 
127     // Then
128     assertEquals(folder.getRoot().getPath() + "/d/4/1/d/8/c/d/9/d41d8cd98f00b204e9800998ecf8427e", path);
129   }
130 
131   @Test
132   public void shouldReturnNullWhenChecksumIsNotValid() throws Exception {
133     // Given
134     BinaryProvider fileResourceProvider = new FileSystemResourceProvider(folder.getRoot().getPath());
135 
136     // When
137     FileInfo fileInfo = new FileInfo(1L, "file1", "", null, 1, null, "", "", false);
138     String path = fileResourceProvider.getFilePath(fileInfo);
139 
140     // Then
141     assertNull(path);
142   }
143 }