Skip to content

Commit 966bb6d

Browse files
test for tags
Signed-off-by: tobiasKaminsky <tobias@kaminsky.me>
1 parent f8687ba commit 966bb6d

5 files changed

Lines changed: 94 additions & 16 deletions

File tree

library/src/androidTest/java/com/owncloud/android/lib/resources/tags/GetTagsRemoteOperationIT.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,19 @@ package com.owncloud.android.lib.resources.tags
99

1010
import com.nextcloud.test.RandomStringGenerator
1111
import com.owncloud.android.AbstractIT
12+
import com.owncloud.android.lib.common.network.WebdavEntry
13+
import com.owncloud.android.lib.resources.files.CreateFolderRemoteOperation
14+
import com.owncloud.android.lib.resources.files.ReadFileRemoteOperation
15+
import com.owncloud.android.lib.resources.files.ReadFolderRemoteOperation
16+
import com.owncloud.android.lib.resources.files.model.RemoteFile
1217
import junit.framework.TestCase.assertEquals
1318
import junit.framework.TestCase.assertTrue
19+
import org.apache.commons.httpclient.HttpStatus
20+
import org.apache.jackrabbit.webdav.client.methods.PropPatchMethod
21+
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet
22+
import org.apache.jackrabbit.webdav.property.DavPropertySet
23+
import org.apache.jackrabbit.webdav.property.DefaultDavProperty
24+
import org.apache.jackrabbit.webdav.xml.Namespace
1425
import org.junit.Test
1526

1627
class GetTagsRemoteOperationIT : AbstractIT() {
@@ -34,5 +45,51 @@ class GetTagsRemoteOperationIT : AbstractIT() {
3445
sut = GetTagsRemoteOperation().execute(client)
3546
assertTrue(sut.isSuccess)
3647
assertEquals(count + 1, sut.resultData.size)
48+
49+
// add color to one tag
50+
val plainColor = "ff00ff"
51+
val colorWithHex = "#$plainColor"
52+
val tag = sut.resultData.first()
53+
val newProps = DavPropertySet()
54+
newProps.add(
55+
DefaultDavProperty(
56+
"nc:color",
57+
plainColor,
58+
Namespace.getNamespace(WebdavEntry.NAMESPACE_NC)
59+
)
60+
)
61+
val propPatchMethod =
62+
PropPatchMethod(
63+
client.baseUri.toString() + "/remote.php/dav/systemtags/" + tag.id,
64+
newProps,
65+
DavPropertyNameSet()
66+
)
67+
assertTrue(client.executeMethod(propPatchMethod) == HttpStatus.SC_MULTI_STATUS)
68+
69+
sut = GetTagsRemoteOperation().execute(client)
70+
71+
assertEquals(colorWithHex, sut.resultData.find { it.id == tag.id }?.color)
72+
73+
// add colored tag to file
74+
val tagFolder = "/coloredFolder"
75+
assertTrue(CreateFolderRemoteOperation(tagFolder, true).execute(client).isSuccess)
76+
val folderMetadata = ReadFileRemoteOperation(tagFolder).execute(client)
77+
assertTrue(
78+
PutTagRemoteOperation(
79+
tag.id,
80+
(folderMetadata.data[0] as RemoteFile).localId
81+
).execute(nextcloudClient).isSuccess
82+
)
83+
84+
// read metadata
85+
val rootMetadata = ReadFolderRemoteOperation("/").execute(client)
86+
assertEquals(
87+
colorWithHex,
88+
(rootMetadata.data as ArrayList<RemoteFile>)
89+
.find { it.remotePath == tagFolder }
90+
?.tags
91+
?.first()
92+
?.color
93+
)
3794
}
3895
}

