FileDataStorageManagerIT.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /*
  2. *
  3. * Nextcloud Android client application
  4. *
  5. * @author Tobias Kaminsky
  6. * Copyright (C) 2020 Tobias Kaminsky
  7. * Copyright (C) 2020 Nextcloud GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. package com.owncloud.android.datamodel;
  23. import android.content.ContentValues;
  24. import com.owncloud.android.AbstractOnServerIT;
  25. import com.owncloud.android.db.ProviderMeta;
  26. import com.owncloud.android.lib.common.operations.RemoteOperationResult;
  27. import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation;
  28. import com.owncloud.android.lib.resources.files.SearchRemoteOperation;
  29. import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation;
  30. import com.owncloud.android.lib.resources.files.model.RemoteFile;
  31. import com.owncloud.android.lib.resources.status.CapabilityBooleanType;
  32. import com.owncloud.android.lib.resources.status.OCCapability;
  33. import com.owncloud.android.operations.RefreshFolderOperation;
  34. import com.owncloud.android.utils.FileStorageUtils;
  35. import junit.framework.TestCase;
  36. import org.junit.After;
  37. import org.junit.Before;
  38. import org.junit.Test;
  39. import java.io.File;
  40. import java.io.IOException;
  41. import java.util.ArrayList;
  42. import java.util.List;
  43. import static com.owncloud.android.lib.resources.files.SearchRemoteOperation.SearchType.GALLERY_SEARCH;
  44. import static com.owncloud.android.lib.resources.files.SearchRemoteOperation.SearchType.PHOTO_SEARCH;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertNotNull;
  47. import static org.junit.Assert.assertNull;
  48. import static org.junit.Assert.assertTrue;
  49. abstract public class FileDataStorageManagerIT extends AbstractOnServerIT {
  50. protected FileDataStorageManager sut;
  51. @Before
  52. public void before() {
  53. // make sure everything is removed
  54. sut.deleteAllFiles();
  55. sut.deleteVirtuals(VirtualFolderType.GALLERY);
  56. assertEquals(0, sut.getAllFiles().size());
  57. }
  58. @After
  59. public void after() {
  60. super.after();
  61. sut.deleteAllFiles();
  62. sut.deleteVirtuals(VirtualFolderType.GALLERY);
  63. }
  64. @Test
  65. public void simpleTest() {
  66. OCFile file = sut.getFileByDecryptedRemotePath("/");
  67. assertNotNull(file);
  68. assertTrue(file.fileExists());
  69. assertNull(sut.getFileByDecryptedRemotePath("/123123"));
  70. }
  71. @Test
  72. public void getAllFiles_NoAvailable() {
  73. assertEquals(0, sut.getAllFiles().size());
  74. }
  75. @Test
  76. public void testFolderContent() throws IOException {
  77. assertEquals(0, sut.getAllFiles().size());
  78. assertTrue(new CreateFolderRemoteOperation("/1/1/", true).execute(client).isSuccess());
  79. assertTrue(new CreateFolderRemoteOperation("/1/2/", true).execute(client).isSuccess());
  80. assertTrue(new UploadFileRemoteOperation(getDummyFile("/chunkedFile.txt").getAbsolutePath(),
  81. "/1/1/chunkedFile.txt",
  82. "text/plain",
  83. String.valueOf(System.currentTimeMillis() / 1000))
  84. .execute(client).isSuccess());
  85. assertTrue(new UploadFileRemoteOperation(getDummyFile("/chunkedFile.txt").getAbsolutePath(),
  86. "/1/1/chunkedFile2.txt",
  87. "text/plain",
  88. String.valueOf(System.currentTimeMillis() / 1000))
  89. .execute(client).isSuccess());
  90. File imageFile = getFile("imageFile.png");
  91. assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(),
  92. "/1/1/imageFile.png",
  93. "image/png",
  94. String.valueOf(System.currentTimeMillis() / 1000))
  95. .execute(client).isSuccess());
  96. // sync
  97. assertNull(sut.getFileByDecryptedRemotePath("/1/1/"));
  98. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"),
  99. System.currentTimeMillis() / 1000,
  100. false,
  101. false,
  102. sut,
  103. user,
  104. targetContext).execute(client).isSuccess());
  105. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/"),
  106. System.currentTimeMillis() / 1000,
  107. false,
  108. false,
  109. sut,
  110. user,
  111. targetContext).execute(client).isSuccess());
  112. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/1/"),
  113. System.currentTimeMillis() / 1000,
  114. false,
  115. false,
  116. sut,
  117. user,
  118. targetContext).execute(client).isSuccess());
  119. assertEquals(3, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/1/1/"), false).size());
  120. }
  121. /**
  122. * This test creates an image, does a photo search (now returned image is not yet in file hierarchy), then root
  123. * folder is refreshed and it is verified that the same image file is used in database
  124. */
  125. @Test
  126. public void testPhotoSearch() throws IOException {
  127. String remotePath = "/imageFile.png";
  128. VirtualFolderType virtualType = VirtualFolderType.GALLERY;
  129. assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
  130. assertEquals(1, sut.getAllFiles().size());
  131. File imageFile = getFile("imageFile.png");
  132. assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(),
  133. remotePath,
  134. "image/png",
  135. String.valueOf(System.currentTimeMillis() / 1000))
  136. .execute(client).isSuccess());
  137. assertNull(sut.getFileByDecryptedRemotePath(remotePath));
  138. // search
  139. SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("image/%",
  140. PHOTO_SEARCH,
  141. false);
  142. RemoteOperationResult<List<RemoteFile>> searchResult = searchRemoteOperation.execute(client);
  143. TestCase.assertTrue(searchResult.isSuccess());
  144. TestCase.assertEquals(1, searchResult.getResultData().size());
  145. OCFile ocFile = FileStorageUtils.fillOCFile(searchResult.getResultData().get(0));
  146. sut.saveFile(ocFile);
  147. List<ContentValues> contentValues = new ArrayList<>();
  148. ContentValues cv = new ContentValues();
  149. cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
  150. cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
  151. contentValues.add(cv);
  152. sut.saveVirtuals(contentValues);
  153. assertEquals(remotePath, ocFile.getRemotePath());
  154. assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
  155. assertEquals(1, sut.getVirtualFolderContent(virtualType, false).size());
  156. assertEquals(2, sut.getAllFiles().size());
  157. // update root
  158. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"),
  159. System.currentTimeMillis() / 1000,
  160. false,
  161. false,
  162. sut,
  163. user,
  164. targetContext).execute(client).isSuccess());
  165. assertEquals(1, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
  166. assertEquals(1, sut.getVirtualFolderContent(virtualType, false).size());
  167. assertEquals(2, sut.getAllFiles().size());
  168. assertEquals(sut.getVirtualFolderContent(virtualType, false).get(0),
  169. sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).get(0));
  170. }
  171. /**
  172. * This test creates an image and a video, does a gallery search (now returned image and video is not yet in file
  173. * hierarchy), then root folder is refreshed and it is verified that the same image file is used in database
  174. */
  175. @Test
  176. public void testGallerySearch() throws IOException {
  177. sut = new FileDataStorageManager(account,
  178. targetContext
  179. .getContentResolver()
  180. .acquireContentProviderClient(ProviderMeta.ProviderTableMeta.CONTENT_URI)
  181. );
  182. String imagePath = "/imageFile.png";
  183. VirtualFolderType virtualType = VirtualFolderType.GALLERY;
  184. assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
  185. assertEquals(1, sut.getAllFiles().size());
  186. File imageFile = getFile("imageFile.png");
  187. assertTrue(new UploadFileRemoteOperation(imageFile.getAbsolutePath(),
  188. imagePath,
  189. "image/png",
  190. String.valueOf((System.currentTimeMillis() - 10000) / 1000))
  191. .execute(client).isSuccess());
  192. // Check that file does not yet exist in local database
  193. assertNull(sut.getFileByDecryptedRemotePath(imagePath));
  194. String videoPath = "/videoFile.mp4";
  195. File videoFile = getFile("videoFile.mp4");
  196. assertTrue(new UploadFileRemoteOperation(videoFile.getAbsolutePath(),
  197. videoPath,
  198. "video/mpeg",
  199. String.valueOf((System.currentTimeMillis() + 10000) / 1000))
  200. .execute(client).isSuccess());
  201. // Check that file does not yet exist in local database
  202. assertNull(sut.getFileByDecryptedRemotePath(videoPath));
  203. // search
  204. SearchRemoteOperation searchRemoteOperation = new SearchRemoteOperation("",
  205. GALLERY_SEARCH,
  206. false);
  207. RemoteOperationResult<List<RemoteFile>> searchResult = searchRemoteOperation.execute(client);
  208. TestCase.assertTrue(searchResult.isSuccess());
  209. TestCase.assertEquals(2, searchResult.getResultData().size());
  210. // newest file must be video path (as sorted by recently modified)
  211. OCFile ocFile = FileStorageUtils.fillOCFile( searchResult.getResultData().get(0));
  212. sut.saveFile(ocFile);
  213. assertEquals(videoPath, ocFile.getRemotePath());
  214. List<ContentValues> contentValues = new ArrayList<>();
  215. ContentValues cv = new ContentValues();
  216. cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
  217. cv.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile.getFileId());
  218. contentValues.add(cv);
  219. // second is image file, as older
  220. OCFile ocFile2 = FileStorageUtils.fillOCFile(searchResult.getResultData().get(1));
  221. sut.saveFile(ocFile2);
  222. assertEquals(imagePath, ocFile2.getRemotePath());
  223. ContentValues cv2 = new ContentValues();
  224. cv2.put(ProviderMeta.ProviderTableMeta.VIRTUAL_TYPE, virtualType.toString());
  225. cv2.put(ProviderMeta.ProviderTableMeta.VIRTUAL_OCFILE_ID, ocFile2.getFileId());
  226. contentValues.add(cv2);
  227. sut.saveVirtuals(contentValues);
  228. assertEquals(0, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
  229. assertEquals(2, sut.getVirtualFolderContent(virtualType, false).size());
  230. assertEquals(3, sut.getAllFiles().size());
  231. // update root
  232. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"),
  233. System.currentTimeMillis() / 1000,
  234. false,
  235. false,
  236. sut,
  237. user,
  238. targetContext).execute(client).isSuccess());
  239. assertEquals(2, sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).size());
  240. assertEquals(2, sut.getVirtualFolderContent(virtualType, false).size());
  241. assertEquals(3, sut.getAllFiles().size());
  242. assertEquals(sut.getVirtualFolderContent(virtualType, false).get(0),
  243. sut.getFolderContent(sut.getFileByDecryptedRemotePath("/"), false).get(0));
  244. }
  245. @Test
  246. public void testSaveNewFile() {
  247. assertTrue(new CreateFolderRemoteOperation("/1/1/", true).execute(client).isSuccess());
  248. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/"),
  249. System.currentTimeMillis() / 1000,
  250. false,
  251. false,
  252. sut,
  253. user,
  254. targetContext).execute(client).isSuccess());
  255. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/"),
  256. System.currentTimeMillis() / 1000,
  257. false,
  258. false,
  259. sut,
  260. user,
  261. targetContext).execute(client).isSuccess());
  262. assertTrue(new RefreshFolderOperation(sut.getFileByDecryptedRemotePath("/1/1/"),
  263. System.currentTimeMillis() / 1000,
  264. false,
  265. false,
  266. sut,
  267. user,
  268. targetContext).execute(client).isSuccess());
  269. OCFile newFile = new OCFile("/1/1/1.txt");
  270. newFile.setRemoteId("123");
  271. sut.saveNewFile(newFile);
  272. }
  273. @Test(expected = IllegalArgumentException.class)
  274. public void testSaveNewFile_NonExistingParent() {
  275. assertTrue(new CreateFolderRemoteOperation("/1/1/", true).execute(client).isSuccess());
  276. OCFile newFile = new OCFile("/1/1/1.txt");
  277. sut.saveNewFile(newFile);
  278. }
  279. @Test
  280. public void testOCCapability() {
  281. OCCapability capability = new OCCapability();
  282. capability.setUserStatus(CapabilityBooleanType.TRUE);
  283. sut.saveCapabilities(capability);
  284. OCCapability newCapability = sut.getCapability(user);
  285. assertEquals(capability.getUserStatus(), newCapability.getUserStatus());
  286. }
  287. }