Skip to content

Commit

Permalink
Tests for sharing deletion within OCShareViewmodel and OCShareDao
Browse files Browse the repository at this point in the history
  • Loading branch information
davigonz committed May 24, 2019
1 parent ef99a02 commit fe8723f
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
@SmallTest
class OCShareDaoTest {
private lateinit var ocShareDao: OCShareDao

@Rule
@JvmField
val instantExecutorRule = InstantTaskExecutorRule()

private lateinit var ocShareDao: OCShareDao

@Before
fun setUp() {
val context = InstrumentationRegistry.getInstrumentation().targetContext
Expand All @@ -61,9 +61,8 @@ class OCShareDaoTest {
"/Test/", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(shares, notNullValue())
assertEquals(shares.size, 0)
assertEquals(0, shares.size)
}

@Test
Expand Down Expand Up @@ -96,42 +95,39 @@ class OCShareDaoTest {
"/Photos/", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(photosFolderShares, notNullValue())
assertEquals(photosFolderShares.size, 1)
assertEquals(photosFolderShares.get(0).path, "/Photos/")
assertEquals(photosFolderShares.get(0).isFolder, true)
assertEquals(photosFolderShares.get(0).accountOwner, "admin@server")
assertEquals(photosFolderShares.get(0).name, "Photos folder link")
assertEquals(photosFolderShares.get(0).shareLink, "http://server:port/s/1")
assertEquals(1, photosFolderShares.size)
assertEquals("/Photos/", photosFolderShares.get(0).path)
assertEquals(true, photosFolderShares.get(0).isFolder)
assertEquals("admin@server", photosFolderShares.get(0).accountOwner)
assertEquals("Photos folder link", photosFolderShares.get(0).name)
assertEquals("http://server:port/s/1", photosFolderShares.get(0).shareLink)

val image1Shares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Photos/image1.jpg", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(image1Shares, notNullValue())
assertEquals(image1Shares.size, 1)
assertEquals(image1Shares.get(0).path, "/Photos/image1.jpg")
assertEquals(image1Shares.get(0).isFolder, false)
assertEquals(image1Shares.get(0).accountOwner, "admin@server")
assertEquals(image1Shares.get(0).name, "Image 1 link")
assertEquals(image1Shares.get(0).shareLink, "http://server:port/s/2")
assertEquals(1, image1Shares.size)
assertEquals("/Photos/image1.jpg", image1Shares.get(0).path)
assertEquals(false, image1Shares.get(0).isFolder)
assertEquals("admin@server", image1Shares.get(0).accountOwner)
assertEquals("Image 1 link", image1Shares.get(0).name)
assertEquals("http://server:port/s/2", image1Shares.get(0).shareLink)

val image2Shares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Photos/image2.jpg", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(image2Shares, notNullValue())
assertEquals(image2Shares.size, 1)
assertEquals(image2Shares.get(0).path, "/Photos/image2.jpg")
assertEquals(image2Shares.get(0).isFolder, false)
assertEquals(image1Shares.get(0).accountOwner, "admin@server")
assertEquals(image2Shares.get(0).name, "Image 2 link")
assertEquals(image2Shares.get(0).shareLink, "http://server:port/s/3")
assertEquals(1, image2Shares.size)
assertEquals("/Photos/image2.jpg", image2Shares.get(0).path)
assertEquals(false, image2Shares.get(0).isFolder)
assertEquals("admin@server", image1Shares.get(0).accountOwner)
assertEquals("Image 2 link", image2Shares.get(0).name)
assertEquals("http://server:port/s/3", image2Shares.get(0).shareLink)
}

@Test
Expand Down Expand Up @@ -167,183 +163,143 @@ class OCShareDaoTest {
"/Documents/document1.docx", "user1@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(document1SharesForUser1, notNullValue())
assertEquals(document1SharesForUser1.size, 1)
assertEquals(document1SharesForUser1.get(0).path, "/Documents/document1.docx")
assertEquals(document1SharesForUser1.get(0).isFolder, false)
assertEquals(document1SharesForUser1.get(0).accountOwner, "user1@server")
assertEquals(document1SharesForUser1.get(0).name, "Document 1 link")
assertEquals(document1SharesForUser1.get(0).shareLink, "http://server:port/s/1")
assertEquals(1, document1SharesForUser1.size)
assertEquals("/Documents/document1.docx", document1SharesForUser1.get(0).path)
assertEquals(false, document1SharesForUser1.get(0).isFolder)
assertEquals("user1@server", document1SharesForUser1.get(0).accountOwner)
assertEquals("Document 1 link", document1SharesForUser1.get(0).name)
assertEquals("http://server:port/s/1", document1SharesForUser1.get(0).shareLink)

val document1SharesForUser2 = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Documents/document1.docx", "user2@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(document1SharesForUser2, notNullValue())
assertEquals(document1SharesForUser2.size, 1)
assertEquals(document1SharesForUser2.get(0).path, "/Documents/document1.docx")
assertEquals(document1SharesForUser2.get(0).isFolder, false)
assertEquals(document1SharesForUser2.get(0).accountOwner, "user2@server")
assertEquals(document1SharesForUser2.get(0).name, "Document 1 link")
assertEquals(document1SharesForUser2.get(0).shareLink, "http://server:port/s/2")
assertEquals(1, document1SharesForUser2.size)
assertEquals("/Documents/document1.docx", document1SharesForUser2.get(0).path)
assertEquals(false, document1SharesForUser2.get(0).isFolder)
assertEquals("user2@server", document1SharesForUser2.get(0).accountOwner)
assertEquals("Document 1 link", document1SharesForUser2.get(0).name)
assertEquals("http://server:port/s/2", document1SharesForUser2.get(0).shareLink)

val document1SharesForUser3 = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Documents/document1.docx", "user3@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(document1SharesForUser3, notNullValue())
assertEquals(document1SharesForUser3.size, 1)
assertEquals(document1SharesForUser3.get(0).path, "/Documents/document1.docx")
assertEquals(document1SharesForUser3.get(0).isFolder, false)
assertEquals(document1SharesForUser3.get(0).accountOwner, "user3@server")
assertEquals(document1SharesForUser3.get(0).name, "Document 1 link")
assertEquals(document1SharesForUser3.get(0).shareLink, "http://server:port/s/3")
assertEquals(1, document1SharesForUser3.size)
assertEquals("/Documents/document1.docx", document1SharesForUser3.get(0).path)
assertEquals(false, document1SharesForUser3.get(0).isFolder)
assertEquals("user3@server", document1SharesForUser3.get(0).accountOwner)
assertEquals("Document 1 link", document1SharesForUser3.get(0).name)
assertEquals("http://server:port/s/3", document1SharesForUser3.get(0).shareLink)
}

@Test
fun getNonExistingPublicShare() {
ocShareDao.insert(
listOf(
TestUtil.createPublicShare(
path = "/Videos/video1.mp4",
isFolder = false,
accountOwner = "user@server",
name = "Video 1 link",
shareLink = "http://server:port/s/1"
)
)
)
ocShareDao.insert(listOf(newPublicShare()))

val video2Shares = getValue(
val nonExistingShare = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Videos/video2.mp4", "user@server", listOf(ShareType.PUBLIC_LINK.value)
"/Texts/text2.txt", "user@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(video2Shares, notNullValue())
assertEquals(video2Shares.size, 0)
assertThat(nonExistingShare, notNullValue())
assertEquals(0, nonExistingShare.size)
}

@Test
fun replacePublicShareIfAlreadyExists_exists() {
ocShareDao.insert(
listOf(
TestUtil.createPublicShare(
path = "/Texts/text1.txt",
isFolder = false,
name = "Text 1 link",
shareLink = "http://server:port/s/1"
)
)
)
ocShareDao.insert(listOf(newPublicShare()))

ocShareDao.replaceSharesForFile(
listOf( // Update link name
TestUtil.createPublicShare(
path = "/Texts/text1.txt",
isFolder = false,
name = "Text 2 link",
shareLink = "http://server:port/s/1"
)
)
listOf(newPublicShare(name = "Text 2 link"))
)

val textShares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(textShares, notNullValue())
assertEquals(textShares.size, 1)
assertEquals(textShares.get(0).name, "Text 2 link")
assertEquals(1, textShares.size)
assertEquals("Text 2 link", textShares.get(0).name)
}

@Test
fun replacePublicShareIfAlreadyExists_doesNotExist() {
ocShareDao.insert(
listOf(
TestUtil.createPublicShare(
path = "/Texts/text1.txt",
isFolder = false,
name = "Text 1 link",
shareLink = "http://server:port/s/1"
)
)
)
ocShareDao.insert(listOf(newPublicShare()))

ocShareDao.replaceSharesForFile(
listOf( // New link
TestUtil.createPublicShare(
path = "/Texts/text2.txt",
isFolder = false,
name = "Text 2 link",
shareLink = "http://server:port/s/2"
)
)
listOf(newPublicShare(path = "/Texts/text2.txt", name = "Text 2 link"))
)

val text1Shares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(text1Shares, notNullValue())
assertEquals(text1Shares.size, 1)
assertEquals(text1Shares.get(0).name, "Text 1 link")
assertEquals(1, text1Shares.size)
assertEquals("Text 1 link", text1Shares.get(0).name)

// text2 link didn't exist before, it should not replace the old one but be created
val text2Shares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Texts/text2.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)

assertThat(text2Shares, notNullValue())
assertEquals(text2Shares.size, 1)
assertEquals(text2Shares.get(0).name, "Text 2 link")
assertEquals(1, text2Shares.size)
assertEquals("Text 2 link", text2Shares.get(0).name)
}

@Test
fun updatePublicShare() {
ocShareDao.insert(
listOf(
TestUtil.createPublicShare(
path = "/Images/image.png",
expirationDate = 1000,
isFolder = false,
name = "Image link",
shareLink = "http://server:port/s/1"
)
)
)
ocShareDao.insert(listOf(newPublicShare()))

ocShareDao.update(
// Updated link
TestUtil.createPublicShare(
path = "/Images/image.png",
expirationDate = 2000,
isFolder = false,
name = "Image link updated",
shareLink = "http://server:port/s/1"
)
newPublicShare(name = "Text 1 link updated", expirationDate = 2000)
)

val imageShares = getValue(
val textShares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Images/image.png", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
"/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)
assertThat(textShares, notNullValue())
assertEquals(1, textShares.size)
assertEquals("Text 1 link updated", textShares.get(0).name)
assertEquals(2000, textShares.get(0).expirationDate)
}

@Test
fun deletePublicShare() {
ocShareDao.insert(listOf(newPublicShare()))

ocShareDao.deleteShare(1)

assertThat(imageShares, notNullValue())
assertEquals(imageShares.size, 1)
assertEquals(imageShares.get(0).name, "Image link updated")
assertEquals(imageShares.get(0).expirationDate, 2000)
val textShares = getValue(
ocShareDao.getSharesForFileAsLiveData(
"/Texts/text1.txt", "admin@server", listOf(ShareType.PUBLIC_LINK.value)
)
)
assertThat(textShares, notNullValue())
assertEquals(0, textShares.size) // List of textShares empty after deleting the existing share
}

private fun newPublicShare(
path: String = "/Texts/text1.txt",
expirationDate: Long = 1000,
name: String = "Text 1 link"
) = TestUtil.createPublicShare(
path = path,
expirationDate = expirationDate,
isFolder = false,
name = name,
shareLink = "http://server:port/s/1"
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,28 @@ class OCShareViewModelTest {
assertShareParameters(resource?.data)
}

@Test
fun deletePublicShare() {
val ocShareRepository = mock(OCShareRepository::class.java)

`when`(
ocShareRepository.deletePublicShare(
3
)
).thenReturn(
publicShareResourcesAsLiveData
)

// Viewmodel that will ask ocShareRepository for shares
val ocShareViewModel = createOCShareViewModel(ocShareRepository)

val resource: Resource<List<OCShare>>? = ocShareViewModel.deletePublicShare(
3
).value

assertShareParameters(resource?.data)
}

private fun createOCShareViewModel(ocShareRepository: OCShareRepository): OCShareViewModel =
OCShareViewModel(
"/Photos/image.jpg",
Expand Down

0 comments on commit fe8723f

Please sign in to comment.