-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: alperozturk <[email protected]>
- Loading branch information
1 parent
cae5794
commit 02077ca
Showing
4 changed files
with
125 additions
and
15 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
library/src/androidTest/java/com/nextcloud/extensions/DavPropertyProcessorTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
library/src/androidTest/java/com/nextcloud/extensions/ElementCreator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 6 additions & 1 deletion
7
library/src/main/java/com/nextcloud/extensions/GsonExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters