Skip to content

Commit

Permalink
ci: prove that scenario completion has been tracked synchonously
Browse files Browse the repository at this point in the history
integration test that proves that all data has been persisted at
end of scenario execution.
see [this PR](#223)
for more information.
  • Loading branch information
bbortt committed Feb 13, 2024
1 parent cff7926 commit 2403382
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2017 the original author or authors.
* Copyright 2006-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.
Expand Down Expand Up @@ -30,13 +30,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.citrusframework.TestResult.failed;
import static org.citrusframework.TestResult.success;
import static org.citrusframework.util.StringUtils.hasText;
import static org.springframework.util.StringUtils.arrayToCommaDelimitedString;

/**
* @author Christoph Deppisch
*/
Expand All @@ -50,7 +54,7 @@ public class SimulatorStatusListener extends AbstractTestListener implements Tes

/**
* Currently running test.
*
* <p>
* TODO: Replace with metric.
*/
private Map<String, TestResult> runningTests = new ConcurrentHashMap<>();
Expand All @@ -68,25 +72,25 @@ public SimulatorStatusListener(ScenarioActionService scenarioActionService, Scen

@Override
public void onTestStart(TestCase test) {
if (test instanceof DefaultTestCase) {
runningTests.put(StringUtils.arrayToCommaDelimitedString(getParameters(test)), TestResult.success(test.getName(), test.getTestClass().getSimpleName(), ((DefaultTestCase)test).getParameters()));
if (test instanceof DefaultTestCase defaultTestCase) {
runningTests.put(arrayToCommaDelimitedString(getParameters(test)), success(test.getName(), test.getTestClass().getSimpleName(), defaultTestCase.getParameters()));
} else {
runningTests.put(StringUtils.arrayToCommaDelimitedString(getParameters(test)), TestResult.success(test.getName(), test.getTestClass().getSimpleName()));
runningTests.put(arrayToCommaDelimitedString(getParameters(test)), success(test.getName(), test.getTestClass().getSimpleName()));
}
}

@Override
public void onTestFinish(TestCase test) {
runningTests.remove(StringUtils.arrayToCommaDelimitedString(getParameters(test)));
runningTests.remove(arrayToCommaDelimitedString(getParameters(test)));
}

@Override
public void onTestSuccess(TestCase test) {
TestResult result;
if (test instanceof DefaultTestCase) {
result = TestResult.success(test.getName(), test.getTestClass().getSimpleName(), ((DefaultTestCase)test).getParameters());
if (test instanceof DefaultTestCase defaultTestCase) {
result = success(test.getName(), test.getTestClass().getSimpleName(), defaultTestCase.getParameters());
} else {
result = TestResult.success(test.getName(), test.getTestClass().getSimpleName());
result = success(test.getName(), test.getTestClass().getSimpleName());
}

testResultService.transformAndSave(result);
Expand All @@ -98,10 +102,10 @@ public void onTestSuccess(TestCase test) {
@Override
public void onTestFailure(TestCase test, Throwable cause) {
TestResult result;
if (test instanceof DefaultTestCase) {
result = TestResult.failed(test.getName(), test.getTestClass().getSimpleName(), cause, ((DefaultTestCase)test).getParameters());
if (test instanceof DefaultTestCase defaultTestCase) {
result = failed(test.getName(), test.getTestClass().getSimpleName(), cause, defaultTestCase.getParameters());
} else {
result = TestResult.failed(test.getName(), test.getTestClass().getSimpleName(), cause);
result = failed(test.getName(), test.getTestClass().getSimpleName(), cause);
}

testResultService.transformAndSave(result);
Expand All @@ -116,9 +120,9 @@ public void onTestActionStart(TestCase testCase, TestAction testAction) {
if (!ignoreTestAction(testAction)) {
if (logger.isDebugEnabled()) {
logger.debug(testCase.getName() + "(" +
StringUtils.arrayToCommaDelimitedString(getParameters(testCase)) + ") - " +
testAction.getName() + ": " +
(testAction instanceof Described && StringUtils.hasText(((Described) testAction).getDescription()) ? ((Described) testAction).getDescription() : ""));
arrayToCommaDelimitedString(getParameters(testCase)) + ") - " +
testAction.getName() + ": " +
(testAction instanceof Described described && hasText(described.getDescription()) ? described.getDescription() : ""));
}

scenarioActionService.createForScenarioExecutionAndSave(testCase, testAction);
Expand All @@ -140,8 +144,8 @@ public void onTestActionSkipped(TestCase testCase, TestAction testAction) {
private String[] getParameters(TestCase test) {
List<String> parameterStrings = new ArrayList<>();

if (test instanceof DefaultTestCase) {
for (Map.Entry<String, Object> param : ((DefaultTestCase) test).getParameters().entrySet()) {
if (test instanceof DefaultTestCase defaultTestCase) {
for (Map.Entry<String, Object> param : defaultTestCase.getParameters().entrySet()) {
parameterStrings.add(param.getKey() + "=" + param.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.MessageHeader;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.Message;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.ScenarioAction;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.ScenarioExecution;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.ScenarioParameter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.TestParameter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* 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.repository;

import org.citrusframework.simulator.model.TestResult;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
@ExtendWith(MockitoExtension.class)
class ScenarioLookupServiceImplTest {

private static final String SCENARIO_NAME = "ScenarioLookupServiceImplTest#testScenario";
private static final String STARTER_NAME = "ScenarioLookupServiceImplTest#testScenarioStarter";

private static final ScenarioParameter SCENARIO_PARAMETER = ScenarioParameter.builder()
.name("parameter-name")
.value("parameter-value")
Expand Down Expand Up @@ -61,11 +64,11 @@ static Stream<Arguments> evictAndReloadScenarioCacheIsIdempotent() {
@MethodSource
@ParameterizedTest
void evictAndReloadScenarioCacheIsIdempotent(Consumer<ScenarioLookupServiceImpl> invocation) {
final String testSimulatorScenario = "testSimulatorScenario";
final String testSimulatorScenario = SCENARIO_NAME;
Map<String, SimulatorScenario> contextSimulatorScenarios = Map.of(testSimulatorScenario, new TestSimulatorScenario(), "invalidTestSimulatorScenario", new InvalidTestSimulatorScenario());
doReturn(contextSimulatorScenarios).when(applicationContextMock).getBeansOfType(SimulatorScenario.class);

final String testScenarioStarter = "testScenarioStarter";
final String testScenarioStarter = STARTER_NAME;
Map<String, ScenarioStarter> contextScenarioStarters = Map.of(testScenarioStarter, new TetsScenarioStarter());
doReturn(contextScenarioStarters).when(applicationContextMock).getBeansOfType(ScenarioStarter.class);

Expand Down Expand Up @@ -96,7 +99,7 @@ private void verifyScenariosHaveBeenReloadedFromApplicationContext(String testSi

@Test
void getScenarioNames() {
final String testSimulatorScenario = "testSimulatorScenario";
final String testSimulatorScenario = SCENARIO_NAME;
ReflectionTestUtils.setField(fixture, "scenarios", Map.of(testSimulatorScenario, new TestSimulatorScenario()), Map.class);

assertThat(fixture.getScenarioNames())
Expand All @@ -106,7 +109,7 @@ void getScenarioNames() {

@Test
void getStarterNames() {
final String testScenarioStarter = "testScenarioStarter";
final String testScenarioStarter = STARTER_NAME;
ReflectionTestUtils.setField(fixture, "scenarioStarters", Map.of(testScenarioStarter, new TetsScenarioStarter()), Map.class);

assertThat(fixture.getStarterNames())
Expand Down Expand Up @@ -139,7 +142,7 @@ void lookupScenarioParametersReturnsEmptyListForInvalidScenarioNames() {
.isEmpty();
}

@Scenario("testSimulatorScenario")
@Scenario(SCENARIO_NAME)
private static class TestSimulatorScenario implements SimulatorScenario {

@Override
Expand All @@ -148,7 +151,7 @@ public ScenarioEndpoint getScenarioEndpoint() {
}
}

@Starter("testScenarioStarter")
@Starter(STARTER_NAME)
private static class TetsScenarioStarter implements ScenarioStarter {

@Override
Expand All @@ -162,7 +165,7 @@ public ScenarioEndpoint getScenarioEndpoint() {
}
}

@Starter("invalidTestScenarioStarter")
@Starter("ScenarioLookupServiceImplTest#invalidTestScenarioStarter")
private static class InvalidTestSimulatorScenario implements SimulatorScenario {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,58 @@
package org.citrusframework.simulator.service.runner;

import org.citrusframework.simulator.IntegrationTest;
import org.citrusframework.simulator.model.ScenarioExecution;
import org.citrusframework.simulator.repository.ScenarioExecutionRepository;
import org.citrusframework.simulator.scenario.AbstractSimulatorScenario;
import org.citrusframework.simulator.scenario.Scenario;
import org.citrusframework.simulator.service.ScenarioExecutorService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;

import static java.util.Collections.emptyList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.citrusframework.simulator.model.ScenarioExecution.Status.SUCCESS;

@IntegrationTest
class DefaultScenarioExecutorServiceIT {

private static final String SCENARIO_NAME = "DefaultScenarioExecutorServiceIT#testSimulatorScenario";

@Autowired
private ScenarioExecutorService scenarioExecutorService;

@Autowired
private ApplicationContext applicationContext;
private ScenarioExecutionRepository scenarioExecutionRepository;

@Test
void isDefaultScenarioExecutorService() {
assertThat(applicationContext.getBean(ScenarioExecutorService.class))
assertThat(scenarioExecutorService)
.isInstanceOf(DefaultScenarioExecutorService.class);
}

@Test
void throwsExceptionGivenInexistendScenarioName() {
var beanName = "sherlock";

assertThatThrownBy(() -> scenarioExecutorService.run(beanName, emptyList()))
.isInstanceOf(NoSuchBeanDefinitionException.class)
.hasMessage("No bean named '%s' available".formatted(beanName));
}

@Test
void resultsBeingPersistedSynchronously() {
var executionId = scenarioExecutorService.run(SCENARIO_NAME, emptyList());

assertThat(scenarioExecutionRepository.findOneByExecutionId(executionId))
.hasValueSatisfying(scenarioExecution -> assertThat(scenarioExecution)
.hasNoNullFieldsOrPropertiesExcept("errorMessage")
.extracting(ScenarioExecution::getStatus)
.isEqualTo(SUCCESS));
}

@Scenario(SCENARIO_NAME)
private static class TestSimulatorScenario extends AbstractSimulatorScenario {
}
}

0 comments on commit 2403382

Please sign in to comment.