#168 start position indices by 0 and test position helper
This commit is contained in:
parent
c5e05df01a
commit
22c53f5abe
@ -59,7 +59,7 @@ internal class Upgrade3To4 @Inject constructor() : DatabaseUpgrade(3, 4) {
|
|||||||
.map {
|
.map {
|
||||||
Sql.update("VAULT_ENTITY") //
|
Sql.update("VAULT_ENTITY") //
|
||||||
.where("_id", Sql.eq(it.id)) //
|
.where("_id", Sql.eq(it.id)) //
|
||||||
.set("POSITION", Sql.toInteger(it.id)) //
|
.set("POSITION", Sql.toInteger(it.id - 1)) //
|
||||||
.executeOn(db)
|
.executeOn(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ class CreateVault {
|
|||||||
return vaultRepository.store(aVault() //
|
return vaultRepository.store(aVault() //
|
||||||
.thatIsNew() //
|
.thatIsNew() //
|
||||||
.withNamePathAndCloudFrom(vaultFolder) //
|
.withNamePathAndCloudFrom(vaultFolder) //
|
||||||
.withPosition(vaultRepository.vaults().size() + 1) //
|
.withPosition(vaultRepository.vaults().size()) //
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,17 +21,12 @@ class MoveVaultHelper {
|
|||||||
Collections.swap(vaults, i, i - 1)
|
Collections.swap(vaults, i, i - 1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return reorderVaults(vaults)
|
||||||
for (i in 0 until vaults.size) {
|
|
||||||
vaults[i] = Vault.aCopyOf(vaults[i]).withPosition(i + 1).build()
|
|
||||||
}
|
|
||||||
|
|
||||||
return vaults
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun reorderVaults(vaults: MutableList<Vault>) : List<Vault> {
|
private fun reorderVaults(vaults: MutableList<Vault>) : List<Vault> {
|
||||||
for (i in 0 until vaults.size) {
|
for (i in 0 until vaults.size) {
|
||||||
vaults[i] = Vault.aCopyOf(vaults[i]).withPosition(i + 1).build()
|
vaults[i] = Vault.aCopyOf(vaults[i]).withPosition(i).build()
|
||||||
}
|
}
|
||||||
return vaults;
|
return vaults;
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,100 @@
|
|||||||
|
package org.cryptomator.domain.usecases.vault
|
||||||
|
|
||||||
|
import org.cryptomator.domain.CloudType
|
||||||
|
import org.cryptomator.domain.Vault
|
||||||
|
import org.cryptomator.domain.repository.VaultRepository
|
||||||
|
import org.junit.jupiter.api.Assertions
|
||||||
|
import org.junit.jupiter.api.Assertions.assertEquals
|
||||||
|
import org.junit.jupiter.api.BeforeEach
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import org.mockito.Mockito
|
||||||
|
|
||||||
|
|
||||||
|
class MoveVaultHelperTest {
|
||||||
|
|
||||||
|
private lateinit var orderedVaults: ArrayList<Vault>
|
||||||
|
private lateinit var unorderedVaults: ArrayList<Vault>
|
||||||
|
|
||||||
|
private lateinit var vaultRepository: VaultRepository
|
||||||
|
private lateinit var cloudType: CloudType
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun reorderVaults() {
|
||||||
|
Mockito.`when`(vaultRepository.vaults()).thenReturn(unorderedVaults)
|
||||||
|
assertEquals(orderedVaults, MoveVaultHelper.Companion.reorderVaults(vaultRepository), "Failed to reorderVaults")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun movePositionUp() {
|
||||||
|
Mockito.`when`(vaultRepository.vaults()).thenReturn(orderedVaults)
|
||||||
|
|
||||||
|
val resultList = ArrayList<Vault>()
|
||||||
|
resultList.add(Vault.aVault().withId(2).withPath("").withCloudType(cloudType).withName("foo 5").withPosition(0).build())
|
||||||
|
resultList.add(Vault.aVault().withId(3).withPath("").withCloudType(cloudType).withName("foo 10").withPosition(1).build())
|
||||||
|
resultList.add(Vault.aVault().withId(24).withPath("").withCloudType(cloudType).withName("foo 1").withPosition(2).build())
|
||||||
|
resultList.add(Vault.aVault().withId(4).withPath("").withCloudType(cloudType).withName("foo 15").withPosition(3).build())
|
||||||
|
|
||||||
|
assertEquals(resultList, MoveVaultHelper.Companion.updateVaultPosition(0, 2, vaultRepository), "Failed to movePositionUp")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun movePositionDown() {
|
||||||
|
Mockito.`when`(vaultRepository.vaults()).thenReturn(orderedVaults)
|
||||||
|
|
||||||
|
val resultList2 = ArrayList<Vault>()
|
||||||
|
resultList2.add(Vault.aVault().withId(3).withPath("").withCloudType(cloudType).withName("foo 10").withPosition(0).build())
|
||||||
|
resultList2.add(Vault.aVault().withId(24).withPath("").withCloudType(cloudType).withName("foo 1").withPosition(1).build())
|
||||||
|
resultList2.add(Vault.aVault().withId(2).withPath("").withCloudType(cloudType).withName("foo 5").withPosition(2).build())
|
||||||
|
resultList2.add(Vault.aVault().withId(4).withPath("").withCloudType(cloudType).withName("foo 15").withPosition(3).build())
|
||||||
|
|
||||||
|
assertEquals(resultList2, MoveVaultHelper.Companion.updateVaultPosition(2, 0, vaultRepository), "Failed to movePositionDown")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun movePositionToSelf() {
|
||||||
|
Mockito.`when`(vaultRepository.vaults()).thenReturn(orderedVaults)
|
||||||
|
|
||||||
|
val resultList2 = ArrayList<Vault>()
|
||||||
|
resultList2.add(Vault.aVault().withId(24).withPath("").withCloudType(cloudType).withName("foo 1").withPosition(0).build())
|
||||||
|
resultList2.add(Vault.aVault().withId(2).withPath("").withCloudType(cloudType).withName("foo 5").withPosition(1).build())
|
||||||
|
resultList2.add(Vault.aVault().withId(3).withPath("").withCloudType(cloudType).withName("foo 10").withPosition(2).build())
|
||||||
|
resultList2.add(Vault.aVault().withId(4).withPath("").withCloudType(cloudType).withName("foo 15").withPosition(3).build())
|
||||||
|
|
||||||
|
assertEquals(resultList2, MoveVaultHelper.Companion.updateVaultPosition(1, 1, vaultRepository), "Failed to movePositionToSelf")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun movePositionOutOfBounds() {
|
||||||
|
Mockito.`when`(vaultRepository.vaults()).thenReturn(orderedVaults)
|
||||||
|
Assertions.assertThrows(IndexOutOfBoundsException::class.java) { MoveVaultHelper.Companion.updateVaultPosition(1, 4, vaultRepository) }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun verifyStoreInVaultRepo() {
|
||||||
|
Mockito.`when`(vaultRepository.vaults()).thenReturn(orderedVaults)
|
||||||
|
val result = MoveVaultHelper.Companion.updateVaultsInDatabase(orderedVaults, vaultRepository)
|
||||||
|
assertEquals(orderedVaults, result, "Failed to verifyStoreInVaultRepo")
|
||||||
|
|
||||||
|
orderedVaults.forEach {
|
||||||
|
Mockito.verify(vaultRepository).store(Mockito.eq(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
fun setup() {
|
||||||
|
vaultRepository = Mockito.mock(VaultRepository::class.java)
|
||||||
|
cloudType = Mockito.mock(CloudType::class.java)
|
||||||
|
|
||||||
|
unorderedVaults = ArrayList()
|
||||||
|
unorderedVaults.add(Vault.aVault().withId(24).withPath("").withCloudType(cloudType).withName("foo 1").withPosition(1).build())
|
||||||
|
unorderedVaults.add(Vault.aVault().withId(3).withPath("").withCloudType(cloudType).withName("foo 10").withPosition(10).build())
|
||||||
|
unorderedVaults.add(Vault.aVault().withId(2).withPath("").withCloudType(cloudType).withName("foo 5").withPosition(5).build())
|
||||||
|
unorderedVaults.add(Vault.aVault().withId(4).withPath("").withCloudType(cloudType).withName("foo 15").withPosition(15).build())
|
||||||
|
|
||||||
|
orderedVaults = ArrayList()
|
||||||
|
orderedVaults.add(Vault.aVault().withId(24).withPath("").withCloudType(cloudType).withName("foo 1").withPosition(0).build())
|
||||||
|
orderedVaults.add(Vault.aVault().withId(2).withPath("").withCloudType(cloudType).withName("foo 5").withPosition(1).build())
|
||||||
|
orderedVaults.add(Vault.aVault().withId(3).withPath("").withCloudType(cloudType).withName("foo 10").withPosition(2).build())
|
||||||
|
orderedVaults.add(Vault.aVault().withId(4).withPath("").withCloudType(cloudType).withName("foo 15").withPosition(3).build())
|
||||||
|
}
|
||||||
|
}
|
@ -128,7 +128,7 @@ public class AddExistingVaultWorkflow extends Workflow<AddExistingVaultWorkflow.
|
|||||||
saveVaultUseCase//
|
saveVaultUseCase//
|
||||||
.withVault(aVault() //
|
.withVault(aVault() //
|
||||||
.withNamePathAndCloudFrom(state().masterkeyFile.getParent()) //
|
.withNamePathAndCloudFrom(state().masterkeyFile.getParent()) //
|
||||||
.withPosition(vaults.size() + 1) //
|
.withPosition(vaults.size()) //
|
||||||
.thatIsNew() //
|
.thatIsNew() //
|
||||||
.build()) //
|
.build()) //
|
||||||
.run(presenter().new ProgressCompletingResultHandler<Vault>() {
|
.run(presenter().new ProgressCompletingResultHandler<Vault>() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user