Skip to content

Commit

Permalink
Refactor deployments tests
Browse files Browse the repository at this point in the history
Leveraged the AbstractDeploymentTest for the TimeForced tests to
 enhance the clarity and readability of the tests.

Signed-off-by: Saeed Rezaee <[email protected]>
  • Loading branch information
SaeedRe committed Feb 20, 2024
1 parent 4e91a77 commit 6b74ead
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
*/
package org.eclipse.hara.ddiclient.integrationtest

import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import okhttp3.OkHttpClient
import org.eclipse.hara.ddiclient.api.ConfigDataProvider
import org.eclipse.hara.ddiclient.api.DeploymentPermitProvider
Expand All @@ -27,10 +29,12 @@ 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.Action
import org.eclipse.hara.ddiclient.integrationtest.api.management.ActionStatus
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.eclipse.hara.ddiclient.integrationtest.utils.internalLog
Expand Down Expand Up @@ -85,6 +89,10 @@ abstract class AbstractDeploymentTest {
open fun afterTest() {
}

protected fun setPollingTime(time: String) = runBlocking {
managementApi.setPollingTime(TestUtils.basic, ServerSystemConfig(time))
}

protected fun defaultClientFromTargetId(
directoryDataProvider: DirectoryForArtifactsProvider = TestUtils.directoryDataProvider,
configDataProvider: ConfigDataProvider = TestUtils.configDataProvider,
Expand Down Expand Up @@ -119,6 +127,18 @@ abstract class AbstractDeploymentTest {
client
}

protected fun createHaraClientWithAuthorizationPermissions(
downloadAllowed: Boolean = true,
updateAllowed: Boolean = true): HaraClient {

val deploymentBehavior = object : DeploymentPermitProvider {
override fun downloadAllowed() = CompletableDeferred(downloadAllowed)
override fun updateAllowed() = CompletableDeferred(updateAllowed)
}
return defaultClientFromTargetId(
deploymentPermitProvider = deploymentBehavior).invoke(targetId)
}

protected suspend fun reCreateTestTargetOnServer() {
runCatching {
managementApi.deleteTarget(TestUtils.basic, targetId)
Expand Down Expand Up @@ -178,6 +198,10 @@ abstract class AbstractDeploymentTest {
}
}

protected val expectedActionOnStart = ActionStatus(setOf(
TestUtils.firstActionWithAssignmentEntry
))

private suspend fun assertEquals(actual: Any?, expected: Any?) {
throwableJob = throwableScope.async {
Assert.assertEquals(actual, expected)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@
*/
package org.eclipse.hara.ddiclient.integrationtest

import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.runBlocking
import org.eclipse.hara.ddiclient.api.DeploymentPermitProvider
import org.eclipse.hara.ddiclient.api.HaraClient
import org.eclipse.hara.ddiclient.integrationtest.api.management.ActionStatus
import org.eclipse.hara.ddiclient.integrationtest.api.management.AssignDistributionType
import org.eclipse.hara.ddiclient.integrationtest.api.management.HawkbitAssignDistributionBody
import org.eclipse.hara.ddiclient.integrationtest.api.management.ServerSystemConfig
import org.eclipse.hara.ddiclient.integrationtest.utils.TestUtils
import org.eclipse.hara.ddiclient.integrationtest.utils.TestUtils.endMessagesOnSuccessUpdate
import org.eclipse.hara.ddiclient.integrationtest.utils.TestUtils.messagesOnSoftUpdateAuthorization
Expand All @@ -36,11 +32,7 @@ class HawkbitDownloadOnlyDeploymentTest : AbstractDeploymentTest() {
@BeforeTest
override fun beforeTest() {
super.beforeTest()
setPollingTime()
}

private fun setPollingTime() = runBlocking {
managementApi.setPollingTime(TestUtils.basic, ServerSystemConfig("00:00:05"))
setPollingTime("00:00:05")
}

@Test(enabled = true, timeOut = 60_000)
Expand All @@ -50,10 +42,11 @@ class HawkbitDownloadOnlyDeploymentTest : AbstractDeploymentTest() {

assignDownloadOnlyDistribution()

val client = createClient()
val client = createHaraClientWithAuthorizationPermissions(
downloadAllowed = false, updateAllowed = true)

startTheTestAndWaitForResult(client,
createTargetTestDeployment(getActionsOnFinishForForceUpdateScenario))
createTargetTestDeployment(expectedActionsAfterDownloadOnlyDeployment))
}

private suspend fun assignDownloadOnlyDistribution() {
Expand All @@ -62,17 +55,6 @@ class HawkbitDownloadOnlyDeploymentTest : AbstractDeploymentTest() {
assignDistributionToTheTarget(distribution)
}

private fun createClient(): HaraClient {

val deploymentBehavior = object : DeploymentPermitProvider {
override fun downloadAllowed() = CompletableDeferred(false)
override fun updateAllowed() = CompletableDeferred(true)
}
return defaultClientFromTargetId(
deploymentPermitProvider = deploymentBehavior)
.invoke(targetId)
}

private fun createTargetTestDeployment(
actionsOnFinish: ActionStatus): TestUtils.TargetDeployments {
val filesDownloadedPairedToServerFile = setOf(
Expand All @@ -85,18 +67,15 @@ class HawkbitDownloadOnlyDeploymentTest : AbstractDeploymentTest() {
deploymentInfo = listOf(
TestUtils.TargetDeployments.DeploymentInfo(
actionId = actionId,
actionStatusOnStart = ActionStatus(
setOf(
TestUtils.firstActionWithAssignmentEntry
)),
actionStatusOnStart = expectedActionOnStart,
actionStatusOnFinish = actionsOnFinish,
filesDownloadedPairedWithServerFile = filesDownloadedPairedToServerFile
)
)
)
}

private val getActionsOnFinishForForceUpdateScenario : ActionStatus =
private val expectedActionsAfterDownloadOnlyDeployment : ActionStatus =
ActionStatus(setOf(
*endMessagesOnSuccessUpdate,
*messagesOnSoftUpdateAuthorization,
Expand Down
Loading

0 comments on commit 6b74ead

Please sign in to comment.