Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

feat: Add component to kotlin feature #22

Merged
merged 2 commits into from
Sep 15, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ repositories {
dependencies {

implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.9.5")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.5")
implementation("org.jetbrains.kotlin:kotlin-reflect:1.4.0")
implementation("org.jetbrains.kotlin:kotlin-script-runtime:1.4.0")
implementation("org.apache.commons:commons-lang3:3.12.0")
implementation("ch.qos.logback:logback-classic:1.2.3")

testImplementation(kotlin("test-junit"))
testImplementation("com.gregwoodfill.assert:kotlin-json-assert:0.1.0")
testImplementation("org.springframework:spring-expression:5.1.5.RELEASE")
testImplementation(kotlin("scripting-jsr223"))
testImplementation("org.mockito.kotlin:mockito-kotlin:3.2.0")
}

tasks.withType<KotlinCompile> {
Expand Down
1 change: 0 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
kotlin.code.style=official
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package com.chutneytesting.kotlin.dsl

fun json(variable: String, path: String = JSON_PATH_ROOT): String {
require(variable.isNotBlank()) { "variable cannot be empty" }
return "json(${variable.spELVar}, ${path.elString()})".spEL();
return "json(${variable.spELVar}, ${path.elString()})".spEL()
}

private fun chutneyFunction(
name: String,
elEval: Boolean = true,
vararg expressionParameters: String
): String {
val expr = "$name".spELVar
val expr = name.spELVar
.plus(expressionParameters.joinToString(prefix = "(", postfix = ")"))

if (elEval) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ open class Strategy(val type: String, val parameters: Map<String, String> = empt
open class RetryTimeOutStrategy(timeout: String, retryDelay: String) :
Strategy(type = "retry-with-timeout", parameters = mapOf("timeOut" to timeout, "retryDelay" to retryDelay))

open class SoftAssertStrategy() :
open class SoftAssertStrategy :
Strategy(type = "soft-assert")

@ChutneyScenarioDsl
Expand Down Expand Up @@ -128,7 +128,7 @@ object Mapper {
}
}

val mapper = ObjectMapper()
val mapper: ObjectMapper = ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.registerModule(KotlinModule())
.setDefaultPrettyPrinter(pp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ fun ChutneyStepBuilder.HttpSoapTask(
"body" to body,
"username" to username,
"password" to password,
("timeout" to timeout).takeIf { timeout.isNullOrBlank().not() }
("timeout" to timeout).takeIf { timeout.isBlank().not() }
).toMap(),
outputs = outputs
)
Expand Down Expand Up @@ -497,7 +497,7 @@ fun ChutneyStepBuilder.JmsCleanQueuesTask(
target: String,
queueName: String
) {
JmsCleanQueueTask(target, queueName);
JmsCleanQueueTask(target, queueName)
}

fun ChutneyStepBuilder.JmsCleanQueueTask(
Expand Down Expand Up @@ -581,12 +581,16 @@ fun ChutneyStepBuilder.JmsBrokerStartTask(
fun ChutneyStepBuilder.SqlTask(
target: String,
statements: List<String>,
outputs: Map<String, Any> = mapOf()
outputs: Map<String, Any> = mapOf(),
nbLoggedRow: Integer
) {
implementation = ChutneyStepImpl(
type = "sql",
target = target,
inputs = mapOf("statements" to statements),
inputs = mapOf(
"statements" to statements,
"nbLoggedRow" to nbLoggedRow
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't a required input.
Maybe something like this : ("nbLoggedRow" to nbLoggedRow).takeIf { nbLoggedRow.isNotBlank() }

),
outputs = outputs
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.chutneytesting.kotlin.synchronize

import com.chutneytesting.kotlin.util.ChutneyServerInfo
import com.chutneytesting.kotlin.dsl.ChutneyScenario
import com.chutneytesting.kotlin.transformation.ChutneyServerServiceImpl
import java.io.File

/**
* Synchronise scenario locally and/or remotely and returns elapsed time in milliseconds.
*/
fun ChutneyScenario.synchronise(serverInfo: ChutneyServerInfo? = null, updateRemote: Boolean = false, path: String = "src/main/resources/chutney/") {
val json = this.toString()
val fileName = (this.id?.let { this.id.toString() + "-" } ?: "") + title + ".chutney.json"
File(path).walkTopDown().filter { it.isFile }.firstOrNull {
val chutneyScenarioIdFromFileName = getChutneyScenarioIdFromFileName(it.name)
it.name.equals(fileName, ignoreCase = true) || (this.id != null && chutneyScenarioIdFromFileName != null && this.id == chutneyScenarioIdFromFileName)
}?.apply {
this.writeText(json)
}?.also {
println("| AT json synchronized:: ${it.absolutePath}")
} ?: File("src/main/resources/chutney/in_progress/$fileName").apply { writeText(json) }
.also { println("| AT json created:: ${it.absolutePath}") }

if (updateRemote && this.id != null && serverInfo != null) {
updateJsonRemoteScenario(serverInfo, this.id, json)
}
}

/**
* Cosmetic to create a list of scenarios
*/
class SynchronizeScenariosBuilder {
var scenarios: List<ChutneyScenario> = mutableListOf()

operator fun ChutneyScenario.unaryPlus() {
scenarios = scenarios + this
}

operator fun List<ChutneyScenario>.unaryPlus() {
scenarios = scenarios + this
}

operator fun ChutneyScenario.unaryMinus() {
// scenarios = scenarios - this
// cosmetic to ignore scenario
}
}

private fun updateJsonRemoteScenario(serverInfo: ChutneyServerInfo, id: Int, content: String) {
try {
ChutneyServerServiceImpl.updateJsonScenario(serverInfo, content, id.toString())
println("| remote AT json synchronized:: ${serverInfo.remoteServerUrl}/#/scenario/$id/execution/last")
} catch (e: Exception) {
println("| remote AT json cannot be synchronized:: $id")
}
}

private fun getChutneyScenarioIdFromFileName(fileName: String): Int? {
val dashIndex = fileName.indexOf("-")
return try {
if (dashIndex > 0) Integer.valueOf(fileName.substring(0, dashIndex)) else null
} catch (e: Exception) {
null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.chutneytesting.kotlin.transformation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe put this in synchornize package ?


import com.chutneytesting.kotlin.transformation.from_component_to_kotlin.ComposableStepDto
import com.chutneytesting.kotlin.transformation.from_component_to_kotlin.ComposableTestCaseDto
import com.chutneytesting.kotlin.util.ChutneyServerInfo
import com.chutneytesting.kotlin.util.HttpClient
import org.apache.commons.lang3.StringUtils


interface ChutneyServerService {
fun getAllComponent(serverInfo: ChutneyServerInfo): List<ComposableStepDto>
fun getAllScenarios(serverInfo: ChutneyServerInfo): List<LinkedHashMap<String, Any>>
fun getComposedScenario(serverInfo: ChutneyServerInfo, scenarioId: String): ComposableTestCaseDto
fun updateJsonScenario(serverInfo: ChutneyServerInfo, scenarioContent: String, scenarioId: String)
}

object ChutneyServerServiceImpl : ChutneyServerService {

override fun getAllComponent(serverInfo: ChutneyServerInfo): List<ComposableStepDto> {
return HttpClient.get(serverInfo, "/api/steps/v1/all")
}

override fun getAllScenarios(serverInfo: ChutneyServerInfo): List<LinkedHashMap<String, Any>> {
return HttpClient.get(serverInfo, "/api/scenario/v2")
}

override fun getComposedScenario(serverInfo: ChutneyServerInfo, scenarioId: String): ComposableTestCaseDto {
return HttpClient.get(serverInfo, "/api/scenario/component-edition/$scenarioId")
}

override fun updateJsonScenario(serverInfo: ChutneyServerInfo, scenarioContent: String, scenarioId: String) {
val generatedTag = "GENERATED"
val escapeSql = escapeSql(scenarioContent)
val body = "update scenario set content='$escapeSql', version=version+1, tags=CASE WHEN tags like '%$generatedTag%' THEN tags ELSE CONCAT_WS(',',tags,'$generatedTag') end where id = '$scenarioId'"

HttpClient.post<Any>(serverInfo, "/api/v1/admin/database/execute/jdbc", body)
}

private fun escapeSql(str: String?): String? = if (str == null) null else StringUtils.replace(str, "'", "''")

}

Loading