Skip to content

Commit

Permalink
use dav4jvm search and proppatch
Browse files Browse the repository at this point in the history
Signed-off-by: tobiasKaminsky <[email protected]>
  • Loading branch information
tobiasKaminsky committed Dec 12, 2022
1 parent 2550afb commit d432b17
Show file tree
Hide file tree
Showing 29 changed files with 1,760 additions and 116 deletions.
1 change: 1 addition & 0 deletions dav4jvm
Submodule dav4jvm added at c61e4b
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ configurations {
dependencies {
implementation 'org.apache.jackrabbit:jackrabbit-webdav:2.13.5'
api 'com.squareup.okhttp3:okhttp:5.0.0-alpha.10'
implementation 'com.gitlab.bitfireAT:dav4jvm:2.1.3' // in transition phase, we use old and new libs
implementation 'com.github.bitfireAT:dav4jvm:2.2' // in transition phase, we use old and new libs
implementation group: 'com.google.code.gson', name: 'gson', version: '2.10'
implementation 'androidx.annotation:annotation:1.5.0'
compileOnly 'com.google.code.findbugs:annotations:3.0.1u2'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public static void beforeAll() throws InterruptedException,
String userId = loginName; // for test same as userId
String credentials = Credentials.basic(loginName, password);
nextcloudClient = new NextcloudClient(url, userId, credentials, context);
nextcloudClient.setUserId(userId);

waitForServer(client, url);
testConnection();
Expand Down
252 changes: 252 additions & 0 deletions library/src/androidTest/java/com/owncloud/android/Dav4JVM.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
/* Nextcloud Android Library is available under MIT license
*
* @author Tobias Kaminsky
* Copyright (C) 2022 Tobias Kaminsky
* Copyright (C) 2022 Nextcloud GmbH
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/

package com.owncloud.android

import at.bitfire.dav4jvm.DavResource
import at.bitfire.dav4jvm.Response
import at.bitfire.dav4jvm.property.CreationDate
import com.nextcloud.common.NextcloudAuthenticator
import com.owncloud.android.lib.common.network.WebdavUtils
import com.owncloud.android.lib.common.utils.WebDavFileUtils
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
import com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation
import com.owncloud.android.lib.resources.files.SearchRemoteOperation
import com.owncloud.android.lib.resources.files.ToggleFavoriteRemoteOperation
import com.owncloud.android.lib.resources.files.UploadFileRemoteOperation
import com.owncloud.android.lib.resources.files.model.RemoteFile
import com.owncloud.android.lib.resources.files.webdav.NCFavorite
import com.owncloud.android.lib.resources.shares.CreateShareRemoteOperation
import com.owncloud.android.lib.resources.shares.OCShare
import com.owncloud.android.lib.resources.shares.ShareType
import com.owncloud.android.lib.resources.status.OCCapability
import okhttp3.HttpUrl.Companion.toHttpUrl
import org.apache.jackrabbit.webdav.DavConstants
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.IOException

/*
can be removed after fully switching to dav4jvm as other tests should cover it
*/
class Dav4JVM : AbstractIT() {
@Test
@Throws(IOException::class)
fun singlePropfind() {
val path = "/testFolder/"

// create folder
CreateFolderRemoteOperation(
path,
true
).execute(client).isSuccess

// verify folder
assertTrue(ReadFolderRemoteOperation(path).execute(client).isSuccess)

// add favorite
assertTrue(ToggleFavoriteRemoteOperation(true, path).execute(client).isSuccess)

// share it
assertTrue(
CreateShareRemoteOperation(
path,
ShareType.USER,
"admin",
false,
"",
OCShare.MAXIMUM_PERMISSIONS_FOR_FOLDER,
true
).execute(client)
.isSuccess
)

// do old read folder operation to compare data against it
val result = ReadFolderRemoteOperation(path).execute(client).data as List<RemoteFile>
val oldRemoteFile = result[0]

// new
val httpUrl = (nextcloudClient.filesDavUri.toString() + path).toHttpUrl()

var davResponse: Response? = null

val memberElements: MutableList<Response> = ArrayList()
var rootElement: Response? = null

// disable redirect
val client = nextcloudClient.client
.newBuilder()
.followRedirects(false)
.authenticator(NextcloudAuthenticator(nextcloudClient.credentials, "Authorization"))
.build()

// register custom property
WebdavUtils.registerCustomFactories()

DavResource(client, httpUrl)
.propfind(
DavConstants.DEPTH_1,
*WebdavUtils.getAllPropertiesList()
) { response: Response, hrefRelation: Response.HrefRelation? ->
davResponse = response
when (hrefRelation) {
Response.HrefRelation.MEMBER -> memberElements.add(response)
Response.HrefRelation.SELF -> rootElement = response
Response.HrefRelation.OTHER -> {}
else -> {}
}
}

assertTrue(davResponse?.isSuccess() == true)
assertTrue(rootElement != null)
assertEquals(0, memberElements.size)

val remoteFile = WebDavFileUtils().parseResponse(rootElement, nextcloudClient.filesDavUri)

val date = davResponse?.get(CreationDate::class.java)
assertEquals(
oldRemoteFile.creationTimestamp,
(WebdavUtils.parseResponseDate(date?.creationDate)?.time ?: 0) / 1000
)

assertTrue(oldRemoteFile.isFavorite)
val favorite = davResponse?.get(NCFavorite::class.java)
assertTrue(favorite?.isOcFavorite == true)

assertEquals(oldRemoteFile.remotePath, remoteFile.remotePath)
assertEquals(oldRemoteFile.mimeType, remoteFile.mimeType)
assertEquals(oldRemoteFile.length, remoteFile.length)
assertEquals(oldRemoteFile.creationTimestamp, remoteFile.creationTimestamp)
// assertEquals(oldRemoteFile.modifiedTimestamp, remoteFile.modifiedTimestamp)
assertEquals(oldRemoteFile.uploadTimestamp, remoteFile.uploadTimestamp)
assertEquals(oldRemoteFile.etag, remoteFile.etag)
assertEquals(oldRemoteFile.permissions, remoteFile.permissions)
assertEquals(oldRemoteFile.remoteId, remoteFile.remoteId)
assertEquals(oldRemoteFile.size, remoteFile.size)
assertEquals(oldRemoteFile.isFavorite, remoteFile.isFavorite)
assertEquals(oldRemoteFile.isEncrypted, remoteFile.isEncrypted)
assertEquals(oldRemoteFile.mountType, remoteFile.mountType)
assertEquals(oldRemoteFile.ownerId, remoteFile.ownerId)
assertEquals(oldRemoteFile.ownerDisplayName, remoteFile.ownerDisplayName)
assertEquals(oldRemoteFile.unreadCommentsCount, remoteFile.unreadCommentsCount)
assertEquals(oldRemoteFile.isHasPreview, remoteFile.isHasPreview)
assertEquals(oldRemoteFile.note, remoteFile.note)
assertEquals(oldRemoteFile.sharees.size, remoteFile.sharees.size)
assertEquals(oldRemoteFile.richWorkspace, remoteFile.richWorkspace)
assertEquals(oldRemoteFile.isLocked, remoteFile.isLocked)
assertEquals(oldRemoteFile.lockType, remoteFile.lockType)
assertEquals(oldRemoteFile.lockOwner, remoteFile.lockOwner)
assertEquals(oldRemoteFile.lockOwnerDisplayName, remoteFile.lockOwnerDisplayName)
assertEquals(oldRemoteFile.lockTimestamp, remoteFile.lockTimestamp)
assertEquals(oldRemoteFile.lockOwnerEditor, remoteFile.lockOwnerEditor)
assertEquals(oldRemoteFile.lockTimeout, remoteFile.lockTimeout)
assertEquals(oldRemoteFile.lockToken, remoteFile.lockToken)
assertEquals(oldRemoteFile.localId, remoteFile.localId)
}

@Test
fun search() {
val path = "/testFolder/"

// create folder
assertTrue(
CreateFolderRemoteOperation(
path,
true
).execute(client).isSuccess
)

// create file
val filePath = createFile("text")
val remotePath = "/test.md"

assertTrue(
UploadFileRemoteOperation(
filePath,
remotePath,
"text/markdown",
"",
RANDOM_MTIME,
System.currentTimeMillis(),
true
).execute(client).isSuccess
)

WebdavUtils.registerCustomFactories()

var ror = SearchRemoteOperation(
"test",
SearchRemoteOperation.SearchType.FILE_SEARCH,
false,
OCCapability(23, 0, 0)
).execute(
client
)

assertTrue(ror.isSuccess)
assertEquals(2, ror.resultData.size)

val oldRemoteFile = ror.resultData[0]
assertEquals(path, oldRemoteFile.remotePath)

ror = SearchRemoteOperation(
"test",
SearchRemoteOperation.SearchType.FILE_SEARCH,
false,
OCCapability(23, 0, 0)
).execute(
nextcloudClient
)

assertTrue(ror.isSuccess)
assertEquals(2, ror.resultData.size)

val remoteFile = ror.resultData[0]
assertEquals(path, remoteFile.remotePath)

assertEquals(oldRemoteFile.remoteId, remoteFile.remoteId)
}

@Test
fun propPatch() {
val path = "/testFolder/"

// create folder
assertTrue(CreateFolderRemoteOperation(path, true).execute(client).isSuccess)

// make it favorite
assertTrue(
ToggleFavoriteRemoteOperation(true, path).execute(nextcloudClient).isSuccess
)

val result = ReadFolderRemoteOperation(path).execute(client)
assertTrue(result.isSuccess)
val list = result.data as List<RemoteFile>
assertTrue(list[0].isFavorite)
}
}
9 changes: 5 additions & 4 deletions library/src/androidTest/java/com/owncloud/android/FileIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
package com.owncloud.android;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import android.net.Uri;

import com.owncloud.android.lib.common.operations.RemoteOperationResult;
Expand All @@ -43,10 +47,6 @@
import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
* Tests related to file operations
*/
Expand All @@ -60,6 +60,7 @@ public void testCreateFolderSuccess() {

// verify folder
assertTrue(new ReadFolderRemoteOperation(path).execute(client).isSuccess());
assertTrue(new ReadFolderRemoteOperation(path).execute(nextcloudClient).isSuccess());

// remove folder
assertTrue(new RemoveFileRemoteOperation(path).execute(client).isSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,22 +164,43 @@ public void noFavorites() {
@Test
public void oneFavorite() {
String path = "/testFolder/";
String path2 = "/testFolder2/";

// create folder, make it favorite
new CreateFolderRemoteOperation(path, true).execute(client);
new CreateFolderRemoteOperation(path2, true).execute(client);
assertTrue(new ToggleFavoriteRemoteOperation(true, path).execute(client).isSuccess());
assertTrue(new ToggleFavoriteRemoteOperation(true, path2).execute(nextcloudClient).isSuccess());

SearchRemoteOperation sut = new SearchRemoteOperation("",
SearchRemoteOperation.SearchType.FAVORITE_SEARCH,
false,
capability);
RemoteOperationResult<List<RemoteFile>> result = sut.execute(client);
RemoteOperationResult<List<RemoteFile>> result = sut.execute(nextcloudClient);

// test
assertTrue(result.isSuccess());
assertEquals(1, result.getResultData().size());
assertEquals(2, result.getResultData().size());

RemoteFile remoteFile = result.getResultData().get(0);
assertEquals(path, remoteFile.getRemotePath());

RemoteFile remoteFile2 = result.getResultData().get(1);
assertEquals(path2, remoteFile2.getRemotePath());

// unfavorite
assertTrue(new ToggleFavoriteRemoteOperation(false, path).execute(client).isSuccess());
assertTrue(new ToggleFavoriteRemoteOperation(false, path2).execute(nextcloudClient).isSuccess());

// test
sut = new SearchRemoteOperation("",
SearchRemoteOperation.SearchType.FAVORITE_SEARCH,
false,
capability);
result = sut.execute(nextcloudClient);

assertTrue(result.isSuccess());
assertEquals(0, result.getResultData().size());
}

@Test
Expand Down
Loading

0 comments on commit d432b17

Please sign in to comment.