Skip to content

Commit

Permalink
Add DavPropertyProcessorTest
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 authored and tobiasKaminsky committed Mar 25, 2024
1 parent cae5794 commit 02077ca
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.nextcloud.extensions

import com.google.gson.Gson
import org.apache.jackrabbit.webdav.property.DavProperty
import org.apache.jackrabbit.webdav.property.DavPropertyName
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.w3c.dom.Document
import org.w3c.dom.Element

class DavPropertyProcessorTest {
data class TestData(val name: String, val age: Int)

private val gson = Gson()

@Test
fun testFromDavPropertyWhenGivenValidDataShouldReturnExpectedData() {
val result =
gson.fromDavProperty<TestData>(
object : DavProperty<String> {
override fun toXml(document: Document?): Element {
return createElement("TestData", value)
}

override fun getName(): DavPropertyName {
return DavPropertyName.DISPLAYNAME
}

override fun getValue(): String {
return "{\"name\":\"John\",\"age\":55}"
}

override fun isInvisibleInAllprop(): Boolean {
return true
}
}
)
val expected = TestData("John", 55)
assertEquals(expected, result)
}

@Test
fun testFromDavPropertyWhenGivenValidDataAndExpectDifferentTypeShouldReturnNull() {
val result =
gson.fromDavProperty<ArrayList<String>>(
object : DavProperty<String> {
override fun toXml(document: Document?): Element {
return createElement("TestData", value)
}

override fun getName(): DavPropertyName {
return DavPropertyName.DISPLAYNAME
}

override fun getValue(): String {
return "{\"name\":\"John\",\"age\":55}"
}

override fun isInvisibleInAllprop(): Boolean {
return true
}
}
)
assertNull(result)
}

@Test
fun testFromDavPropertyWhenGivenInvalidDataShouldReturnNull() {
val result =
gson.fromDavProperty<TestData>(
object : DavProperty<String?> {
override fun toXml(document: Document?): Element {
return createElement("TestData", "")
}

override fun getName(): DavPropertyName {
return DavPropertyName.DISPLAYNAME
}

override fun getValue(): String? {
return null
}

override fun isInvisibleInAllprop(): Boolean {
return true
}
}
)
assertNull(result)
}

@Test
fun testFromDavPropertyWhenGivenNullDataShouldReturnNull() {
val result = gson.fromDavProperty<TestData>(null)
assertNull(result)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.nextcloud.extensions

import org.w3c.dom.Element
import javax.xml.parsers.DocumentBuilderFactory

fun createElement(
xml: String,
value: String
): Element {
val builder =
DocumentBuilderFactory.newInstance().run {
newDocumentBuilder()
}
val document = builder.newDocument()
val element =
document.createElement(xml).apply {
textContent = value
}
document.appendChild(element)
return element
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.w3c.dom.Element
import javax.xml.parsers.DocumentBuilderFactory

class XmlDataProcessorTest {

@Test
fun testProcessXmlDataWhenGivenEmptyArrayListShouldReturnNull() {
val tag = "width"
Expand Down Expand Up @@ -66,16 +64,4 @@ class XmlDataProcessorTest {
val result = xmlData.processXmlData<Float>(tag)
assertNull(result)
}

private fun createElement(xml: String, value: String): Element {
val builder = DocumentBuilderFactory.newInstance().run {
newDocumentBuilder()
}
val document = builder.newDocument()
val element = document.createElement(xml).apply {
textContent = value
}
document.appendChild(element)
return element
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package com.nextcloud.extensions

import com.google.gson.Gson
import com.google.gson.JsonSyntaxException
import org.apache.jackrabbit.webdav.property.DavProperty

inline fun <reified T> Gson.fromDavProperty(davProperty: DavProperty<*>?): T? {
return if (davProperty != null && davProperty.value != null) {
fromJson(davProperty.value.toString(), T::class.java)
try {
fromJson(davProperty.value.toString(), T::class.java)
} catch (e: JsonSyntaxException) {
null
}
} else {
null
}
Expand Down

0 comments on commit 02077ca

Please sign in to comment.