library/src/main/java/com/owncloud/android/lib/common/network/WebdavEntry.kt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -393,20 +393,22 @@ class WebdavEntry constructor(
393393

394394
prop = propSet[EXTENDED_PROPERTY_SYSTEM_TAGS, ncNamespace]
395395
if (prop?.value != null) {
396-
tags = when (prop.value) {
397-
is ArrayList<*> -> (prop.value as ArrayList<*>)
398-
.filterIsInstance<Element>()
399-
.map { parseTag(it) }
400-
.toTypedArray()
401-
402-
is Element -> {
403-
val element = (prop.value as Element)
404-
val tag = parseTag(element)
405-
arrayOf(tag)
406-
}
396+
tags =
397+
when (prop.value) {
398+
is ArrayList<*> ->
399+
(prop.value as ArrayList<*>)
400+
.filterIsInstance<Element>()
401+
.map { parseTag(it) }
402+
.toTypedArray()
403+
404+
is Element -> {
405+
val element = (prop.value as Element)
406+
val tag = parseTag(element)
407+
arrayOf(tag)
408+
}
407409

408-
else -> emptyArray()
409-
}
410+
else -> emptyArray()
411+
}
410412
}
411413

412414
// NC metadata size property <nc:file-metadata-size>

library/src/main/java/com/owncloud/android/lib/resources/systemTag/SystemTag.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77

88
package com.owncloud.android.lib.resources.systemTag
99

10-
data class SystemTag(val name: String, val color: String?)
10+
data class SystemTag(
11+
val name: String,
12+
val color: String?
13+
)

library/src/main/java/com/owncloud/android/lib/resources/tags/GetTagsRemoteOperation.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ package com.owncloud.android.lib.resources.tags
99

1010
import com.owncloud.android.lib.common.OwnCloudClient
1111
import com.owncloud.android.lib.common.network.WebdavEntry.Companion.EXTENDED_PROPERTY_NAME_REMOTE_ID
12+
import com.owncloud.android.lib.common.network.WebdavEntry.Companion.EXTENDED_PROPERTY_SYSTEM_TAGS_COLOR
13+
import com.owncloud.android.lib.common.network.WebdavEntry.Companion.NAMESPACE_NC
1214
import com.owncloud.android.lib.common.network.WebdavEntry.Companion.NAMESPACE_OC
1315
import com.owncloud.android.lib.common.network.WebdavEntry.Companion.SHAREES_DISPLAY_NAME
1416
import com.owncloud.android.lib.common.operations.RemoteOperation
@@ -18,15 +20,18 @@ import org.apache.jackrabbit.webdav.client.methods.PropFindMethod
1820
import org.apache.jackrabbit.webdav.property.DavPropertyNameSet
1921
import org.apache.jackrabbit.webdav.xml.Namespace
2022

23+
@Suppress("NestedBlockDepth")
2124
class GetTagsRemoteOperation : RemoteOperation<List<Tag>>() {
2225
@Deprecated("Deprecated in Java")
2326
override fun run(client: OwnCloudClient): RemoteOperationResult<List<Tag>> {
2427
val ocNamespace = Namespace.getNamespace(NAMESPACE_OC)
28+
val ncNamespace = Namespace.getNamespace(NAMESPACE_NC)
2529

2630
val propSet =
2731
DavPropertyNameSet().apply {
2832
add(EXTENDED_PROPERTY_NAME_REMOTE_ID, ocNamespace)
2933
add(SHAREES_DISPLAY_NAME, ocNamespace)
34+
add(EXTENDED_PROPERTY_SYSTEM_TAGS_COLOR, ncNamespace)
3035
}
3136

3237
val propFindMethod =
@@ -55,7 +60,17 @@ class GetTagsRemoteOperation : RemoteOperation<List<Tag>>() {
5560
.get(SHAREES_DISPLAY_NAME, ocNamespace)
5661
.value as String
5762

58-
result.add(Tag(id, name))
63+
var color =
64+
it
65+
.getProperties(HttpStatus.SC_OK)
66+
.get(EXTENDED_PROPERTY_SYSTEM_TAGS_COLOR, ncNamespace)
67+
?.value as String?
68+
69+
if (color != null) {
70+
color = "#$color"
71+
}
72+
73+
result.add(Tag(id, name, color))
5974
}
6075
}
6176

library/src/main/java/com/owncloud/android/lib/resources/tags/Tag.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ package com.owncloud.android.lib.resources.tags
99

1010
data class Tag(
1111
val id: String,
12-
val name: String
12+
val name: String,
13+
val color: String?
1314
)

0 commit comments

Comments
 (0)