Skip to content

Commit

Permalink
Start of api tests
Browse files Browse the repository at this point in the history
add first parts of api tests
  • Loading branch information
FabianDevel committed Jan 11, 2022
1 parent 8b85dda commit 200a85e
Show file tree
Hide file tree
Showing 4 changed files with 237 additions and 14 deletions.
144 changes: 144 additions & 0 deletions app/src/androidTest/java/com/infomaniak/drive/ApiRepositoryTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Infomaniak kDrive - Android
* Copyright (C) 2022 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.drive

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.infomaniak.drive.data.api.ApiRepository.deleteFavoriteFile
import com.infomaniak.drive.data.api.ApiRepository.deleteFileComment
import com.infomaniak.drive.data.api.ApiRepository.getAllDrivesData
import com.infomaniak.drive.data.api.ApiRepository.getFavoriteFiles
import com.infomaniak.drive.data.api.ApiRepository.getFileActivities
import com.infomaniak.drive.data.api.ApiRepository.getFileComments
import com.infomaniak.drive.data.api.ApiRepository.getUserProfile
import com.infomaniak.drive.data.api.ApiRepository.postFavoriteFile
import com.infomaniak.drive.data.api.ApiRepository.postFileComment
import com.infomaniak.drive.data.api.ApiRepository.putFileComment
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.utils.ApiTestUtils
import com.infomaniak.drive.utils.ApiTestUtils.assertApiResponse
import com.infomaniak.drive.utils.ApiTestUtils.createFileForTest
import com.infomaniak.drive.utils.ApiTestUtils.deleteTestFile
import com.infomaniak.drive.utils.KDriveHttpClient
import io.realm.Realm
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

