Skip to content

Commit

Permalink
test: Add ConnectionInfo (De-)Serialization Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wba2hi committed Oct 12, 2023
1 parent b64721e commit 9eb7d15
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ android {
}
}

tasks.withType<Test>().configureEach {
useJUnitPlatform()

kotlin {
compilerOptions {
// https://youtrack.jetbrains.com/issue/KT-48678/Coroutine-debugger-disable-was-optimised-out-compiler-feature
// We don't want local variables to be optimized out while debugging into tests
freeCompilerArgs.add("-Xdebug")
}
}
}

plugins {
id("com.android.application")
kotlin("plugin.serialization") version "1.9.0"
Expand Down Expand Up @@ -104,4 +116,3 @@ dependencies {
implementation(libs.androidx.activity.compose)
implementation(libs.androidx.constraintlayout.compose)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*
*/

package org.eclipse.kuksa.testapp.model

import io.kotest.core.spec.style.BehaviorSpec
import io.kotest.matchers.shouldBe
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.io.FileWriter

class ConnectionInfoTest : BehaviorSpec({
given("A custom ConnectionInfo with a custom Certificate") {
// Uri is an android specific class, which does not exist in plain JUnit. Methods like Uri.parse will simply
// return null due to the addition of testOptions.unitTests.isReturnDefaultValues true
val certificate = Certificate(
uriPath = "content://com.android.providers.downloads.documents/document/msf%3A1000000116",
overrideAuthority = "server",
)
val classUnderTest = ConnectionInfo(
host = "someHost",
port = 12345,
certificate = certificate,
isTlsEnabled = true,
)
and("it has been serialized to file") {
val tempFile = withContext(Dispatchers.IO) {
File.createTempFile("connection_info", "tmp")
}

tempFile.outputStream().use {
ConnectionInfoSerializer.writeTo(classUnderTest, it)
}

`when`("Trying to de-serialize it") {
tempFile.inputStream().use {
val deserializedConnectionInfo = ConnectionInfoSerializer.readFrom(it)

then("It should be deserialized correctly") {
deserializedConnectionInfo.host shouldBe classUnderTest.host
deserializedConnectionInfo.port shouldBe classUnderTest.port
deserializedConnectionInfo.isTlsEnabled shouldBe classUnderTest.isTlsEnabled
deserializedConnectionInfo.certificate shouldBe classUnderTest.certificate
}
}
}
}
}

given("An invalid serialized ConnectionInfo") {
val tempFile = withContext(Dispatchers.IO) {
File.createTempFile("connection_info", "tmp")
}

withContext(Dispatchers.IO) {
FileWriter(tempFile).use { writer ->
writer.write("{ invalid }")
}
}

`when`("Trying to de-serialize it") {
var isExceptionThrown = false
var deserializedConnectionInfo: ConnectionInfo? = null
tempFile.inputStream().use {
try {
deserializedConnectionInfo = ConnectionInfoSerializer.readFrom(it)
} catch (_: Exception) {
isExceptionThrown = true
}
}

then("No Exception is thrown") {
isExceptionThrown shouldBe false
}

then("We are falling back to the defaultValue") {
deserializedConnectionInfo shouldBe ConnectionInfoSerializer.defaultValue
}
}
}
})

0 comments on commit 9eb7d15

Please sign in to comment.