-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(simulator): different running modes
- Loading branch information
Showing
12 changed files
with
216 additions
and
74 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
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
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
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
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
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
96 changes: 96 additions & 0 deletions
96
...a/org/citrusframework/simulator/service/runner/AbstractLegacyScenarioExecutorService.java
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,96 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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 org.citrusframework.simulator.service.runner; | ||
|
||
import jakarta.annotation.Nullable; | ||
import org.citrusframework.Citrus; | ||
import org.citrusframework.context.TestContext; | ||
import org.citrusframework.simulator.model.ScenarioExecution; | ||
import org.citrusframework.simulator.model.ScenarioParameter; | ||
import org.citrusframework.simulator.scenario.ScenarioRunner; | ||
import org.citrusframework.simulator.scenario.SimulatorScenario; | ||
import org.citrusframework.simulator.service.ScenarioExecutionService; | ||
import org.citrusframework.simulator.service.ScenarioExecutorService; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
import java.util.List; | ||
|
||
import static org.citrusframework.annotations.CitrusAnnotations.injectAll; | ||
import static org.citrusframework.simulator.model.ScenarioExecution.EXECUTION_ID; | ||
|
||
abstract class AbstractLegacyScenarioExecutorService implements ScenarioExecutorService { | ||
|
||
private final ApplicationContext applicationContext; | ||
private final Citrus citrus; | ||
private final ScenarioExecutionService scenarioExecutionService; | ||
|
||
public AbstractLegacyScenarioExecutorService(ApplicationContext applicationContext, Citrus citrus, ScenarioExecutionService scenarioExecutionService) { | ||
this.applicationContext = applicationContext; | ||
this.citrus = citrus; | ||
this.scenarioExecutionService = scenarioExecutionService; | ||
} | ||
|
||
@Override | ||
public final Long run(String name, @Nullable List<ScenarioParameter> scenarioParameters) { | ||
return run(applicationContext.getBean(name, SimulatorScenario.class), name, scenarioParameters); | ||
} | ||
|
||
@Override | ||
public final Long run(SimulatorScenario scenario, String name, @Nullable List<ScenarioParameter> scenarioParameters) { | ||
ScenarioExecution scenarioExecution = scenarioExecutionService.createAndSaveExecutionScenario(name, scenarioParameters); | ||
|
||
prepareBeforeExecution(scenario); | ||
|
||
startScenario(scenarioExecution.getExecutionId(), name, scenario, scenarioParameters); | ||
|
||
return scenarioExecution.getExecutionId(); | ||
} | ||
|
||
protected abstract void startScenario(Long executionId, String name, SimulatorScenario scenario, List<ScenarioParameter> scenarioParameters); | ||
|
||
/** | ||
* Prepare scenario instance before execution. Subclasses can add custom preparation steps in | ||
* here. | ||
* | ||
* @param scenario | ||
*/ | ||
protected void prepareBeforeExecution(SimulatorScenario scenario) { | ||
} | ||
|
||
protected TestContext createTestContext() { | ||
return citrus.getCitrusContext().createTestContext(); | ||
} | ||
|
||
protected void createAndRunScenarioRunner(TestContext context, Long executionId, String name, SimulatorScenario scenario, List<ScenarioParameter> scenarioParameters) { | ||
var runner = new ScenarioRunner(scenario.getScenarioEndpoint(), applicationContext, context); | ||
if (scenarioParameters != null) { | ||
scenarioParameters.forEach(p -> runner.variable(p.getName(), p.getValue())); | ||
} | ||
|
||
runner.variable(EXECUTION_ID, executionId); | ||
runner.name(String.format("Scenario(%s)", name)); | ||
|
||
injectAll(scenario, citrus, context); | ||
|
||
try { | ||
runner.start(); | ||
scenario.run(runner); | ||
} finally { | ||
runner.stop(); | ||
} | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
...spring-boot/src/main/java/org/citrusframework/simulator/service/runner/SimulatorMode.java
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,7 @@ | ||
package org.citrusframework.simulator.service.runner; | ||
|
||
public enum SimulatorMode { | ||
|
||
ASYNC, | ||
SYNC | ||
} |
65 changes: 65 additions & 0 deletions
65
...in/java/org/citrusframework/simulator/service/runner/SyncScenarioExecutorServiceImpl.java
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,65 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* | ||
* 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 org.citrusframework.simulator.service.runner; | ||
|
||
import org.citrusframework.Citrus; | ||
import org.citrusframework.simulator.config.SimulatorConfigurationProperties; | ||
import org.citrusframework.simulator.model.ScenarioParameter; | ||
import org.citrusframework.simulator.scenario.SimulatorScenario; | ||
import org.citrusframework.simulator.service.ScenarioExecutionService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Service | ||
@ConditionalOnProperty(name= "citrus.simulator.mode", havingValue = "sync", matchIfMissing = true) | ||
public class SyncScenarioExecutorServiceImpl extends AbstractLegacyScenarioExecutorService { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger( SyncScenarioExecutorServiceImpl.class); | ||
|
||
public SyncScenarioExecutorServiceImpl(ApplicationContext applicationContext, Citrus citrus, ScenarioExecutionService scenarioExecutionService, SimulatorConfigurationProperties properties) { | ||
super(applicationContext, citrus,scenarioExecutionService); | ||
} | ||
|
||
@Override | ||
public final void startScenario(Long executionId, String name,SimulatorScenario scenario, List<ScenarioParameter> scenarioParameters) { | ||
logger.info("Starting scenario : {}", name); | ||
try { | ||
var context = createTestContext(); | ||
createAndRunScenarioRunner(context, executionId, name, scenario, scenarioParameters); | ||
logger.debug("Scenario completed: {}", name); | ||
} catch (Exception e) { | ||
logger.error("Scenario completed with error: {}!", name, e); | ||
} | ||
} | ||
|
||
/** | ||
* Prepare scenario instance before execution. Subclasses can add custom preparation steps in | ||
* here. | ||
* | ||
* @param scenario | ||
*/ | ||
protected void prepareBeforeExecution(SimulatorScenario scenario) { | ||
} | ||
} |
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
Oops, something went wrong.