diff --git a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/GreaterThanConstraint.java b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/GreaterThanConstraint.java index a6330b7cb..9eab3b8bb 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/GreaterThanConstraint.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/GreaterThanConstraint.java @@ -45,7 +45,7 @@ public Field getField() { @Override public AtomicConstraint negate() { - return new IsLessThanOrEqualToConstantConstraint(field, referenceValue); + return new LessThanOrEqualToConstraint(field, referenceValue); } @Override diff --git a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsLessThanOrEqualToConstantConstraint.java b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/LessThanOrEqualToConstraint.java similarity index 90% rename from generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsLessThanOrEqualToConstantConstraint.java rename to generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/LessThanOrEqualToConstraint.java index 36f58c763..31f4d711c 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsLessThanOrEqualToConstantConstraint.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/LessThanOrEqualToConstraint.java @@ -29,11 +29,11 @@ import static com.scottlogic.deg.generator.restrictions.linear.LinearRestrictionsFactory.createNumericRestrictions; import static com.scottlogic.deg.generator.utils.Defaults.NUMERIC_MIN_LIMIT; -public class IsLessThanOrEqualToConstantConstraint implements AtomicConstraint { +public class LessThanOrEqualToConstraint implements AtomicConstraint { public final Field field; public final HelixNumber referenceValue; - public IsLessThanOrEqualToConstantConstraint(Field field, HelixNumber referenceValue) { + public LessThanOrEqualToConstraint(Field field, HelixNumber referenceValue) { this.referenceValue = referenceValue; this.field = field; } @@ -61,7 +61,7 @@ public boolean equals(Object o){ return o.equals(this); } if (o == null || getClass() != o.getClass()) return false; - IsLessThanOrEqualToConstantConstraint constraint = (IsLessThanOrEqualToConstantConstraint) o; + LessThanOrEqualToConstraint constraint = (LessThanOrEqualToConstraint) o; return Objects.equals(field, constraint.field) && Objects.equals(referenceValue, constraint.referenceValue); } diff --git a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsStringLongerThanConstraint.java b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/LongerThanConstraint.java similarity index 85% rename from generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsStringLongerThanConstraint.java rename to generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/LongerThanConstraint.java index f4a706631..c331551e1 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsStringLongerThanConstraint.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/LongerThanConstraint.java @@ -24,11 +24,11 @@ import java.util.Objects; -public class IsStringLongerThanConstraint implements AtomicConstraint { +public class LongerThanConstraint implements AtomicConstraint { public final Field field; public final HelixStringLength referenceValue; - public IsStringLongerThanConstraint(Field field, HelixStringLength referenceValue) { + public LongerThanConstraint(Field field, HelixStringLength referenceValue) { this.referenceValue = referenceValue; this.field = field; } @@ -41,7 +41,7 @@ public Field getField() { @Override public AtomicConstraint negate() { - return new IsStringShorterThanConstraint(field, HelixStringLength.create(referenceValue.getValue() + 1)); + return new ShorterThanConstraint(field, HelixStringLength.create(referenceValue.getValue() + 1)); } @Override @@ -56,7 +56,7 @@ public boolean equals(Object o){ return o.equals(this); } if (o == null || getClass() != o.getClass()) return false; - IsStringLongerThanConstraint constraint = (IsStringLongerThanConstraint) o; + LongerThanConstraint constraint = (LongerThanConstraint) o; return Objects.equals(field, constraint.field) && Objects.equals(referenceValue, constraint.referenceValue); } diff --git a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/NotStringLengthConstraint.java b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/NotStringLengthConstraint.java index 5a2789223..d2370c97d 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/NotStringLengthConstraint.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/NotStringLengthConstraint.java @@ -24,7 +24,7 @@ public Field getField() { @Override public AtomicConstraint negate() { - return new StringHasLengthConstraint(field, referenceValue); + return new OfLengthConstraint(field, referenceValue); } @Override diff --git a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/StringHasLengthConstraint.java b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/OfLengthConstraint.java similarity index 90% rename from generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/StringHasLengthConstraint.java rename to generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/OfLengthConstraint.java index 87be1ecc9..21d6e00ab 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/StringHasLengthConstraint.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/OfLengthConstraint.java @@ -24,11 +24,11 @@ import java.util.Objects; -public class StringHasLengthConstraint implements AtomicConstraint { +public class OfLengthConstraint implements AtomicConstraint { public final Field field; public final HelixStringLength referenceValue; - public StringHasLengthConstraint(Field field, HelixStringLength referenceValue) { + public OfLengthConstraint(Field field, HelixStringLength referenceValue) { this.referenceValue = referenceValue; this.field = field; } @@ -55,7 +55,7 @@ public boolean equals(Object o){ return o.equals(this); } if (o == null || getClass() != o.getClass()) return false; - StringHasLengthConstraint constraint = (StringHasLengthConstraint) o; + OfLengthConstraint constraint = (OfLengthConstraint) o; return Objects.equals(field, constraint.field) && Objects.equals(referenceValue.getValue(), constraint.referenceValue.getValue()); } diff --git a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsStringShorterThanConstraint.java b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/ShorterThanConstraint.java similarity index 85% rename from generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsStringShorterThanConstraint.java rename to generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/ShorterThanConstraint.java index e5de1ea5b..e56bf244f 100644 --- a/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/IsStringShorterThanConstraint.java +++ b/generator/src/main/java/com/scottlogic/deg/generator/profile/constraints/atomic/ShorterThanConstraint.java @@ -24,11 +24,11 @@ import java.util.Objects; -public class IsStringShorterThanConstraint implements AtomicConstraint { +public class ShorterThanConstraint implements AtomicConstraint { public final Field field; public final HelixStringLength referenceValue; - public IsStringShorterThanConstraint(Field field, HelixStringLength referenceValue) { + public ShorterThanConstraint(Field field, HelixStringLength referenceValue) { this.referenceValue = referenceValue; this.field = field; } @@ -40,7 +40,7 @@ public Field getField() { @Override public AtomicConstraint negate() { - return new IsStringLongerThanConstraint(field, HelixStringLength.create(referenceValue.getValue() - 1)); + return new LongerThanConstraint(field, HelixStringLength.create(referenceValue.getValue() - 1)); } @Override @@ -55,7 +55,7 @@ public boolean equals(Object o){ return o.equals(this); } if (o == null || getClass() != o.getClass()) return false; - IsStringShorterThanConstraint constraint = (IsStringShorterThanConstraint) o; + ShorterThanConstraint constraint = (ShorterThanConstraint) o; return Objects.equals(field, constraint.field) && Objects.equals(referenceValue, constraint.referenceValue); } diff --git a/generator/src/test/java/com/scottlogic/deg/generator/builders/ConstraintChainBuilder.java b/generator/src/test/java/com/scottlogic/deg/generator/builders/ConstraintChainBuilder.java index a47ea3d91..95a23ce22 100644 --- a/generator/src/test/java/com/scottlogic/deg/generator/builders/ConstraintChainBuilder.java +++ b/generator/src/test/java/com/scottlogic/deg/generator/builders/ConstraintChainBuilder.java @@ -130,7 +130,7 @@ public ConstraintChainBuilder withInSetConstraint(Field field, Object[] legal } public ConstraintChainBuilder withOfLengthConstraint(Field fooField, int length) { - return saveAndSet(new StringHasLengthConstraint(fooField, HelixStringLength.create(length))); + return saveAndSet(new OfLengthConstraint(fooField, HelixStringLength.create(length))); } public ConstraintChainBuilder withAfterConstraint(Field field, OffsetDateTime dateTime) { diff --git a/generator/src/test/java/com/scottlogic/deg/generator/restrictions/ConstraintToFieldSpecTests.java b/generator/src/test/java/com/scottlogic/deg/generator/restrictions/ConstraintToFieldSpecTests.java index eadc30f93..de215f7c0 100644 --- a/generator/src/test/java/com/scottlogic/deg/generator/restrictions/ConstraintToFieldSpecTests.java +++ b/generator/src/test/java/com/scottlogic/deg/generator/restrictions/ConstraintToFieldSpecTests.java @@ -18,11 +18,10 @@ import com.scottlogic.deg.common.profile.Field; import com.scottlogic.deg.common.profile.HelixStringLength; -import com.scottlogic.deg.generator.fieldspecs.FieldSpec; import com.scottlogic.deg.generator.fieldspecs.RestrictionsFieldSpec; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsStringLongerThanConstraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsStringShorterThanConstraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.StringHasLengthConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.LongerThanConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.ShorterThanConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.OfLengthConstraint; import com.scottlogic.deg.generator.profile.constraints.atomic.ViolatedAtomicConstraint; import org.junit.jupiter.api.Test; @@ -34,7 +33,7 @@ class ConstraintToFieldSpecTests { @Test void construct_stringHasLengthConstraintRetrievedTwice_returnsTheSameGeneratorInstance() { - StringHasLengthConstraint constraint = new StringHasLengthConstraint(testField, HelixStringLength.create(10)); + OfLengthConstraint constraint = new OfLengthConstraint(testField, HelixStringLength.create(10)); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec)constraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec)constraint.toFieldSpec(); @@ -44,7 +43,7 @@ void construct_stringHasLengthConstraintRetrievedTwice_returnsTheSameGeneratorIn @Test void construct_stringHasLengthConstraintViolatedTwice_returnsTheSameGeneratorInstance() { - ViolatedAtomicConstraint constraint = new ViolatedAtomicConstraint(new StringHasLengthConstraint(testField,HelixStringLength.create(10))); + ViolatedAtomicConstraint constraint = new ViolatedAtomicConstraint(new OfLengthConstraint(testField,HelixStringLength.create(10))); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); @@ -54,8 +53,8 @@ void construct_stringHasLengthConstraintViolatedTwice_returnsTheSameGeneratorIns @Test void construct_twoInstancesOfStringHasLengthConstraintCalledWithEqualValues_returnsTheSameGeneratorInstance() { - StringHasLengthConstraint firstConstraint = new StringHasLengthConstraint(testField,HelixStringLength.create(20)); - StringHasLengthConstraint secondConstraint = new StringHasLengthConstraint( testField, HelixStringLength.create(20)); + OfLengthConstraint firstConstraint = new OfLengthConstraint(testField,HelixStringLength.create(20)); + OfLengthConstraint secondConstraint = new OfLengthConstraint( testField, HelixStringLength.create(20)); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) firstConstraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec) secondConstraint.toFieldSpec(); @@ -65,7 +64,7 @@ void construct_twoInstancesOfStringHasLengthConstraintCalledWithEqualValues_retu @Test void construct_isStringLongerThanConstraintRetrievedTwice_returnsTheSameGeneratorInstance() { - IsStringLongerThanConstraint constraint = new IsStringLongerThanConstraint(testField, HelixStringLength.create(15)); + LongerThanConstraint constraint = new LongerThanConstraint(testField, HelixStringLength.create(15)); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); @@ -76,7 +75,7 @@ void construct_isStringLongerThanConstraintRetrievedTwice_returnsTheSameGenerato @Test void construct_isStringLongerThanConstraintViolatedTwice_returnsTheSameGeneratorInstance() { ViolatedAtomicConstraint constraint = new ViolatedAtomicConstraint( - new IsStringLongerThanConstraint( testField,HelixStringLength.create(10)) + new LongerThanConstraint( testField,HelixStringLength.create(10)) ); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); @@ -87,8 +86,8 @@ void construct_isStringLongerThanConstraintViolatedTwice_returnsTheSameGenerator @Test void construct_twoInstancesOfIsStringLongerThanConstraintCalledWithEqualValues_returnsTheSameGeneratorInstance() { - IsStringLongerThanConstraint firstConstraint = new IsStringLongerThanConstraint(testField, HelixStringLength.create(20)); - IsStringLongerThanConstraint secondConstraint = new IsStringLongerThanConstraint(testField, HelixStringLength.create(20)); + LongerThanConstraint firstConstraint = new LongerThanConstraint(testField, HelixStringLength.create(20)); + LongerThanConstraint secondConstraint = new LongerThanConstraint(testField, HelixStringLength.create(20)); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) firstConstraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec) secondConstraint.toFieldSpec(); @@ -98,7 +97,7 @@ void construct_twoInstancesOfIsStringLongerThanConstraintCalledWithEqualValues_r @Test void construct_isStringShorterThanConstraintRetrievedTwice_returnsTheSameGeneratorInstance() { - IsStringShorterThanConstraint constraint = new IsStringShorterThanConstraint(testField, HelixStringLength.create(25)); + ShorterThanConstraint constraint = new ShorterThanConstraint(testField, HelixStringLength.create(25)); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); @@ -109,7 +108,7 @@ void construct_isStringShorterThanConstraintRetrievedTwice_returnsTheSameGenerat @Test void construct_isStringShorterThanConstraintViolatedTwice_returnsTheSameGeneratorInstance() { ViolatedAtomicConstraint constraint = new ViolatedAtomicConstraint( - new IsStringShorterThanConstraint(testField, HelixStringLength.create(10)) + new ShorterThanConstraint(testField, HelixStringLength.create(10)) ); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) constraint.toFieldSpec(); @@ -120,8 +119,8 @@ void construct_isStringShorterThanConstraintViolatedTwice_returnsTheSameGenerato @Test void construct_twoInstancesOfIsStringShorterThanConstraintCalledWithEqualValues_returnsTheSameGeneratorInstance() { - IsStringShorterThanConstraint firstConstraint = new IsStringShorterThanConstraint(testField, HelixStringLength.create(20)); - IsStringShorterThanConstraint secondConstraint = new IsStringShorterThanConstraint(testField, HelixStringLength.create(20)); + ShorterThanConstraint firstConstraint = new ShorterThanConstraint(testField, HelixStringLength.create(20)); + ShorterThanConstraint secondConstraint = new ShorterThanConstraint(testField, HelixStringLength.create(20)); final RestrictionsFieldSpec firstInstance = (RestrictionsFieldSpec) firstConstraint.toFieldSpec(); final RestrictionsFieldSpec secondInstance = (RestrictionsFieldSpec) secondConstraint.toFieldSpec(); diff --git a/generator/src/test/java/com/scottlogic/deg/generator/walker/pruner/TreePrunerTests.java b/generator/src/test/java/com/scottlogic/deg/generator/walker/pruner/TreePrunerTests.java index 844d2a4be..36c51b331 100644 --- a/generator/src/test/java/com/scottlogic/deg/generator/walker/pruner/TreePrunerTests.java +++ b/generator/src/test/java/com/scottlogic/deg/generator/walker/pruner/TreePrunerTests.java @@ -26,8 +26,8 @@ import com.scottlogic.deg.generator.fieldspecs.FieldSpecMerger; import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; import com.scottlogic.deg.generator.generation.databags.DataBagValue; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsStringLongerThanConstraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsStringShorterThanConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.LongerThanConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.ShorterThanConstraint; import com.scottlogic.deg.generator.reducer.ConstraintReducer; import org.junit.jupiter.api.Test; @@ -68,7 +68,7 @@ class TreePrunerTests { public void pruneConstraintNode_leafNodeContradictionsWithParent_returnsContradictory() { //Arrange Set inputWhitelist = new HashSet<>(Arrays.asList("a", "b")); - ConstraintNode tree = new ConstraintNodeBuilder().addAtomicConstraints(new IsStringLongerThanConstraint(field, HelixStringLength.create(5))).build(); + ConstraintNode tree = new ConstraintNodeBuilder().addAtomicConstraints(new LongerThanConstraint(field, HelixStringLength.create(5))).build(); FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) .withNotNull(); @@ -87,7 +87,7 @@ public void pruneConstraintNode_leafNodeContradictionsWithParent_returnsContradi public void pruneConstraintNode_leafNodeNoContradictionsWithParent_returnsLeafNode() { //Arrange Set inputWhitelist = new HashSet<>(Arrays.asList("a", "b")); - ConstraintNode tree = new ConstraintNodeBuilder().addAtomicConstraints(new IsStringShorterThanConstraint(field, HelixStringLength.create(5))).build(); + ConstraintNode tree = new ConstraintNodeBuilder().addAtomicConstraints(new ShorterThanConstraint(field, HelixStringLength.create(5))).build(); FieldSpec inputFieldSpec = FieldSpecFactory.fromList( (DistributedList.uniform(inputWhitelist))); diff --git a/orchestrator/src/main/java/com/scottlogic/deg/orchestrator/violate/AtomicConstraintTypeMapper.java b/orchestrator/src/main/java/com/scottlogic/deg/orchestrator/violate/AtomicConstraintTypeMapper.java index 090cfcca5..6c38ac842 100644 --- a/orchestrator/src/main/java/com/scottlogic/deg/orchestrator/violate/AtomicConstraintTypeMapper.java +++ b/orchestrator/src/main/java/com/scottlogic/deg/orchestrator/violate/AtomicConstraintTypeMapper.java @@ -29,11 +29,11 @@ public Class toConstraintClass(ConstraintType type) { case CONTAINS_REGEX: return ContainsRegexConstraint.class; case OF_LENGTH: - return StringHasLengthConstraint.class; + return OfLengthConstraint.class; case LONGER_THAN: - return IsStringLongerThanConstraint.class; + return LongerThanConstraint.class; case SHORTER_THAN: - return IsStringShorterThanConstraint.class; + return ShorterThanConstraint.class; case GREATER_THAN: return GreaterThanConstraint.class; case GREATER_THAN_OR_EQUAL_TO: @@ -41,7 +41,7 @@ public Class toConstraintClass(ConstraintType type) { case LESS_THAN: return LessThanConstraint.class; case LESS_THAN_OR_EQUAL_TO: - return IsLessThanOrEqualToConstantConstraint.class; + return LessThanOrEqualToConstraint.class; case AFTER: return AfterConstraint.class; case AFTER_OR_AT: diff --git a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/testframework/utils/CucumberTestState.java b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/testframework/utils/CucumberTestState.java index 73cbc157b..87054e894 100644 --- a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/testframework/utils/CucumberTestState.java +++ b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/cucumber/testframework/utils/CucumberTestState.java @@ -149,7 +149,7 @@ private ConstraintDTO createConstraint(String fieldName, ConstraintType type, Ob values = (Collection) _value; }}; case IS_NULL: - return new NullConstraintDTO() {{ + return new IsNullConstraintDTO() {{ field = fieldName; isNull = (boolean) _value; }}; diff --git a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violate/violator/ProfileViolationTests.java b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violate/violator/ProfileViolationTests.java index 81d09c76e..c5294c8f6 100644 --- a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violate/violator/ProfileViolationTests.java +++ b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violate/violator/ProfileViolationTests.java @@ -86,9 +86,9 @@ private static Stream allAtomicConstraints() { Arguments.of(ContainsRegexConstraint.class, Pattern.compile("\\w+")), Arguments.of(MatchesRegexConstraint.class, Pattern.compile("\\d+")), - Arguments.of(IsStringLongerThanConstraint.class, HelixStringLength.create(10)), - Arguments.of(IsStringShorterThanConstraint.class, HelixStringLength.create(20)), - Arguments.of(StringHasLengthConstraint.class, HelixStringLength.create(15)), + Arguments.of(LongerThanConstraint.class, HelixStringLength.create(10)), + Arguments.of(ShorterThanConstraint.class, HelixStringLength.create(20)), + Arguments.of(OfLengthConstraint.class, HelixStringLength.create(15)), Arguments.of(AfterConstraint.class, HelixDateTime.create(sampleDate)), Arguments.of(AfterOrAtConstraint.class, HelixDateTime.create(sampleDate.plusDays(1))), @@ -98,7 +98,7 @@ private static Stream allAtomicConstraints() { Arguments.of(GreaterThanConstraint.class, HelixNumber.create(100)), Arguments.of(GreaterThanOrEqualToConstraint.class, HelixNumber.create(200)), Arguments.of(LessThanConstraint.class, HelixNumber.create(300)), - Arguments.of(IsLessThanOrEqualToConstantConstraint.class, HelixNumber.create(400)) + Arguments.of(LessThanOrEqualToConstraint.class, HelixNumber.create(400)) ); } diff --git a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violation/ViolationFiltersProviderTest.java b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violation/ViolationFiltersProviderTest.java index 80894656e..1041dc038 100644 --- a/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violation/ViolationFiltersProviderTest.java +++ b/orchestrator/src/test/java/com/scottlogic/deg/orchestrator/violation/ViolationFiltersProviderTest.java @@ -17,8 +17,8 @@ package com.scottlogic.deg.orchestrator.violation; import com.scottlogic.deg.common.profile.HelixStringLength; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsStringShorterThanConstraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.StringHasLengthConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.ShorterThanConstraint; +import com.scottlogic.deg.generator.profile.constraints.atomic.OfLengthConstraint; import com.scottlogic.deg.generator.violations.filters.ConstraintTypeViolationFilter; import com.scottlogic.deg.generator.violations.filters.ViolationFilter; import com.scottlogic.deg.orchestrator.violate.AtomicConstraintTypeMapper; @@ -72,11 +72,11 @@ void hasLengthConstraintsToViolate_ReturnsOneFilter_ThatDoesNotAcceptHasLengthCo assertThat(filter.canViolate( - new StringHasLengthConstraint(null, HelixStringLength.create(2))), + new OfLengthConstraint(null, HelixStringLength.create(2))), is(false)); assertThat(filter.canViolate( - new IsStringShorterThanConstraint(null, HelixStringLength.create(5))), + new ShorterThanConstraint(null, HelixStringLength.create(5))), is(true)); } diff --git a/profile/src/main/java/com/scottlogic/deg/profile/dtos/constraints/atomic/NullConstraintDTO.java b/profile/src/main/java/com/scottlogic/deg/profile/dtos/constraints/atomic/IsNullConstraintDTO.java similarity index 87% rename from profile/src/main/java/com/scottlogic/deg/profile/dtos/constraints/atomic/NullConstraintDTO.java rename to profile/src/main/java/com/scottlogic/deg/profile/dtos/constraints/atomic/IsNullConstraintDTO.java index 09497e029..aecf633d2 100644 --- a/profile/src/main/java/com/scottlogic/deg/profile/dtos/constraints/atomic/NullConstraintDTO.java +++ b/profile/src/main/java/com/scottlogic/deg/profile/dtos/constraints/atomic/IsNullConstraintDTO.java @@ -21,12 +21,12 @@ import com.scottlogic.deg.profile.common.ConstraintType; import com.scottlogic.deg.profile.common.ConstraintTypeJsonProperty; -@JsonDeserialize(as = NullConstraintDTO.class) -public class NullConstraintDTO extends AtomicConstraintDTO { +@JsonDeserialize(as = IsNullConstraintDTO.class) +public class IsNullConstraintDTO extends AtomicConstraintDTO { @JsonProperty(ConstraintTypeJsonProperty.IS_NULL) public boolean isNull; - public NullConstraintDTO() { + public IsNullConstraintDTO() { super(ConstraintType.IS_NULL); } } diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/AtomicConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/AtomicConstraintFactory.java deleted file mode 100644 index c1821d8e7..000000000 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/AtomicConstraintFactory.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.scottlogic.deg.profile.reader.AtomicConstraintFactory; - -import com.scottlogic.deg.common.profile.Field; -import com.scottlogic.deg.common.profile.HelixDateTime; -import com.scottlogic.deg.common.profile.ProfileFields; -import com.scottlogic.deg.common.util.NumberUtils; -import com.scottlogic.deg.generator.fieldspecs.relations.InMapRelation; -import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; -import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.EqualToConstraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsInSetConstraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.IsNullConstraint; -import com.scottlogic.deg.profile.dtos.constraints.*; -import com.scottlogic.deg.profile.reader.FileReader; -import com.scottlogic.deg.profile.reader.InvalidProfileException; -import org.jetbrains.annotations.Nullable; - -import java.util.stream.Collectors; - -abstract class AtomicConstraintFactory { - - private final FileReader fileReader; - - AtomicConstraintFactory(FileReader fileReader) { - this.fileReader = fileReader; - } - - Constraint readAtomicConstraintDto(AtomicConstraintDTO dto, ProfileFields profileFields) { - Field field = profileFields.getByName(dto.field); - switch (dto.getType()) { - case EQUAL_TO: - return getEqualToConstraint((EqualToConstraintDTO) dto, field); - case IN_SET: - return getInSetConstraint((InSetConstraintDTO) dto, field); - case IN_MAP: - return getInMapConstraint((InMapConstraintDTO) dto, profileFields, field); - case MATCHES_REGEX: - return getMatchesRegexConstraint((MatchesRegexConstraintDTO) dto, field); - case CONTAINS_REGEX: - return getContainsRegexConstraint((ContainsRegexConstraintDTO) dto, field); - case OF_LENGTH: - return getOfLengthConstraint((OfLengthConstraintDTO) dto, field); - case SHORTER_THAN: - return getShorterThanConstraint((ShorterThanConstraintDTO) dto, field); - case LONGER_THAN: - return getIsLongerThanConstraint((LongerThanConstraintDTO) dto, field); - case GREATER_THAN: - return getIsGreaterThanConstraint((GreaterThanConstraintDTO) dto, field); - case GREATER_THAN_OR_EQUAL_TO: - return getIsGreaterThanOrEqualToConstraint((GreaterThanOrEqualToConstraintDTO) dto, field); - case LESS_THAN: - return getIsLessThanConstraint((LessThanConstraintDTO) dto, field); - case LESS_THAN_OR_EQUAL_TO: - return getLessThanOrEqualToConstraint((LessThanOrEqualToConstraintDTO) dto, field); - case AFTER: - return getAfterConstraint((AfterConstraintDTO) dto, field); - case AFTER_OR_AT: - return getAfterOrAtConstraint((AfterOrAtConstraintDTO) dto, field); - case BEFORE: - return getBeforeConstraint((BeforeConstraintDTO) dto, field); - case BEFORE_OR_AT: - return getBeforeOrAtConstraint((BeforeOrAtConstraintDTO) dto, field); - case GRANULAR_TO: - return getGranularToConstraint((GranularToConstraintDTO) dto, field); - case IS_NULL: - return getIsNullConstraint((NullConstraintDTO) dto, profileFields); - default: - throw new InvalidProfileException("Atomic constraint type not found: " + dto); - } - } - - Constraint getIsNullConstraint(NullConstraintDTO dto, ProfileFields profileFields) { - IsNullConstraint isNullConstraint = new IsNullConstraint(profileFields.getByName(dto.field)); - return dto.isNull - ? isNullConstraint - : isNullConstraint.negate(); - } - - Constraint getInSetConstraint(InSetConstraintDTO dto, Field field) { - return new IsInSetConstraint(field, prepareValuesForSet(dto, field)); - } - - EqualToConstraint getEqualToConstraint(EqualToConstraintDTO dto, Field field){ - return new EqualToConstraint(field, readAnyType(field, dto.value)); - } - - Constraint getGranularToConstraint(GranularToConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("GranularTo Constraint is not supported with type " + field.getType()); - } - - Constraint getBeforeOrAtConstraint(BeforeOrAtConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("beforeOrAt Constraint is not supported with type " + field.getType()); - } - - Constraint getBeforeConstraint(BeforeConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("before Constraint is not supported with type " + field.getType()); - } - - Constraint getAfterOrAtConstraint(AfterOrAtConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("afterOrAt Constraint is not supported with type " + field.getType()); - } - - Constraint getAfterConstraint(AfterConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("after Constraint is not supported with type " + field.getType()); - } - - Constraint getLessThanOrEqualToConstraint(LessThanOrEqualToConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("lessThanOrEqualTo Constraint is not supported with type " + field.getType()); - } - - Constraint getIsLessThanConstraint(LessThanConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("lessThan Constraint is not supported with type " + field.getType()); - } - - Constraint getIsGreaterThanOrEqualToConstraint(GreaterThanOrEqualToConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("isGreaterThanOrEqualTo Constraint is not supported with type " + field.getType()); - } - - Constraint getIsGreaterThanConstraint(GreaterThanConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("greaterThan Constraint is not supported with type " + field.getType()); - } - - Constraint getIsLongerThanConstraint(LongerThanConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("longerThan Constraint is not supported with type " + field.getType()); - } - - Constraint getShorterThanConstraint(ShorterThanConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("shorterThan Constraint is not supported with type " + field.getType()); - } - - Constraint getOfLengthConstraint(OfLengthConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("ofLength Constraint is not supported with type " + field.getType()); - } - - Constraint getContainsRegexConstraint(ContainsRegexConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("containsRegex Constraint is not supported with type " + field.getType()); - } - - Constraint getMatchesRegexConstraint(MatchesRegexConstraintDTO dto, Field field) { - throw new UnsupportedOperationException("matchesRegex Constraint is not supported with type " + field.getType()); - } - - Constraint getInMapConstraint(InMapConstraintDTO dto, ProfileFields profileFields, Field field) { - return new InMapRelation(field, profileFields.getByName(dto.file), - DistributedList.uniform(fileReader.listFromMapFile(dto.file, dto.key).stream() - .map(value -> readAnyType(field, value)) - .collect(Collectors.toList()))); - } - - - @Nullable - private Object readAnyType(Field field, Object value) { - switch (field.getType()) { - case DATETIME: - return HelixDateTime.create((String) value).getValue(); - case NUMERIC: - return NumberUtils.coerceToBigDecimal(value); - default: - return value; - } - } - - private DistributedList prepareValuesForSet(InSetConstraintDTO inSetConstraintDTO, Field field) { - return (inSetConstraintDTO instanceof InSetFromFileConstraintDTO - ? fileReader.setFromFile(((InSetFromFileConstraintDTO) inSetConstraintDTO).file) - : DistributedList.uniform(((InSetOfValuesConstraintDTO) inSetConstraintDTO).values.stream() - .distinct() - .map(o -> readAnyType(field, o)) - .collect(Collectors.toList()))); - } -} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/DateTimeConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/DateTimeConstraintFactory.java deleted file mode 100644 index 6c54ac7b8..000000000 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/DateTimeConstraintFactory.java +++ /dev/null @@ -1,62 +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.deg.profile.reader.AtomicConstraintFactory; - -import com.google.inject.Inject; -import com.scottlogic.deg.common.profile.DateTimeGranularity; -import com.scottlogic.deg.common.profile.Field; -import com.scottlogic.deg.common.profile.HelixDateTime; -import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.*; -import com.scottlogic.deg.profile.dtos.constraints.*; -import com.scottlogic.deg.profile.reader.FileReader; -import org.jetbrains.annotations.NotNull; - -class DateTimeConstraintFactory extends AtomicConstraintFactory { - - @Inject - DateTimeConstraintFactory(FileReader fileReader) { - super(fileReader); - } - - @Override - Constraint getAfterOrAtConstraint(AfterOrAtConstraintDTO dto, Field field) { - return new IsAfterOrEqualToConstantDateTimeConstraint(field, HelixDateTime.create(dto.value)); - } - - @Override - Constraint getAfterConstraint(AfterConstraintDTO dto, Field field) { - return new IsAfterConstantDateTimeConstraint(field, HelixDateTime.create(dto.value)); - } - - @Override - Constraint getBeforeOrAtConstraint(BeforeOrAtConstraintDTO dto, Field field) { - return new IsBeforeOrEqualToConstantDateTimeConstraint(field, HelixDateTime.create(dto.value)); - } - - @Override - Constraint getBeforeConstraint(BeforeConstraintDTO dto, Field field) { - return new IsBeforeConstantDateTimeConstraint(field, HelixDateTime.create(dto.value)); - } - - @Override - Constraint getGranularToConstraint(GranularToConstraintDTO dto, Field field) { - return new IsGranularToDateConstraint(field, DateTimeGranularity.create((String) dto.value)); - } - -} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/NumericConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/NumericConstraintFactory.java deleted file mode 100644 index a5e98983f..000000000 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/NumericConstraintFactory.java +++ /dev/null @@ -1,61 +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.deg.profile.reader.AtomicConstraintFactory; - -import com.google.inject.Inject; -import com.scottlogic.deg.common.profile.Field; -import com.scottlogic.deg.common.profile.HelixNumber; -import com.scottlogic.deg.common.profile.NumericGranularity; -import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.*; -import com.scottlogic.deg.profile.dtos.constraints.*; -import com.scottlogic.deg.profile.reader.FileReader; -import org.jetbrains.annotations.NotNull; - -class NumericConstraintFactory extends AtomicConstraintFactory { - - @Inject - NumericConstraintFactory(FileReader fileReader) { - super(fileReader); - } - - @Override - Constraint getGranularToConstraint(GranularToConstraintDTO dto, Field field) { - return new IsGranularToNumericConstraint(field, NumericGranularity.create(dto.value)); - } - - @Override - Constraint getLessThanOrEqualToConstraint(LessThanOrEqualToConstraintDTO dto, Field field) { - return new IsLessThanOrEqualToConstantConstraint(field, HelixNumber.create(dto.value)); - } - - @Override - Constraint getIsLessThanConstraint(LessThanConstraintDTO dto, Field field) { - return new IsLessThanConstantConstraint(field, HelixNumber.create(dto.value)); - } - - @Override - Constraint getIsGreaterThanOrEqualToConstraint(GreaterThanOrEqualToConstraintDTO dto, Field field) { - return new IsGreaterThanOrEqualToConstantConstraint(field, HelixNumber.create(dto.value)); - } - - @Override - Constraint getIsGreaterThanConstraint(GreaterThanConstraintDTO dto, Field field) { - return new IsGreaterThanConstantConstraint(field, HelixNumber.create(dto.value)); - } -} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/StringConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/StringConstraintFactory.java deleted file mode 100644 index 4f65e6f47..000000000 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/StringConstraintFactory.java +++ /dev/null @@ -1,73 +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.deg.profile.reader.AtomicConstraintFactory; - -import com.google.inject.Inject; -import com.scottlogic.deg.common.profile.Field; -import com.scottlogic.deg.common.profile.HelixStringLength; -import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.*; -import com.scottlogic.deg.profile.dtos.constraints.*; -import com.scottlogic.deg.profile.reader.FileReader; -import com.scottlogic.deg.profile.reader.InvalidProfileException; -import org.jetbrains.annotations.NotNull; - -import java.util.regex.Pattern; - -class StringConstraintFactory extends AtomicConstraintFactory { - - @Inject - StringConstraintFactory(FileReader fileReader) { - super(fileReader); - } - - @Override - Constraint getIsLongerThanConstraint(LongerThanConstraintDTO dto, Field field) { - return new IsStringLongerThanConstraint(field, HelixStringLength.create(dto.value)); - } - - @Override - Constraint getShorterThanConstraint(ShorterThanConstraintDTO dto, Field field) { - return new IsStringShorterThanConstraint(field, HelixStringLength.create(dto.value)); - } - - @Override - Constraint getOfLengthConstraint(OfLengthConstraintDTO dto, Field field) { - return new StringHasLengthConstraint(field, HelixStringLength.create(dto.value)); - } - - @Override - Constraint getContainsRegexConstraint(ContainsRegexConstraintDTO dto, Field field) { - return new ContainsRegexConstraint(field, readPattern(dto.value)); - } - - @Override - Constraint getMatchesRegexConstraint(MatchesRegexConstraintDTO dto, Field field) { - return new MatchesRegexConstraint(field, readPattern(dto.value)); - } - - private Pattern readPattern(Object value) { - if (value instanceof Pattern) return (Pattern) value; - try { - return Pattern.compile((String) value); - } catch (IllegalArgumentException e) { - throw new InvalidProfileException(e.getMessage()); - } - } -} - diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/ConstraintReader.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/ConstraintReader.java deleted file mode 100644 index 077cd66d8..000000000 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/ConstraintReader.java +++ /dev/null @@ -1,124 +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.deg.profile.reader; - -import com.google.inject.Inject; -import com.scottlogic.deg.common.profile.*; -import com.scottlogic.deg.common.util.defaults.DateTimeDefaults; -import com.scottlogic.deg.common.util.defaults.NumericDefaults; -import com.scottlogic.deg.generator.fieldspecs.relations.*; -import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.grammatical.AndConstraint; -import com.scottlogic.deg.generator.profile.constraints.grammatical.ConditionalConstraint; -import com.scottlogic.deg.generator.profile.constraints.grammatical.OrConstraint; -import com.scottlogic.deg.profile.dtos.constraints.*; -import com.scottlogic.deg.profile.reader.AtomicConstraintFactory.AtomicConstraintReader; - -import java.util.Collection; -import java.util.Set; -import java.util.stream.Collectors; - -public class ConstraintReader { - - private final AtomicConstraintReader atomicConstraintReader; - - @Inject - public ConstraintReader(AtomicConstraintReader atomicConstraintReader) { - this.atomicConstraintReader = atomicConstraintReader; - } - - public Set read(Collection constraints, Fields fields) { - return constraints.stream() - .map(subConstraintDto -> read(subConstraintDto, fields)) - .collect(Collectors.toSet()); - } - - public Constraint read(ConstraintDTO dto, Fields fields) { - if (dto == null) { - throw new InvalidProfileException("Constraint is null"); - } else if (dto instanceof RelationalConstraintDTO) { - return readRelationalConstraintDto((RelationalConstraintDTO) dto, fields); - } else if (dto instanceof AtomicConstraintDTO) - return atomicConstraintReader.readAtomicConstraintDto((AtomicConstraintDTO) dto, fields); - else { - return readGrammaticalConstraintDto(dto, fields); - } - } - - private Constraint readRelationalConstraintDto(RelationalConstraintDTO dto, Fields fields) { - Field main = fields.getByName(dto.field); - Field other = fields.getByName(dto.getOtherField()); - switch (dto.getType()) { - case EQUAL_TO_FIELD: - Granularity offsetGranularity = readGranularity(main.getType(), dto.offsetUnit); - return offsetGranularity != null - ? new EqualToOffsetRelation(main, other, offsetGranularity, dto.offset) - : new EqualToRelation(main, other); - case AFTER_FIELD: - return new AfterRelation(main, other, false, DateTimeDefaults.get()); - case AFTER_OR_AT_FIELD: - return new AfterRelation(main, other, true, DateTimeDefaults.get()); - case BEFORE_FIELD: - return new BeforeRelation(main, other, false, DateTimeDefaults.get()); - case BEFORE_OR_AT_FIELD: - return new BeforeRelation(main, other, true, DateTimeDefaults.get()); - case GREATER_THAN_FIELD: - return new AfterRelation(main, other, false, NumericDefaults.get()); - case GREATER_THAN_OR_EQUAL_TO_FIELD: - return new AfterRelation(main, other, true, NumericDefaults.get()); - case LESS_THAN_FIELD: - return new BeforeRelation(main, other, false, NumericDefaults.get()); - case LESS_THAN_OR_EQUAL_TO_FIELD: - return new BeforeRelation(main, other, true, NumericDefaults.get()); - default: - throw new InvalidProfileException("Unexpected relation data type " + dto.getType()); - } - } - - private Constraint readGrammaticalConstraintDto(ConstraintDTO dto, Fields fields) { - switch (dto.getType()) { - case ALL_OF: - return new AndConstraint(read(((AllOfConstraintDTO) dto).constraints, fields)); - case ANY_OF: - return new OrConstraint(read(((AnyOfConstraintDTO) dto).constraints, fields)); - case IF: - IfConstraintDTO conditionalConstraintDTO = (IfConstraintDTO) dto; - Constraint ifConstraint = read(conditionalConstraintDTO.ifConstraint, fields); - Constraint thenConstraint = read(conditionalConstraintDTO.thenConstraint, fields); - Constraint elseConstraint = conditionalConstraintDTO.elseConstraint == null ? null - : read(conditionalConstraintDTO.elseConstraint, fields); - return new ConditionalConstraint(ifConstraint, thenConstraint, elseConstraint); - case NOT: - return read(((NotConstraintDTO) dto).constraint, fields).negate(); - default: - throw new InvalidProfileException("Grammatical constraint type not found: " + dto); - } - } - - private Granularity readGranularity(FieldType type, String offsetUnit) { - if (offsetUnit == null) return null; - switch (type) { - case NUMERIC: - return NumericGranularity.create(offsetUnit); - case DATETIME: - return DateTimeGranularity.create(offsetUnit); - default: - return null; - } - } - -} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/handlers/CreateConstraintsHandler.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/handlers/CreateConstraintsHandler.java index f728cd567..3f8c2ea1d 100644 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/handlers/CreateConstraintsHandler.java +++ b/profile/src/main/java/com/scottlogic/deg/profile/reader/handlers/CreateConstraintsHandler.java @@ -10,7 +10,6 @@ import com.scottlogic.deg.generator.fieldspecs.relations.*; import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.*; import com.scottlogic.deg.generator.profile.constraints.grammatical.AndConstraint; import com.scottlogic.deg.generator.profile.constraints.grammatical.ConditionalConstraint; import com.scottlogic.deg.generator.profile.constraints.grammatical.GrammaticalConstraint; @@ -19,7 +18,7 @@ import com.scottlogic.deg.profile.dtos.constraints.ConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.InMapConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.NotConstraintDTO; -import com.scottlogic.deg.profile.dtos.constraints.atomic.*; +import com.scottlogic.deg.profile.dtos.constraints.atomic.AtomicConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.grammatical.AllOfConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.grammatical.AnyOfConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.grammatical.ConditionalConstraintDTO; @@ -28,20 +27,22 @@ import com.scottlogic.deg.profile.dtos.constraints.relations.RelationalConstraintDTO; import com.scottlogic.deg.profile.reader.FileReader; import com.scottlogic.deg.profile.reader.commands.CreateConstraints; +import com.scottlogic.deg.profile.reader.services.constraints.atomic.AtomicConstraintService; import java.util.Collections; import java.util.List; -import java.util.regex.Pattern; import java.util.stream.Collectors; public class CreateConstraintsHandler extends CommandHandler> { private final FileReader fileReader; + private final AtomicConstraintService atomicConstraintService; public CreateConstraintsHandler(FileReader fileReader, Validator validator) { super(validator); this.fileReader = fileReader; + this.atomicConstraintService = new AtomicConstraintService(fileReader); } @Override @@ -78,7 +79,7 @@ private Constraint createConstraint(ConstraintDTO dto, Fields fields) } if (dto instanceof AtomicConstraintDTO) { - return createAtomicConstraint((AtomicConstraintDTO) dto, fields); + return atomicConstraintService.create((AtomicConstraintDTO) dto, fields); } if (dto instanceof GrammaticalConstraintDTO) { @@ -133,52 +134,7 @@ private GrammaticalConstraint createGrammaticalConstraint(GrammaticalConstraintD } } - private AtomicConstraint createAtomicConstraint(AtomicConstraintDTO dto, Fields fields) - { - Field field = fields.getByName(dto.field); - switch (dto.getType()) - { - case EQUAL_TO: - return new EqualToConstraint(field, parseGenericValue(field, ((EqualToConstraintDTO) dto).value)); - case IN_SET: - return createInSetConstraint((InSetConstraintDTO) dto, field); - case MATCHES_REGEX: - return new MatchesRegexConstraint(field, createPattern(((MatchesRegexConstraintDTO) dto).value)); - case CONTAINS_REGEX: - return new ContainsRegexConstraint(field, createPattern(((ContainsRegexConstraintDTO) dto).value)); - case OF_LENGTH: - return new StringHasLengthConstraint(field, HelixStringLength.create(((OfLengthConstraintDTO) dto).value)); - case SHORTER_THAN: - return new IsStringShorterThanConstraint(field, HelixStringLength.create(((ShorterThanConstraintDTO) dto).value)); - case LONGER_THAN: - return new IsStringLongerThanConstraint(field, HelixStringLength.create(((LongerThanConstraintDTO) dto).value)); - case GREATER_THAN: - return new GreaterThanConstraint(field, HelixNumber.create(((GreaterThanConstraintDTO) dto).value)); - case GREATER_THAN_OR_EQUAL_TO: - return new GreaterThanOrEqualToConstraint(field, HelixNumber.create(((GreaterThanOrEqualToConstraintDTO) dto).value)); - case LESS_THAN: - return new LessThanConstraint(field, HelixNumber.create(((LessThanConstraintDTO) dto).value)); - case LESS_THAN_OR_EQUAL_TO: - return new IsLessThanOrEqualToConstantConstraint(field, HelixNumber.create(((LessThanOrEqualToConstraintDTO) dto).value)); - case AFTER: - return new AfterConstraint(field, HelixDateTime.create(((AfterConstraintDTO) dto).value)); - case AFTER_OR_AT: - return new AfterOrAtConstraint(field, HelixDateTime.create(((AfterOrAtConstraintDTO) dto).value)); - case BEFORE: - return new BeforeConstraint(field, HelixDateTime.create(((BeforeConstraintDTO) dto).value)); - case BEFORE_OR_AT: - return new BeforeOrAtConstraint(field, HelixDateTime.create(((BeforeOrAtConstraintDTO) dto).value)); - case GRANULAR_TO: - return createGranularToRelation((GranularToConstraintDTO) dto, field); - case IS_NULL: - IsNullConstraint isNullConstraint = new IsNullConstraint(fields.getByName(((NullConstraintDTO) dto).field)); - return ((NullConstraintDTO) dto).isNull - ? isNullConstraint - : isNullConstraint.negate(); - default: - throw new IllegalStateException("Unexpected atomic constraint type: " + dto.getType()); - } - } + private InMapRelation createInMapRelation(InMapConstraintDTO dto, Fields fields) { @@ -217,48 +173,8 @@ private ConditionalConstraint createConditionalConstraint(ConditionalConstraintD return new ConditionalConstraint(ifConstraint, thenConstraint, elseConstraint); } - private InSetConstraint createInSetConstraint(InSetConstraintDTO dto, Field field) - { - if (dto instanceof InSetFromFileConstraintDTO) - { - return createInSetConstraint((InSetFromFileConstraintDTO) dto, field); - } - if (dto instanceof InSetOfValuesConstraintDTO) - { - return createInSetConstraint((InSetOfValuesConstraintDTO) dto, field); - } - throw new IllegalStateException("Unexpected value: " + dto.getType()); - } - - private InSetConstraint createInSetConstraint(InSetFromFileConstraintDTO dto, Field field) - { - return new InSetConstraint(field, fileReader.setFromFile(dto.file)); - } - - private InSetConstraint createInSetConstraint(InSetOfValuesConstraintDTO dto, Field field) - { - DistributedList values = DistributedList.uniform((dto.values.stream() - .distinct() - .map(o -> parseGenericValue(field, o)) - .collect(Collectors.toList()))); - - return new InSetConstraint(field, values); - } - - private AtomicConstraint createGranularToRelation(GranularToConstraintDTO dto, Field field) - { - switch (field.getType()) - { - case NUMERIC: - return new GranularToNumericConstraint(field, NumericGranularity.create(dto.value)); - case DATETIME: - return new GranularToDateConstraint(field, DateTimeGranularity.create((String) dto.value)); - default: - throw new IllegalStateException("Unexpected value: " + field.getType()); - } - } - private Object parseGenericValue(Field field, Object value) + private Object parseGenericValue(Field field, Object value) { switch (field.getType()) { @@ -270,9 +186,4 @@ private Object parseGenericValue(Field field, Object value) return value; } } - - private Pattern createPattern(Object value) - { - return value instanceof Pattern ? (Pattern) value : Pattern.compile((String) value); - } } diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/AtomicConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/AtomicConstraintFactory.java new file mode 100644 index 000000000..6873ab1c7 --- /dev/null +++ b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/AtomicConstraintFactory.java @@ -0,0 +1,102 @@ +package com.scottlogic.deg.profile.reader.services.constraints.atomic; + +import com.scottlogic.deg.common.profile.Field; +import com.scottlogic.deg.common.profile.Fields; +import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; +import com.scottlogic.deg.generator.profile.constraints.atomic.*; +import com.scottlogic.deg.profile.dtos.constraints.atomic.*; +import com.scottlogic.deg.profile.reader.FileReader; +import com.scottlogic.deg.profile.reader.InvalidProfileException; + +import java.util.stream.Collectors; + +abstract class AtomicConstraintFactory { + + private final FileReader fileReader; + + AtomicConstraintFactory(FileReader fileReader) { + this.fileReader = fileReader; + } + + AtomicConstraint createAtomicConstraint(AtomicConstraintDTO dto, Fields fields) { + Field field = fields.getByName(dto.field); + switch (dto.getType()) { + case EQUAL_TO: + return new EqualToConstraint(field, parseValue(((EqualToConstraintDTO)dto).value)); + case IN_SET: + return createInSetConstraint((InSetConstraintDTO) dto, field); + case MATCHES_REGEX: + return createMatchesRegexConstraint((MatchesRegexConstraintDTO) dto, field); + case CONTAINS_REGEX: + return createContainsRegexConstraint((ContainsRegexConstraintDTO) dto, field); + case OF_LENGTH: + return createOfLengthConstraint((OfLengthConstraintDTO) dto, field); + case SHORTER_THAN: + return createShorterThanConstraint((ShorterThanConstraintDTO) dto, field); + case LONGER_THAN: + return createLongerThanConstraint((LongerThanConstraintDTO) dto, field); + case GREATER_THAN: + return createGreaterThanConstraint((GreaterThanConstraintDTO) dto, field); + case GREATER_THAN_OR_EQUAL_TO: + return createGreaterThanOrEqualToConstraint((GreaterThanOrEqualToConstraintDTO) dto, field); + case LESS_THAN: + return createLessThanConstraint((LessThanConstraintDTO) dto, field); + case LESS_THAN_OR_EQUAL_TO: + return createLessThanOrEqualToConstraint((LessThanOrEqualToConstraintDTO) dto, field); + case AFTER: + return createAfterConstraint((AfterConstraintDTO) dto, field); + case AFTER_OR_AT: + return createAfterOrAtConstraint((AfterOrAtConstraintDTO) dto, field); + case BEFORE: + return createBeforeConstraint((BeforeConstraintDTO) dto, field); + case BEFORE_OR_AT: + return createBeforeOrAtConstraint((BeforeOrAtConstraintDTO) dto, field); + case GRANULAR_TO: + return createGranularToConstraint((GranularToConstraintDTO) dto, field); + case IS_NULL: + return createIsNullConstraint((IsNullConstraintDTO) dto, fields); + default: + throw new InvalidProfileException("Atomic constraint type not found: " + dto); + } + } + + abstract Object parseValue(Object value); + abstract MatchesRegexConstraint createMatchesRegexConstraint(MatchesRegexConstraintDTO dto, Field field); + abstract ContainsRegexConstraint createContainsRegexConstraint(ContainsRegexConstraintDTO dto, Field field); + abstract OfLengthConstraint createOfLengthConstraint(OfLengthConstraintDTO dto, Field field); + abstract ShorterThanConstraint createShorterThanConstraint(ShorterThanConstraintDTO dto, Field field); + abstract LongerThanConstraint createLongerThanConstraint(LongerThanConstraintDTO dto, Field field); + abstract GreaterThanConstraint createGreaterThanConstraint(GreaterThanConstraintDTO dto, Field field); + abstract GreaterThanOrEqualToConstraint createGreaterThanOrEqualToConstraint(GreaterThanOrEqualToConstraintDTO dto, Field field); + abstract LessThanConstraint createLessThanConstraint(LessThanConstraintDTO dto, Field field); + abstract LessThanOrEqualToConstraint createLessThanOrEqualToConstraint(LessThanOrEqualToConstraintDTO dto, Field field); + abstract AfterConstraint createAfterConstraint(AfterConstraintDTO dto, Field field); + abstract AfterOrAtConstraint createAfterOrAtConstraint(AfterOrAtConstraintDTO dto, Field field); + abstract BeforeConstraint createBeforeConstraint(BeforeConstraintDTO dto, Field field); + abstract BeforeOrAtConstraint createBeforeOrAtConstraint(BeforeOrAtConstraintDTO dto, Field field); + abstract AtomicConstraint createGranularToConstraint(GranularToConstraintDTO dto, Field field); + + private InSetConstraint createInSetConstraint(InSetConstraintDTO dto, Field field) + { + if (dto instanceof InSetFromFileConstraintDTO) + { + return new InSetConstraint(field, fileReader.setFromFile(((InSetFromFileConstraintDTO) dto).file)); + } + if (dto instanceof InSetOfValuesConstraintDTO) + { + DistributedList values = DistributedList.uniform(((InSetOfValuesConstraintDTO) dto).values.stream() + .distinct() + .map(this::parseValue) + .collect(Collectors.toList())); + + return new InSetConstraint(field, values); + } + throw new IllegalStateException("Unexpected value: " + dto.getType()); + } + + private AtomicConstraint createIsNullConstraint(IsNullConstraintDTO dto, Fields fields) + { + IsNullConstraint isNullConstraint = new IsNullConstraint(fields.getByName(dto.field)); + return dto.isNull ? isNullConstraint : isNullConstraint.negate(); + } +} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/AtomicConstraintReader.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/AtomicConstraintService.java similarity index 52% rename from profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/AtomicConstraintReader.java rename to profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/AtomicConstraintService.java index d82a986da..37ded07ee 100644 --- a/profile/src/main/java/com/scottlogic/deg/profile/reader/AtomicConstraintFactory/AtomicConstraintReader.java +++ b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/AtomicConstraintService.java @@ -14,40 +14,28 @@ * limitations under the License. */ -package com.scottlogic.deg.profile.reader.AtomicConstraintFactory; +package com.scottlogic.deg.profile.reader.services.constraints.atomic; import com.google.inject.Inject; -import com.scottlogic.deg.common.profile.*; -import com.scottlogic.deg.common.util.NumberUtils; -import com.scottlogic.deg.generator.fieldspecs.relations.InMapRelation; -import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; -import com.scottlogic.deg.generator.profile.constraints.Constraint; -import com.scottlogic.deg.generator.profile.constraints.atomic.*; -import com.scottlogic.deg.profile.dtos.constraints.*; -import com.scottlogic.deg.profile.reader.AtomicConstraintFactory.AtomicConstraintFactory; -import com.scottlogic.deg.profile.reader.AtomicConstraintFactory.StringConstraintFactory; +import com.scottlogic.deg.common.profile.FieldType; +import com.scottlogic.deg.common.profile.Fields; +import com.scottlogic.deg.generator.profile.constraints.atomic.AtomicConstraint; +import com.scottlogic.deg.profile.dtos.constraints.atomic.AtomicConstraintDTO; import com.scottlogic.deg.profile.reader.FileReader; -import com.scottlogic.deg.profile.reader.InvalidProfileException; -import org.jetbrains.annotations.Nullable; - -import javax.activation.UnsupportedDataTypeException; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -public class AtomicConstraintReader { +public class AtomicConstraintService +{ private final FileReader fileReader; @Inject - public AtomicConstraintReader(FileReader fileReader) { + public AtomicConstraintService(FileReader fileReader) { this.fileReader = fileReader; } - public Constraint readAtomicConstraintDto(AtomicConstraintDTO dto, ProfileFields profileFields) { - + public AtomicConstraint create(AtomicConstraintDTO dto, Fields fields) + { AtomicConstraintFactory constraintFactory; - - FieldType fieldType = profileFields.getByName(dto.field).getType(); + FieldType fieldType = fields.getByName(dto.field).getType(); switch(fieldType) { case STRING: constraintFactory = new StringConstraintFactory(fileReader); @@ -61,6 +49,6 @@ public Constraint readAtomicConstraintDto(AtomicConstraintDTO dto, ProfileFields default: throw new UnsupportedOperationException("No constraint factory for type " + fieldType); } - return constraintFactory.readAtomicConstraintDto(dto, profileFields); + return constraintFactory.createAtomicConstraint(dto, fields); } } diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/DateTimeConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/DateTimeConstraintFactory.java new file mode 100644 index 000000000..4db3c4473 --- /dev/null +++ b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/DateTimeConstraintFactory.java @@ -0,0 +1,120 @@ +/* + * 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.deg.profile.reader.services.constraints.atomic; + +import com.google.inject.Inject; +import com.scottlogic.deg.common.profile.DateTimeGranularity; +import com.scottlogic.deg.common.profile.Field; +import com.scottlogic.deg.common.profile.HelixDateTime; +import com.scottlogic.deg.generator.profile.constraints.atomic.*; +import com.scottlogic.deg.profile.dtos.constraints.atomic.*; +import com.scottlogic.deg.profile.reader.FileReader; + +class DateTimeConstraintFactory extends AtomicConstraintFactory { + + @Inject + DateTimeConstraintFactory(FileReader fileReader) { + super(fileReader); + } + + @Override + Object parseValue(Object value) + { + return HelixDateTime.create((String) value).getValue(); + } + + @Override + MatchesRegexConstraint createMatchesRegexConstraint(MatchesRegexConstraintDTO dto, Field field) + { + return null; + } + + @Override + ContainsRegexConstraint createContainsRegexConstraint(ContainsRegexConstraintDTO dto, Field field) + { + return null; + } + + @Override + OfLengthConstraint createOfLengthConstraint(OfLengthConstraintDTO dto, Field field) + { + return null; + } + + @Override + ShorterThanConstraint createShorterThanConstraint(ShorterThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + LongerThanConstraint createLongerThanConstraint(LongerThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + GreaterThanConstraint createGreaterThanConstraint(GreaterThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + GreaterThanOrEqualToConstraint createGreaterThanOrEqualToConstraint(GreaterThanOrEqualToConstraintDTO dto, Field field) + { + return null; + } + + @Override + LessThanConstraint createLessThanConstraint(LessThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + LessThanOrEqualToConstraint createLessThanOrEqualToConstraint(LessThanOrEqualToConstraintDTO dto, Field field) + { + return null; + } + + @Override + AfterOrAtConstraint createAfterOrAtConstraint(AfterOrAtConstraintDTO dto, Field field) { + return new AfterOrAtConstraint(field, HelixDateTime.create(dto.value)); + } + + @Override + AfterConstraint createAfterConstraint(AfterConstraintDTO dto, Field field) { + return new AfterConstraint(field, HelixDateTime.create(dto.value)); + } + + @Override + BeforeOrAtConstraint createBeforeOrAtConstraint(BeforeOrAtConstraintDTO dto, Field field) { + return new BeforeOrAtConstraint(field, HelixDateTime.create(dto.value)); + } + + @Override + BeforeConstraint createBeforeConstraint(BeforeConstraintDTO dto, Field field) { + return new BeforeConstraint(field, HelixDateTime.create(dto.value)); + } + + @Override + AtomicConstraint createGranularToConstraint(GranularToConstraintDTO dto, Field field) { + return new GranularToDateConstraint(field, DateTimeGranularity.create((String) dto.value)); + } + +} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/NumericConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/NumericConstraintFactory.java new file mode 100644 index 000000000..8839f5737 --- /dev/null +++ b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/NumericConstraintFactory.java @@ -0,0 +1,127 @@ +/* + * 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.deg.profile.reader.services.constraints.atomic; + +import com.google.inject.Inject; +import com.scottlogic.deg.common.profile.Field; +import com.scottlogic.deg.common.profile.HelixNumber; +import com.scottlogic.deg.common.profile.NumericGranularity; +import com.scottlogic.deg.common.util.NumberUtils; +import com.scottlogic.deg.generator.profile.constraints.atomic.*; +import com.scottlogic.deg.profile.dtos.constraints.atomic.*; +import com.scottlogic.deg.profile.reader.FileReader; + +class NumericConstraintFactory extends AtomicConstraintFactory +{ + + @Inject + NumericConstraintFactory(FileReader fileReader) + { + super(fileReader); + } + + @Override + Object parseValue(Object value) + { + return NumberUtils.coerceToBigDecimal(value); + } + + @Override + MatchesRegexConstraint createMatchesRegexConstraint(MatchesRegexConstraintDTO dto, Field field) + { + return null; + } + + @Override + ContainsRegexConstraint createContainsRegexConstraint(ContainsRegexConstraintDTO dto, Field field) + { + return null; + } + + @Override + OfLengthConstraint createOfLengthConstraint(OfLengthConstraintDTO dto, Field field) + { + return null; + } + + @Override + ShorterThanConstraint createShorterThanConstraint(ShorterThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + LongerThanConstraint createLongerThanConstraint(LongerThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + GreaterThanConstraint createGreaterThanConstraint(GreaterThanConstraintDTO dto, Field field) + { + return new GreaterThanConstraint(field, HelixNumber.create(dto.value)); + } + + @Override + GreaterThanOrEqualToConstraint createGreaterThanOrEqualToConstraint(GreaterThanOrEqualToConstraintDTO dto, Field field) + { + return new GreaterThanOrEqualToConstraint(field, HelixNumber.create(dto.value)); + } + + @Override + LessThanConstraint createLessThanConstraint(LessThanConstraintDTO dto, Field field) + { + return new LessThanConstraint(field, HelixNumber.create(dto.value)); + } + + @Override + LessThanOrEqualToConstraint createLessThanOrEqualToConstraint(LessThanOrEqualToConstraintDTO dto, Field field) + { + return new LessThanOrEqualToConstraint(field, HelixNumber.create(dto.value)); + } + + @Override + AfterConstraint createAfterConstraint(AfterConstraintDTO dto, Field field) + { + return null; + } + + @Override + AfterOrAtConstraint createAfterOrAtConstraint(AfterOrAtConstraintDTO dto, Field field) + { + return null; + } + + @Override + BeforeConstraint createBeforeConstraint(BeforeConstraintDTO dto, Field field) + { + return null; + } + + @Override + BeforeOrAtConstraint createBeforeOrAtConstraint(BeforeOrAtConstraintDTO dto, Field field) + { + return null; + } + + @Override + AtomicConstraint createGranularToConstraint(GranularToConstraintDTO dto, Field field) + { + return new GranularToNumericConstraint(field, NumericGranularity.create(dto.value)); + } +} diff --git a/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/StringConstraintFactory.java b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/StringConstraintFactory.java new file mode 100644 index 000000000..3a11b7290 --- /dev/null +++ b/profile/src/main/java/com/scottlogic/deg/profile/reader/services/constraints/atomic/StringConstraintFactory.java @@ -0,0 +1,126 @@ +/* + * 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.deg.profile.reader.services.constraints.atomic; + +import com.google.inject.Inject; +import com.scottlogic.deg.common.profile.Field; +import com.scottlogic.deg.common.profile.HelixStringLength; +import com.scottlogic.deg.generator.profile.constraints.atomic.*; +import com.scottlogic.deg.profile.dtos.constraints.atomic.*; +import com.scottlogic.deg.profile.reader.FileReader; + +import java.util.regex.Pattern; + +class StringConstraintFactory extends AtomicConstraintFactory { + + @Inject + StringConstraintFactory(FileReader fileReader) { + super(fileReader); + } + + @Override + Object parseValue(Object value) + { + return value; + } + + @Override + MatchesRegexConstraint createMatchesRegexConstraint(MatchesRegexConstraintDTO dto, Field field) { + return new MatchesRegexConstraint(field, createPattern(dto.value)); + } + + @Override + ContainsRegexConstraint createContainsRegexConstraint(ContainsRegexConstraintDTO dto, Field field) { + return new ContainsRegexConstraint(field, createPattern(dto.value)); + } + + @Override + OfLengthConstraint createOfLengthConstraint(OfLengthConstraintDTO dto, Field field) { + return new OfLengthConstraint(field, HelixStringLength.create(dto.value)); + } + + @Override + ShorterThanConstraint createShorterThanConstraint(ShorterThanConstraintDTO dto, Field field) { + return new ShorterThanConstraint(field, HelixStringLength.create(dto.value)); + } + + @Override + LongerThanConstraint createLongerThanConstraint(LongerThanConstraintDTO dto, Field field) { + return new LongerThanConstraint(field, HelixStringLength.create(dto.value)); + } + + @Override + GreaterThanConstraint createGreaterThanConstraint(GreaterThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + GreaterThanOrEqualToConstraint createGreaterThanOrEqualToConstraint(GreaterThanOrEqualToConstraintDTO dto, Field field) + { + return null; + } + + @Override + LessThanConstraint createLessThanConstraint(LessThanConstraintDTO dto, Field field) + { + return null; + } + + @Override + LessThanOrEqualToConstraint createLessThanOrEqualToConstraint(LessThanOrEqualToConstraintDTO dto, Field field) + { + return null; + } + + @Override + AfterConstraint createAfterConstraint(AfterConstraintDTO dto, Field field) + { + return null; + } + + @Override + AfterOrAtConstraint createAfterOrAtConstraint(AfterOrAtConstraintDTO dto, Field field) + { + return null; + } + + @Override + BeforeConstraint createBeforeConstraint(BeforeConstraintDTO dto, Field field) + { + return null; + } + + @Override + BeforeOrAtConstraint createBeforeOrAtConstraint(BeforeOrAtConstraintDTO dto, Field field) + { + return null; + } + + @Override + AtomicConstraint createGranularToConstraint(GranularToConstraintDTO dto, Field field) + { + return null; + } + + private Pattern createPattern(Object value) + { + return value instanceof Pattern ? (Pattern) value : Pattern.compile((String) value); + } +} + diff --git a/profile/src/main/java/com/scottlogic/deg/profile/serialisation/ConstraintDeserializer.java b/profile/src/main/java/com/scottlogic/deg/profile/serialisation/ConstraintDeserializer.java index e292a85d8..a79aa9e88 100644 --- a/profile/src/main/java/com/scottlogic/deg/profile/serialisation/ConstraintDeserializer.java +++ b/profile/src/main/java/com/scottlogic/deg/profile/serialisation/ConstraintDeserializer.java @@ -44,7 +44,7 @@ public ConstraintDTO deserialize(JsonParser jsonParser, DeserializationContext c case IN_MAP: return mapper.treeToValue(node, InMapConstraintDTO.class); case IS_NULL: - return mapper.treeToValue(node, NullConstraintDTO.class); + return mapper.treeToValue(node, IsNullConstraintDTO.class); case GRANULAR_TO: return mapper.treeToValue(node, GranularToConstraintDTO.class); case MATCHES_REGEX: diff --git a/profile/src/test/java/com/scottlogic/deg/profile/AtomicConstraintDeserialiserTests.java b/profile/src/test/java/com/scottlogic/deg/profile/AtomicConstraintDeserialiserTests.java index 5c616f7de..146da7b74 100644 --- a/profile/src/test/java/com/scottlogic/deg/profile/AtomicConstraintDeserialiserTests.java +++ b/profile/src/test/java/com/scottlogic/deg/profile/AtomicConstraintDeserialiserTests.java @@ -140,7 +140,7 @@ public void shouldDeserialiseNullWithoutException() throws IOException { ConstraintDTO actual = deserialiseJsonString(json); // Assert - NullConstraintDTO expected = new NullConstraintDTO(); + IsNullConstraintDTO expected = new IsNullConstraintDTO(); expected.field = "country"; expected.isNull = true; diff --git a/profile/src/test/java/com/scottlogic/deg/profile/GrammaticalConstraintDeserialiserTests.java b/profile/src/test/java/com/scottlogic/deg/profile/GrammaticalConstraintDeserialiserTests.java index 06709ee33..fbb76ae69 100644 --- a/profile/src/test/java/com/scottlogic/deg/profile/GrammaticalConstraintDeserialiserTests.java +++ b/profile/src/test/java/com/scottlogic/deg/profile/GrammaticalConstraintDeserialiserTests.java @@ -24,7 +24,7 @@ import com.scottlogic.deg.profile.dtos.constraints.atomic.EqualToConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.atomic.GreaterThanConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.atomic.LessThanConstraintDTO; -import com.scottlogic.deg.profile.dtos.constraints.atomic.NullConstraintDTO; +import com.scottlogic.deg.profile.dtos.constraints.atomic.IsNullConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.grammatical.AllOfConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.grammatical.AnyOfConstraintDTO; import com.scottlogic.deg.profile.dtos.constraints.grammatical.ConditionalConstraintDTO; @@ -56,7 +56,7 @@ public void shouldDeserialiseAnyOfWithoutException() throws IOException { EqualToConstraintDTO expectedEqualsTo = new EqualToConstraintDTO(); expectedEqualsTo.field = "foo"; expectedEqualsTo.value = "0"; - NullConstraintDTO expectedNull = new NullConstraintDTO(); + IsNullConstraintDTO expectedNull = new IsNullConstraintDTO(); expectedNull.field = "foo"; expectedNull.isNull = true; @@ -159,7 +159,7 @@ public void shouldDeserialiseNotWithoutException() throws IOException { ConstraintDTO actual = deserialiseJsonString(json); // Assert - NullConstraintDTO expectedNull = new NullConstraintDTO(); + IsNullConstraintDTO expectedNull = new IsNullConstraintDTO(); expectedNull.field = "foo"; expectedNull.isNull = true; diff --git a/profile/src/test/java/com/scottlogic/deg/profile/reader/JsonProfileReaderTests.java b/profile/src/test/java/com/scottlogic/deg/profile/reader/JsonProfileReaderTests.java index cec997ad7..f9e9cb40c 100644 --- a/profile/src/test/java/com/scottlogic/deg/profile/reader/JsonProfileReaderTests.java +++ b/profile/src/test/java/com/scottlogic/deg/profile/reader/JsonProfileReaderTests.java @@ -361,7 +361,7 @@ public void shouldDeserialiseIsOfLengthConstraint() throws IOException { expectRules( ruleWithConstraints( typedConstraint( - StringHasLengthConstraint.class, + OfLengthConstraint.class, c -> Assert.assertThat(c.referenceValue.getValue(), equalTo(5))))); } @@ -484,7 +484,7 @@ public void shouldDeserialiseIfConstraint() throws IOException { Assert.assertThat( c.whenConditionIsFalse, - instanceOf(IsStringLongerThanConstraint.class)); + instanceOf(LongerThanConstraint.class)); }))); }