|
| 1 | +package org.cryptomator.domain.usecases.cloud |
| 2 | + |
| 3 | +import org.cryptomator.domain.CachedVaultFile |
| 4 | +import org.cryptomator.domain.CachedVaultFolder |
| 5 | +import org.cryptomator.domain.Cloud |
| 6 | +import org.cryptomator.domain.CloudFile |
| 7 | +import org.cryptomator.domain.CloudFolder |
| 8 | +import org.cryptomator.domain.CloudNode |
| 9 | +import org.cryptomator.domain.CloudType |
| 10 | +import org.cryptomator.domain.Vault |
| 11 | +import org.cryptomator.domain.VaultPath |
| 12 | +import org.cryptomator.domain.exception.NetworkConnectionException |
| 13 | +import org.cryptomator.domain.repository.CloudContentRepository |
| 14 | +import org.cryptomator.domain.repository.VaultDocumentRepository |
| 15 | +import org.cryptomator.domain.repository.VaultListing |
| 16 | +import org.junit.jupiter.api.Assertions.assertEquals |
| 17 | +import org.junit.jupiter.api.Assertions.assertSame |
| 18 | +import org.junit.jupiter.api.Test |
| 19 | +import org.junit.jupiter.api.assertThrows |
| 20 | +import org.mockito.kotlin.any |
| 21 | +import org.mockito.kotlin.mock |
| 22 | +import org.mockito.kotlin.never |
| 23 | +import org.mockito.kotlin.verify |
| 24 | +import org.mockito.kotlin.verifyNoInteractions |
| 25 | +import org.mockito.kotlin.whenever |
| 26 | + |
| 27 | +class GetCloudListRecursiveTest { |
| 28 | + |
| 29 | + private val vault = Vault.aVault().withId(1L).withName("vault").withPath("/vault").withCloudType(CloudType.LOCAL).withPosition(0).build() |
| 30 | + private val cloudContentRepository: CloudContentRepository<Cloud, CloudNode, CloudFolder, CloudFile> = mock() |
| 31 | + private val vaultDocumentRepository: VaultDocumentRepository = mock() |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `a vault folder is listed through the vault's cache, so that pending uploads are included`() { |
| 35 | + val folder = CachedVaultFolder(null, "/a") |
| 36 | + val subfolder = CachedVaultFolder(null, "/a/b") |
| 37 | + val pending = CachedVaultFile(folder, "pending.txt", 3, null, CachedVaultFile.Upload.PENDING, null) |
| 38 | + val uploaded = CachedVaultFile(subfolder, "uploaded.txt", 5, null, CachedVaultFile.Upload.NONE, null) |
| 39 | + whenever(vaultDocumentRepository.pathOf(folder)).thenReturn(VaultPath(vault, "a")) |
| 40 | + whenever(vaultDocumentRepository.pathOf(subfolder)).thenReturn(VaultPath(vault, "a/b")) |
| 41 | + whenever(vaultDocumentRepository.list(folder, true)).thenReturn(VaultListing(listOf(subfolder, pending), false, null)) |
| 42 | + whenever(vaultDocumentRepository.list(subfolder, true)).thenReturn(VaultListing(listOf(uploaded), false, null)) |
| 43 | + |
| 44 | + val listing = GetCloudListRecursive(cloudContentRepository, vaultDocumentRepository, listOf(folder)).execute() |
| 45 | + |
| 46 | + val content = listing.foldersContent.single() |
| 47 | + assertSame(folder, content.parent) |
| 48 | + assertEquals(listOf<CloudFile>(pending), content.files) |
| 49 | + assertSame(subfolder, content.folders.single().parent) |
| 50 | + assertEquals(listOf<CloudFile>(uploaded), content.folders.single().files) |
| 51 | + verifyNoInteractions(cloudContentRepository) |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `a folder outside a vault is listed from the cloud`() { |
| 56 | + val folder: CloudFolder = mock() |
| 57 | + val file: CloudFile = mock() |
| 58 | + whenever(cloudContentRepository.list(folder)).thenReturn(listOf(file)) |
| 59 | + |
| 60 | + val listing = GetCloudListRecursive(cloudContentRepository, vaultDocumentRepository, listOf(folder)).execute() |
| 61 | + |
| 62 | + assertEquals(listOf(file), listing.foldersContent.single().files) |
| 63 | + verify(vaultDocumentRepository, never()).list(any(), any()) |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + fun `a vault folder that can't be listed fails the listing`() { |
| 68 | + val folder = CachedVaultFolder(null, "/a") |
| 69 | + whenever(vaultDocumentRepository.pathOf(folder)).thenReturn(VaultPath(vault, "a")) |
| 70 | + whenever(vaultDocumentRepository.list(folder, true)).thenThrow(NetworkConnectionException()) |
| 71 | + |
| 72 | + assertThrows<NetworkConnectionException> { GetCloudListRecursive(cloudContentRepository, vaultDocumentRepository, listOf(folder)).execute() } |
| 73 | + } |
| 74 | +} |
0 commit comments