forked from eclipse-hara/hara-ddiclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit introduces the AbstractTest class for general tests. It also adds the AbstractHaraMessageTest to be used for tests dependent on the Hara client messages. Signed-off-by: Saeed Rezaee <[email protected]>
- Loading branch information
Showing
7 changed files
with
347 additions
and
242 deletions.
There are no files selected for viewing
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
131 changes: 131 additions & 0 deletions
131
src/test/kotlin/org/eclipse/hara/ddiclient/integrationtest/AbstractHaraMessageTest.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,131 @@ | ||
/* | ||
* | ||
* * Copyright © 2017-2024 Kynetics LLC | ||
* * | ||
* * This program and the accompanying materials are made | ||
* * available under the terms of the Eclipse Public License 2.0 | ||
* * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* * | ||
* * SPDX-License-Identifier: EPL-2.0 | ||
* | ||
*/ | ||
|
||
package org.eclipse.hara.ddiclient.integrationtest | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.channels.BufferOverflow | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.launch | ||
import okhttp3.OkHttpClient | ||
import org.eclipse.hara.ddiclient.api.ConfigDataProvider | ||
import org.eclipse.hara.ddiclient.api.DeploymentPermitProvider | ||
import org.eclipse.hara.ddiclient.api.DirectoryForArtifactsProvider | ||
import org.eclipse.hara.ddiclient.api.DownloadBehavior | ||
import org.eclipse.hara.ddiclient.api.HaraClient | ||
import org.eclipse.hara.ddiclient.api.MessageListener | ||
import org.eclipse.hara.ddiclient.api.Updater | ||
import org.eclipse.hara.ddiclient.integrationtest.utils.internalLog | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
abstract class AbstractHaraMessageTest : AbstractTest() { | ||
|
||
protected var expectedHaraMessages = mutableListOf<ExpectedMessage>() | ||
open val expectedMessagesAssertionListener: | ||
List<suspend (ExpectedMessage) -> Unit> = listOf() | ||
|
||
private val expectedMessageChannel = | ||
Channel<ExpectedMessage>(5, BufferOverflow.DROP_OLDEST) | ||
|
||
private val checkMessagesScope = CoroutineScope(Dispatchers.IO) | ||
private var checkExpectedMessagesJob: Deferred<Unit>? = null | ||
|
||
open val expectedMessagesList: MutableList<MutableList<ExpectedMessage>> = | ||
mutableListOf(expectedHaraMessages) | ||
|
||
open fun filterHaraMessages(message: MessageListener.Message): Boolean = true | ||
|
||
protected fun sendExpectedMessage(message: ExpectedMessage) { | ||
checkMessagesScope.launch { | ||
expectedMessageChannel.send(message) | ||
} | ||
} | ||
|
||
protected suspend fun startWatchingExpectedMessages(lastTest: Boolean = false) { | ||
checkExpectedMessagesJob = getExpectedMessagesCheckingJob(lastTest) | ||
try { | ||
checkExpectedMessagesJob?.await() | ||
} catch (ignored: CancellationException) { | ||
} | ||
} | ||
|
||
open val messageListener: MessageListener | ||
get() = object : MessageListener { | ||
override fun onMessage(message: MessageListener.Message) { | ||
"Received message: $message".internalLog() | ||
if (filterHaraMessages(message)) { | ||
sendExpectedMessage(ExpectedMessage.HaraMessage(message)) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun getExpectedMessagesCheckingJob(lastTest: Boolean): Deferred<Unit> { | ||
return checkMessagesScope.async { | ||
for (msg in expectedMessageChannel) { | ||
when (msg) { | ||
is ExpectedMessage.HaraMessage -> { | ||
if (expectedHaraMessages.isNotEmpty()) { | ||
assertEquals(msg, expectedHaraMessages.removeFirst()) | ||
} | ||
} | ||
|
||
else -> { | ||
expectedMessagesAssertionListener.forEach { | ||
it.invoke(msg) | ||
} | ||
} | ||
} | ||
val finished = expectedMessagesList.map { | ||
it.isEmpty() | ||
} | ||
if (finished.all { it }) { | ||
"All expected messages received".internalLog() | ||
checkExpectedMessagesJob?.cancel() | ||
if (lastTest) { | ||
safeStopClient() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun clientFromTargetId( | ||
directoryDataProvider: DirectoryForArtifactsProvider, | ||
configDataProvider: ConfigDataProvider, updater: Updater, | ||
messageListeners: List<MessageListener>, | ||
deploymentPermitProvider: DeploymentPermitProvider, | ||
downloadBehavior: DownloadBehavior, | ||
okHttpClientBuilder: OkHttpClient.Builder, | ||
targetToken: String?, | ||
gatewayToken: String?): (String) -> HaraClient { | ||
return super.clientFromTargetId( | ||
directoryDataProvider, configDataProvider, updater, | ||
listOf(messageListener), | ||
deploymentPermitProvider, | ||
downloadBehavior, okHttpClientBuilder, | ||
targetToken, gatewayToken) | ||
} | ||
|
||
protected fun expectMessages(vararg messages: ExpectedMessage) { | ||
expectedHaraMessages.clear() | ||
expectedHaraMessages.addAll(messages) | ||
} | ||
|
||
|
||
sealed class ExpectedMessage { | ||
|
||
data class HaraMessage(val message: MessageListener.Message) : ExpectedMessage() | ||
} | ||
} |
144 changes: 144 additions & 0 deletions
144
src/test/kotlin/org/eclipse/hara/ddiclient/integrationtest/AbstractTest.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,144 @@ | ||
/* | ||
* | ||
* * Copyright © 2017-2024 Kynetics LLC | ||
* * | ||
* * This program and the accompanying materials are made | ||
* * available under the terms of the Eclipse Public License 2.0 | ||
* * which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* * | ||
* * SPDX-License-Identifier: EPL-2.0 | ||
* | ||
*/ | ||
|
||
package org.eclipse.hara.ddiclient.integrationtest | ||
|
||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Deferred | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.runBlocking | ||
import okhttp3.OkHttpClient | ||
import org.eclipse.hara.ddiclient.api.ConfigDataProvider | ||
import org.eclipse.hara.ddiclient.api.DeploymentPermitProvider | ||
import org.eclipse.hara.ddiclient.api.DirectoryForArtifactsProvider | ||
import org.eclipse.hara.ddiclient.api.DownloadBehavior | ||
import org.eclipse.hara.ddiclient.api.HaraClient | ||
import org.eclipse.hara.ddiclient.api.HaraClientData | ||
import org.eclipse.hara.ddiclient.api.HaraClientDefaultImpl | ||
import org.eclipse.hara.ddiclient.api.MessageListener | ||
import org.eclipse.hara.ddiclient.api.Updater | ||
import org.eclipse.hara.ddiclient.integrationtest.api.management.HawkbitAssignDistributionBody | ||
import org.eclipse.hara.ddiclient.integrationtest.api.management.HawkbitTargetInfo | ||
import org.eclipse.hara.ddiclient.integrationtest.api.management.ManagementApi | ||
import org.eclipse.hara.ddiclient.integrationtest.api.management.ManagementClient | ||
import org.eclipse.hara.ddiclient.integrationtest.api.management.ServerSystemConfig | ||
import org.eclipse.hara.ddiclient.integrationtest.utils.TestUtils | ||
import org.eclipse.hara.ddiclient.integrationtest.utils.addOkhttpLogger | ||
import org.testng.Assert | ||
import org.testng.annotations.AfterTest | ||
import org.testng.annotations.BeforeTest | ||
import java.lang.Exception | ||
import kotlin.coroutines.cancellation.CancellationException | ||
|
||
abstract class AbstractTest { | ||
|
||
protected lateinit var managementApi: ManagementApi | ||
abstract val targetId: String | ||
|
||
private val throwableScope = CoroutineScope(Dispatchers.Default) | ||
|
||
private var throwableJob: Deferred<Unit>? = null | ||
|
||
protected var client: HaraClient? = null | ||
set(value) { | ||
safeStopClient() | ||
field = value | ||
} | ||
|
||
|
||
@BeforeTest | ||
open fun beforeTest() { | ||
managementApi = ManagementClient.newInstance(TestUtils.hawkbitUrl) | ||
} | ||
|
||
@AfterTest | ||
open fun afterTest() { | ||
} | ||
|
||
|
||
protected fun setPollingTime(time: String) = runBlocking { | ||
managementApi.setPollingTime(TestUtils.basic, ServerSystemConfig(time)) | ||
} | ||
|
||
protected suspend fun reCreateTestTargetOnServer() { | ||
runCatching { | ||
managementApi.deleteTarget(TestUtils.basic, targetId) | ||
} | ||
runCatching { | ||
managementApi.createTarget( | ||
TestUtils.basic, listOf(HawkbitTargetInfo(targetId))) | ||
} | ||
} | ||
|
||
protected suspend fun assignDistributionToTheTarget( | ||
distribution: HawkbitAssignDistributionBody): Int { | ||
val response = | ||
managementApi.assignDistributionToTarget(TestUtils.basic, | ||
targetId, distribution) | ||
if (response.assignedActions.isNotEmpty()) { | ||
return response.assignedActions.first().id | ||
} | ||
return -1 | ||
} | ||
|
||
protected fun safeStopClient() { | ||
try { | ||
client?.stop() | ||
} catch (ignored: Exception) { | ||
} | ||
} | ||
|
||
protected suspend fun assertEquals(actual: Any?, expected: Any?) { | ||
throwableJob = throwableScope.async { | ||
Assert.assertEquals(actual, expected) | ||
} | ||
try { | ||
throwableJob?.await() | ||
} catch (ignored: CancellationException) { | ||
} | ||
} | ||
|
||
open fun clientFromTargetId( | ||
directoryDataProvider: DirectoryForArtifactsProvider = TestUtils.directoryDataProvider, | ||
configDataProvider: ConfigDataProvider = TestUtils.configDataProvider, | ||
updater: Updater = TestUtils.updater, | ||
messageListeners: List<MessageListener> = listOf(), | ||
deploymentPermitProvider: DeploymentPermitProvider = object : | ||
DeploymentPermitProvider {}, | ||
downloadBehavior: DownloadBehavior = TestUtils.downloadBehavior, | ||
okHttpClientBuilder: OkHttpClient.Builder = OkHttpClient.Builder().addOkhttpLogger(), | ||
targetToken: String? = "", | ||
gatewayToken: String? = TestUtils.gatewayToken): (String) -> HaraClient = | ||
{ targetId -> | ||
val clientData = HaraClientData( | ||
TestUtils.tenantName, | ||
targetId, | ||
TestUtils.hawkbitUrl, | ||
gatewayToken, targetToken) | ||
|
||
val client = HaraClientDefaultImpl() | ||
|
||
client.init( | ||
clientData, | ||
directoryDataProvider, | ||
configDataProvider, | ||
deploymentPermitProvider, | ||
listOf(*messageListeners.toTypedArray()), | ||
listOf(updater), | ||
downloadBehavior, | ||
httpBuilder = okHttpClientBuilder | ||
) | ||
|
||
client | ||
} | ||
} |
Oops, something went wrong.