From 244eaeea13c4f798a904d082d37c822683a3e2d2 Mon Sep 17 00:00:00 2001 From: Matthew Dunsdon Date: Tue, 22 Dec 2020 17:23:48 +0000 Subject: [PATCH] fix(#1713): CVE-2020-15250 (Medium) detected in junit-4.12.jar --- custom/build.gradle | 2 +- gradle.properties | 11 +- orchestrator/build.gradle | 6 +- .../operators/general/ConstraintTypes.feature | 6 +- .../cucumber/testframework/TestRunner.java | 2 +- .../testframework/steps/BooleanValueStep.java | 9 +- .../steps/CombinationStrategyStep.java | 31 ++++ .../testframework/steps/CucumberHooks.java | 4 +- .../steps/DataGenerationStep.java | 39 +++++ .../steps/DateTimeValueStep.java | 24 ++- .../testframework/steps/DateValueStep.java | 2 +- .../testframework/steps/GeneralTestStep.java | 12 +- .../testframework/steps/JSONTestStep.java | 2 +- .../testframework/steps/MapValueStep.java | 6 +- .../testframework/steps/NullValueStep.java | 4 +- .../testframework/steps/NumericValueStep.java | 12 +- .../testframework/steps/OperatorStep.java | 52 ++++++ .../testframework/steps/RegexValueStep.java | 15 +- .../testframework/steps/SetValueStep.java | 30 +++- .../testframework/steps/StringValueStep.java | 4 +- .../testframework/steps/TimeValueStep.java | 2 +- .../steps/TypeRegistryConfiguration.java | 155 ------------------ .../utils/GeneratorCucumber.java | 28 +++- 23 files changed, 252 insertions(+), 206 deletions(-) create mode 100644 orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CombinationStrategyStep.java create mode 100644 orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DataGenerationStep.java create mode 100644 orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/OperatorStep.java delete mode 100644 orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TypeRegistryConfiguration.java diff --git a/custom/build.gradle b/custom/build.gradle index 513ac3f21..32c8ffd35 100644 --- a/custom/build.gradle +++ b/custom/build.gradle @@ -11,5 +11,5 @@ repositories { } dependencies { - testCompile group: 'junit', name: 'junit', version: '4.12' + testCompile group: 'junit', name: 'junit', version: '4.13.1' } diff --git a/gradle.properties b/gradle.properties index b6e641930..446c7c3df 100644 --- a/gradle.properties +++ b/gradle.properties @@ -29,15 +29,14 @@ LEADPONY_JUSTIFY_VERSION=0.14.0 JACKSON_VERSION=2.11.1 MEDEIA_VALIDATOR_JACKSON_VERSION=1.1.1 -JUNIT_JUPITER_VERSION=5.5.1 -JUNIT_4_VERSION=4.12 -JUNIT_PLATFORM_RUNNER_VERSION=1.3.1 +JUNIT_JUPITER_VERSION=5.6.3 +JUNIT_4_VERSION=4.13.1 +JUNIT_PLATFORM_RUNNER_VERSION=1.6.3 MOCKITO_VERSION=1.9.5 MOCKITO_JUNIT_JUPITER_VERSION=2.28.2 GHERKIN_VERSION=5.0.0 -CUCUMBER_VERSION=4.0.0 -CUCUMBER_EXPRESSIONS_VERSION=6.0.1 -CUCUMBER_PICOCONTAINER_VERSION=1.2.5 +CUCUMBER_VERSION=5.0.0 +CUCUMBER_PICOCONTAINER_VERSION=5.0.0 SELENIUM_VERSION=3.141.59 FAKER_VERSION=1.0.2 diff --git a/orchestrator/build.gradle b/orchestrator/build.gradle index 6cabb6f6a..93e1d1133 100644 --- a/orchestrator/build.gradle +++ b/orchestrator/build.gradle @@ -45,13 +45,11 @@ dependencies { testCompile "org.junit.platform:junit-platform-runner:${JUNIT_PLATFORM_RUNNER_VERSION}" testCompile "org.junit.vintage:junit-vintage-engine:${JUNIT_JUPITER_VERSION}" testCompile "org.junit.jupiter:junit-jupiter-params:${JUNIT_JUPITER_VERSION}" - testCompile "io.cucumber:cucumber-core:${CUCUMBER_VERSION}" testCompile "io.cucumber:cucumber-junit:${CUCUMBER_VERSION}" testCompile "io.cucumber:cucumber-java:${CUCUMBER_VERSION}" - testCompile "io.cucumber:cucumber-jvm:${CUCUMBER_VERSION}" + testCompile "io.cucumber:cucumber-java8:${CUCUMBER_VERSION}" testCompile "io.cucumber:gherkin:${GHERKIN_VERSION}" - testCompile "io.cucumber:cucumber-expressions:${CUCUMBER_EXPRESSIONS_VERSION}" - testCompile "info.cukes:cucumber-picocontainer:${CUCUMBER_PICOCONTAINER_VERSION}" + testCompile "io.cucumber:cucumber-picocontainer:${CUCUMBER_PICOCONTAINER_VERSION}" testCompile "org.mockito:mockito-all:${MOCKITO_VERSION}" testCompile "com.shazam:shazamcrest:${SHAZAMCREST_VERSION}" testCompile "org.junit.jupiter:junit-jupiter-engine:${JUNIT_JUPITER_VERSION}" diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/ConstraintTypes.feature b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/ConstraintTypes.feature index aeed9d67e..f67b740e8 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/ConstraintTypes.feature +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/ConstraintTypes.feature @@ -41,9 +41,9 @@ Feature: Correct Constraint Types, validation exceptions should be raised if the Given there is a non nullable field foo And foo has type "datetime" And foo is in set: - | "aaa"| - | "bbb | - | "ccc | + | "aaa" | + | "bbb" | + | "ccc" | Then the profile is invalid Scenario Outline: constraint cannot be applied to fields diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/TestRunner.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/TestRunner.java index ac5ab8905..2e432f1ca 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/TestRunner.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/TestRunner.java @@ -17,8 +17,8 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.GeneratorCucumber; +import io.cucumber.junit.CucumberOptions; import org.junit.runner.RunWith; -import cucumber.api.CucumberOptions; @RunWith(GeneratorCucumber.class) @CucumberOptions( diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/BooleanValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/BooleanValueStep.java index 2b2b57313..19dd001e5 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/BooleanValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/BooleanValueStep.java @@ -16,9 +16,11 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; +import com.fasterxml.jackson.core.JsonParseException; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.When; +import io.cucumber.java.ParameterType; +import io.cucumber.java.en.When; public class BooleanValueStep { private final CucumberTestState state; @@ -27,6 +29,11 @@ public BooleanValueStep(CucumberTestState state) { this.state = state; } + @ParameterType(name = "boolean", value = "(true|false)$") + public Boolean defineBoolean(String value) throws JsonParseException { + return Boolean.valueOf(value); + } + @When("{fieldVar} is equal to {boolean}") public void whenFieldIsConstrainedByNumericValue(String fieldName, Boolean value) { this.state.addConstraint(fieldName, ConstraintType.EQUAL_TO, value); diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CombinationStrategyStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CombinationStrategyStep.java new file mode 100644 index 000000000..12319aeee --- /dev/null +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CombinationStrategyStep.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019 Scott Logic Ltd + * + * 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.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; + +import com.scottlogic.datahelix.generator.core.config.detail.CombinationStrategyType; +import io.cucumber.java.ParameterType; + +import java.util.Arrays; + +public class CombinationStrategyStep { + @ParameterType(name = "combinationStrategy", value = "(.*)$") + public CombinationStrategyType defineCombinationStrategy(String value) { + return Arrays.stream(CombinationStrategyType.values()) + .filter(val -> val.toString().equalsIgnoreCase(value)) + .findFirst().orElse(CombinationStrategyType.PINNING); + } +} diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CucumberHooks.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CucumberHooks.java index f002a7665..fab428e37 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CucumberHooks.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/CucumberHooks.java @@ -16,8 +16,8 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; -import cucumber.api.Scenario; -import cucumber.api.java.Before; +import io.cucumber.java.Before; +import io.cucumber.java.Scenario; import org.junit.AssumptionViolatedException; public class CucumberHooks { diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DataGenerationStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DataGenerationStep.java new file mode 100644 index 000000000..b5343aa10 --- /dev/null +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DataGenerationStep.java @@ -0,0 +1,39 @@ +/* + * Copyright 2019 Scott Logic Ltd + * + * 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.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; + +import com.scottlogic.datahelix.generator.core.config.detail.DataGenerationType; +import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberGenerationMode; +import io.cucumber.java.ParameterType; + +import java.util.Arrays; + +public class DataGenerationStep { + @ParameterType(name = "generationStrategy", value = "(.*)$") + public DataGenerationType defineGenerationStrategy(String value) { + return Arrays.stream(DataGenerationType.values()) + .filter(val -> val.toString().equalsIgnoreCase(value)) + .findFirst().orElse(DataGenerationType.FULL_SEQUENTIAL); + } + + @ParameterType(name = "generationMode", value = "(.*)$") + public CucumberGenerationMode defineGenerationMode(String value) { + return Arrays.stream(CucumberGenerationMode.values()) + .filter(val -> val.toString().equalsIgnoreCase(value)) + .findFirst().orElse(CucumberGenerationMode.VALIDATING); + } +} diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateTimeValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateTimeValueStep.java index 3f6a25a8f..96c1f6ea0 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateTimeValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateTimeValueStep.java @@ -20,12 +20,16 @@ import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.GeneratorTestUtilities; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.And; -import cucumber.api.java.en.Then; -import cucumber.api.java.en.When; +import io.cucumber.java.ParameterType; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; import java.time.OffsetDateTime; +import java.util.Arrays; +import java.util.List; import java.util.function.Function; +import java.util.stream.Collectors; public class DateTimeValueStep { public static final String DATETIME_REGEX = "(-?(\\d{4,19})-(\\d{2})-(\\d{2}T(\\d{2}:\\d{2}:\\d{2}\\.\\d{3}))Z?)"; @@ -37,6 +41,20 @@ public DateTimeValueStep(CucumberTestState state, CucumberTestHelper helper){ this.helper = helper; } + @ParameterType(name = "date", value = DateTimeValueStep.DATETIME_REGEX) + public String defineDate(String value) { + return extractConstraint(value); + } + + private String extractConstraint(String gherkinConstraint) { + List allConstraints = Arrays.asList(gherkinConstraint.split(" ")); + return allConstraints.get(0) + allConstraints + .stream() + .skip(1) + .map(value -> value.substring(0, 1).toUpperCase() + value.substring(1)) + .collect(Collectors.joining()); + } + @When("^([A-z0-9]+) is equal to boolean true") public void equalToTrue(String fieldName) { state.addConstraint(fieldName, ConstraintType.EQUAL_TO, true); diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateValueStep.java index cdf727d4b..5e5d8f0de 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/DateValueStep.java @@ -18,7 +18,7 @@ import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.When; +import io.cucumber.java.en.When; public class DateValueStep { private final CucumberTestState state; diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/GeneralTestStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/GeneralTestStep.java index b99b14aa8..55c9a65bb 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/GeneralTestStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/GeneralTestStep.java @@ -22,8 +22,8 @@ import com.scottlogic.datahelix.generator.core.config.detail.DataGenerationType; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.*; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.Before; -import cucumber.api.java.en.*; +import io.cucumber.java.Before; +import io.cucumber.java.en.*; import org.hamcrest.Matcher; import org.junit.Assert; @@ -352,5 +352,13 @@ static class GeneratedTestData { this.expectedData = expectedData; this.generatedData = generatedData; } + + @Override + public String toString() { + return "GeneratedTestData{" + + "expectedData=" + expectedData + + ", generatedData=" + generatedData + + '}'; + } } } diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/JSONTestStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/JSONTestStep.java index 7f499e48a..35e613c05 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/JSONTestStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/JSONTestStep.java @@ -17,7 +17,7 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; -import cucumber.api.java.en.And; +import io.cucumber.java.en.And; import java.io.IOException; diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/MapValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/MapValueStep.java index 21edc912f..79ce64fd9 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/MapValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/MapValueStep.java @@ -17,7 +17,7 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; -import cucumber.api.java.en.And; +import io.cucumber.java.en.And; import java.util.List; @@ -28,12 +28,12 @@ public MapValueStep(CucumberTestState state) { this.state = state; } - @And("the file \"(.+)\" contains the following data:") + @And("the file {string} contains the following data:") public void whenMapDataStructure(String name, List> values) { state.addMapFile(name, values); } - @And("^(.+) is from (.+) in (.+)$") + @And("{word} is from {word} in {word}") public void whenFieldIsInMap(String field, String key, String map) { state.addInMapConstraint(field, key, map); } diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NullValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NullValueStep.java index 2baea1819..c78e86343 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NullValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NullValueStep.java @@ -19,8 +19,8 @@ import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestHelper; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.Then; -import cucumber.api.java.en.When; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; import java.util.Objects; diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NumericValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NumericValueStep.java index 63d35b078..ab4d3cb8f 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NumericValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/NumericValueStep.java @@ -16,11 +16,14 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; +import com.fasterxml.jackson.core.JsonParseException; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestHelper; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; +import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.GeneratorTestUtilities; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.And; -import cucumber.api.java.en.Then; +import io.cucumber.java.ParameterType; +import io.cucumber.java.en.And; +import io.cucumber.java.en.Then; import java.math.BigDecimal; import java.util.function.Function; @@ -37,6 +40,11 @@ public NumericValueStep(CucumberTestState state, CucumberTestHelper helper) { this.helper = helper; } + @ParameterType(name = "number", value = "([+-]?\\d+(\\.\\d+)?)") + public Number defineNumber(String value) throws JsonParseException { + return (Number) GeneratorTestUtilities.parseNumber(value); + } + @And("^([A-z0-9]+) is equal to (-?[0-9\\.]+)$") public void equalToNumber(String field, String value) { state.addConstraint(field, ConstraintType.EQUAL_TO, tryParse(value)); diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/OperatorStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/OperatorStep.java new file mode 100644 index 000000000..6d3915965 --- /dev/null +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/OperatorStep.java @@ -0,0 +1,52 @@ +/* + * Copyright 2019 Scott Logic Ltd + * + * 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.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; + +import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; +import io.cucumber.java8.En; + +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +public class OperatorStep implements En { + + public OperatorStep() { + Set allOperators = new HashSet<>(Arrays.asList(ConstraintType.values())); + String regex = this.getHumanReadableOperationRegex(allOperators); + + ParameterType("operator", regex, this::extractConstraint); + } + + private String getHumanReadableOperationRegex(Set types) { + return + types.stream() + .map(act -> act.propertyName.replaceAll("([a-z])([A-Z]+)", "$1 $2").toLowerCase()) + .collect(Collectors.joining("|", "(", ")")); + } + + private String extractConstraint(String gherkinConstraint) { + List allConstraints = Arrays.asList(gherkinConstraint.split(" ")); + return allConstraints.get(0) + allConstraints + .stream() + .skip(1) + .map(value -> value.substring(0, 1).toUpperCase() + value.substring(1)) + .collect(Collectors.joining()); + } +} diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/RegexValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/RegexValueStep.java index 293568508..e3635ed8d 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/RegexValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/RegexValueStep.java @@ -19,8 +19,9 @@ import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestHelper; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.Then; -import cucumber.api.java.en.When; +import io.cucumber.java.ParameterType; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; import java.util.regex.Pattern; @@ -33,6 +34,16 @@ public RegexValueStep(CucumberTestState state, CucumberTestHelper helper) { this.helper = helper; } + @ParameterType(name = "regex", value = "/(.+)/$") + public String defineRegex(String value) { + return value; + } + + @ParameterType(name = "fieldVar", value = "^(.+)") + public String defineFieldVar(String value) { + return value; + } + @When("^([A-z0-9]+) is matching regex /(.+)/$") public void matchingRegexString(String fieldName, String value) { state.addConstraint(fieldName, ConstraintType.MATCHES_REGEX, value); diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/SetValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/SetValueStep.java index f0efa9703..d823618e9 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/SetValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/SetValueStep.java @@ -16,11 +16,14 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; +import com.fasterxml.jackson.core.JsonParseException; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; +import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.GeneratorTestUtilities; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.When; +import io.cucumber.java.en.When; import java.util.List; +import java.util.stream.Collectors; public class SetValueStep { private final CucumberTestState state; @@ -30,12 +33,29 @@ public SetValueStep(CucumberTestState state) { } @When("^([A-z0-9]+) is in set:") - public void whenFieldIsConstrainedBySetValue(String fieldName, List values) { - this.state.addConstraint(fieldName, ConstraintType.IN_SET, values); + public void whenFieldIsConstrainedBySetValue(String fieldName, List values) { + final List parsedInput = values.stream() + .map(this::parseInputSafely) + .collect(Collectors.toList()); + + this.state.addConstraint(fieldName, ConstraintType.IN_SET, parsedInput); } @When("^([A-z0-9]+) is anything but in set:") - public void whenFieldIsNotConstrainedBySetValue(String fieldName, List values) { - this.state.addNotConstraint(fieldName, ConstraintType.IN_SET, values); + public void whenFieldIsNotConstrainedBySetValue(String fieldName, List values) { + final List parsedInput = values.stream() + .map(this::parseInputSafely) + .collect(Collectors.toList()); + + this.state.addNotConstraint(fieldName, ConstraintType.IN_SET, parsedInput); + } + + private Object parseInputSafely(String value) { + try { + return GeneratorTestUtilities.parseInput(value); + } catch (JsonParseException e) { + state.addException(e); + return value; + } } } diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/StringValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/StringValueStep.java index a0c787ded..a673158cc 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/StringValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/StringValueStep.java @@ -19,8 +19,8 @@ import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestHelper; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.Then; -import cucumber.api.java.en.When; +import io.cucumber.java.en.Then; +import io.cucumber.java.en.When; import java.util.function.Function; diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TimeValueStep.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TimeValueStep.java index bb9313cae..ee14a52e0 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TimeValueStep.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TimeValueStep.java @@ -18,7 +18,7 @@ import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestHelper; import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberTestState; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; -import cucumber.api.java.en.When; +import io.cucumber.java.en.When; public class TimeValueStep { public static final String TIME_REGEX = "([0-9]{2}:[0-9]{2}:[0-9]{2}\\.[0-9]{3})"; diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TypeRegistryConfiguration.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TypeRegistryConfiguration.java deleted file mode 100644 index ffd18ddd1..000000000 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/steps/TypeRegistryConfiguration.java +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright 2019 Scott Logic Ltd - * - * 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.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.steps; - -import com.fasterxml.jackson.core.JsonParseException; -import com.scottlogic.datahelix.generator.core.config.detail.CombinationStrategyType; -import com.scottlogic.datahelix.generator.core.config.detail.DataGenerationType; -import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.CucumberGenerationMode; -import com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils.GeneratorTestUtilities; -import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; - -import com.scottlogic.datahelix.generator.common.ValidationException; -import cucumber.api.TypeRegistry; -import cucumber.api.TypeRegistryConfigurer; -import io.cucumber.cucumberexpressions.ParameterType; -import io.cucumber.cucumberexpressions.Transformer; -import io.cucumber.datatable.TableCellByTypeTransformer; - -import java.util.*; -import java.util.stream.Collectors; - -public class TypeRegistryConfiguration implements TypeRegistryConfigurer { - private final Set allOperators = new HashSet<>(Arrays.asList(ConstraintType.values())); - - @Override - public Locale locale() { - return Locale.ENGLISH; - } - - @Override - public void configureTypeRegistry(TypeRegistry tr) { - this.defineDataGenerationStrategyType(tr); - this.defineCombinationStrategyType(tr); - this.defineOperationParameterType(tr); - this.defineGenerationMode(tr); - this.defineParameterType(tr, "fieldVar", "^(.+)"); - this.defineParameterType(tr, "regex", "/(.+)/$"); - tr.setDefaultDataTableCellTransformer(new DataTableCellTransformer()); - - tr.defineParameterType(new ParameterType<>( - "number", - "([+-]?\\d+(\\.\\d+)?)", - Number.class, - (Transformer) value -> (Number) GeneratorTestUtilities.parseNumber(value))); - - tr.defineParameterType(new ParameterType<>( - "boolean", - "(true|false)$", - Boolean.class, - (Transformer) Boolean::valueOf)); - - tr.defineParameterType(new ParameterType<>( - "date", - DateTimeValueStep.DATETIME_REGEX, - String.class, - this::extractConstraint)); - } - - private void defineOperationParameterType(TypeRegistry tr) { - tr.defineParameterType(new ParameterType<>( - "operator", - this.getHumanReadableOperationRegex(allOperators), - String.class, - this::extractConstraint - )); - } - - private void defineParameterType(TypeRegistry tr, String name, String regex) { - tr.defineParameterType(new ParameterType<>( - name, - regex, - String.class, - (Transformer) fieldName -> fieldName)); - } - - private void defineDataGenerationStrategyType(TypeRegistry tr) { - Transformer transformer = strategyString -> - Arrays.stream(DataGenerationType.values()) - .filter(val -> val.toString().equalsIgnoreCase(strategyString)) - .findFirst().orElse(DataGenerationType.FULL_SEQUENTIAL); - - tr.defineParameterType(new ParameterType<>( - "generationStrategy", - "(.*)$", - DataGenerationType.class, - transformer)); - } - - private void defineCombinationStrategyType(TypeRegistry tr) { - Transformer transformer = strategyString -> - Arrays.stream(CombinationStrategyType.values()) - .filter(val -> val.toString().equalsIgnoreCase(strategyString)) - .findFirst().orElse(CombinationStrategyType.PINNING); - - tr.defineParameterType(new ParameterType<>( - "combinationStrategy", - "(.*)$", - CombinationStrategyType.class, - transformer)); - } - - private void defineGenerationMode(TypeRegistry tr) { - Transformer transformer = strategyString -> - Arrays.stream(CucumberGenerationMode.values()) - .filter(val -> val.toString().equalsIgnoreCase(strategyString)) - .findFirst().orElse(CucumberGenerationMode.VALIDATING); - - tr.defineParameterType(new ParameterType<>( - "generationMode", - "(.*)$", - CucumberGenerationMode.class, - transformer)); - } - - private String extractConstraint(String gherkinConstraint) { - List allConstraints = Arrays.asList(gherkinConstraint.split(" ")); - return allConstraints.get(0) + allConstraints - .stream() - .skip(1) - .map(value -> value.substring(0, 1).toUpperCase() + value.substring(1)) - .collect(Collectors.joining()); - } - - private String getHumanReadableOperationRegex(Set types) { - return - types.stream() - .map(act -> act.propertyName.replaceAll("([a-z])([A-Z]+)", "$1 $2").toLowerCase()) - .collect(Collectors.joining("|", "(", ")")); - } - - private class DataTableCellTransformer implements TableCellByTypeTransformer { - @Override - public T transform(String value, Class aClass) throws Throwable { - try { - return aClass.cast(GeneratorTestUtilities.parseInput(value.trim())); - } catch (JsonParseException | ValidationException e) { - return (T) e; - } - } - } -} diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/GeneratorCucumber.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/GeneratorCucumber.java index 0ebcb5c3a..1a7fdd3c8 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/GeneratorCucumber.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/GeneratorCucumber.java @@ -16,29 +16,39 @@ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils; -import cucumber.api.junit.Cucumber; -import cucumber.runtime.junit.FeatureRunner; +import io.cucumber.junit.Cucumber; +import org.junit.runner.Description; +import org.junit.runner.Runner; import org.junit.runner.notification.RunNotifier; import org.junit.runners.model.InitializationError; -public class GeneratorCucumber extends Cucumber { +public class GeneratorCucumber extends Runner { private RunNotifier notifier; + private Class clazz; + private Cucumber cucumber; /** * Constructor called by JUnit. * - * @param clazz the class with the @RunWith annotation. + * @param clazzValue the class with the @RunWith annotation. * @throws InitializationError if there is another problem */ - public GeneratorCucumber(Class clazz) throws InitializationError { - super(clazz); + public GeneratorCucumber(Class clazzValue) throws InitializationError { + clazz = clazzValue; + cucumber = new Cucumber(clazzValue); } @Override - protected void runChild(FeatureRunner child, RunNotifier notifier) { - if (this.notifier == null) + public Description getDescription() { + return cucumber.getDescription(); + } + + @Override + public void run(RunNotifier notifier) { + if (this.notifier == null) { this.notifier = new TreatUndefinedCucumberStepsAsTestFailuresRunNotifier(notifier); //NOTE: Not thread safe + } - super.runChild(child, this.notifier); + cucumber.run(this.notifier); } }