From 7fd9a05e7241079305163eacd30754e420c6686e Mon Sep 17 00:00:00 2001 From: Jens Mueller Date: Tue, 20 Oct 2020 21:54:09 +0200 Subject: [PATCH] fix documentprovider test failure because of asynchronous upload Signed-off-by: Jens Mueller Signed-off-by: tobiasKaminsky --- .../providers/DocumentsStorageProviderIT.kt | 66 +++++++++++-------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt b/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt index ad56ae53ea..185a9813df 100644 --- a/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt +++ b/src/androidTest/java/com/owncloud/android/providers/DocumentsStorageProviderIT.kt @@ -6,6 +6,7 @@ import androidx.documentfile.provider.DocumentFile import com.owncloud.android.AbstractOnServerIT import com.owncloud.android.R import com.owncloud.android.datamodel.OCFile.ROOT_PATH +import com.owncloud.android.lib.common.network.WebdavUtils import com.owncloud.android.providers.DocumentsProviderUtils.assertExistsOnServer import com.owncloud.android.providers.DocumentsProviderUtils.assertListFilesEquals import com.owncloud.android.providers.DocumentsProviderUtils.assertReadEquals @@ -18,6 +19,9 @@ import com.owncloud.android.providers.DocumentsProviderUtils.listFilesBlocking import com.owncloud.android.providers.DocumentsStorageProvider.DOCUMENTID_SEPARATOR import kotlinx.coroutines.runBlocking import net.bytebuddy.utility.RandomString +import org.apache.commons.httpclient.HttpStatus +import org.apache.commons.httpclient.methods.ByteArrayRequestEntity +import org.apache.jackrabbit.webdav.client.methods.PutMethod import org.junit.After import org.junit.Assert.assertEquals import org.junit.Assert.assertFalse @@ -173,32 +177,38 @@ class DocumentsStorageProviderIT : AbstractOnServerIT() { assertExistsOnServer(client, ocFile1.remotePath, false) } - // disabled as flaky test - // @Test - // fun testServerChangedFileContent() { - // // create random file - // val file1 = rootDir.createFile("text/plain", RandomString.make())!! - // file1.assertRegularFile(size = 0L) - // - // val content1 = "initial content".toByteArray() - // - // // write content bytes to file - // contentResolver.openOutputStream(file1.uri, "wt").use { - // it!!.write(content1) - // } - // - // val remotePath = file1.getOCFile(storageManager)!!.remotePath - // - // val content2 = "new content".toByteArray() - // - // // modify content on server side - // val putMethod = PutMethod(client.webdavUri.toString() + WebdavUtils.encodePath(remotePath)) - // putMethod.setRequestEntity(ByteArrayRequestEntity(content2)) - // assertEquals(HttpStatus.SC_NO_CONTENT, client.executeMethod(putMethod)) - // client.exhaustResponse(putMethod.responseBodyAsStream) - // putMethod.releaseConnection() // let the connection available for other methods - // - // // read back content bytes - // assertReadEquals(content2, contentResolver.openInputStream(file1.uri)) - // } + @Test + fun testServerChangedFileContent() { + // create random file + val file1 = rootDir.createFile("text/plain", RandomString.make())!! + file1.assertRegularFile(size = 0L) + + val createdETag = file1.getOCFile(storageManager)!!.etag + + val content1 = "initial content".toByteArray() + + // write content bytes to file + contentResolver.openOutputStream(file1.uri, "wt").use { + it!!.write(content1) + } + + while (file1.getOCFile(storageManager)!!.etag == createdETag) { + shortSleep() + } + + val remotePath = file1.getOCFile(storageManager)!!.remotePath + + val content2 = "new content".toByteArray() + + // modify content on server side + val putMethod = PutMethod(client.webdavUri.toString() + WebdavUtils.encodePath(remotePath)) + putMethod.requestEntity = ByteArrayRequestEntity(content2) + assertEquals(HttpStatus.SC_NO_CONTENT, client.executeMethod(putMethod)) + client.exhaustResponse(putMethod.responseBodyAsStream) + putMethod.releaseConnection() // let the connection available for other methods + + // read back content bytes + val bytes = contentResolver.openInputStream(file1.uri)?.readBytes() ?: ByteArray(0) + assertEquals(String(content2), String(bytes)) + } }