/**
* Logging activity testing class
*/
@RunWith(AndroidJUnit4::class)
class ApiRepositoryTest : KDriveTest() {
private lateinit var realm: Realm

@Before
@Throws(Exception::class)
fun setUp() {
realm = Realm.getInstance(getConfig())
}

@After
@Throws(Exception::class)
fun tearDown() {
if (!realm.isClosed) realm.close()
Realm.deleteRealm(getConfig())
}

@Test
fun getDriveData(): Unit = runBlocking {
val okHttpClient = userDrive.userId.let { KDriveHttpClient.getHttpClient(it) }
val drivesDataResponse = getAllDrivesData(okHttpClient)
ApiTestUtils.assertApiResponse(drivesDataResponse)
}

@Test
fun getTestUserProfile(): Unit = runBlocking {
val okHttpClient = userDrive.userId.let { KDriveHttpClient.getHttpClient(it) }
val userProfileResponse = getUserProfile(okHttpClient)
ApiTestUtils.assertApiResponse(userProfileResponse)
Assert.assertEquals("User ids should be the same", userDrive.userId, userProfileResponse.data?.id)
}

@Test
fun manageFavoriteFileLifecycle(): Unit = runBlocking {
val remoteFile = createFileForTest()
// Make the file a favorite
val postFavoriteFileResponse = postFavoriteFile(remoteFile)
ApiTestUtils.assertApiResponse(postFavoriteFileResponse)
// Get the favorite files
var getFavoriteResponse = getFavoriteFiles(userDrive.driveId, File.SortType.NAME_AZ, 1)
ApiTestUtils.assertApiResponse(getFavoriteResponse)
Assert.assertTrue("Favorite file should exists", getFavoriteResponse.data!!.isNotEmpty())
val favoriteFileCount = getFavoriteResponse.data!!.size
// Delete the favorite file
val deleteFavoriteFileResponse = deleteFavoriteFile(remoteFile)
ApiTestUtils.assertApiResponse(deleteFavoriteFileResponse)
getFavoriteResponse = getFavoriteFiles(userDrive.driveId, File.SortType.NAME_AZ, 1)
Assert.assertEquals(
"The number of favorite files should be lowered by 1",
favoriteFileCount - 1,
getFavoriteResponse.data?.size
)
}

@Test
fun getTestFileActivities() {
val remoteFile = createFileForTest()
assertApiResponse(getFileActivities(remoteFile, 1))
deleteTestFile(remoteFile)
val deletedFileActivitiesResponse = getFileActivities(remoteFile, 1)
Assert.assertFalse(deletedFileActivitiesResponse.isSuccess())
}

@Test
fun manageTestFileCommentLifeCycle() {
val remoteFile = createFileForTest()
// Get comments
getFileComments(remoteFile, 1).also {
assertApiResponse(it)
Assert.assertTrue("Test file should not have comments", it.data.isNullOrEmpty())
}

// Post 2 comments
val commentBody = "Hello world"
val commentID = postFileComment(remoteFile, commentBody).also {
assertApiResponse(it)
Assert.assertEquals(commentBody, it.data?.body)
}.data!!.id
val commentID2 = postFileComment(remoteFile, commentBody).also { assertApiResponse(it) }.data!!.id
Assert.assertNotEquals("Comments id should be different", commentID, commentID2)
// Get new comments
getFileComments(remoteFile, 1).also {
assertApiResponse(it)
Assert.assertEquals("There should be 2 comments on the test file", 2, it.data?.size)
}
// Delete first comment
deleteFileComment(remoteFile, commentID)
Assert.assertEquals("There should be 1 comment on the test file", 1, getFileComments(remoteFile, 1).data?.size)

// Put second comment
val putCommentResponse = putFileComment(remoteFile, commentID2, "42")
assertApiResponse(putCommentResponse)
putCommentResponse.data?.let { Assert.assertTrue(it) }
// Delete file
deleteTestFile(remoteFile)
}
}
59 changes: 46 additions & 13 deletions app/src/androidTest/java/com/infomaniak/drive/FileControllerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.infomaniak.drive

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.infomaniak.drive.data.api.ApiRepository
import com.infomaniak.drive.data.api.ApiRepository.getFileListForFolder
import com.infomaniak.drive.data.cache.FileController
import com.infomaniak.drive.data.cache.FileController.getFileById
import com.infomaniak.drive.data.models.CreateFile
import com.infomaniak.drive.data.models.File
import com.infomaniak.drive.utils.ApiTestUtils
import com.infomaniak.drive.utils.ApiTestUtils.assertApiResponse
import com.infomaniak.drive.utils.ApiTestUtils.createFileForTest
import com.infomaniak.drive.utils.ApiTestUtils.deleteTestFile
import com.infomaniak.drive.utils.Env
import com.infomaniak.drive.utils.KDriveHttpClient
import com.infomaniak.drive.utils.Utils
import io.realm.Realm
import kotlinx.android.parcel.RawValue
Expand Down Expand Up @@ -39,6 +43,19 @@ class FileControllerTest : KDriveTest() {
Realm.deleteRealm(getConfig())
}

@Test
fun createTestFolder(): Unit = runBlocking {
val okHttpClient = userDrive.userId.let { KDriveHttpClient.getHttpClient(it) }
val folderName = "TestFolder"
// Create a folder under root
val apiRes = ApiRepository.createFolder(okHttpClient, userDrive.driveId, Utils.ROOT_ID, folderName, true)
ApiTestUtils.assertApiResponse(apiRes)
Assert.assertEquals("The name should correspond", folderName, apiRes.data?.name)

// Delete the test folder
apiRes.data?.let { deleteTestFile(it) }
}

@Test
fun getRootFiles_CanGetRemoteSavedFilesFromRealm() {
val remoteResult = getAndSaveRemoteRootFiles()
Expand All @@ -53,6 +70,21 @@ class FileControllerTest : KDriveTest() {
)
}

@Test
fun deleteAddedFileFromAPI() {
// Create a file
val remoteFile = createAndStoreOfficeFile()
val order = File.SortType.NAME_AZ

// Delete the file
deleteTestFile(remoteFile)

// Search the deleted file
val apiSearchResponse = ApiRepository.searchFiles(userDrive.driveId, remoteFile.name, order.order, order.orderBy, 1)
Assert.assertTrue("Api response must be a success", apiSearchResponse.isSuccess())
Assert.assertTrue("Founded files should be empty", apiSearchResponse.data.isNullOrEmpty())
}

