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

Commit

Permalink
feat(): execute on default env when no provided one
Browse files Browse the repository at this point in the history
  • Loading branch information
KarimGl committed Jan 15, 2024
1 parent 4b69070 commit d1d1fd5
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.chutneytesting.kotlin.dsl.ChutneyTarget
const val CHUTNEY_ROOT_PATH_DEFAULT = ".chutney"
const val CHUTNEY_ENV_ROOT_PATH_DEFAULT = "$CHUTNEY_ROOT_PATH_DEFAULT/environments"

class CannotResolveDefaultEnvironmentException : Exception("No environment name was given and there is more than one environment. Defaulting is impossible. Please, specify a name or declare only one environment.")

class ExecutionService(
environmentJsonRootPath: String = CHUTNEY_ENV_ROOT_PATH_DEFAULT
Expand All @@ -22,9 +21,6 @@ class ExecutionService(
private val executionConfiguration = ExecutionConfiguration()
private val embeddedEnvironmentApi = EnvironmentConfiguration(environmentJsonRootPath).embeddedEnvironmentApi

companion object {
val EMPTY = ChutneyEnvironment("EMPTY")
}

fun execute(
scenario: ChutneyScenario,
Expand Down Expand Up @@ -59,23 +55,8 @@ class ExecutionService(
}

fun getEnvironment(environmentName: String? = null): ChutneyEnvironment {
val environmentDto: EnvironmentDto
val environments = embeddedEnvironmentApi.listEnvironments()

environmentDto = if (environmentName.isNullOrBlank()) {
if (environments.isNotEmpty()) {
if (environments.size > 1) {
throw CannotResolveDefaultEnvironmentException()
} else {
environments.first()
}
} else {
return EMPTY
}
} else {
embeddedEnvironmentApi.getEnvironment(environmentName)
}

val executionEnv = environmentName.takeUnless { it.isNullOrBlank() } ?: embeddedEnvironmentApi.defaultEnvironmentName()
val environmentDto = embeddedEnvironmentApi.getEnvironment(executionEnv)
return mapEnvironmentNameToChutneyEnvironment(environmentDto)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package com.chutneytesting.kotlin.junit.engine.execution
import com.chutneytesting.engine.domain.execution.engine.step.Step
import com.chutneytesting.engine.domain.execution.report.Status
import com.chutneytesting.environment.domain.exception.EnvironmentNotFoundException
import com.chutneytesting.environment.domain.exception.NoEnvironmentFoundException
import com.chutneytesting.environment.domain.exception.UnresolvedEnvironmentException
import com.chutneytesting.kotlin.ChutneyConfigurationParameters
import com.chutneytesting.kotlin.dsl.ChutneyStep
import com.chutneytesting.kotlin.dsl.ChutneyStepImpl
import com.chutneytesting.kotlin.dsl.Strategy
import com.chutneytesting.kotlin.execution.CannotResolveDefaultEnvironmentException
import com.chutneytesting.kotlin.execution.report.JsonReportWriter
import com.chutneytesting.kotlin.junit.engine.ChutneyScenarioDescriptor
import com.chutneytesting.kotlin.junit.engine.ChutneyStepDescriptor
Expand Down Expand Up @@ -173,7 +174,8 @@ class ChutneyScenarioExecutionContext(

private fun convertExecuteException(t: Throwable, scenarioDescriptor: ChutneyScenarioDescriptor): Throwable {
return when (t) {
is CannotResolveDefaultEnvironmentException -> UnresolvedScenarioEnvironmentException(t.message)
is UnresolvedEnvironmentException -> UnresolvedScenarioEnvironmentException(t.message + " Please, specify a name or declare only one environment.")
is NoEnvironmentFoundException -> UnresolvedScenarioEnvironmentException(t.message + " Please, declare one.")
is EnvironmentNotFoundException -> UnresolvedScenarioEnvironmentException("Environment [${scenarioDescriptor.environmentName}] not found. ${t.message}")
else -> AssertionError(t)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,27 @@ import com.chutneytesting.engine.domain.execution.report.StepExecutionReportBuil
object ReportUtil {

fun generateReportDto(step: Step): StepExecutionReportDto {
return toDto(generateReport(step))
return toDto(generateReport(step, step.scenarioContext["environment"] as String))
}

private fun generateReport(step: Step): StepExecutionReport {
private fun generateReport(step: Step, env: String): StepExecutionReport {
return StepExecutionReportBuilder().setName(step.definition().name)
.setDuration(step.duration().toMillis())
.setStartDate(step.startDate())
.setStatus(step.status())
.setInformation(step.informations())
.setErrors(step.errors())
.setSteps(step.subSteps().map { generateReport(it) }.toList())
.setSteps(step.subSteps().map { generateReport(it, step.scenarioContext["environment"] as String) }.toList())
.setEvaluatedInputs(step.evaluatedInputs)
.setStepResults(step.stepOutputs)
.setScenarioContext(step.scenarioContext)
.setType(step.type())
.setTarget(step.target())
.setStrategy(step.strategy().map { it.type }.orElse(null))
.setEnvironment(getEnvironment(step))
.setEnvironment(env)
.createStepExecutionReport()
}

private fun getEnvironment(step: Step): String? {
return if (step.isParentStep) {
getEnvironment(step.subSteps()[0])
} else step.scenarioContext["environment"] as String?
}

private fun toDto(report: StepExecutionReport): StepExecutionReportDto {
return StepExecutionReportDto(
report.name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.chutneytesting.kotlin.execution

import com.chutneytesting.engine.api.execution.StatusDto
import com.chutneytesting.environment.domain.exception.EnvironmentNotFoundException
import com.chutneytesting.environment.domain.exception.UnresolvedEnvironmentException
import com.chutneytesting.kotlin.asResource
import com.chutneytesting.kotlin.dsl.ForStrategy
import com.chutneytesting.kotlin.dsl.Scenario
Expand Down Expand Up @@ -42,11 +43,11 @@ class ExecutionServiceTest {
fun `should throw exception when multi env defined and none asked for`() {
val sut = ExecutionService(File("execution/multiEnv".asResource().path).path)

assertThrows<CannotResolveDefaultEnvironmentException> {
assertThrows<UnresolvedEnvironmentException> {
sut.getEnvironment()
}

assertThrows<CannotResolveDefaultEnvironmentException> {
assertThrows<UnresolvedEnvironmentException> {
sut.getEnvironment(null)
}
}
Expand Down

0 comments on commit d1d1fd5

Please sign in to comment.