Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ dependencies {

// Testing
testImplementation(libs.junit)
testImplementation(libs.robolectric)
testImplementation(libs.room.testing)
testImplementation(libs.androidx.arch.core.testing)
testImplementation(libs.androidx.test.core)
Expand Down
35 changes: 19 additions & 16 deletions app/src/main/java/at/techbee/jtx/contract/JtxContract.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,29 +97,32 @@ object JtxContract {
* This function takes a string and tries to parse it to a list of XProperty.
* This is the counterpart of getJsonStringFromXProperties(...)
* @param [string] that should be parsed
* @return The list of XProperty parsed from the string
* @return A PropertyList containing the XProperty instances parsed from the string
*/
fun getXPropertyListFromJson(string: String): PropertyList {
val propertyList = PropertyList()

if (string.isBlank())
return propertyList

try {
val jsonObject = JSONObject(string)
for (i in 0 until jsonObject.length()) {
val names = jsonObject.names() ?: break
val propertyName = names[i]?.toString() ?: break
val propertyValue = jsonObject.getString(propertyName).toString()
if (propertyName.isNotBlank() && propertyValue.isNotBlank()) {
val prop = XProperty(propertyName, propertyValue)
propertyList.add(prop)
if (string.isBlank()) {
return PropertyList()
}

return try {
val properties = buildList {
val jsonObject = JSONObject(string)
for (i in 0 until jsonObject.length()) {
val names = jsonObject.names() ?: break
val propertyName = names[i]?.toString() ?: break
val propertyValue = jsonObject.getString(propertyName).toString()
if (propertyName.isNotBlank() && propertyValue.isNotBlank()) {
val prop = XProperty(propertyName, propertyValue)
add(prop)
}
}
}

PropertyList(properties)
} catch (e: NullPointerException) {
logger.log(Level.WARNING, "Error parsing x-property-list $string", e)
PropertyList()
}
return propertyList
}


Expand Down
254 changes: 254 additions & 0 deletions app/src/test/java/at/techbee/jtx/contract/JtxContractTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
package at.techbee.jtx.contract

import net.fortuna.ical4j.model.Parameter
import net.fortuna.ical4j.model.ParameterList
import net.fortuna.ical4j.model.Property
import net.fortuna.ical4j.model.PropertyList
import net.fortuna.ical4j.model.parameter.XParameter
import net.fortuna.ical4j.model.property.XProperty
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNull
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
// Can be removed once Robolectric supports SDK 37
@Config(sdk = [36])
class JtxContractTest {

// region getXPropertyListFromJson() tests

@Test
fun `getXPropertyListFromJson with single property`() {
val input = """{"X-PROPERTY":"value"}"""

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(propertyListOf(XProperty("X-PROPERTY", "value")), result)
}

@Test
fun `getXPropertyListFromJson with multiple property`() {
val input = """{"X-PROPERTY-1":"value1","X-PROPERTY-2":"value2","X-PROPERTY-3":"value3"}"""

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(
propertyListOf(
XProperty("X-PROPERTY-1", "value1"),
XProperty("X-PROPERTY-2", "value2"),
XProperty("X-PROPERTY-3", "value3"),
),
result
)
}

@Test
fun `getXPropertyListFromJson with blank property name`() {
val input = """{" ":"value"}"""

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(PropertyList(), result)
}

@Test
fun `getXPropertyListFromJson with blank property value`() {
val input = """{"X-PROPERTY":" "}"""

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(PropertyList(), result)
}

@Test
fun `getXPropertyListFromJson without property`() {
val input = "{}"

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(PropertyList(), result)
}

@Test
fun `getXPropertyListFromJson with empty string`() {
val input = ""

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(PropertyList(), result)
}

@Test
fun `getXPropertyListFromJson with blank string`() {
val input = " "

val result = JtxContract.getXPropertyListFromJson(input)

assertEquals(PropertyList(), result)
}

// endregion

// region getJsonStringFromXProperties() tests

@Test
fun `getJsonStringFromXProperties with single property`() {
val input = propertyListOf(XProperty("X-PROPERTY", "value"))

val result = JtxContract.getJsonStringFromXProperties(input)

assertEquals("""{"X-PROPERTY":"value"}""", result)
}

@Test
fun `getJsonStringFromXProperties with multiple properties`() {
val input = propertyListOf(
XProperty("X-PROPERTY-1", "value1"),
XProperty("X-PROPERTY-2", "value2"),
XProperty("X-PROPERTY-3", "value3"),
)

val result = JtxContract.getJsonStringFromXProperties(input)

assertEquals(
"""{"X-PROPERTY-1":"value1","X-PROPERTY-2":"value2","X-PROPERTY-3":"value3"}""",
result
)
}

@Test
fun `getJsonStringFromXProperties with empty PropertyList`() {
val input = PropertyList()

val result = JtxContract.getJsonStringFromXProperties(input)

assertNull(result)
}

@Test
fun `getJsonStringFromXProperties with null argument`() {
val input = null

val result = JtxContract.getJsonStringFromXProperties(input)

assertNull(result)
}

// endregion

// region getXParametersFromJson() tests

@Test
fun `getXParametersFromJson with single parameter`() {
val input = """{"X-PARAMETER":"value"}"""

val result = JtxContract.getXParametersFromJson(input)

assertEquals(listOf(XParameter("X-PARAMETER", "value")), result)
}

@Test
fun `getXParametersFromJson with multiple parameters`() {
val input = """
{
"X-PARAMETER-1": "value1",
"X-PARAMETER-2": "value2",
"X-PARAMETER-3": "value3"
}
""".trimIndent()

val result = JtxContract.getXParametersFromJson(input)

assertEquals(
listOf(
XParameter("X-PARAMETER-1", "value1"),
XParameter("X-PARAMETER-2", "value2"),
XParameter("X-PARAMETER-3", "value3"),
),
result
)
}

@Test
fun `getXParametersFromJson with blank property name`() {
val input = """{" ":"value"}"""

val result = JtxContract.getXParametersFromJson(input)

assertEquals(emptyList<XParameter>(), result)
}

@Test
fun `getXParametersFromJson with blank property value`() {
val input = """{"X-PARAMETER":" "}"""

val result = JtxContract.getXParametersFromJson(input)

assertEquals(emptyList<XParameter>(), result)
}

@Test
fun `getXParametersFromJson without property`() {
val input = "{}"

val result = JtxContract.getXParametersFromJson(input)

assertEquals(emptyList<XParameter>(), result)
}

// endregion

// region getJsonStringFromXParameters() tests

@Test
fun `getJsonStringFromXParameters with single property`() {
val input = parameterListOf(XParameter("X-PARAMETER", "value"))

val result = JtxContract.getJsonStringFromXParameters(input)

assertEquals("""{"X-PARAMETER":"value"}""", result)
}

@Test
fun `getJsonStringFromXParameters with multiple properties`() {
val input = parameterListOf(
XParameter("X-PARAMETER-1", "value1"),
XParameter("X-PARAMETER-2", "value2"),
XParameter("X-PARAMETER-3", "value3"),
)

val result = JtxContract.getJsonStringFromXParameters(input)

assertEquals(
"""{"X-PARAMETER-1":"value1","X-PARAMETER-2":"value2","X-PARAMETER-3":"value3"}""",
result
)
}

@Test
fun `getJsonStringFromXParameters with empty ParameterList`() {
val input = ParameterList()

val result = JtxContract.getJsonStringFromXParameters(input)

assertNull(result)
}

@Test
fun `getJsonStringFromXParameters with null argument`() {
val input = null

val result = JtxContract.getJsonStringFromXParameters(input)

assertNull(result)
}

// endregion
}

private fun propertyListOf(vararg properties: Property) = PropertyList(properties.toList())

private fun parameterListOf(vararg parameters: Parameter) = ParameterList(parameters.toList())
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ profileinstaller = "1.4.1"
reorderable = "3.1.0"
synctools = "b5d43f5712"
uiTextGoogleFonts = "1.11.3"
robolectric = "4.16.1"
room = "2.8.4"
volley = "1.2.1"
ktor = "3.5.0"
Expand Down Expand Up @@ -107,6 +108,7 @@ osmdroid-android = { module = "org.osmdroid:osmdroid-android", version.ref = "os
play-services-location = { module = "com.google.android.gms:play-services-location", version.ref = "playServicesLocation" }
play-services-maps = { module = "com.google.android.gms:play-services-maps", version.ref = "playServicesMaps" }
reorderable = { module = "sh.calvin.reorderable:reorderable", version.ref = "reorderable" }
robolectric = { module = "org.robolectric:robolectric", version.ref = "robolectric" }
room-base = { module = "androidx.room:room-ktx", version.ref = "room" }
room-compiler = { module = "androidx.room:room-compiler", version.ref = "room" }
room-runtime = { module = "androidx.room:room-runtime", version.ref = "room" }
Expand Down