@Test
fun getFavoriteFiles_CanGetRemoteSavedFilesFromRealm() {

Expand Down Expand Up @@ -177,6 +209,16 @@ class FileControllerTest : KDriveTest() {
Assert.assertTrue("Realm must not contain any files", realmResult.isNullOrEmpty())
}

@Test
fun getTestFileListForFolder() = runBlocking {
val okHttpClient = userDrive.userId.let { KDriveHttpClient.getHttpClient(it) }
val fileListResponse = getFileListForFolder(okHttpClient, userDrive.driveId, Utils.ROOT_ID, order = File.SortType.NAME_AZ)
assertApiResponse(fileListResponse)
// Use non null assertion because data nullability has been checked in assertApiResponse()
Assert.assertTrue("Root folder should contains files", fileListResponse.data!!.children.isNotEmpty())
}


private fun getAndSaveRemoteRootFiles(): Pair<File, ArrayList<File>>? {
// Get and save remote root files in realm db test
val remoteResult =
Expand All @@ -189,20 +231,11 @@ class FileControllerTest : KDriveTest() {
FileController.getFilesFromCacheOrDownload(Utils.ROOT_ID, 1, false, userDrive = userDrive, customRealm = realm)

private fun createAndStoreOfficeFile(transaction: ((remoteFile: File) -> Unit)? = null): @RawValue File {
val createFile = CreateFile("offline doc ${UUID.randomUUID()}", File.Office.DOCS.extension)
val apiResponse = ApiRepository.createOfficeFile(Env.DRIVE_ID, Utils.ROOT_ID, createFile)
Assert.assertTrue("create office file request must pass", apiResponse.isSuccess())
Assert.assertNotNull("create office api response data cannot be null", apiResponse.data)

// Save and set file as offline file
val remoteFile = apiResponse.data!!
val remoteFile = createFileForTest()
// Save the file as offline file
transaction?.invoke(remoteFile)
realm.executeTransaction { realm.insert(remoteFile) }
return remoteFile
}

private fun deleteTestFile(remoteFile: File) {
val deleteResponse = ApiRepository.deleteFile(remoteFile)
Assert.assertTrue("created file couldn't be deleted from the remote", deleteResponse.isSuccess())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Infomaniak kDrive - Android
* Copyright (C) 2022 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.drive.utils

import com.infomaniak.drive.data.api.ApiRepository
import com.infomaniak.drive.data.models.CreateFile
import com.infomaniak.drive.data.models.File
import com.infomaniak.lib.core.models.ApiResponse
import kotlinx.android.parcel.RawValue
import org.junit.Assert
import java.util.*

object ApiTestUtils {

fun assertApiResponse(response: ApiResponse<*>, dataShouldNotBeNull: Boolean = true) {
Assert.assertTrue("This should succeed", response.isSuccess())
Assert.assertNull("There should be no error", response.error)
if (dataShouldNotBeNull) Assert.assertNotNull("The data cannot be null", response.data)
}

fun deleteTestFile(remoteFile: File) {
val deleteResponse = ApiRepository.deleteFile(remoteFile)
Assert.assertTrue("created file couldn't be deleted from the remote", deleteResponse.isSuccess())
}

fun createFileForTest(): @RawValue File {
val createFile = CreateFile("offline doc ${UUID.randomUUID()}", File.Office.DOCS.extension)
val apiResponse = ApiRepository.createOfficeFile(Env.DRIVE_ID, Utils.ROOT_ID, createFile)
assertApiResponse(apiResponse)
return apiResponse.data!!
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package com.infomaniak.drive.data.cache

import android.content.Context
import android.util.Log
import androidx.collection.ArrayMap
import androidx.collection.arrayMapOf
import com.infomaniak.drive.data.api.ApiRepository
Expand Down

0 comments on commit 200a85e

Please sign in to comment.