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

Commit

Permalink
feat(): env variables in execution context
Browse files Browse the repository at this point in the history
  • Loading branch information
KarimGl committed Jan 12, 2024
1 parent 41df7dd commit acef679
Show file tree
Hide file tree
Showing 46 changed files with 294 additions and 146 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.chutneytesting.junit.engine;

import com.chutneytesting.ExecutionConfiguration;
import com.chutneytesting.engine.api.execution.EnvironmentDto;
import com.chutneytesting.engine.api.execution.StepDefinitionDto;
import com.chutneytesting.engine.api.execution.StepExecutionReportDto;
import com.chutneytesting.glacio.api.ExecutionRequestMapper;
Expand All @@ -26,15 +27,15 @@
public class ChutneyEngineExecutionContext implements EngineExecutionContext {

private final ExecutionConfiguration executionConfiguration;
private final String environmentName;
private final EnvironmentDto environment;

protected ChutneyEngineExecutionContext(ExecutionConfiguration executionConfiguration, String environmentName) {
protected ChutneyEngineExecutionContext(ExecutionConfiguration executionConfiguration, EnvironmentDto environment) {
this.executionConfiguration = executionConfiguration;
this.environmentName = environmentName;
this.environment = environment;
}

protected Observable<StepExecutionReportDto> executeScenario(StepDefinitionDto stepDefinitionDto) {
Long executionId = executionConfiguration.embeddedTestEngine().executeAsync(ExecutionRequestMapper.toDto(stepDefinitionDto, environmentName));
Long executionId = executionConfiguration.embeddedTestEngine().executeAsync(ExecutionRequestMapper.toDto(stepDefinitionDto, environment));
return executionConfiguration.embeddedTestEngine().receiveNotification(executionId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@

package com.chutneytesting.junit.engine;

import com.chutneytesting.engine.api.execution.EnvironmentDto;
import com.chutneytesting.environment.EnvironmentConfiguration;
import com.chutneytesting.environment.api.variable.dto.EnvironmentVariableDto;
import com.chutneytesting.glacio.GlacioAdapterConfiguration;
import com.chutneytesting.glacio.api.GlacioAdapter;
import com.chutneytesting.junit.api.Chutney;
import com.chutneytesting.junit.api.EnvironmentService;
import com.chutneytesting.tools.UncheckedException;
import java.lang.reflect.Constructor;
import java.util.Collections;
import java.util.Optional;
import java.util.stream.Collectors;
import org.junit.platform.commons.support.ReflectionSupport;
import org.junit.platform.engine.ConfigurationParameters;
import org.junit.platform.engine.EngineDiscoveryRequest;
Expand All @@ -38,6 +42,7 @@ public class ChutneyTestEngine extends HierarchicalTestEngine<ChutneyEngineExecu

public static final String CHUTNEY_JUNIT_ENGINE_ID = "chutney-junit-engine";
private static final Logger LOGGER = LoggerFactory.getLogger(ChutneyTestEngine.class);
public static final String CHUTNEY_JUNIT_ENV_PATH = ".chutney/junit/conf";

@Override
public String getId() {
Expand Down Expand Up @@ -73,7 +78,7 @@ protected ChutneyEngineExecutionContext createExecutionContext(ExecutionRequest
try {
ConfigurationParameters cp = new SystemEnvConfigurationParameters(executionRequest.getConfigurationParameters());
GlacioAdapterConfiguration glacioAdapterConfiguration = new GlacioAdapterConfiguration(getEnvironmentDirectoryPath(cp));
return new ChutneyEngineExecutionContext(glacioAdapterConfiguration.executionConfiguration(), getEnvironmentName(cp));
return new ChutneyEngineExecutionContext(glacioAdapterConfiguration.executionConfiguration(), getEnvironment(cp));
} catch (Exception e) {
LOGGER.error("{} create execution context error", getId(), e);
throw UncheckedException.throwUncheckedException(e);
Expand Down Expand Up @@ -104,7 +109,17 @@ private Object newInstance(Class<?> aClass, String storeFolderPath) {
}

private String getEnvironmentDirectoryPath(ConfigurationParameters cp) {
return cp.get("chutney.junit.engine.conf.env.path").orElse(".chutney/junit/conf");
return cp.get("chutney.junit.engine.conf.env.path").orElse(CHUTNEY_JUNIT_ENV_PATH);
}

private EnvironmentDto getEnvironment(ConfigurationParameters cp) {
EnvironmentConfiguration environmentConfiguration = new EnvironmentConfiguration(getEnvironmentDirectoryPath(cp));
String environmentName = getEnvironmentName(cp);
return cp.get("chutney.junit.engine.conf.env.name")
.map(name -> environmentConfiguration.getEmbeddedEnvironmentApi().getEnvironment(environmentName))
.map(env -> env.variables.stream().collect(Collectors.toMap(EnvironmentVariableDto::key,EnvironmentVariableDto::value)))
.map(variables -> new EnvironmentDto(environmentName, variables))
.orElse(new EnvironmentDto(GlacioAdapter.DEFAULT_ENV, Collections.emptyMap()));
}

private String getEnvironmentName(ConfigurationParameters cp) {
Expand Down
4 changes: 4 additions & 0 deletions engine/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-paranamer</artifactId>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
</dependency>
<!-- For ugly hack in InputParameterResolver -->
<dependency>
<groupId>net.minidev</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@

package com.chutneytesting.engine.api.execution;

import static com.chutneytesting.engine.api.execution.NoEnvironmentDto.NO_ENVIRONMENT_DTO;

import com.chutneytesting.action.spi.injectable.ActionsConfiguration;
import com.chutneytesting.engine.domain.execution.ExecutionEngine;
import com.chutneytesting.engine.domain.execution.ExecutionManager;
import com.chutneytesting.engine.domain.execution.ScenarioExecution;
import com.chutneytesting.engine.domain.execution.StepDefinition;
import com.chutneytesting.engine.domain.execution.engine.Dataset;
import com.chutneytesting.engine.domain.execution.engine.Environment;
import com.chutneytesting.engine.domain.report.Reporter;
import io.reactivex.rxjava3.core.Observable;
import java.util.Optional;
Expand All @@ -48,11 +51,16 @@ public StepExecutionReportDto execute(ExecutionRequestDto request) {

@Override
public Long executeAsync(ExecutionRequestDto request) {
StepDefinition stepDefinition = StepDefinitionMapper.toStepDefinition(request.scenario.definition, request.environment);
StepDefinition stepDefinition = StepDefinitionMapper.toStepDefinition(request.scenario.definition);
Dataset dataset = Optional.ofNullable(request.dataset)
.map(d -> new Dataset(d.constants, d.datatable))
.orElseGet(Dataset::new);
return engine.execute(stepDefinition, dataset, ScenarioExecution.createScenarioExecution(actionsConfiguration));
Environment environment = EnvironmentDtoMapper.INSTANCE.toDomain(Optional.ofNullable(request.environment).orElse(NO_ENVIRONMENT_DTO));
return engine.execute(
stepDefinition,
dataset,
ScenarioExecution.createScenarioExecution(actionsConfiguration),
environment);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
*
* * Copyright 2017-2023 Enedis
* *
* * 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.
*
*/

package com.chutneytesting.engine.api.execution;

import java.util.Map;

public record EnvironmentDto(String name, Map<String,String> variables) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
*
* * Copyright 2017-2023 Enedis
* *
* * 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.
*
*/

package com.chutneytesting.engine.api.execution;

import com.chutneytesting.engine.domain.execution.engine.Environment;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

@Mapper
public interface EnvironmentDtoMapper {

EnvironmentDtoMapper INSTANCE = Mappers.getMapper( EnvironmentDtoMapper.class );

Environment toDomain(EnvironmentDto dto);
EnvironmentDto fromDomain(Environment domain);

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@
public class ExecutionRequestDto {

public final StepDefinitionRequestDto scenario;
public final String environment;
public final EnvironmentDto environment;
public final DatasetDto dataset;

@JsonCreator
public ExecutionRequestDto(StepDefinitionRequestDto scenario, String environment, DatasetDto dataset) {
public ExecutionRequestDto(StepDefinitionRequestDto scenario, EnvironmentDto environment, DatasetDto dataset) {
this.scenario = scenario;
this.environment = environment;
this.dataset = dataset;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright 2017-2023 Enedis
*
* 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.
*/

package com.chutneytesting.engine.api.execution;

import static java.util.Collections.emptyMap;

public final class NoEnvironmentDto {

public static final EnvironmentDto NO_ENVIRONMENT_DTO = new EnvironmentDto("", emptyMap());

private NoEnvironmentDto() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class StepDefinitionMapper {
private StepDefinitionMapper() {
}

static StepDefinition toStepDefinition(StepDefinitionDto dto, String environment) {
static StepDefinition toStepDefinition(StepDefinitionDto dto) {
StepStrategyDefinition strategy = null;
if (dto.strategy != null) {
StrategyProperties strategyProperties = new StrategyProperties(dto.strategy.strategyProperties);
Expand All @@ -40,7 +40,7 @@ static StepDefinition toStepDefinition(StepDefinitionDto dto, String environment
}

List<StepDefinition> steps = dto.steps.stream()
.map(dto1 -> toStepDefinition(dto1, environment))
.map(dto1 -> toStepDefinition(dto1))
.collect(toList());

return new StepDefinition(
Expand All @@ -51,8 +51,7 @@ static StepDefinition toStepDefinition(StepDefinitionDto dto, String environment
dto.inputs,
steps,
dto.outputs,
dto.validations,
environment
dto.validations
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package com.chutneytesting.engine.domain.execution;

import com.chutneytesting.engine.domain.execution.engine.Dataset;
import com.chutneytesting.engine.domain.execution.engine.Environment;

public interface ExecutionEngine {

Long execute(StepDefinition stepDefinition, Dataset dataset, ScenarioExecution execution);
Long execute(StepDefinition stepDefinition, Dataset dataset, ScenarioExecution execution, Environment environment);

void shutdown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ public class StepDefinition implements StepDefinitionSpi {

public final Map<String, Object> validations;

public final String environment; // TODO - remove from here and pass as engine argument instead

/**
* Target on which to execute the current step.
* Can be null, a step can have no target defined
Expand All @@ -72,8 +70,7 @@ public StepDefinition(String name,
Map<String, Object> inputs,
List<StepDefinition> steps,
Map<String, Object> outputs,
Map<String, Object> validations,
String environment) {
Map<String, Object> validations) {
this.name = requireNonNull(name, "The argument <name> must not be null");
this.type = requireNonNull(type, "The argument <type> must not be null");

Expand All @@ -84,7 +81,6 @@ public StepDefinition(String name,
this.steps = steps != null ? Collections.unmodifiableList(steps) : Collections.emptyList();
this.outputs = outputs != null ? Collections.unmodifiableMap(outputs) : Collections.emptyMap();
this.validations = validations != null ? Collections.unmodifiableMap(validations) : Collections.emptyMap();
this.environment = environment;
}

public Optional<Target> getTarget() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class StepDefinitionBuilder {
private List<StepDefinition> steps;
private Map<String, Object> outputs;
private Map<String, Object> validations;
private String environment;

public StepDefinitionBuilder withName(String name) {
this.name = name;
Expand Down Expand Up @@ -73,13 +72,8 @@ public StepDefinitionBuilder withValidations(Map<String, Object> validations) {
return this;
}

public StepDefinitionBuilder withEnvironment(String environment) {
this.environment = environment;
return this;
}

public StepDefinition build() {
return new StepDefinition(name, target, type, strategy, inputs, steps, outputs, validations, environment);
return new StepDefinition(name, target, type, strategy, inputs, steps, outputs, validations);
}

public static StepDefinitionBuilder copyFrom(StepDefinition definition) {
Expand All @@ -92,7 +86,6 @@ public static StepDefinitionBuilder copyFrom(StepDefinition definition) {
builder.withSteps(definition.steps);
builder.withOutputs(definition.outputs);
builder.withValidations(definition.validations);
builder.withEnvironment(definition.environment);
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonList;
import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;

Expand Down Expand Up @@ -74,16 +75,17 @@ public DefaultExecutionEngine(StepDataEvaluator dataEvaluator,
}

@Override
public Long execute(StepDefinition stepDefinition, Dataset dataset, ScenarioExecution execution) {
public Long execute(StepDefinition stepDefinition, Dataset dataset, ScenarioExecution execution, Environment environment) {

AtomicReference<Step> rootStep = new AtomicReference<>(Step.nonExecutable(stepDefinition));
reporter.createPublisher(execution.executionId, rootStep.get());

actionExecutor.execute(() -> {

final ScenarioContext scenarioContext = new ScenarioContextImpl();
scenarioContext.put("environment", stepDefinition.environment);
scenarioContext.put("environment", environment.name());
scenarioContext.put("dataset", dataset.datatable);
scenarioContext.putAll(ofNullable(environment.variables()).orElse(emptyMap()));
scenarioContext.putAll(evaluateDatasetConstants(dataset, scenarioContext));

try {
Expand Down Expand Up @@ -125,10 +127,9 @@ public void shutdown() {

private Optional<Step> initFinalRootStep(AtomicReference<Step> rootStep, List<FinallyAction> finallyActionsSnapshot) {
try {
String environment = rootStep.get().definition().environment;
Pair<List<StepDefinition>, List<Step>> finalStepsWithDefinitions = finallyActionsSnapshot.stream()
.map(fa -> {
StepDefinition definition = new FinallyActionMapper().toStepDefinition(fa, environment);
StepDefinition definition = new FinallyActionMapper().toStepDefinition(fa);
return Pair.of(singletonList(definition), singletonList(buildStep(definition)));
})
.reduce(Pair.of(new ArrayList<>(), new ArrayList<>()), (p1, p2) -> {
Expand All @@ -145,8 +146,7 @@ private Optional<Step> initFinalRootStep(AtomicReference<Step> rootStep, List<Fi
emptyMap(),
finalStepsWithDefinitions.getLeft(),
emptyMap(),
emptyMap(),
environment
emptyMap()
);

return Optional.of(
Expand Down
Loading

0 comments on commit acef679

Please sign in to comment.