diff --git a/common/src/main/java/com/scottlogic/datahelix/generator/common/whitelist/DistributedList.java b/common/src/main/java/com/scottlogic/datahelix/generator/common/distribution/DistributedList.java similarity index 91% rename from common/src/main/java/com/scottlogic/datahelix/generator/common/whitelist/DistributedList.java rename to common/src/main/java/com/scottlogic/datahelix/generator/common/distribution/DistributedList.java index f0ed24b26..7f069c18e 100644 --- a/common/src/main/java/com/scottlogic/datahelix/generator/common/whitelist/DistributedList.java +++ b/common/src/main/java/com/scottlogic/datahelix/generator/common/distribution/DistributedList.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.common.whitelist; +package com.scottlogic.datahelix.generator.common.distribution; import com.scottlogic.datahelix.generator.common.RandomNumberGenerator; @@ -54,16 +54,6 @@ public static DistributedList singleton(final T element) { return DistributedList.uniform(Collections.singleton(element)); } - public static DistributedList weightedOrDefault(final Collection underlyingSet) { - return new DistributedList<>( - underlyingSet.stream() - .map(element -> element instanceof WeightedElement - ? (WeightedElement) element - : WeightedElement.withDefaultWeight(element) - ) - .collect(Collectors.toList())); - } - public static DistributedList uniform(final Collection underlyingSet) { return new DistributedList<>( underlyingSet.stream() diff --git a/common/src/main/java/com/scottlogic/datahelix/generator/common/whitelist/WeightedElement.java b/common/src/main/java/com/scottlogic/datahelix/generator/common/distribution/WeightedElement.java similarity index 92% rename from common/src/main/java/com/scottlogic/datahelix/generator/common/whitelist/WeightedElement.java rename to common/src/main/java/com/scottlogic/datahelix/generator/common/distribution/WeightedElement.java index 9fc461577..a5baeb7b9 100644 --- a/common/src/main/java/com/scottlogic/datahelix/generator/common/whitelist/WeightedElement.java +++ b/common/src/main/java/com/scottlogic/datahelix/generator/common/distribution/WeightedElement.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.common.whitelist; +package com.scottlogic.datahelix.generator.common.distribution; import java.util.Objects; import java.util.function.Function; @@ -51,6 +51,10 @@ public double weight() { return weight; } + public WeightedElement withMappedValue(Function parse) { + return new WeightedElement(parse.apply(element), weight); + } + public static WeightedElement withDefaultWeight(final T element) { return new WeightedElement<>(element, DEFAULT_WEIGHT); } diff --git a/common/src/main/java/com/scottlogic/datahelix/generator/common/profile/InSetRecord.java b/common/src/main/java/com/scottlogic/datahelix/generator/common/profile/InSetRecord.java new file mode 100644 index 000000000..17335de40 --- /dev/null +++ b/common/src/main/java/com/scottlogic/datahelix/generator/common/profile/InSetRecord.java @@ -0,0 +1,85 @@ +/* + * Copyright 2019-2021 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.common.profile; + +import java.util.Objects; +import java.util.function.Function; + +public class InSetRecord { + private static final double DEFAULT_WEIGHT = 1.0; + private final Object element; + private final Double weight; + + public InSetRecord(Object element) { + this(element, null); + } + + public InSetRecord(Object element, double weight) { + this(element, (Double)weight); + } + + public InSetRecord(Object element, Double weight) { + this.element = element; + this.weight = weight; + } + + public Object getElement() { + return element; + } + + public boolean hasWeightPresent() { + return weight != null; + } + + public double getWeightValueOrDefault() { + return weight != null ? weight : DEFAULT_WEIGHT; + } + + public InSetRecord mapValue(Function parse) { + Object value = parse.apply(element); + + return weight != null + ? new InSetRecord(value, weight) + : new InSetRecord(value); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + InSetRecord that = (InSetRecord) o; + + if (!Objects.equals(element, that.element)) { + return false; + } + return weight == null || that.weight == null + ? Objects.equals(weight, that.weight) + : Double.compare(weight, that.weight) == 0; + } + + @Override + public int hashCode() { + return Objects.hash(element, weight); + } + + @Override + public String toString() { + return "InSetRecord{" + + "element=" + element + + ", weight=" + weight + + '}'; + } +} diff --git a/common/src/test/java/com/scottlogic/datahelix/generator/common/whitelist/DistributedListTest.java b/common/src/test/java/com/scottlogic/datahelix/generator/common/distribution/DistributedListTest.java similarity index 87% rename from common/src/test/java/com/scottlogic/datahelix/generator/common/whitelist/DistributedListTest.java rename to common/src/test/java/com/scottlogic/datahelix/generator/common/distribution/DistributedListTest.java index b01c3b8f1..b8453d593 100644 --- a/common/src/test/java/com/scottlogic/datahelix/generator/common/whitelist/DistributedListTest.java +++ b/common/src/test/java/com/scottlogic/datahelix/generator/common/distribution/DistributedListTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.common.whitelist; +package com.scottlogic.datahelix.generator.common.distribution; import com.scottlogic.datahelix.generator.common.RandomNumberGenerator; import org.junit.jupiter.api.Test; @@ -25,7 +25,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.*; +import static com.scottlogic.datahelix.generator.common.distribution.WeightedElement.withDefaultWeight; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -54,8 +56,12 @@ public void testUniformGeneratesUniformDistribution() { DistributedList manualSet = new DistributedList<>(weightedElements); - List elements = Arrays.asList("first", "second", "third"); - DistributedList uniformSet = DistributedList.uniform(elements); + List> elements = Arrays.asList( + withDefaultWeight("first"), + withDefaultWeight("second"), + withDefaultWeight("third") + ); + DistributedList uniformSet = new DistributedList(elements); assertEquals(manualSet, uniformSet); } @@ -78,7 +84,7 @@ public void testWeightedOrDefaultPassesThroughWeightedElements() { DistributedList manualSet = new DistributedList<>(manualElements); List> elements = Arrays.asList(first, second, third); - DistributedList> weightedSet = DistributedList.weightedOrDefault(elements); + DistributedList> weightedSet = new DistributedList(elements); assertEquals(manualSet, weightedSet); } diff --git a/common/src/test/java/com/scottlogic/datahelix/generator/common/whitelist/WeightedElementTest.java b/common/src/test/java/com/scottlogic/datahelix/generator/common/distribution/WeightedElementTest.java similarity index 96% rename from common/src/test/java/com/scottlogic/datahelix/generator/common/whitelist/WeightedElementTest.java rename to common/src/test/java/com/scottlogic/datahelix/generator/common/distribution/WeightedElementTest.java index a8711d489..98a36e3cd 100644 --- a/common/src/test/java/com/scottlogic/datahelix/generator/common/whitelist/WeightedElementTest.java +++ b/common/src/test/java/com/scottlogic/datahelix/generator/common/distribution/WeightedElementTest.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.common.whitelist; +package com.scottlogic.datahelix.generator.common.distribution; import org.junit.jupiter.api.Test; diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpec.java index b18cb9a5a..ff28b446a 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpec.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpec.java @@ -19,9 +19,12 @@ import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.NullAppendingValueSource; +import java.util.Optional; + public abstract class FieldSpec { - public abstract boolean canCombineWithWhitelistValue(Object value); + public abstract boolean canCombineWithLegalValue(Object value); public abstract FieldValueSource getFieldValueSource(); + public abstract Optional merge(FieldSpec other, boolean useFinestGranularityAvailable); public abstract FieldSpec withNotNull(); protected final boolean nullable; diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecFactory.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecFactory.java index 5b900f8e6..580cc5d97 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecFactory.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecFactory.java @@ -16,18 +16,23 @@ package com.scottlogic.datahelix.generator.core.fieldspecs; +import com.scottlogic.datahelix.generator.common.distribution.DistributedList; +import com.scottlogic.datahelix.generator.common.distribution.WeightedElement; import com.scottlogic.datahelix.generator.common.profile.FieldType; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.common.util.Defaults; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; import com.scottlogic.datahelix.generator.core.restrictions.TypedRestrictions; import com.scottlogic.datahelix.generator.core.restrictions.bool.BooleanRestrictions; import java.util.Collections; +import java.util.List; import java.util.function.Predicate; +import java.util.stream.Collectors; import static com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsFactory.*; import static com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictionsFactory.forMaxLength; +import static java.util.Collections.singletonList; public class FieldSpecFactory { private static final NullOnlyFieldSpec NULL_ONLY_FIELD_SPEC = new NullOnlyFieldSpec(); @@ -36,8 +41,29 @@ private FieldSpecFactory() { throw new IllegalArgumentException("Should not instantiate factory"); } - public static WhitelistFieldSpec fromList(DistributedList whitelist) { - return new WhitelistFieldSpec(whitelist, true); + public static LegalValuesFieldSpec fromLegalValuesList(List legalValues) { + return new LegalValuesFieldSpec(legalValues, true); + } + + public static LegalValuesFieldSpec fromSingleLegalValue(Object legalValue) { + return new LegalValuesFieldSpec(singletonList(legalValue), true); + } + + public static FieldSpec fromInSetRecords(List inSetRecords) { + if (inSetRecords.stream().anyMatch(InSetRecord::hasWeightPresent)) { + DistributedList distributedList = new DistributedList<>(inSetRecords.stream() + .map(v -> new WeightedElement<>(v.getElement(), v.getWeightValueOrDefault())) + .collect(Collectors.toList())); + + return new WeightedLegalValuesFieldSpec(distributedList, true); + } else { + List legalValues = inSetRecords.stream().map(InSetRecord::getElement).collect(Collectors.toList()); + return new LegalValuesFieldSpec(legalValues, true); + } + } + + public static WeightedLegalValuesFieldSpec fromList(DistributedList weightedLegalValues) { + return new WeightedLegalValuesFieldSpec(weightedLegalValues, true); } public static RestrictionsFieldSpec fromRestriction(TypedRestrictions restrictions) { diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelper.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelper.java index 40139d213..0bbeaaf7a 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelper.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelper.java @@ -18,15 +18,10 @@ import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; -import static com.scottlogic.datahelix.generator.common.whitelist.DistributedList.singleton; - public class FieldSpecHelper { public FieldSpec getFieldSpecForValue(DataBagValue fieldValue) { - if (fieldValue.getValue() == null) { - return FieldSpecFactory.nullOnly(); - } - - return FieldSpecFactory.fromList(singleton(fieldValue.getValue())) - .withNotNull(); + return (fieldValue.getValue() == null) + ? FieldSpecFactory.nullOnly() + : FieldSpecFactory.fromSingleLegalValue(fieldValue.getValue()).withNotNull(); } } diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecMerger.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecMerger.java index b12b9404b..fb2c5aece 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecMerger.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecMerger.java @@ -16,23 +16,22 @@ package com.scottlogic.datahelix.generator.core.fieldspecs; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; -import com.scottlogic.datahelix.generator.core.restrictions.bool.BooleanRestrictionsMerger; -import com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictionsMerger; -import com.scottlogic.datahelix.generator.core.restrictions.TypedRestrictions; -import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsMerger; -import com.scottlogic.datahelix.generator.common.SetUtils; - +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.Optional; -import java.util.stream.Collectors; /** * Returns a FieldSpec that permits only data permitted by all of its inputs */ public class FieldSpecMerger { - private final RestrictionsMergeOperation restrictionMergeOperation = - new RestrictionsMergeOperation(new LinearRestrictionsMerger(), new StringRestrictionsMerger(), new BooleanRestrictionsMerger()); + private final static List> fieldSpecs = new ArrayList<>(Arrays.asList( + NullOnlyFieldSpec.class, + WeightedLegalValuesFieldSpec.class, + LegalValuesFieldSpec.class, + GeneratorFieldSpec.class, + RestrictionsFieldSpec.class + )); /** * Null parameters are permitted, and are synonymous with an empty FieldSpec @@ -40,107 +39,12 @@ public class FieldSpecMerger { * Returning an empty Optional conveys that the fields were unmergeable. */ public Optional merge(FieldSpec left, FieldSpec right, boolean useFinestGranularityAvailable) { - if (nullOnly(left) || nullOnly(right)){ - return nullOnlyOrEmpty(bothAreNullable(left, right)); - } - - if (hasSet(left) && hasSet(right)) { - return mergeSets((WhitelistFieldSpec) left, (WhitelistFieldSpec)right); - } - if (hasSet(left)) { - return combineSetWithRestrictions((WhitelistFieldSpec)left, right); - } - if (hasSet(right)) { - return combineSetWithRestrictions((WhitelistFieldSpec)right, left); - } - - if (isGenerator(left)) { - if (isGenerator(right)){ - throw new UnsupportedOperationException("generators cannot be combined"); - } - return addNullability(left.isNullable(), right.isNullable(), left); - } - if (isGenerator(right)){ - return addNullability(right.isNullable(), left.isNullable(), right); - } - return combineRestrictions((RestrictionsFieldSpec)left, (RestrictionsFieldSpec)right, useFinestGranularityAvailable); - } - - private static WeightedElement mergeElements(WeightedElement left, - WeightedElement right) { - return new WeightedElement<>(left.element(), left.weight() + right.weight()); - } - - //TODO try a performance test with this replaced with combineSetWithRestrictions() - - private Optional mergeSets(WhitelistFieldSpec left, WhitelistFieldSpec right) { - DistributedList set = new DistributedList<>(left.getWhitelist().distributedList().stream() - .flatMap(leftHolder -> right.getWhitelist().distributedList().stream() - .filter(rightHolder -> elementsEqual(leftHolder, rightHolder)) - .map(rightHolder -> mergeElements(leftHolder, rightHolder))) - .distinct() - .collect(Collectors.toList())); - - FieldSpec newFieldSpec = set.isEmpty() ? FieldSpecFactory.nullOnly() : FieldSpecFactory.fromList(set); - return addNullability(left.isNullable(), right.isNullable(), newFieldSpec); - } - private static boolean elementsEqual(WeightedElement left, WeightedElement right) { - return left.element().equals(right.element()); - } - - private Optional combineSetWithRestrictions(WhitelistFieldSpec set, FieldSpec restrictions) { - DistributedList newSet = new DistributedList<>( - set.getWhitelist().distributedList().stream() - .filter(holder -> restrictions.canCombineWithWhitelistValue(holder.element())) - .distinct() - .collect(Collectors.toList())); - - FieldSpec newSpec = newSet.isEmpty() ? FieldSpecFactory.nullOnly() : FieldSpecFactory.fromList(newSet); - return addNullability(set.isNullable(), restrictions.isNullable(), newSpec); - } - - private Optional addNullability(boolean leftIsNullable, boolean rightIsNullable, FieldSpec newFieldSpec) { - if (leftIsNullable && rightIsNullable) { - return Optional.of(newFieldSpec); - } + int leftOrder = fieldSpecs.indexOf(left.getClass()); + int rightOrder = fieldSpecs.indexOf(right.getClass()); - if (nullOnly(newFieldSpec)) { - return Optional.empty(); - } - - return Optional.of(newFieldSpec.withNotNull()); - } - - private boolean nullOnly(FieldSpec fieldSpec) { - return (fieldSpec instanceof NullOnlyFieldSpec); - } - - private boolean isGenerator(FieldSpec fieldSpec) { - return fieldSpec instanceof GeneratorFieldSpec; - } - - private boolean hasSet(FieldSpec fieldSpec) { - return fieldSpec instanceof WhitelistFieldSpec; + return leftOrder <= rightOrder + ? left.merge(right, useFinestGranularityAvailable) + : right.merge(left, useFinestGranularityAvailable); } - private boolean bothAreNullable(FieldSpec left, FieldSpec right) { - return left.isNullable() && right.isNullable(); - } - - private Optional combineRestrictions(RestrictionsFieldSpec left, RestrictionsFieldSpec right, boolean useFinestGranularityAvailable) { - Optional restrictions = restrictionMergeOperation.applyMergeOperation(left.getRestrictions(), right.getRestrictions(), useFinestGranularityAvailable); - - if (!restrictions.isPresent()){ - return nullOnlyOrEmpty(bothAreNullable(left, right)); - } - - RestrictionsFieldSpec merged = FieldSpecFactory.fromRestriction(restrictions.get()); - merged = merged.withBlacklist(SetUtils.union(left.getBlacklist(), right.getBlacklist())); - - return addNullability(left.isNullable(), right.isNullable(), merged); - } - - private Optional nullOnlyOrEmpty(boolean nullable) { - return nullable ? Optional.of(FieldSpecFactory.nullOnly()) : Optional.empty(); - } } diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/GeneratorFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/GeneratorFieldSpec.java index 011daee39..554051a9c 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/GeneratorFieldSpec.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/GeneratorFieldSpec.java @@ -16,21 +16,23 @@ package com.scottlogic.datahelix.generator.core.fieldspecs; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; + +import java.util.Optional; import java.util.function.Predicate; public class GeneratorFieldSpec extends FieldSpec { private final FieldValueSource fieldValueSource; - private final Predicate acceptWhitelistValueFunction; + private final Predicate acceptLegalValueFunction; - public GeneratorFieldSpec(FieldValueSource fieldValueSource, Predicate acceptWhitelistValueFunction, boolean nullable) { + public GeneratorFieldSpec(FieldValueSource fieldValueSource, Predicate acceptLegalValueFunction, boolean nullable) { super(nullable); this.fieldValueSource = fieldValueSource; - this.acceptWhitelistValueFunction = acceptWhitelistValueFunction; + this.acceptLegalValueFunction = acceptLegalValueFunction; } @Override - public boolean canCombineWithWhitelistValue(Object value) { - return acceptWhitelistValueFunction.test(value); + public boolean canCombineWithLegalValue(Object value) { + return acceptLegalValueFunction.test(value); } @Override @@ -38,8 +40,19 @@ public FieldValueSource getFieldValueSource() { return fieldValueSource; } + @Override + public Optional merge(FieldSpec other, boolean useFinestGranularityAvailable) { + final boolean notNullable = !isNullable() || !other.isNullable(); + + if (other instanceof GeneratorFieldSpec) { + throw new UnsupportedOperationException("generators cannot be combined"); + } + + return Optional.of(notNullable ? withNotNull() : this); + } + @Override public FieldSpec withNotNull() { - return new GeneratorFieldSpec(fieldValueSource, acceptWhitelistValueFunction, false); + return new GeneratorFieldSpec(fieldValueSource, acceptLegalValueFunction, false); } } diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/LegalValuesFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/LegalValuesFieldSpec.java new file mode 100644 index 000000000..56a3fc18c --- /dev/null +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/LegalValuesFieldSpec.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.datahelix.generator.core.fieldspecs; + +import com.scottlogic.datahelix.generator.common.distribution.DistributedList; +import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.CannedValuesFieldValueSource; +import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; + +import java.util.Collections; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class LegalValuesFieldSpec extends FieldSpec implements ValuesFieldSpec { + private final List legalValues; + + LegalValuesFieldSpec(List legalValues, boolean nullable) { + super(nullable); + if (legalValues.isEmpty()){ + throw new UnsupportedOperationException("cannot create with empty legalValue"); + } + this.legalValues = Collections.unmodifiableList(legalValues); + } + + @Override + public boolean canCombineWithLegalValue(Object value) { + return legalValues.contains(value); + } + + @Override + public FieldValueSource getFieldValueSource() { + return appendNullSource(new CannedValuesFieldValueSource(DistributedList.uniform(legalValues))); + } + + @Override + public Optional merge(FieldSpec other, boolean useFinestGranularityAvailable) { + final boolean notNullable = !isNullable() || !other.isNullable(); + + final List newSet = other instanceof LegalValuesFieldSpec + ? mergeSets((LegalValuesFieldSpec) other) + : combineSetWithRestrictions(other); + + if (newSet.isEmpty() && notNullable) { + return Optional.empty(); + } + + final FieldSpec newSpec = newSet.isEmpty() ? FieldSpecFactory.nullOnly() : FieldSpecFactory.fromLegalValuesList(newSet); + return Optional.of(notNullable ? newSpec.withNotNull() : newSpec); + } + + private List mergeSets(LegalValuesFieldSpec other) { + List newSet; + newSet = legalValues.stream() + .flatMap(leftHolder -> other.legalValues.stream() + .filter(leftHolder::equals) + ) + .distinct() + .collect(Collectors.toList()); + return newSet; + } + + private List combineSetWithRestrictions(FieldSpec other) { + return legalValues.stream() + .filter(other::canCombineWithLegalValue) + .distinct() + .collect(Collectors.toList()); + } + + @Override + public LegalValuesFieldSpec withNotNull() { + return new LegalValuesFieldSpec(legalValues, false); + } + + @Override + public FieldSpec withMappedValues(Function parse) { + List allowedValuesList = legalValues.stream().map(parse).collect(Collectors.toList()); + + return new LegalValuesFieldSpec(allowedValuesList, nullable); + } + + @Override + public String toString() { + if (legalValues.isEmpty()) { + return "Null only"; + } + return (nullable ? "" : "Not Null ") + String.format("IN %s", legalValues); + } + + public int hashCode() { + return Objects.hash(legalValues, nullable); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + + LegalValuesFieldSpec other = (LegalValuesFieldSpec) obj; + return Objects.equals(legalValues, other.legalValues) + && Objects.equals(nullable, other.nullable); + } +} diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/NullOnlyFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/NullOnlyFieldSpec.java index d64fb8355..55e99bd16 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/NullOnlyFieldSpec.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/NullOnlyFieldSpec.java @@ -19,13 +19,15 @@ import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.NullOnlySource; +import java.util.Optional; + public class NullOnlyFieldSpec extends FieldSpec { NullOnlyFieldSpec() { super(true); } @Override - public boolean canCombineWithWhitelistValue(Object value) { + public boolean canCombineWithLegalValue(Object value) { return false; } @@ -34,8 +36,14 @@ public FieldValueSource getFieldValueSource() { return new NullOnlySource(); } + @Override + public Optional merge(FieldSpec other, boolean useFinestGranularityAvailable) { + return other.isNullable() ? Optional.of(FieldSpecFactory.nullOnly()) : Optional.empty(); + } + @Override public FieldSpec withNotNull() { throw new UnsupportedOperationException("not null on NullOnlyFieldSpec not allowed"); } + } diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/RestrictionsFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/RestrictionsFieldSpec.java index b9bc5d29f..423d1c8e1 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/RestrictionsFieldSpec.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/RestrictionsFieldSpec.java @@ -16,14 +16,22 @@ package com.scottlogic.datahelix.generator.core.fieldspecs; +import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; import com.scottlogic.datahelix.generator.core.restrictions.TypedRestrictions; +import com.scottlogic.datahelix.generator.core.restrictions.bool.BooleanRestrictionsMerger; +import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsMerger; +import com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictionsMerger; import java.util.Collection; import java.util.Objects; +import java.util.Optional; import java.util.Set; public class RestrictionsFieldSpec extends FieldSpec { + private static final RestrictionsMergeOperation restrictionMergeOperation = + new RestrictionsMergeOperation(new LinearRestrictionsMerger(), new StringRestrictionsMerger(), new BooleanRestrictionsMerger()); + private final TypedRestrictions restrictions; private final Set blacklist; @@ -34,7 +42,7 @@ public class RestrictionsFieldSpec extends FieldSpec { } @Override - public boolean canCombineWithWhitelistValue(Object value) { + public boolean canCombineWithLegalValue(Object value) { return !blacklist.contains(value) && restrictions.match(value); } @@ -44,6 +52,23 @@ public FieldValueSource getFieldValueSource() { restrictions.createFieldValueSource(blacklist)); } + @Override + public Optional merge(FieldSpec other, boolean useFinestGranularityAvailable) { + final boolean notNullable = !isNullable() || !other.isNullable(); + + final TypedRestrictions otherRestrictions = ((RestrictionsFieldSpec) other).getRestrictions(); + final Optional restrictions = restrictionMergeOperation.applyMergeOperation(getRestrictions(), otherRestrictions, useFinestGranularityAvailable); + + if (!restrictions.isPresent()) { + return notNullable ? Optional.empty() : Optional.of(FieldSpecFactory.nullOnly()); + } + + final RestrictionsFieldSpec merged = FieldSpecFactory.fromRestriction(restrictions.get()) + .withBlacklist(SetUtils.union(getBlacklist(), ((RestrictionsFieldSpec)other).getBlacklist())); + + return Optional.of(notNullable ? merged.withNotNull(): merged); + } + @Override public FieldSpec withNotNull() { return new RestrictionsFieldSpec(restrictions, false, blacklist); diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/ValuesFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/ValuesFieldSpec.java new file mode 100644 index 000000000..14731642f --- /dev/null +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/ValuesFieldSpec.java @@ -0,0 +1,22 @@ +/* + * Copyright 2019-2021 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.core.fieldspecs; + +import java.util.function.Function; + +public interface ValuesFieldSpec { + FieldSpec withMappedValues(Function parse); +} diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/WeightedLegalValuesFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/WeightedLegalValuesFieldSpec.java new file mode 100644 index 000000000..7b6108f80 --- /dev/null +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/WeightedLegalValuesFieldSpec.java @@ -0,0 +1,111 @@ +/* + * Copyright 2019-2021 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.core.fieldspecs; + +import com.scottlogic.datahelix.generator.common.distribution.DistributedList; +import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.CannedValuesFieldValueSource; +import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; + +import java.util.Objects; +import java.util.Optional; +import java.util.function.Function; +import java.util.stream.Collectors; + +public class WeightedLegalValuesFieldSpec extends FieldSpec implements ValuesFieldSpec { + private final DistributedList legalValues; + + WeightedLegalValuesFieldSpec(DistributedList legalValues, boolean nullable) { + super(nullable); + if (legalValues.isEmpty()){ + throw new UnsupportedOperationException("cannot create with empty legalValues"); + } + this.legalValues = legalValues; + } + + @Override + public boolean canCombineWithLegalValue(Object value) { + return legalValues.list().contains(value); + } + + @Override + public FieldValueSource getFieldValueSource() { + return appendNullSource(new CannedValuesFieldValueSource(legalValues)); + } + + @Override + public Optional merge(FieldSpec other, boolean useFinestGranularityAvailable) { + final boolean notNullable = !isNullable() || !other.isNullable(); + + if (other instanceof WeightedLegalValuesFieldSpec) { + return Optional.empty(); + } + + final DistributedList newSet = new DistributedList<>( + getLegalValues().distributedList().stream() + .filter(holder -> other.canCombineWithLegalValue(holder.element())) + .distinct() + .collect(Collectors.toList())); + + if (newSet.isEmpty() && notNullable) { + return Optional.empty(); + } + + final FieldSpec newSpec = newSet.isEmpty() ? FieldSpecFactory.nullOnly() : FieldSpecFactory.fromList(newSet); + return Optional.of(notNullable ? newSpec.withNotNull() : newSpec); + } + + @Override + public WeightedLegalValuesFieldSpec withNotNull() { + return new WeightedLegalValuesFieldSpec(legalValues, false); + } + + public DistributedList getLegalValues() { + return legalValues; + } + + @Override + public FieldSpec withMappedValues(Function parse) { + DistributedList allowedValuesList = new DistributedList<>(legalValues.distributedList().stream() + .map(value -> value.withMappedValue(parse)).collect(Collectors.toList())); + + return new WeightedLegalValuesFieldSpec(allowedValuesList, nullable); + } + + @Override + public String toString() { + if (legalValues.isEmpty()) { + return "Null only"; + } + return (nullable ? "" : "Not Null ") + String.format("IN %s", legalValues); + } + + public int hashCode() { + return Objects.hash(nullable, legalValues); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || obj.getClass() != this.getClass()) { + return false; + } + + WeightedLegalValuesFieldSpec other = (WeightedLegalValuesFieldSpec) obj; + return Objects.equals(nullable, other.nullable) + && Objects.equals(legalValues, other.legalValues); + } +} diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/WhitelistFieldSpec.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/WhitelistFieldSpec.java deleted file mode 100644 index be7ad07c0..000000000 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/WhitelistFieldSpec.java +++ /dev/null @@ -1,78 +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.core.fieldspecs; - -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.CannedValuesFieldValueSource; -import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; - -import java.util.Objects; - -public class WhitelistFieldSpec extends FieldSpec { - private final DistributedList whitelist; - - WhitelistFieldSpec(DistributedList whitelist, boolean nullable) { - super(nullable); - if (whitelist.isEmpty()){ - throw new UnsupportedOperationException("cannot create with empty whitelist"); - } - this.whitelist = whitelist; - } - - @Override - public boolean canCombineWithWhitelistValue(Object value) { - return whitelist.list().contains(value); - } - - @Override - public FieldValueSource getFieldValueSource() { - return appendNullSource(new CannedValuesFieldValueSource(whitelist)); - } - - @Override - public WhitelistFieldSpec withNotNull() { - return new WhitelistFieldSpec(whitelist, false); - } - - public DistributedList getWhitelist() { - return whitelist; - } - - @Override - public String toString() { - if (whitelist.isEmpty()) { - return "Null only"; - } - return (nullable ? "" : "Not Null ") + String.format("IN %s", whitelist); - } - - public int hashCode() { - return Objects.hash(nullable, whitelist); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) return true; - if (obj == null || obj.getClass() != this.getClass()) { - return false; - } - - WhitelistFieldSpec other = (WhitelistFieldSpec) obj; - return Objects.equals(nullable, other.nullable) - && Objects.equals(whitelist, other.whitelist); - } -} diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/AfterRelation.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/AfterRelation.java index 9a5376114..1927fae02 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/AfterRelation.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/AfterRelation.java @@ -48,7 +48,7 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) { if (otherFieldSpec instanceof NullOnlyFieldSpec) { return FieldSpecFactory.nullOnly(); } - if (otherFieldSpec instanceof WhitelistFieldSpec) { + if (otherFieldSpec instanceof WeightedLegalValuesFieldSpec) { throw new UnsupportedOperationException("cannot combine sets with after relation, Issue #1489"); } diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/BeforeRelation.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/BeforeRelation.java index 0f5566af2..48f0c98c1 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/BeforeRelation.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/BeforeRelation.java @@ -48,7 +48,7 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) { if (otherFieldSpec instanceof NullOnlyFieldSpec){ return FieldSpecFactory.nullOnly(); } - if (otherFieldSpec instanceof WhitelistFieldSpec) { + if (otherFieldSpec instanceof WeightedLegalValuesFieldSpec) { throw new UnsupportedOperationException("cannot combine sets with before relation, Issue #1489"); } diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToOffsetRelation.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToOffsetRelation.java index af6b9cdf6..08db09f68 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToOffsetRelation.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToOffsetRelation.java @@ -19,16 +19,11 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.FieldType; import com.scottlogic.datahelix.generator.common.profile.Granularity; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; import com.scottlogic.datahelix.generator.core.fieldspecs.*; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictions; -import java.util.List; -import java.util.stream.Collectors; - public class EqualToOffsetRelation> implements FieldSpecRelation { private final Field main; private final Field other; @@ -50,15 +45,9 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) { if (otherFieldSpec instanceof NullOnlyFieldSpec) { return FieldSpecFactory.nullOnly(); } - if (otherFieldSpec instanceof WhitelistFieldSpec) { - WhitelistFieldSpec whitelistFieldSpec = (WhitelistFieldSpec) otherFieldSpec; - List> modified = whitelistFieldSpec - .getWhitelist() - .distributedList() - .stream() - .map(x -> new WeightedElement<>(offsetGranularity.getNext((T) x.element(), offset), x.weight())) - .collect(Collectors.toList()); - return FieldSpecFactory.fromList((DistributedList) new DistributedList<>(modified)); + if (otherFieldSpec instanceof ValuesFieldSpec) { + ValuesFieldSpec valuesFieldSpec = (ValuesFieldSpec) otherFieldSpec; + return valuesFieldSpec.withMappedValues(x-> offsetGranularity.getNext((T) x, offset)); } LinearRestrictions otherRestrictions = (LinearRestrictions) ((RestrictionsFieldSpec) otherFieldSpec).getRestrictions(); @@ -82,7 +71,7 @@ public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedVa return FieldSpecFactory.fromType(FieldType.DATETIME); } T offsetValue = offsetGranularity.getNext(value, offset); - return FieldSpecFactory.fromList(DistributedList.singleton(offsetValue)); + return FieldSpecFactory.fromSingleLegalValue(offsetValue); } @Override diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelation.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelation.java index 49f4722cf..7a99da364 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelation.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelation.java @@ -19,7 +19,6 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; @@ -43,7 +42,7 @@ public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedVa if (otherFieldGeneratedValue.getValue() == null){ return FieldSpecFactory.nullOnly(); } - return FieldSpecFactory.fromList(DistributedList.singleton(otherFieldGeneratedValue.getValue())); + return FieldSpecFactory.fromSingleLegalValue(otherFieldGeneratedValue.getValue()); } @Override diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelation.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelation.java index 8c5124823..67cc4b087 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelation.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelation.java @@ -19,7 +19,6 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; @@ -31,9 +30,9 @@ public class InMapIndexRelation implements FieldSpecRelation { private final Field main; private final Field other; - private final DistributedList underlyingList; + private final List underlyingList; - public InMapIndexRelation(Field main, Field other, DistributedList underlyingList) { + public InMapIndexRelation(Field main, Field other, List underlyingList) { this.main = main; this.other = other; this.underlyingList = underlyingList; @@ -41,20 +40,20 @@ public InMapIndexRelation(Field main, Field other, DistributedList under @Override public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) { - List whiteList = new ArrayList<>(); + List legalValuesList = new ArrayList<>(); - for (int i = 0; i < underlyingList.list().size(); i++) { - Object testingElement = underlyingList.list().get(i); - if (otherFieldSpec.canCombineWithWhitelistValue(testingElement)) { - whiteList.add(BigDecimal.valueOf(i)); + for (int i = 0; i < underlyingList.size(); i++) { + Object testingElement = underlyingList.get(i); + if (otherFieldSpec.canCombineWithLegalValue(testingElement)) { + legalValuesList.add(BigDecimal.valueOf(i)); } } - return FieldSpecFactory.fromList(DistributedList.uniform(whiteList)).withNotNull(); + return FieldSpecFactory.fromLegalValuesList(legalValuesList).withNotNull(); } @Override public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedValue) { - throw new UnsupportedOperationException("reduceToFieldSpec is unsuported in InMapIndexRelation"); + throw new UnsupportedOperationException("reduceToFieldSpec is unsupported in InMapIndexRelation"); } @Override @@ -72,10 +71,6 @@ public Field other() { return other; } - public DistributedList getUnderlyingList() { - return this.underlyingList; - } - @Override public Constraint negate() { throw new UnsupportedOperationException("in map relations cannot currently be negated"); diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelation.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelation.java index b5483e43d..1242e3f80 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelation.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelation.java @@ -19,19 +19,19 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; import java.math.BigDecimal; +import java.util.List; public class InMapRelation implements FieldSpecRelation { private final Field main; private final Field other; - private final DistributedList underlyingList; + private final List underlyingList; - public InMapRelation(Field main, Field other, DistributedList underlyingList) { + public InMapRelation(Field main, Field other, List underlyingList) { this.main = main; this.other = other; this.underlyingList = underlyingList; @@ -46,8 +46,7 @@ public FieldSpec createModifierFromOtherFieldSpec(FieldSpec otherFieldSpec) { public FieldSpec createModifierFromOtherValue(DataBagValue otherFieldGeneratedValue) { BigDecimal value = (BigDecimal) otherFieldGeneratedValue.getValue(); - DistributedList newList = DistributedList.singleton(underlyingList.list().get(value.intValue())); - return FieldSpecFactory.fromList(newList); + return FieldSpecFactory.fromSingleLegalValue(underlyingList.get(value.intValue())); } @Override @@ -65,10 +64,6 @@ public Field other() { return other; } - public DistributedList getUnderlyingList() { - return this.underlyingList; - } - @Override public Constraint negate() { throw new UnsupportedOperationException("in map relations cannot currently be negated"); diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSource.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSource.java index 6da4bee7f..b5efe6c02 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSource.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSource.java @@ -16,8 +16,8 @@ package com.scottlogic.datahelix.generator.core.generation.fieldvaluesources; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.common.RandomNumberGenerator; +import com.scottlogic.datahelix.generator.common.distribution.DistributedList; import java.util.Objects; import java.util.stream.Stream; diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/BlacklistConstraint.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/BlacklistConstraint.java deleted file mode 100644 index 47989304f..000000000 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/BlacklistConstraint.java +++ /dev/null @@ -1,82 +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.core.profile.constraints.atomic; - -import com.scottlogic.datahelix.generator.common.profile.Field; -import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; -import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; - -import java.util.HashSet; -import java.util.Objects; -import java.util.stream.Collectors; - -public class BlacklistConstraint implements AtomicConstraint { - public final Field field; - public final DistributedList legalValues; - - public BlacklistConstraint(Field field, DistributedList legalValues) { - this.field = field; - this.legalValues = legalValues; - - if (legalValues.isEmpty()) { - throw new IllegalArgumentException("Cannot create an IsInSetConstraint for field '" + - field.getName() + "' with an empty set"); - } - - if (legalValues.list().contains(null)) { - throw new IllegalArgumentException("Cannot create an IsInSetConstraint for field '" + - field.getName() + "' with a set containing null"); - } - } - - @Override - public Field getField() { - return field; - } - - @Override - public AtomicConstraint negate() { - return new InSetConstraint(field, legalValues); - } - - @Override - public FieldSpec toFieldSpec() { - return FieldSpecFactory.fromType(field.getType()).withBlacklist(new HashSet<>(legalValues.list())); - } - - public String toString(){ - boolean overLimit = legalValues.list().size() > 3; - return String.format("%s in [%s%s](%d values)", - field.getName(), - legalValues.stream().limit(3).map(Object::toString).collect(Collectors.joining(", ")), - overLimit ? ", ..." : "", - legalValues.list().size()); - } - - @Override - public boolean equals(Object o){ - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - BlacklistConstraint constraint = (BlacklistConstraint) o; - return Objects.equals(field, constraint.field) && Objects.equals(legalValues, constraint.legalValues); - } - - @Override - public int hashCode(){ - return Objects.hash(field, legalValues); - } -} diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/EqualToConstraint.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/EqualToConstraint.java index c2a666bb0..7f653bc12 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/EqualToConstraint.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/EqualToConstraint.java @@ -18,7 +18,6 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import java.util.Objects; @@ -43,7 +42,7 @@ public AtomicConstraint negate() { @Override public FieldSpec toFieldSpec() { - return FieldSpecFactory.fromList(DistributedList.singleton(value)); + return FieldSpecFactory.fromSingleLegalValue(value); } @Override diff --git a/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraint.java b/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraint.java index 8582ea320..2028c19c6 100644 --- a/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraint.java +++ b/core/src/main/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraint.java @@ -18,10 +18,9 @@ import com.scottlogic.datahelix.generator.common.ValidationException; import com.scottlogic.datahelix.generator.common.profile.Field; - +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import java.util.List; import java.util.Objects; @@ -29,27 +28,29 @@ public class InSetConstraint implements AtomicConstraint { public final Field field; - public final DistributedList legalValues; + public final List legalValues; + private final boolean negated; + + public InSetConstraint(Field field, List legalValues) { + this(field, legalValues, false); + } - public InSetConstraint(Field field, DistributedList legalValues) { + private InSetConstraint(Field field, List legalValues, boolean negated) { this.field = field; this.legalValues = legalValues; + this.negated = negated; - if (legalValues.distributedList().isEmpty()) { + if (legalValues.isEmpty()) { throw new ValidationException("Cannot create an IsInSetConstraint for field '" + field.getName() + "' with an empty set"); } - if (legalValues.list().contains(null)) { + if (legalValues.stream().map(InSetRecord::getElement).anyMatch(Objects::isNull)) { throw new ValidationException("Cannot create an IsInSetConstraint for field '" + field.getName() + "' with a set containing null"); } } - public List legalValuesWithoutFrequency() { - return legalValues.list(); - } - @Override public Field getField() { return field; @@ -57,29 +58,39 @@ public Field getField() { @Override public AtomicConstraint negate() { - return new BlacklistConstraint(field, legalValues); + return new InSetConstraint(field, legalValues, !negated); } @Override public FieldSpec toFieldSpec() { - return FieldSpecFactory.fromList(legalValues); + if (negated) { + return FieldSpecFactory.fromType(field.getType()) + .withBlacklist(legalValues.stream().map(InSetRecord::getElement).collect(Collectors.toSet())); + } + return FieldSpecFactory.fromInSetRecords(legalValues); } public String toString(){ - boolean overLimit = legalValues.list().size() > 3; - return String.format("%s in [%s%s](%d values)", + boolean overLimit = legalValues.size() > 3; + String legalValuesSummary = legalValues.stream().limit(3) + .map(InSetRecord::getElement) + .map(Object::toString) + .collect(Collectors.joining(", ")); + + return String.format("%s%s in [%s%s](%d values)", field.getName(), - legalValues.stream().limit(3).map(Object::toString).collect(Collectors.joining(", ")), + negated ? "not " : "", + legalValuesSummary, overLimit ? ", ..." : "", - legalValues.list().size()); + legalValues.size()); } @Override - public boolean equals(Object o){ + public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; InSetConstraint constraint = (InSetConstraint) o; - return Objects.equals(field, constraint.field) && Objects.equals(legalValues, constraint.legalValues); + return negated == constraint.negated && Objects.equals(field, constraint.field) && Objects.equals(legalValues, constraint.legalValues); } @Override diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/ConstraintBuilder.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/ConstraintBuilder.java index f4c991ea5..be05df1a7 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/ConstraintBuilder.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/ConstraintBuilder.java @@ -16,15 +16,17 @@ package com.scottlogic.datahelix.generator.core; +import com.scottlogic.datahelix.generator.common.profile.Field; +import com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.IsNullConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.AndConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.ConditionalConstraint; -import com.scottlogic.datahelix.generator.common.profile.Field; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; import java.util.stream.Collectors; public class ConstraintBuilder { @@ -41,14 +43,14 @@ public List build() { public ConstraintBuilder addInSetConstraint(String fieldname, List values) { constraints.add(new InSetConstraint(fields.get(fieldname), - DistributedList.uniform(values))); + TestAtomicConstraintBuilder.inSetRecordsFromList(values))); return this; } public ConstraintBuilder addEqualToConstraint(String fieldname, Object value) { constraints.add(new InSetConstraint( fields.get(fieldname), - DistributedList.singleton(value))); + TestAtomicConstraintBuilder.inSetRecordsFrom(value))); return this; } diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/ConstraintChainBuilder.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/ConstraintChainBuilder.java index 2c254b514..87335089e 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/ConstraintChainBuilder.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/ConstraintChainBuilder.java @@ -16,20 +16,20 @@ package com.scottlogic.datahelix.generator.core.builders; +import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.util.NumberUtils; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.*; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.AndConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.ConditionalConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.OrConstraint; -import com.scottlogic.datahelix.generator.common.SetUtils; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.time.OffsetDateTime; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.regex.Pattern; @@ -93,7 +93,7 @@ public ConstraintChainBuilder withEqualToConstraint(Field barField, Object re return saveAndSet( new InSetConstraint( barField, - DistributedList.singleton(referenceValue))); + TestAtomicConstraintBuilder.inSetRecordsFrom(referenceValue))); } public ConstraintChainBuilder withOrConstraint(ConstraintChainBuilder orBuilder) { @@ -111,7 +111,8 @@ public ConstraintChainBuilder withIfConstraint(BaseConstraintBuilder withInSetConstraint(Field field, Object[] legalArray) { return saveAndSet(new InSetConstraint( field, - DistributedList.uniform(SetUtils.setOf(legalArray)))); + TestAtomicConstraintBuilder.inSetRecordsFromList(Arrays.asList(legalArray)) + )); } public ConstraintChainBuilder withOfLengthConstraint(Field fooField, int length) { diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/TestAtomicConstraintBuilder.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/TestAtomicConstraintBuilder.java index 07c55033c..cbc620d75 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/TestAtomicConstraintBuilder.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/builders/TestAtomicConstraintBuilder.java @@ -16,12 +16,16 @@ package com.scottlogic.datahelix.generator.core.builders; +import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.common.profile.Field; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.AtomicConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.IsNullConstraint; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.SetUtils; + +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; public class TestAtomicConstraintBuilder { private TestConstraintNodeBuilder testConstraintNodeBuilder; @@ -32,14 +36,22 @@ protected TestAtomicConstraintBuilder(TestConstraintNodeBuilder testConstraintNo this.field = field; } - private DistributedList whitelistOf(Object... values) { - return DistributedList.uniform(SetUtils.setOf(values)); + public static List inSetRecordsFromList(List values) { + return inSetRecordsFromSet(SetUtils.setOf(values)); + } + + public static List inSetRecordsFrom(Object... values) { + return inSetRecordsFromSet(SetUtils.setOf(values)); + } + + private static List inSetRecordsFromSet(Set values) { + return values.stream().map(InSetRecord::new).collect(Collectors.toList()); } public TestConstraintNodeBuilder isInSet(Object... legalValues) { InSetConstraint inSetConstraint = new InSetConstraint( field, - whitelistOf(legalValues)); + inSetRecordsFrom(legalValues)); testConstraintNodeBuilder.constraints.add(inSetConstraint); return testConstraintNodeBuilder; } @@ -47,7 +59,7 @@ public TestConstraintNodeBuilder isInSet(Object... legalValues) { public TestConstraintNodeBuilder isNotInSet(Object... legalValues) { AtomicConstraint isInSetConstraint = new InSetConstraint( field, - whitelistOf(legalValues) + inSetRecordsFrom(legalValues) ).negate(); testConstraintNodeBuilder.constraints.add(isInSetConstraint); return testConstraintNodeBuilder; diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeFactoryTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeFactoryTests.java index 36f9f8cd6..da71e50c6 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeFactoryTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeFactoryTests.java @@ -16,12 +16,12 @@ package com.scottlogic.datahelix.generator.core.decisiontree; +import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.Fields; import com.scottlogic.datahelix.generator.common.profile.ProfileFields; import com.scottlogic.datahelix.generator.common.util.NumberUtils; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder; import com.scottlogic.datahelix.generator.core.decisiontree.testutils.*; import com.scottlogic.datahelix.generator.core.profile.Profile; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; @@ -33,7 +33,6 @@ import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.ConditionalConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.NegatedGrammaticalConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.OrConstraint; -import com.scottlogic.datahelix.generator.common.SetUtils; import org.hamcrest.core.Is; import org.hamcrest.core.IsNull; import org.junit.Assert; @@ -52,6 +51,7 @@ import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.Matchers.empty; +import static org.hamcrest.core.IsCollectionContaining.hasItem; class DecisionTreeFactoryTests { private final Field fieldA = createField("A"); @@ -123,7 +123,7 @@ void shouldReturnAnalysedRuleWithNoDecisions_IfProfileContainsOnlyAtomicConstrai List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); InSetConstraint constraint0 = new InSetConstraint( inputFieldList.get(0), - new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraint1 = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); MatchesRegexConstraint constraint2 = new MatchesRegexConstraint(inputFieldList.get(1), Pattern.compile("start.*end")); Profile testInput = new Profile(inputFieldList, Arrays.asList(constraint0, constraint1, constraint2), new ArrayList<>()); @@ -144,7 +144,7 @@ void shouldReturnAnalysedRuleWithAllConstraintsInAtomicConstraintsCollection_IfP List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); InSetConstraint constraint0 = new InSetConstraint( inputFieldList.get(0), - new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraint1 = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); MatchesRegexConstraint constraint2 = new MatchesRegexConstraint(inputFieldList.get(1), Pattern.compile("start.*end")); List inputConstraints = Arrays.asList(constraint0, constraint1, constraint2); @@ -166,7 +166,7 @@ void shouldReturnAnalysedRuleWithAllConstraintsInAtomicConstraintsCollection_IfP @Test void shouldReturnAnalysedRuleWithNoDecisions_IfProfileContainsOnlyAtomicConstraintsAndAndConstraints() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraint0 = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraint0 = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraint1 = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); AndConstraint andConstraint0 = new AndConstraint(Arrays.asList(constraint0, constraint1)); MatchesRegexConstraint constraint2 = new MatchesRegexConstraint(inputFieldList.get(1), Pattern.compile("start.*end")); @@ -185,7 +185,7 @@ void shouldReturnAnalysedRuleWithNoDecisions_IfProfileContainsOnlyAtomicConstrai @Test void shouldReturnAnalysedRuleWithAllAtomicConstraintsInAtomicConstraintsCollection_IfProfileContainsOnlyAtomicConstraintsAndAndConstraints() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraint0 = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraint0 = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraint1 = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); AndConstraint andConstraint0 = new AndConstraint(Arrays.asList(constraint0, constraint1)); MatchesRegexConstraint constraint2 = new MatchesRegexConstraint(inputFieldList.get(1), Pattern.compile("start.*end")); @@ -209,15 +209,15 @@ void shouldReturnAnalysedRuleWithAllAtomicConstraintsInAtomicConstraintsCollecti @Test void shouldReturnAnalysedRuleWithDecisionForEachOrConstraint() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraint0 = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraint0 = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraint1 = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); OrConstraint orConstraint0 = new OrConstraint(Arrays.asList(constraint0, constraint1)); InSetConstraint constraint2 = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("steam", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("steam")); InSetConstraint constraint3 = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("diesel", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("diesel")); OrConstraint orConstraint1 = new OrConstraint(Arrays.asList(constraint2, constraint3)); Profile testInput = new Profile(inputFieldList, Arrays.asList(orConstraint0, orConstraint1), new ArrayList<>()); DecisionTreeFactory testObject = new DecisionTreeFactory(); @@ -233,15 +233,15 @@ void shouldReturnAnalysedRuleWithDecisionForEachOrConstraint() { @Test void shouldReturnAnalysedRuleWithNoAtomicConstraints_IfAllAtomicConstraintsInProfileAreChildrenOfOrConstraints() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraintB = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); OrConstraint orConstraint0 = new OrConstraint(Arrays.asList(constraintA, constraintB)); InSetConstraint constraintC = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("steam", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("steam")); InSetConstraint constraintD = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("diesel", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("diesel")); OrConstraint orConstraint1 = new OrConstraint(Arrays.asList(constraintC, constraintD)); Profile testInput = new Profile(inputFieldList, Arrays.asList(orConstraint0, orConstraint1), new ArrayList<>()); DecisionTreeFactory testObject = new DecisionTreeFactory(); @@ -257,15 +257,15 @@ void shouldReturnAnalysedRuleWithNoAtomicConstraints_IfAllAtomicConstraintsInPro @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfAllAtomicConstraintsInProfileAreChildrenOfOrConstraints() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraintB = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); OrConstraint orConstraint0 = new OrConstraint(Arrays.asList(constraintA, constraintB)); InSetConstraint constraintC = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("steam", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("steam")); InSetConstraint constraintD = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("diesel", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("diesel")); OrConstraint orConstraint1 = new OrConstraint(Arrays.asList(constraintC, constraintD)); Profile testInput = new Profile(inputFieldList, Arrays.asList(orConstraint0, orConstraint1), new ArrayList<>()); DecisionTreeFactory testObject = new DecisionTreeFactory(); @@ -292,17 +292,17 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfAllAtomicConstraints @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfAllAtomicConstraintsInProfileAreChildrenOfOrAndAndConstraints() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraintB = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(0)); GreaterThanConstraint constraintC = new GreaterThanConstraint(inputFieldList.get(0), NumberUtils.coerceToBigDecimal(5)); AndConstraint andConstraint0 = new AndConstraint(Arrays.asList(constraintC, constraintB)); OrConstraint orConstraint0 = new OrConstraint(Arrays.asList(constraintA, andConstraint0)); InSetConstraint constraintD = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("steam", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("steam")); InSetConstraint constraintE = new InSetConstraint( inputFieldList.get(1), - new DistributedList<>(Collections.singletonList(new WeightedElement<>("diesel", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("diesel")); OrConstraint orConstraint1 = new OrConstraint(Arrays.asList(constraintD, constraintE)); Profile testInput = new Profile(inputFieldList, Arrays.asList(orConstraint0, orConstraint1), new ArrayList<>()); DecisionTreeFactory testObject = new DecisionTreeFactory(); @@ -329,7 +329,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfAllAtomicConstraints @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfConditionalConstraintIsPresent() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraintB = new GreaterThanConstraint(inputFieldList.get(1), NumberUtils.coerceToBigDecimal(10)); GreaterThanConstraint constraintC = new GreaterThanConstraint(inputFieldList.get(1), NumberUtils.coerceToBigDecimal(20)); ConditionalConstraint conditionalConstraint = new ConditionalConstraint(constraintA, constraintB, constraintC); @@ -358,7 +358,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfConditionalConstrain // Checks IF (A OR B) THEN C @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfConditionalConstraintWithNestedOrIsPresent() { - AtomicConstraint aEquals10 = new InSetConstraint(fieldA, new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + AtomicConstraint aEquals10 = new InSetConstraint(fieldA, TestAtomicConstraintBuilder.inSetRecordsFrom(10)); AtomicConstraint aGreaterThan10 = new GreaterThanConstraint(fieldA, NumberUtils.coerceToBigDecimal(10)); AtomicConstraint bGreaterThan20 = new GreaterThanConstraint(fieldB, NumberUtils.coerceToBigDecimal(20)); @@ -390,7 +390,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfConditionalConstrain @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfNegatedConditionalConstraintIsPresent() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraintB = new GreaterThanConstraint(inputFieldList.get(1), NumberUtils.coerceToBigDecimal(20)); GreaterThanConstraint constraintC = new GreaterThanConstraint(inputFieldList.get(1), NumberUtils.coerceToBigDecimal(10)); ConditionalConstraint conditionalConstraint = new ConditionalConstraint(constraintA, constraintB, constraintC); @@ -425,7 +425,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfNegatedConditionalCo // is equivalent to // A ^ ¬B - AtomicConstraint aEqualTo10 = new InSetConstraint(fieldA, new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + AtomicConstraint aEqualTo10 = new InSetConstraint(fieldA, TestAtomicConstraintBuilder.inSetRecordsFrom(10)); AtomicConstraint bGreaterThan20 = new GreaterThanConstraint(fieldB, NumberUtils.coerceToBigDecimal(20)); Constraint inputRule = new ConditionalConstraint(aEqualTo10, bGreaterThan20).negate(); @@ -445,7 +445,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfNegatedConditionalCo @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfDoubleNegationIsPresent() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); Constraint notConstraint0 = constraintA.negate(); Constraint notConstraint1 = notConstraint0.negate(); Profile testInput = new Profile(inputFieldList, Collections.singletonList(notConstraint1), new ArrayList<>()); @@ -467,7 +467,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfDoubleNegationIsPres @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfNegatedAndIsPresent() { List inputFieldList = Arrays.asList(createField("one"), createField("two"), createField("three")); - InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + InSetConstraint constraintA = new InSetConstraint(inputFieldList.get(0), TestAtomicConstraintBuilder.inSetRecordsFrom(10)); GreaterThanConstraint constraintB = new GreaterThanConstraint(inputFieldList.get(1), NumberUtils.coerceToBigDecimal(5)); NegatedGrammaticalConstraint notConstraint = (NegatedGrammaticalConstraint) new AndConstraint(Arrays.asList(constraintA, constraintB)).negate(); Profile testInput = new Profile(inputFieldList, Collections.singletonList(notConstraint), new ArrayList<>()); @@ -491,7 +491,7 @@ void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfNegatedAndIsPresent( // (A OR B) OR C @Test void shouldReturnAnalysedRuleWithCorrectDecisionStructure_IfNestedOrsArePresent() { - AtomicConstraint constraintA = new InSetConstraint(fieldA, new DistributedList<>(Collections.singletonList(new WeightedElement<>(10, 1.0F)))); + AtomicConstraint constraintA = new InSetConstraint(fieldA, TestAtomicConstraintBuilder.inSetRecordsFrom(10)); AtomicConstraint constraintB = new GreaterThanConstraint(fieldB, NumberUtils.coerceToBigDecimal(20)); AtomicConstraint constraintC = new GreaterThanConstraint(fieldB, NumberUtils.coerceToBigDecimal(10)); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeSimplifierTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeSimplifierTests.java index acc7dba14..7bef563b9 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeSimplifierTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/DecisionTreeSimplifierTests.java @@ -19,8 +19,6 @@ import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.ProfileFields; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.AtomicConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.IsNullConstraint; @@ -31,31 +29,23 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.stream.Collectors; -import java.util.stream.Stream; import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; +import static com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder.inSetRecordsFrom; class DecisionTreeSimplifierTests { - // TODO: Simplifier tests needs fleshing out - - private static DistributedList setOf(Object... objects) { - return new DistributedList<>(Stream.of(objects) - .map(element -> new WeightedElement<>(element, 1.0F)) - .collect(Collectors.toList())); - } @Test void simplify_decisionContainsSingleOptiontWithMatchingConstraintOnRootNode_doesNotSimplifyTree() { DecisionTree tree = new DecisionTree( new ConstraintNodeBuilder().addAtomicConstraints(SetUtils.setOf( - new InSetConstraint(createField("Field 1"), setOf(1, 2)), + new InSetConstraint(createField("Field 1"), inSetRecordsFrom(1, 2)), new IsNullConstraint(createField("Field 1")).negate() )).setDecisions(Collections.singleton( new DecisionNode( Collections.singleton( new ConstraintNodeBuilder().addAtomicConstraints(Collections.singleton( - new InSetConstraint(createField("Field 1"), setOf(1, 2)) + new InSetConstraint(createField("Field 1"), inSetRecordsFrom(1, 2)) )).setDecisions(Collections.emptySet()).build() ) ) @@ -76,13 +66,13 @@ void simplify_decisionContainsSingleOptiontWithMatchingConstraintOnRootNode_does void simplify_decisionContainsSingleOptionWithDifferingConstraintOnRootNode_simplifiesDecision() { DecisionTree tree = new DecisionTree( new ConstraintNodeBuilder().addAtomicConstraints(SetUtils.setOf( - new InSetConstraint(createField("Field 1"), setOf(1, 2)), + new InSetConstraint(createField("Field 1"), inSetRecordsFrom(1, 2)), new IsNullConstraint(createField("Field 1")).negate() )).setDecisions(Collections.singleton( new DecisionNode( Collections.singleton( new ConstraintNodeBuilder().addAtomicConstraints(Collections.singleton( - new InSetConstraint(createField("Field 2"), setOf("A", "B")) + new InSetConstraint(createField("Field 2"), inSetRecordsFrom("A", "B")) )).setDecisions(Collections.emptySet()).build() ) ) @@ -96,9 +86,9 @@ void simplify_decisionContainsSingleOptionWithDifferingConstraintOnRootNode_simp final DecisionTree result = simplifier.simplify(tree); final List expectedConstraints = Arrays.asList( - new InSetConstraint(createField("Field 1"), setOf(1, 2)), + new InSetConstraint(createField("Field 1"), inSetRecordsFrom(1, 2)), new IsNullConstraint(createField("Field 1")).negate(), - new InSetConstraint(createField("Field 2"), setOf("A", "B")) + new InSetConstraint(createField("Field 2"), inSetRecordsFrom("A", "B")) ); Assert.assertTrue(result.rootNode.getAtomicConstraints().containsAll(expectedConstraints)); Assert.assertTrue(result.rootNode.getDecisions().isEmpty()); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/RowSpecTreeSolverTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/RowSpecTreeSolverTests.java index b2d33aee2..78e39520f 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/RowSpecTreeSolverTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/RowSpecTreeSolverTests.java @@ -17,17 +17,17 @@ package com.scottlogic.datahelix.generator.core.decisiontree; import com.scottlogic.datahelix.generator.common.profile.Field; +import com.scottlogic.datahelix.generator.common.profile.Fields; import com.scottlogic.datahelix.generator.common.profile.ProfileFields; +import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecHelper; +import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecMerger; +import com.scottlogic.datahelix.generator.core.fieldspecs.RowSpec; +import com.scottlogic.datahelix.generator.core.generation.databags.DataBag; +import com.scottlogic.datahelix.generator.core.generation.databags.RowSpecDataBagGenerator; import com.scottlogic.datahelix.generator.core.profile.Profile; -import com.scottlogic.datahelix.generator.common.profile.Fields; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; -import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.ConditionalConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; -import com.scottlogic.datahelix.generator.core.fieldspecs.*; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.core.generation.databags.DataBag; -import com.scottlogic.datahelix.generator.core.generation.databags.RowSpecDataBagGenerator; +import com.scottlogic.datahelix.generator.core.profile.constraints.grammatical.ConditionalConstraint; import com.scottlogic.datahelix.generator.core.reducer.ConstraintReducer; import com.scottlogic.datahelix.generator.core.walker.decisionbased.RowSpecTreeSolver; import com.scottlogic.datahelix.generator.core.walker.decisionbased.SequentialOptionPicker; @@ -35,15 +35,18 @@ import org.junit.Assert; import org.junit.jupiter.api.Test; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; +import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; +import static com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder.inSetRecordsFrom; import static org.hamcrest.Matchers.notNullValue; import static org.mockito.Matchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; class RowSpecTreeSolverTests { private final FieldSpecMerger fieldSpecMerger = new FieldSpecMerger(); @@ -90,45 +93,21 @@ void test() List constraints = Arrays.asList( new ConditionalConstraint( - new InSetConstraint( - country, - new DistributedList<>(Collections.singletonList(new WeightedElement<>("US", 1.0F))) - ), - new InSetConstraint( - city, - new DistributedList<>(new ArrayList<>(Arrays.asList( - new WeightedElement<>("New York", 1.0F), - new WeightedElement<>("Washington DC", 1.0F))) - ))), + new InSetConstraint(country, inSetRecordsFrom("US")), + new InSetConstraint(city, inSetRecordsFrom("New York", "Washington DC")) + ), new ConditionalConstraint( - new InSetConstraint( - country, - new DistributedList<>(Collections.singletonList(new WeightedElement<>("GB", 1.0F))) - ), - new InSetConstraint( - city, - new DistributedList<>(new ArrayList<>(Arrays.asList( - new WeightedElement<>("Bristol", 1.0F), - new WeightedElement<>("London", 1.0F))) - ))), + new InSetConstraint(country, inSetRecordsFrom("GB")), + new InSetConstraint(city, inSetRecordsFrom("Bristol", "London")) + ), new ConditionalConstraint( - new InSetConstraint( - country, - new DistributedList<>(Collections.singletonList(new WeightedElement<>("US", 1.0F))) - ), - new InSetConstraint( - currency, - new DistributedList<>(Collections.singletonList(new WeightedElement<>("USD", 1.0F))) - )), + new InSetConstraint(country, inSetRecordsFrom("US")), + new InSetConstraint(currency, inSetRecordsFrom("USD"))), new ConditionalConstraint( - new InSetConstraint( - country, - new DistributedList<>(Collections.singletonList(new WeightedElement<>("GB", 1.0F))) - ), - new InSetConstraint( - currency, - new DistributedList<>(Collections.singletonList(new WeightedElement<>("GBP", 1.0F))) - ))); + new InSetConstraint(country, inSetRecordsFrom("GB")), + new InSetConstraint(currency, inSetRecordsFrom("GBP")) + ) + ); Profile profile = new Profile(fields, constraints, new ArrayList<>()); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/ConstraintToFieldMapperTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/ConstraintToFieldMapperTests.java index 4ae5f2975..830afdf84 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/ConstraintToFieldMapperTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/ConstraintToFieldMapperTests.java @@ -16,18 +16,16 @@ package com.scottlogic.datahelix.generator.core.decisiontree.treepartitioning; +import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.FieldBuilder; import com.scottlogic.datahelix.generator.common.profile.Fields; import com.scottlogic.datahelix.generator.common.profile.ProfileFields; import com.scottlogic.datahelix.generator.core.decisiontree.ConstraintNodeBuilder; import com.scottlogic.datahelix.generator.core.decisiontree.DecisionNode; -import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; -import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.AtomicConstraint; import com.scottlogic.datahelix.generator.core.decisiontree.DecisionTree; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; -import com.scottlogic.datahelix.generator.common.SetUtils; +import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.AtomicConstraint; +import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.jupiter.api.BeforeEach; @@ -35,18 +33,17 @@ import java.util.*; import java.util.stream.Collectors; + import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; +import static com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder.inSetRecordsFrom; class ConstraintToFieldMapperTests { - private DistributedList whitelistOf(Object element) { - return new DistributedList<>(Collections.singletonList(new WeightedElement<>(element, 1.0F))); - } @Test void shouldFindConstraintMappings() { givenFields("A"); - final AtomicConstraint constraint = new InSetConstraint(createField("A"), whitelistOf("test-value")); + final AtomicConstraint constraint = new InSetConstraint(createField("A"), inSetRecordsFrom("test-value")); givenConstraints(constraint); givenFields("A"); @@ -57,7 +54,7 @@ void shouldFindConstraintMappings() { void shouldFindRootDecisionNodeMapping() { givenFields("B"); - final AtomicConstraint constraint = new InSetConstraint(createField("B"), whitelistOf("test-value")); + final AtomicConstraint constraint = new InSetConstraint(createField("B"), inSetRecordsFrom("test-value")); final DecisionNode decision = new DecisionNode( new ConstraintNodeBuilder().addAtomicConstraints(constraint).build()); @@ -70,9 +67,9 @@ void shouldFindRootDecisionNodeMapping() { void shouldCreateCorrectNumberOfMappings() { givenFields("A", "B", "C"); - final AtomicConstraint constraintA = new InSetConstraint(createField("A"), whitelistOf("test-value")); - final AtomicConstraint constraintB = new InSetConstraint(createField("B"), whitelistOf("test-value")); - final AtomicConstraint constraintC = new InSetConstraint(createField("C"), whitelistOf("test-value")); + final AtomicConstraint constraintA = new InSetConstraint(createField("A"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintB = new InSetConstraint(createField("B"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintC = new InSetConstraint(createField("C"), inSetRecordsFrom("test-value")); givenConstraints(constraintA, constraintB, constraintC); @@ -83,12 +80,12 @@ void shouldCreateCorrectNumberOfMappings() { void shouldMapTopLevelConstraintsToNestedFields() { givenFields("A", "B", "C", "D", "E", "F"); - final AtomicConstraint constraintA = new InSetConstraint(createField("A"), whitelistOf("test-value")); - final AtomicConstraint constraintB = new InSetConstraint(createField("B"), whitelistOf("test-value")); - final AtomicConstraint constraintC = new InSetConstraint(createField("C"), whitelistOf("test-value")); - final AtomicConstraint constraintD = new InSetConstraint(createField("D"), whitelistOf("test-value")); - final AtomicConstraint constraintE = new InSetConstraint(createField("E"), whitelistOf("test-value")); - final AtomicConstraint constraintF = new InSetConstraint(createField("F"), whitelistOf("test-value")); + final AtomicConstraint constraintA = new InSetConstraint(createField("A"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintB = new InSetConstraint(createField("B"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintC = new InSetConstraint(createField("C"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintD = new InSetConstraint(createField("D"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintE = new InSetConstraint(createField("E"), inSetRecordsFrom("test-value")); + final AtomicConstraint constraintF = new InSetConstraint(createField("F"), inSetRecordsFrom("test-value")); final DecisionNode decisionABC = new DecisionNode( new ConstraintNodeBuilder().addAtomicConstraints(Collections.emptySet()).setDecisions(SetUtils.setOf( diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/TreePartitionerTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/TreePartitionerTests.java index 88754be63..4d34b9775 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/TreePartitionerTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/decisiontree/treepartitioning/TreePartitionerTests.java @@ -16,11 +16,11 @@ package com.scottlogic.datahelix.generator.core.decisiontree.treepartitioning; +import com.scottlogic.datahelix.generator.common.SetUtils; import com.scottlogic.datahelix.generator.common.profile.FieldBuilder; import com.scottlogic.datahelix.generator.common.profile.Fields; import com.scottlogic.datahelix.generator.common.profile.ProfileFields; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder; import com.scottlogic.datahelix.generator.core.decisiontree.ConstraintNode; import com.scottlogic.datahelix.generator.core.decisiontree.ConstraintNodeBuilder; import com.scottlogic.datahelix.generator.core.decisiontree.DecisionNode; @@ -28,7 +28,6 @@ import com.scottlogic.datahelix.generator.core.decisiontree.testutils.*; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.AtomicConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; -import com.scottlogic.datahelix.generator.common.SetUtils; import org.junit.Assert; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -276,9 +275,7 @@ private AtomicConstraint atomicConstraint(String fieldName) { if (constraint == null) { constraint = new InSetConstraint( createField(fieldName), - new DistributedList<>( - Collections.singletonList( - new WeightedElement<>("sample-value", 1.0F)))); + TestAtomicConstraintBuilder.inSetRecordsFrom("sample-value")); this.constraints.put(fieldName, constraint); } diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelperTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelperTests.java index 81027d512..511b2074c 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelperTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecHelperTests.java @@ -18,7 +18,6 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.FieldBuilder; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import org.junit.jupiter.api.Test; @@ -34,8 +33,7 @@ void getFieldSpecForValue() { FieldSpec actual = fieldSpecHelper.getFieldSpecForValue(input); - FieldSpec expected = FieldSpecFactory.fromList(DistributedList.singleton("value")) - .withNotNull(); + FieldSpec expected = FieldSpecFactory.fromSingleLegalValue("value").withNotNull(); assertEquals(actual, expected); } diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecTests.java index 1aec52176..000cd4967 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/FieldSpecTests.java @@ -17,10 +17,11 @@ package com.scottlogic.datahelix.generator.core.fieldspecs; import com.scottlogic.datahelix.generator.common.profile.FieldType; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; -import com.scottlogic.datahelix.generator.core.restrictions.*; -import com.scottlogic.datahelix.generator.core.restrictions.linear.*; +import com.scottlogic.datahelix.generator.core.restrictions.TypedRestrictions; +import com.scottlogic.datahelix.generator.core.restrictions.linear.Limit; +import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictions; +import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsFactory; import com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictions; import org.junit.Assert; import org.junit.jupiter.api.Test; @@ -32,7 +33,7 @@ import java.util.Set; import static com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictionsFactory.forMaxLength; -import static com.scottlogic.datahelix.generator.core.utils.GeneratorDefaults.*; +import static com.scottlogic.datahelix.generator.core.utils.GeneratorDefaults.NUMERIC_MAX_LIMIT; import static org.hamcrest.core.IsEqual.equalTo; import static org.hamcrest.core.IsNot.not; import static org.junit.Assert.assertFalse; @@ -56,7 +57,7 @@ void equals_objTypeIsNotFieldSpec_returnsFalse() { @Test void equals_fieldSpecHasSetRestrictionsAndOtherObjectSetRestrictionsNull_returnsFalse() { - FieldSpec fieldSpec = FieldSpecFactory.fromList(DistributedList.singleton("whitelist")); + FieldSpec fieldSpec = FieldSpecFactory.fromSingleLegalValue("legalValue"); boolean result = fieldSpec.equals(FieldSpecFactory.fromType(FieldType.STRING)); @@ -70,8 +71,7 @@ void equals_fieldSpecHasSetRestrictionsAndOtherObjectSetRestrictionsNull_returns void equals_fieldSpecSetRestrictionsNullAndOtherObjectHasSetRestrictions_returnsFalse() { FieldSpec fieldSpec = FieldSpecFactory.fromType(FieldType.STRING); - boolean result = fieldSpec.equals( - FieldSpecFactory.fromList(DistributedList.singleton("whitelist"))); + boolean result = fieldSpec.equals(FieldSpecFactory.fromSingleLegalValue("legalValue")); assertFalse( "Expected that when the field spec does not have set restrictions and the other object has set restrictions a false value is returned but was true", @@ -81,10 +81,9 @@ void equals_fieldSpecSetRestrictionsNullAndOtherObjectHasSetRestrictions_returns @Test void equals_fieldSpecSetRestrictionsNotNullAndOtherObjectSetRestrictionsNotNullAndSetRestrictionsAreNotEqual_returnsFalse() { - FieldSpec fieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(Arrays.asList(1, 2, 3))); + FieldSpec fieldSpec = FieldSpecFactory.fromLegalValuesList(Arrays.asList(1, 2, 3)); - boolean result = fieldSpec.equals( - FieldSpecFactory.fromList(DistributedList.uniform(Arrays.asList(1, 2, 3, 4)))); + boolean result = fieldSpec.equals(FieldSpecFactory.fromLegalValuesList(Arrays.asList(1, 2, 3, 4))); assertFalse( "Expected that when the items in the set restrictions are not equal a false value is returned but was true", @@ -131,8 +130,8 @@ public void emptyFieldSpecsShouldBeEqualAndHaveSameHashCode() { @Test public void fieldSpecsWithEqualSetRestrictionsShouldBeEqual() { - FieldSpec a = FieldSpecFactory.fromList(DistributedList.singleton("same")); - FieldSpec b = FieldSpecFactory.fromList(DistributedList.singleton("same")); + FieldSpec a = FieldSpecFactory.fromSingleLegalValue("same"); + FieldSpec b = FieldSpecFactory.fromSingleLegalValue("same"); Assert.assertThat(a, equalTo(b)); Assert.assertThat(a.hashCode(), equalTo(b.hashCode())); @@ -140,8 +139,8 @@ public void fieldSpecsWithEqualSetRestrictionsShouldBeEqual() { @Test public void fieldSpecsWithUnequalSetRestrictionsShouldBeUnequal() { - FieldSpec a = FieldSpecFactory.fromList(DistributedList.singleton("not same")); - FieldSpec b = FieldSpecFactory.fromList(DistributedList.singleton("different")); + FieldSpec a = FieldSpecFactory.fromSingleLegalValue("not same"); + FieldSpec b = FieldSpecFactory.fromSingleLegalValue("different"); Assert.assertThat(a, not(equalTo(b))); } @@ -198,7 +197,7 @@ void permitsRejectsInvalidNumeric() { LinearRestrictions numeric = LinearRestrictionsFactory.createNumericRestrictions(new Limit<>(BigDecimal.TEN, true), NUMERIC_MAX_LIMIT); FieldSpec spec = FieldSpecFactory.fromRestriction(numeric); - assertFalse(spec.canCombineWithWhitelistValue(BigDecimal.ONE)); + assertFalse(spec.canCombineWithLegalValue(BigDecimal.ONE)); } @Test @@ -208,7 +207,7 @@ void permitsRejectsInvalidDateTime() { OffsetDateTime time = OffsetDateTime.of(100, 1, 1, 1, 1, 1, 1, ZoneOffset.UTC); - assertFalse(spec.canCombineWithWhitelistValue(time.plusNanos(1_000_000))); + assertFalse(spec.canCombineWithLegalValue(time.plusNanos(1_000_000))); } @Test @@ -242,21 +241,21 @@ void permitsRejectsInvalidString() { FieldSpec spec = FieldSpecFactory.fromRestriction(mockTypedRestrictions); - assertFalse(spec.canCombineWithWhitelistValue("Anything")); + assertFalse(spec.canCombineWithLegalValue("Anything")); } @Test - void permits_whenNotInWhiteList_returnsFalse() { - FieldSpec spec = FieldSpecFactory.fromList(DistributedList.singleton(10)); + void permits_whenCanCombineWithLegalValue_returnsFalse() { + FieldSpec spec = FieldSpecFactory.fromSingleLegalValue(10); - assertFalse(spec.canCombineWithWhitelistValue(11)); + assertFalse(spec.canCombineWithLegalValue(11)); } @Test - void permits_whenInWhiteList_returnsTrue() { - FieldSpec spec = FieldSpecFactory.fromList(DistributedList.singleton(10)); + void permits_whenCanCombineWithLegalValue_returnsTrue() { + FieldSpec spec = FieldSpecFactory.fromSingleLegalValue(10); - assertTrue(spec.canCombineWithWhitelistValue(10)); + assertTrue(spec.canCombineWithLegalValue(10)); } private class MockedRestrictions implements TypedRestrictions { diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelationTest.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelationTest.java index 4df07cb61..3c027a508 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelationTest.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/EqualToRelationTest.java @@ -16,11 +16,9 @@ package com.scottlogic.datahelix.generator.core.fieldspecs.relations; import com.scottlogic.datahelix.generator.common.profile.Field; -import com.scottlogic.datahelix.generator.common.profile.SpecificFieldType; import com.scottlogic.datahelix.generator.common.profile.StandardSpecificFieldType; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import org.junit.jupiter.api.Test; @@ -50,7 +48,7 @@ public void testReduceToFieldSpec_withNotNull_reducesToSpec() { FieldSpec result = equalToDateRelations.createModifierFromOtherValue(generatedValue); - FieldSpec expected = FieldSpecFactory.fromList(DistributedList.singleton(value)); + FieldSpec expected = FieldSpecFactory.fromSingleLegalValue(value); assertThat(result, sameBeanAs(expected)); } diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/FieldSpecRelationTest.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/FieldSpecRelationTest.java index e15496455..856edc314 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/FieldSpecRelationTest.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/FieldSpecRelationTest.java @@ -17,19 +17,16 @@ import com.scottlogic.datahelix.generator.common.profile.*; import com.scottlogic.datahelix.generator.common.util.defaults.DateTimeDefaults; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; import com.scottlogic.datahelix.generator.core.fieldspecs.RestrictionsFieldSpec; -import com.scottlogic.datahelix.generator.core.fieldspecs.WhitelistFieldSpec; import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictions; -import com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsFactory; import org.junit.jupiter.api.Test; import java.math.BigDecimal; import java.time.OffsetDateTime; import java.time.ZoneOffset; -import java.util.Set; +import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -159,17 +156,11 @@ public void before_range_returnsFromMin() { @Test public void equalTo_forSet_returns() { - DistributedList expectedValues = DistributedList.uniform( - Stream.of("2", "3", "4") - .map(BigDecimal::new) - .collect(Collectors.toSet())); - WhitelistFieldSpec expected = FieldSpecFactory.fromList(expectedValues); - - DistributedList secondValues = DistributedList.uniform( - Stream.of("1", "2", "3") - .map(BigDecimal::new) - .collect(Collectors.toSet())); - WhitelistFieldSpec second = FieldSpecFactory.fromList(secondValues); + List expectedValues = Stream.of("2", "3", "4").map(BigDecimal::new).collect(Collectors.toList()); + FieldSpec expected = FieldSpecFactory.fromLegalValuesList(expectedValues); + + List secondValues = Stream.of("1", "2", "3").map(BigDecimal::new).collect(Collectors.toList()); + FieldSpec second = FieldSpecFactory.fromLegalValuesList(secondValues); Field firstField = FieldBuilder.createField("first"); Field secondField = FieldBuilder.createField("second"); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelationTest.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelationTest.java index f1c7fbd27..654b57579 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelationTest.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapIndexRelationTest.java @@ -19,13 +19,11 @@ import com.scottlogic.datahelix.generator.common.profile.FieldType; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.restrictions.string.StringRestrictionsFactory; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.regex.Pattern; @@ -42,24 +40,24 @@ static void before() { Field f2 = createField("field1"); List values = Arrays.asList("foo", "bar"); - testInstance = new InMapIndexRelation(f1, f2, DistributedList.uniform(values)); + testInstance = new InMapIndexRelation(f1, f2, values); } @Test - void reduceToRelatedFieldSpec_whenAllValid_returnCompleteWhiteList() { + void reduceToRelatedFieldSpec_whenAllValid_returnCompleteLegalValuesList() { FieldSpec parameter = FieldSpecFactory.fromType(FieldType.STRING); - FieldSpec expected = FieldSpecFactory.fromList(DistributedList.uniform(Arrays.asList(0, 1))).withNotNull(); + FieldSpec expected = FieldSpecFactory.fromLegalValuesList(Arrays.asList(0, 1)).withNotNull(); FieldSpec actual = testInstance.createModifierFromOtherFieldSpec(parameter); assertThat(actual, sameBeanAs(expected)); } @Test - void reduceToRelatedFieldSpec_whenSomeValid_returnReducedWhiteList() { + void reduceToRelatedFieldSpec_whenSomeValid_returnReducedLegalValuesList() { FieldSpec parameter = FieldSpecFactory.fromRestriction(StringRestrictionsFactory.forStringContaining(Pattern.compile("^f.*"), false)); - FieldSpec expected = FieldSpecFactory.fromList(DistributedList.uniform(Collections.singletonList(0))).withNotNull(); + FieldSpec expected = FieldSpecFactory.fromSingleLegalValue(0).withNotNull(); FieldSpec actual = testInstance.createModifierFromOtherFieldSpec(parameter); assertThat(actual, sameBeanAs(expected)); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelationTest.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelationTest.java index 8ab30fe46..4c4584fa9 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelationTest.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/fieldspecs/relations/InMapRelationTest.java @@ -18,7 +18,6 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import org.junit.Assert; import org.junit.jupiter.api.BeforeAll; @@ -40,12 +39,12 @@ static void before() { Field f2 = createField("field1"); List values = Arrays.asList("foo", "bar"); - testInstance = new InMapRelation(f1, f2, DistributedList.uniform(values)); + testInstance = new InMapRelation(f1, f2, values); } @Test void reduceValueToFieldSpec_whenValidIndex_returnFieldSpec() { - FieldSpec expected = FieldSpecFactory.fromList(DistributedList.singleton("bar")); + FieldSpec expected = FieldSpecFactory.fromSingleLegalValue("bar"); FieldSpec actual = testInstance.createModifierFromOtherValue(new DataBagValue(BigDecimal.valueOf(1))); Assert.assertEquals(expected, actual); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecGetFieldValueSourceTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecGetFieldValueSourceTests.java index 4e3fb2fa3..e64d68593 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecGetFieldValueSourceTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecGetFieldValueSourceTests.java @@ -18,7 +18,6 @@ import com.scottlogic.datahelix.generator.common.RandomNumberGenerator; import com.scottlogic.datahelix.generator.common.profile.FieldType; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; @@ -32,8 +31,10 @@ import java.math.BigDecimal; import java.time.OffsetDateTime; -import java.util.*; -import java.util.stream.Collectors; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Iterator; +import java.util.List; import static com.scottlogic.datahelix.generator.core.restrictions.linear.LinearRestrictionsFactory.createNumericRestrictions; import static org.hamcrest.core.Is.is; @@ -60,7 +61,7 @@ public void shouldReturnNullSourceLastWithNoRestrictions() { @Test public void shouldReturnNullSourceLastWithInSetRestrictionsAndNullNotDisallowed() { - FieldSpec fieldSpecInSetAndNullNotDisallowed = FieldSpecFactory.fromList(DistributedList.uniform(new HashSet<>(Arrays.asList(15, 25)))); + FieldSpec fieldSpecInSetAndNullNotDisallowed = FieldSpecFactory.fromLegalValuesList(Arrays.asList(15, 25)); FieldValueSource sources = fieldSpecInSetAndNullNotDisallowed.getFieldValueSource(); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecValueGeneratorTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecValueGeneratorTests.java index 231fbfb8b..a59e95a8d 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecValueGeneratorTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/FieldSpecValueGeneratorTests.java @@ -18,10 +18,8 @@ import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.StandardSpecificFieldType; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpec; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; -import com.scottlogic.datahelix.generator.core.fieldspecs.WhitelistFieldSpec; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import com.scottlogic.datahelix.generator.core.generation.fieldvaluesources.FieldValueSource; import com.scottlogic.datahelix.generator.core.restrictions.linear.Limit; @@ -34,6 +32,7 @@ import java.math.BigDecimal; import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -48,7 +47,8 @@ class FieldSpecValueGeneratorTests { @Test void generate_fieldSpecMustContainRestrictionNullAndSetRestrictionsHasValues_returnsDataBagsWithValuesInSetRestrictions() { - WhitelistFieldSpec fieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(Arrays.asList(10, 20, 30))) + final List legalValuesList = Arrays.asList(10, 20, 30); + FieldSpec fieldSpec = FieldSpecFactory.fromLegalValuesList(legalValuesList) .withNotNull(); FieldSpecValueGenerator fieldSpecFulfiller = new FieldSpecValueGenerator( RANDOM, @@ -56,7 +56,7 @@ void generate_fieldSpecMustContainRestrictionNullAndSetRestrictionsHasValues_ret final Set result = fieldSpecFulfiller.generate(createField(null), fieldSpec).limit(3).collect(Collectors.toSet()); - Set expectedDataBags = fieldSpec.getWhitelist().list() + Set expectedDataBags = legalValuesList .stream() .map(DataBagValue::new) .collect(Collectors.toSet()); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSourceEqualityTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSourceEqualityTests.java index d63384d0a..23ecf593e 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSourceEqualityTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/generation/fieldvaluesources/CannedValuesFieldValueSourceEqualityTests.java @@ -16,7 +16,7 @@ package com.scottlogic.datahelix.generator.core.generation.fieldvaluesources; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; +import com.scottlogic.datahelix.generator.common.distribution.DistributedList; import org.junit.Assert; import org.junit.jupiter.api.Test; @@ -30,9 +30,9 @@ class CannedValuesFieldValueSourceEqualityTests { private FieldValueSource valueSourceOf(String... elements) { Set set = Arrays.stream(elements).collect(Collectors.toSet()); - DistributedList whitelist = DistributedList.uniform(set); + DistributedList legalValues = DistributedList.uniform(set); //noinspection unchecked - return new CannedValuesFieldValueSource(whitelist); + return new CannedValuesFieldValueSource(legalValues); } @Test diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraintTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraintTests.java index 6a38fc5fe..f783f14f6 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraintTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/atomic/InSetConstraintTests.java @@ -18,10 +18,14 @@ import com.scottlogic.datahelix.generator.common.ValidationException; import com.scottlogic.datahelix.generator.common.profile.Field; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; + import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; +import static com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder.inSetRecordsFrom; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; public class InSetConstraintTests { @@ -31,7 +35,7 @@ public void testConstraintThrowsIfGivenEmptySet(){ Assertions.assertThrows( ValidationException.class, - () -> new InSetConstraint(field1, DistributedList.empty())); + () -> new InSetConstraint(field1, emptyList())); } @Test @@ -40,14 +44,14 @@ public void testConstraintThrowsIfGivenNullInASet(){ Assertions.assertThrows( ValidationException.class, - () -> new InSetConstraint(field1, DistributedList.singleton(null))); + () -> new InSetConstraint(field1, singletonList(new InSetRecord(null)))); } @Test public void testConstraintThrowsNothingIfGivenAValidSet(){ Field field1 = createField("TestField"); Assertions.assertDoesNotThrow( - () -> new InSetConstraint(field1, DistributedList.singleton("foo"))); + () -> new InSetConstraint(field1, inSetRecordsFrom("foo"))); } } diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/grammatical/NotConstraintTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/grammatical/NotConstraintTests.java index 6f47abc21..619222588 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/grammatical/NotConstraintTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/profile/constraints/grammatical/NotConstraintTests.java @@ -17,13 +17,14 @@ package com.scottlogic.datahelix.generator.core.profile.constraints.grammatical; import com.scottlogic.datahelix.generator.common.profile.Field; +import com.scottlogic.datahelix.generator.core.builders.TestAtomicConstraintBuilder; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.InSetConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.IsNullConstraint; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import org.hamcrest.Matchers; import org.junit.Assert; import org.junit.jupiter.api.Test; + import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; public class NotConstraintTests { @@ -69,11 +70,11 @@ public void testConstraintIsNotEqualDueToValue() { Field field2 = createField("TestField"); Constraint constraint1 = new InSetConstraint( field1, - DistributedList.singleton("abc") + TestAtomicConstraintBuilder.inSetRecordsFrom("abc") ).negate(); Constraint constraint2 = new InSetConstraint( field2, - DistributedList.singleton("abcd") + TestAtomicConstraintBuilder.inSetRecordsFrom("abcd") ).negate(); Assert.assertNotEquals(constraint1, constraint2); } diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolverTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolverTests.java index 0b3526c05..8be26d51f 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolverTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/decisionbased/RowSpecTreeSolverTests.java @@ -22,7 +22,6 @@ import com.scottlogic.datahelix.generator.core.decisiontree.ConstraintNode; import com.scottlogic.datahelix.generator.core.decisiontree.DecisionTree; import com.scottlogic.datahelix.generator.core.fieldspecs.*; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.reducer.ConstraintReducer; import com.scottlogic.datahelix.generator.core.walker.pruner.TreePruner; import org.junit.jupiter.api.Test; @@ -31,9 +30,9 @@ import java.util.stream.Collectors; import java.util.stream.Stream; -import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; -import static com.shazam.shazamcrest.MatcherAssert.assertThat; import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; +import static com.shazam.shazamcrest.MatcherAssert.assertThat; +import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; class RowSpecTreeSolverTests { private Field fieldA = createField("A"); @@ -76,7 +75,7 @@ void createRowSpecs_whenRootNodeHasNoDecisionsButSomeConstraints_returnsRowSpecO //Assert List expectedRowSpecs = new ArrayList<>(); Map fieldToFieldSpec = new HashMap<>(); - fieldToFieldSpec.put(fieldA, FieldSpecFactory.fromList(DistributedList.uniform(Arrays.asList("1", "2", "3")))); + fieldToFieldSpec.put(fieldA, FieldSpecFactory.fromLegalValuesList(Arrays.asList("1", "2", "3"))); fieldToFieldSpec.put(fieldB, FieldSpecFactory.fromType(fieldB.getType())); expectedRowSpecs.add(new RowSpec(fields, fieldToFieldSpec, Collections.emptyList())); @@ -106,7 +105,7 @@ void createRowSpecs_whenRootNodeHasSomeDecisions_returnsRowSpecOfRoot() { expectedRowSpecs.add(new RowSpec(fields, option0, Collections.emptyList())); Map option1 = new HashMap<>(); option1.put(fieldA, FieldSpecFactory.fromType(fieldA.getType())); - option1.put(fieldB, FieldSpecFactory.fromList(DistributedList.uniform(Arrays.asList("1","2","3")))); + option1.put(fieldB, FieldSpecFactory.fromLegalValuesList(Arrays.asList("1","2","3"))); expectedRowSpecs.add(new RowSpec(fields, option1, Collections.emptyList())); assertThat(rowSpecs, sameBeanAs(expectedRowSpecs)); diff --git a/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/pruner/TreePrunerTests.java b/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/pruner/TreePrunerTests.java index d4d4360f8..c97c8d830 100644 --- a/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/pruner/TreePrunerTests.java +++ b/core/src/test/java/com/scottlogic/datahelix/generator/core/walker/pruner/TreePrunerTests.java @@ -23,7 +23,6 @@ import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecFactory; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecHelper; import com.scottlogic.datahelix.generator.core.fieldspecs.FieldSpecMerger; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.generation.databags.DataBagValue; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.LongerThanConstraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.ShorterThanConstraint; @@ -32,8 +31,7 @@ import java.util.Arrays; import java.util.Collections; -import java.util.HashSet; -import java.util.Set; +import java.util.List; import static com.scottlogic.datahelix.generator.common.profile.FieldBuilder.createField; import static com.scottlogic.datahelix.generator.core.builders.TestConstraintNodeBuilder.constraintNode; @@ -65,9 +63,9 @@ class TreePrunerTests { @Test public void pruneConstraintNode_leafNodeContradictionsWithParent_returnsContradictory() { //Arrange - Set inputWhitelist = new HashSet<>(Arrays.asList("a", "b")); + List inputLegalList = Arrays.asList("a", "b"); ConstraintNode tree = new ConstraintNodeBuilder().addAtomicConstraints(new LongerThanConstraint(field, 5)).build(); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -84,10 +82,9 @@ public void pruneConstraintNode_leafNodeContradictionsWithParent_returnsContradi @Test public void pruneConstraintNode_leafNodeNoContradictionsWithParent_returnsLeafNode() { //Arrange - Set inputWhitelist = new HashSet<>(Arrays.asList("a", "b")); + List inputLegalList = Arrays.asList("a", "b"); ConstraintNode tree = new ConstraintNodeBuilder().addAtomicConstraints(new ShorterThanConstraint(field, 5)).build(); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList( - (DistributedList.uniform(inputWhitelist))); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -109,8 +106,8 @@ public void pruneConstraintNode_withContradictoryDecision_returnsContradictory() constraintNode().where(field).isInSet("a"), constraintNode().where(field).isInSet("b")) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("c")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("c"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -133,8 +130,8 @@ public void pruneConstraintNode_withNoContradictoryDecision_returnsSameNode() { constraintNode().where(field).isInSet("a"), constraintNode().where(field).isInSet("b")) .build(); - DistributedList inputWhitelist = DistributedList.uniform(new HashSet<>(Arrays.asList("a", "b"))); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(inputWhitelist).withNotNull(); + List inputLegalList = Arrays.asList("a", "b"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList).withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -156,8 +153,8 @@ public void pruneConstraintNode_withOneContradictoryDecision_returnConstraintNod constraintNode().where(field).isInSet("a"), constraintNode().where(field).isInSet("b")) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("a")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("a"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -183,8 +180,8 @@ public void pruneConstraintNode_withTwoContradictoryDecisions_returnConstraintNo constraintNode().where(field).isInSet("b"), constraintNode().where(field).isInSet("c")) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("a")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("a"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -211,8 +208,8 @@ public void pruneConstraintNode_withOneContradictoryDecisionAndMultipleRemaining constraintNode().where(field).isInSet("c")) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("a", "b")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Arrays.asList("a", "b"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -243,8 +240,8 @@ public void pruneConstraintNode_twoDecisionsOneContradictory_returnContradictory constraintNode().where(field).isInSet("contradictory"), constraintNode().where(field).isInSet("contradictory")) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("valid")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("valid"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -273,8 +270,8 @@ public void pruneConstraintNode_MultiLayerOneValidOptionInEach_returnsSingleNode constraintNode().where(unrelatedField).isInSet("unrelated2") )) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("valid")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("valid"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -304,8 +301,8 @@ public void pruneConstraintNode_MultiLayerOneOptionContradicts_returnsSingleLaye constraintNode().where(unrelatedField).isInSet("unrelated2") )) .build(); - Set inputWhitelist = Collections.singleton("valid"); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("valid"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -337,8 +334,8 @@ public void pruneConstraintNode_MultiLayerOneOptionContradicts2_returnsSingleLay constraintNode().where(unrelatedField).isInSet("unrelated2") )) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("valid")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("valid"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -371,8 +368,8 @@ public void pruneConstraintNode_MultiLayerMultiContradicts_returnsContradictory( constraintNode().where(field).isInSet("contradiction") )) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("valid")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList(DistributedList.uniform(inputWhitelist)) + List inputLegalList = Collections.singletonList("valid"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList) .withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); @@ -399,9 +396,8 @@ public void pruneConstraintNode_MultiLayerNoContradicts_returnsOriginalTree() { constraintNode().where(unrelatedField).isInSet("unrelated2") )) .build(); - Set inputWhitelist = new HashSet<>(Arrays.asList("valid")); - FieldSpec inputFieldSpec = FieldSpecFactory.fromList((DistributedList.uniform(inputWhitelist))) - .withNotNull(); + List inputLegalList = Collections.singletonList("valid"); + FieldSpec inputFieldSpec = FieldSpecFactory.fromLegalValuesList(inputLegalList).withNotNull(); when(fieldSpecHelper.getFieldSpecForValue(any())).thenReturn(inputFieldSpec); diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/InMap.feature b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/InMap.feature index 49731e7f1..96d5ca03c 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/InMap.feature +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/features/operators/general/InMap.feature @@ -96,7 +96,7 @@ Feature: User can specify that a field value belongs to a set of predetermined o | "Wales" | "Cardiff" | 2 | "two" | - Scenario: Running an 'inMap' with text a restriction + Scenario: Running an 'inMap' with invalid typed data Given the following non nullable fields exist: | HomeNation | | Population | diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberFileReader.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberFileReader.java index 386100fca..c1cccf7f7 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberFileReader.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberFileReader.java @@ -15,12 +15,12 @@ */ package com.scottlogic.datahelix.generator.orchestrator.cucumber.testframework.utils; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.profile.reader.CsvInputStreamReaderFactory; import com.scottlogic.datahelix.generator.profile.reader.FileReader; import javax.inject.Inject; import java.io.File; +import java.util.List; public class CucumberFileReader extends FileReader { private final CucumberTestState testState; @@ -32,8 +32,8 @@ public CucumberFileReader(CucumberTestState testState) { } @Override - public DistributedList listFromMapFile(File file, String key) { - return DistributedList.uniform(testState.getValuesFromMap(file.getName(), key)); + public List listFromMapFile(File file, String key) { + return testState.getValuesFromMap(file.getName(), key); } } diff --git a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberTestState.java b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberTestState.java index fa252fb1d..673cfca5c 100644 --- a/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberTestState.java +++ b/orchestrator/src/test/java/com/scottlogic/datahelix/generator/orchestrator/cucumber/testframework/utils/CucumberTestState.java @@ -152,7 +152,7 @@ private ConstraintDTO createConstraint(String fieldName, ConstraintType type, Ob field = fieldName; file = (String) _value; }} - : new InSetConstraintDTO() {{ + : new InSetFromListConstraintDTO() {{ field = fieldName; values = (List) _value; }}; diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetConstraintDTO.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetConstraintDTO.java index 74cb400bd..013329c22 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetConstraintDTO.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetConstraintDTO.java @@ -17,15 +17,14 @@ package com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import java.util.List; -@JsonDeserialize(as = InSetConstraintDTO.class) public class InSetConstraintDTO extends AtomicConstraintDTO { public static final String NAME = "inSet"; @JsonProperty(NAME) - public List values; + public List values; } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetFromListConstraintDTO.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetFromListConstraintDTO.java new file mode 100644 index 000000000..96c683dd1 --- /dev/null +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/dtos/constraints/atomic/InSetFromListConstraintDTO.java @@ -0,0 +1,31 @@ +/* + * Copyright 2019-2021 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.profile.dtos.constraints.atomic; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; + +import java.util.List; + +@JsonDeserialize(as = InSetFromListConstraintDTO.class) +public class InSetFromListConstraintDTO extends AtomicConstraintDTO +{ + public static final String NAME = "inSet"; + + @JsonProperty(NAME) + public List values; +} diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/factories/constraint_factories/AtomicConstraintFactory.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/factories/constraint_factories/AtomicConstraintFactory.java index dd37f9606..9850cc27a 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/factories/constraint_factories/AtomicConstraintFactory.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/factories/constraint_factories/AtomicConstraintFactory.java @@ -19,15 +19,17 @@ import com.scottlogic.datahelix.generator.common.ValidationException; import com.scottlogic.datahelix.generator.common.profile.Field; import com.scottlogic.datahelix.generator.common.profile.Fields; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.core.fieldspecs.relations.InMapRelation; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.*; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.*; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.LongerThanConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.OfLengthConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.ShorterThanConstraintDTO; -import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.*; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.GreaterThanConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.GreaterThanOrEqualToConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.LessThanConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.LessThanOrEqualToConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.temporal.AfterConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.temporal.AfterOrAtConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.temporal.BeforeConstraintDTO; @@ -87,7 +89,7 @@ public InMapRelation createInMapRelation(InMapConstraintDTO dto, Fields fields) Field main = fields.getByName(dto.field); Field other = fields.getByName(dto.otherField); List values = dto.values.stream().map(this::parseValue).collect(Collectors.toList()); - return new InMapRelation(main, other, DistributedList.uniform(values)); + return new InMapRelation(main, other, values); } abstract Object parseValue(Object value); @@ -108,13 +110,10 @@ public InMapRelation createInMapRelation(InMapConstraintDTO dto, Fields fields) private InSetConstraint createInSetConstraint(InSetConstraintDTO dto, Field field) { - DistributedList values = DistributedList.weightedOrDefault(dto.values.stream() - .distinct() - .map(value -> (value instanceof WeightedElement) - ? WeightedElement.parseValue((WeightedElement) value, this::parseValue) - : this.parseValue(value) - ) - .collect(Collectors.toList())); + final List values = dto.values.stream() + .map(value -> value.mapValue(this::parseValue)) + .collect(Collectors.toList()); + return new InSetConstraint(field, values); } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvFileInputReader.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvFileInputReader.java index 76e948f04..a624be45a 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvFileInputReader.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvFileInputReader.java @@ -17,9 +17,10 @@ package com.scottlogic.datahelix.generator.profile.reader; import com.scottlogic.datahelix.generator.common.ValidationException; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import java.io.*; +import java.util.List; public class CsvFileInputReader implements CsvInputReader { private final File path; @@ -28,17 +29,17 @@ public CsvFileInputReader(File path) { this.path = path; } - public DistributedList retrieveLines() { + public List retrieveInSetElements() { try (InputStream stream = createStream()) { - return new CsvStreamInputReader(stream, path.getName()).retrieveLines(); + return new CsvStreamInputReader(stream, path.getName()).retrieveInSetElements(); } catch (IOException exc){ throw new UncheckedIOException(exc); } } - public DistributedList retrieveLines(String key) { + public List retrieveLinesForColumn(String key) { try (InputStream stream = createStream()) { - return new CsvStreamInputReader(stream, path.getName()).retrieveLines(key); + return new CsvStreamInputReader(stream, path.getName()).retrieveLinesForColumn(key); } catch (IOException exc){ throw new UncheckedIOException(exc); } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvInputReader.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvInputReader.java index 074c71074..fb7f711b7 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvInputReader.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvInputReader.java @@ -16,9 +16,11 @@ package com.scottlogic.datahelix.generator.profile.reader; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; + +import java.util.List; public interface CsvInputReader{ - DistributedList retrieveLines(); - DistributedList retrieveLines(String key); + List retrieveInSetElements(); + List retrieveLinesForColumn(String key); } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvStreamInputReader.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvStreamInputReader.java index 817d1146a..f0fbf80ca 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvStreamInputReader.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/CsvStreamInputReader.java @@ -17,16 +17,16 @@ package com.scottlogic.datahelix.generator.profile.reader; import com.scottlogic.datahelix.generator.common.ValidationException; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; -import java.io.*; +import java.io.IOException; +import java.io.InputStream; +import java.io.UncheckedIOException; import java.nio.charset.Charset; import java.util.List; -import java.util.Optional; import java.util.stream.Collectors; public class CsvStreamInputReader implements CsvInputReader { @@ -38,14 +38,14 @@ public CsvStreamInputReader(InputStream stream, String file) { this.file = file; } - public DistributedList retrieveLines() { + public List retrieveInSetElements() { List records = parse(stream); - return new DistributedList<>(records.stream() - .map(this::createWeightedElementFromRecord) - .collect(Collectors.toList())); + return records.stream() + .map(this::createInSetRecord) + .collect(Collectors.toList()); } - public DistributedList retrieveLines(String key) { + public List retrieveLinesForColumn(String key) { List records = parse(stream); int index = getIndexForKey(records.get(0), key); @@ -53,10 +53,9 @@ public DistributedList retrieveLines(String key) { //Remove the header records.remove(0); - return new DistributedList<>(records.stream() + return records.stream() .map(record -> record.get(index)) - .map(record -> createWeightedElement(record, Optional.empty())) - .collect(Collectors.toList())); + .collect(Collectors.toList()); } private static int getIndexForKey(CSVRecord header, String key) { @@ -70,26 +69,20 @@ private static int getIndexForKey(CSVRecord header, String key) { throw new ValidationException("unable to find data for key " + key); } - private WeightedElement createWeightedElementFromRecord(CSVRecord record) { + private InSetRecord createInSetRecord(CSVRecord record) { + final String value = record.get(0); try { - Optional weighting = record.size() == 1 - ? Optional.empty() - : Optional.of(Double.parseDouble(record.get(1))); - - return createWeightedElement(record.get(0), weighting); + return record.size() == 1 + ? new InSetRecord(value) + : new InSetRecord(value, Double.parseDouble(record.get(1))); } catch (NumberFormatException e) { throw new RuntimeException( "Weighting '" + record.get(1) + "' is not a valid number\n" + "CSV lines containing 2 columns must hold a weighting (double) in the second column, e.g. ,0.5\n" + - "Value: '" + record.get(0) + "', File: '" + this.file + "', Line " + record.getRecordNumber(), e); + "Value: '" + value + "', File: '" + this.file + "', Line " + record.getRecordNumber(), e); } } - private static WeightedElement createWeightedElement(String element, Optional weight) { - return weight.map(integer -> new WeightedElement<>(element, integer)) - .orElseGet(() -> WeightedElement.withDefaultWeight(element)); - } - private static List parse(InputStream stream) { try { CSVParser parser = CSVParser.parse(stream, Charset.defaultCharset(), CSVFormat.DEFAULT); diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/FileReader.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/FileReader.java index 817f6d4b0..f80762f14 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/FileReader.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/reader/FileReader.java @@ -16,11 +16,10 @@ package com.scottlogic.datahelix.generator.profile.reader; import com.google.inject.Inject; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import java.io.File; -import java.util.stream.Collectors; +import java.util.List; public class FileReader { private final CsvInputStreamReaderFactory csvReaderFactory; @@ -30,24 +29,11 @@ public FileReader(CsvInputStreamReaderFactory csvReaderFactory) { this.csvReaderFactory = csvReaderFactory; } - public DistributedList setFromFile(File file) { - CsvInputReader reader = csvReaderFactory.getReaderForFile(file); - DistributedList names = reader.retrieveLines(); - - return new DistributedList<>( - names.distributedList().stream() - .map(holder -> new WeightedElement<>((Object) holder.element(), holder.weight())) - .distinct() - .collect(Collectors.toList())); + public List setFromFile(File file) { + return csvReaderFactory.getReaderForFile(file).retrieveInSetElements(); } - public DistributedList listFromMapFile(File file, String key) { - CsvInputReader reader = csvReaderFactory.getReaderForFile(file); - DistributedList names = reader.retrieveLines(key); - - return new DistributedList<>( - names.distributedList().stream() - .map(holder -> new WeightedElement<>(holder.element(), holder.weight())) - .collect(Collectors.toList())); + public List listFromMapFile(File file, String key) { + return csvReaderFactory.getReaderForFile(file).retrieveLinesForColumn(key); } } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/serialisation/ConstraintDeserializer.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/serialisation/ConstraintDeserializer.java index e45f6c306..4ab524b5b 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/serialisation/ConstraintDeserializer.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/serialisation/ConstraintDeserializer.java @@ -22,12 +22,14 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintType; import com.scottlogic.datahelix.generator.profile.dtos.constraints.InvalidConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.InMapFromFileConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.InSetConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.InSetFromFileConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.InSetFromListConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.relations.InMapConstraintDTO; import com.scottlogic.datahelix.generator.profile.reader.FileReader; @@ -64,7 +66,7 @@ public ConstraintDTO deserialize(JsonParser jsonParser, DeserializationContext c case IN_SET: JsonNode inSetNode = node.get(InSetConstraintDTO.NAME); return (inSetNode.isNull() || inSetNode.isArray()) - ? mapper.treeToValue(node, InSetConstraintDTO.class) + ? map(mapper.treeToValue(node, InSetFromListConstraintDTO.class)) : map(mapper.treeToValue(node, InSetFromFileConstraintDTO.class)); case IN_MAP: return map(mapper.treeToValue(node, InMapFromFileConstraintDTO.class)); @@ -75,7 +77,8 @@ public ConstraintDTO deserialize(JsonParser jsonParser, DeserializationContext c private InMapConstraintDTO map(InMapFromFileConstraintDTO dto) { - List values = fileReader.listFromMapFile(getFile(dto.file), dto.key).stream().collect(Collectors.toList()); + List values = new ArrayList<>(fileReader.listFromMapFile(getFile(dto.file), dto.key)); + InMapConstraintDTO inMapConstraintDTO = new InMapConstraintDTO(); inMapConstraintDTO.field = dto.field; inMapConstraintDTO.otherField = dto.file; @@ -85,7 +88,20 @@ private InMapConstraintDTO map(InMapFromFileConstraintDTO dto) private InSetConstraintDTO map(InSetFromFileConstraintDTO dto) { - List values = new ArrayList<>(fileReader.setFromFile(getFile(dto.file)).distributedList()); + final List values = fileReader.setFromFile(getFile(dto.file)); + + InSetConstraintDTO inSetConstraintDTO = new InSetConstraintDTO(); + inSetConstraintDTO.field = dto.field; + inSetConstraintDTO.values = values; + return inSetConstraintDTO; + } + + private InSetConstraintDTO map(InSetFromListConstraintDTO dto) + { + final List values = dto.values != null + ? dto.values.stream().map(InSetRecord::new).collect(Collectors.toList()) + : null; + InSetConstraintDTO inSetConstraintDTO = new InSetConstraintDTO(); inSetConstraintDTO.field = dto.field; inSetConstraintDTO.values = values; diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/services/NameRetrievalService.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/services/NameRetrievalService.java index 0b0c0f5a2..fff281ebf 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/services/NameRetrievalService.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/services/NameRetrievalService.java @@ -17,14 +17,14 @@ package com.scottlogic.datahelix.generator.profile.services; import com.google.inject.Inject; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.NameConstraintTypes; import com.scottlogic.datahelix.generator.profile.reader.CsvInputStreamReaderFactory; import java.io.IOException; import java.io.InputStream; import java.io.UncheckedIOException; +import java.util.List; import java.util.stream.Collectors; import static com.scottlogic.datahelix.generator.core.profile.constraints.atomic.NameConstraintTypes.*; @@ -38,47 +38,39 @@ public NameRetrievalService(CsvInputStreamReaderFactory csvReaderFactory) { this.csvReaderFactory = csvReaderFactory; } - public DistributedList loadNamesFromFile(NameConstraintTypes configuration) { + public List loadNamesFromFile(NameConstraintTypes configuration) { if (configuration == FULL) { - return downcastToObject(combineFirstWithLastNames( - generateNamesFromSingleFile(FIRST.getFilePath()), - generateNamesFromSingleFile(LAST.getFilePath()))); + return combineFirstWithLastNames( + generateNamesFromSingleFile(FIRST.getFilePath()), + generateNamesFromSingleFile(LAST.getFilePath())); } else { - return downcastToObject(generateNamesFromSingleFile(configuration.getFilePath())); + return generateNamesFromSingleFile(configuration.getFilePath()); } } - private static DistributedList downcastToObject(DistributedList higher) { - return new DistributedList<>( - higher.distributedList().stream() - .map(holder -> new WeightedElement(holder.element(), holder.weight())) - .distinct() - .collect(Collectors.toList())); - } - - private DistributedList generateNamesFromSingleFile(String source) { + private List generateNamesFromSingleFile(String source) { try (InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(source)) { - return csvReaderFactory.getReaderForStream(stream, source).retrieveLines(); + return csvReaderFactory.getReaderForStream(stream, source).retrieveInSetElements(); } catch (IOException e) { throw new UncheckedIOException(e); } } - private static DistributedList combineFirstWithLastNames(DistributedList firstNames, - DistributedList lastNames) { - return new DistributedList<>(firstNames.distributedList().stream() + private static List combineFirstWithLastNames(List firstNames, + List lastNames) { + return firstNames.stream() .flatMap( - first -> lastNames.distributedList().stream() + first -> lastNames.stream() .map(last -> mergeFrequencies(first, last))) .distinct() - .collect(Collectors.toList())); + .collect(Collectors.toList()); } - private static WeightedElement mergeFrequencies(WeightedElement first, - WeightedElement last) { - String name = String.format("%s %s", first.element(), last.element()); - double frequency = first.weight() + last.weight(); - return new WeightedElement<>(name, frequency); + private static InSetRecord mergeFrequencies(InSetRecord first, + InSetRecord last) { + String name = String.format("%s %s", first.getElement(), last.getElement()); + double frequency = first.getWeightValueOrDefault() + last.getWeightValueOrDefault(); + return new InSetRecord(name, frequency); } } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidator.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidator.java index e83b7e0fd..040ca693b 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidator.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidator.java @@ -17,6 +17,7 @@ package com.scottlogic.datahelix.generator.profile.validators.profile.constraints.atomic; import com.scottlogic.datahelix.generator.common.profile.FieldType; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.common.validators.ValidationResult; import com.scottlogic.datahelix.generator.profile.dtos.FieldDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.InSetConstraintDTO; @@ -52,6 +53,9 @@ private ValidationResult fieldTypeMustBeValid(InSetConstraintDTO dto) { FieldType fieldType = FieldValidator.getSpecificFieldType(getField(dto.field)).getFieldType(); ValueTypeValidator valueTypeValidator = new ValueTypeValidator(fieldType, getErrorInfo(dto)); - return ValidationResult.combine(dto.values.stream().map(valueTypeValidator::validate)); + return ValidationResult.combine(dto.values.stream() + .map(InSetRecord::getElement) + .map(valueTypeValidator::validate) + ); } } diff --git a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/capabilities/ValueTypeValidator.java b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/capabilities/ValueTypeValidator.java index fc7bf7b15..e376196af 100644 --- a/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/capabilities/ValueTypeValidator.java +++ b/profile/src/main/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/capabilities/ValueTypeValidator.java @@ -19,7 +19,6 @@ import com.scottlogic.datahelix.generator.common.profile.FieldType; import com.scottlogic.datahelix.generator.common.validators.ValidationResult; import com.scottlogic.datahelix.generator.common.validators.Validator; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; public class ValueTypeValidator implements Validator { @@ -38,9 +37,6 @@ public final ValidationResult validate(Object value) if (value == null) { return ValidationResult.failure("Values must be specified" + errorInfo); } - if (value instanceof WeightedElement) { - return validate(((WeightedElement) value).element()); - } switch (expectedFieldType) { case BOOLEAN: diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/creation/AtomicConstraintDTOBuilder.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/creation/AtomicConstraintDTOBuilder.java index 558d1a53c..ca7ca2147 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/creation/AtomicConstraintDTOBuilder.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/creation/AtomicConstraintDTOBuilder.java @@ -16,6 +16,7 @@ package com.scottlogic.datahelix.generator.profile.creation; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.EqualToConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.GranularToConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.InSetConstraintDTO; @@ -28,6 +29,7 @@ import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.textual.ContainsRegexConstraintDTO; import java.util.List; +import java.util.stream.Collectors; public class AtomicConstraintDTOBuilder { @@ -67,7 +69,16 @@ public GranularToConstraintDTO buildGranularTo(Object value) return dto; } - public InSetConstraintDTO buildInSet(List values) + public InSetConstraintDTO buildInSetFromList(List values) + { + final List inSetRecords = values != null + ? values.stream().map(InSetRecord::new).collect(Collectors.toList()) + : null; + + return buildInSetFromRecords(inSetRecords); + } + + public InSetConstraintDTO buildInSetFromRecords(List values) { InSetConstraintDTO dto = new InSetConstraintDTO(); dto.field = field; diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/JsonProfileReaderTests.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/JsonProfileReaderTests.java index b35d99ee7..d155de40b 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/JsonProfileReaderTests.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/JsonProfileReaderTests.java @@ -18,12 +18,8 @@ import com.scottlogic.datahelix.generator.common.ValidationException; -import com.scottlogic.datahelix.generator.common.profile.DateTimeGranularity; -import com.scottlogic.datahelix.generator.common.profile.Field; -import com.scottlogic.datahelix.generator.common.profile.FieldType; -import com.scottlogic.datahelix.generator.common.profile.NumericGranularity; +import com.scottlogic.datahelix.generator.common.profile.*; import com.scottlogic.datahelix.generator.common.util.FileUtils; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.core.profile.Profile; import com.scottlogic.datahelix.generator.core.profile.constraints.Constraint; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.*; @@ -52,17 +48,19 @@ import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Iterator; +import java.util.List; import java.util.function.Consumer; import static com.scottlogic.datahelix.generator.common.util.Defaults.DEFAULT_DATE_FORMATTING; +import static java.util.Collections.singletonList; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.core.IsNull.nullValue; public class JsonProfileReaderTests { - private DistributedList inSetReaderReturnValue = DistributedList.singleton("test"); - private DistributedList fromFileReaderReturnValue = DistributedList.singleton("test"); + private final List inSetReaderReturnValue = singletonList(new InSetRecord("test")); + private final List fromFileReaderReturnValue = singletonList("test"); private class MockFromFileReader extends FileReader { public MockFromFileReader() { @@ -70,13 +68,13 @@ public MockFromFileReader() { } @Override - public DistributedList setFromFile(File file) + public List setFromFile(File file) { return inSetReaderReturnValue; } @Override - public DistributedList listFromMapFile(File file, String Key) + public List listFromMapFile(File file, String Key) { return fromFileReaderReturnValue; } diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/CsvStreamInputReaderTest.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/CsvStreamInputReaderTest.java index c71a5a362..e7d00c6d4 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/CsvStreamInputReaderTest.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/CsvStreamInputReaderTest.java @@ -15,13 +15,13 @@ */ package com.scottlogic.datahelix.generator.profile.reader.file; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.profile.reader.CsvInputReader; import com.scottlogic.datahelix.generator.profile.reader.CsvStreamInputReader; import org.junit.jupiter.api.Test; import java.io.InputStream; +import java.util.List; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -35,11 +35,15 @@ public void testReadingLinesFromNames() { final InputStream is = loader.getResourceAsStream("names/firstname.csv"); final CsvInputReader reader = new CsvStreamInputReader(is, "names/firstname.csv"); - final DistributedList names = reader.retrieveLines(); + final List nameElements = reader.retrieveInSetElements(); + final Set names = nameElements.stream() + .map(InSetRecord::getElement) + .collect(Collectors.toSet()); final Set sampleNames = Stream.of("Rory", "Kyle", "Grace").collect(Collectors.toSet()); - assertTrue(names.list().containsAll(sampleNames)); + assertTrue(names.containsAll(sampleNames)); + assertTrue(nameElements.stream().allMatch(InSetRecord::hasWeightPresent)); } @Test @@ -48,14 +52,15 @@ public void testReadingLinesFromFileWithoutFrequencies() { final InputStream is = loader.getResourceAsStream("csv/without-frequencies.csv"); final CsvInputReader reader = new CsvStreamInputReader(is, "csv/without-frequencies.csv"); - final DistributedList set = reader.retrieveLines(); + final List set = reader.retrieveInSetElements(); assertTrue(checkAllWeightsAreEquals(set)); + assertTrue(set.stream().noneMatch(InSetRecord::hasWeightPresent)); } - private boolean checkAllWeightsAreEquals(DistributedList set) { - return set.distributedList().stream() - .map(WeightedElement::weight) + private boolean checkAllWeightsAreEquals(List set) { + return set.stream() + .map(InSetRecord::getWeightValueOrDefault) .distinct() .limit(2).count() <= 1; } diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/names/NameRetrievalServiceTest.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/names/NameRetrievalServiceTest.java index e27cfec13..0cfccc995 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/names/NameRetrievalServiceTest.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/reader/file/names/NameRetrievalServiceTest.java @@ -17,15 +17,18 @@ package com.scottlogic.datahelix.generator.profile.reader.file.names; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.core.profile.constraints.atomic.NameConstraintTypes; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; import com.scottlogic.datahelix.generator.profile.reader.CsvInputStreamReaderFactory; import com.scottlogic.datahelix.generator.profile.services.NameRetrievalService; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -import static org.junit.jupiter.api.Assertions.*; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; public class NameRetrievalServiceTest { @@ -34,9 +37,9 @@ public void testLoadingFirstNames() { CsvInputStreamReaderFactory csvReaderFactory = new CsvInputStreamReaderFactory(); NameRetrievalService service = new NameRetrievalService(csvReaderFactory); - DistributedList names = service.loadNamesFromFile(NameConstraintTypes.FIRST); + List names = service.loadNamesFromFile(NameConstraintTypes.FIRST); - assertEquals(704, names.distributedList().size()); + assertEquals(704, names.size()); } @Test @@ -44,9 +47,9 @@ public void testLoadingLastNames() { CsvInputStreamReaderFactory csvReaderFactory = new CsvInputStreamReaderFactory(); NameRetrievalService service = new NameRetrievalService(csvReaderFactory); - DistributedList names = service.loadNamesFromFile(NameConstraintTypes.LAST); + List names = service.loadNamesFromFile(NameConstraintTypes.LAST); - assertEquals(280, names.distributedList().size()); + assertEquals(280, names.size()); } @Test @@ -54,9 +57,9 @@ public void testLoadingFullNames() { CsvInputStreamReaderFactory csvReaderFactory = new CsvInputStreamReaderFactory(); NameRetrievalService service = new NameRetrievalService(csvReaderFactory); - DistributedList names = service.loadNamesFromFile(NameConstraintTypes.FULL); + List names = service.loadNamesFromFile(NameConstraintTypes.FULL); - assertEquals(197120, names.distributedList().size()); + assertEquals(197120, names.size()); } @ParameterizedTest @@ -65,7 +68,7 @@ public void testAllValuesGiveValidResult(NameConstraintTypes config) { CsvInputStreamReaderFactory csvReaderFactory = new CsvInputStreamReaderFactory(); NameRetrievalService service = new NameRetrievalService(csvReaderFactory); - DistributedList result = service.loadNamesFromFile(config); + List result = service.loadNamesFromFile(config); assertNotNull(result); } diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/AtomicConstraintDeserialiserTests.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/AtomicConstraintDeserialiserTests.java similarity index 95% rename from profile/src/test/java/com/scottlogic/datahelix/generator/profile/AtomicConstraintDeserialiserTests.java rename to profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/AtomicConstraintDeserialiserTests.java index ff5616197..9ae090089 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/AtomicConstraintDeserialiserTests.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/AtomicConstraintDeserialiserTests.java @@ -14,12 +14,12 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.profile; +package com.scottlogic.datahelix.generator.profile.serialisation; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.module.SimpleModule; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.profile.dtos.constraints.ConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.EqualToConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.GranularToConstraintDTO; @@ -28,7 +28,10 @@ import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.LongerThanConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.OfLengthConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.integer.ShorterThanConstraintDTO; -import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.*; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.GreaterThanConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.GreaterThanOrEqualToConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.LessThanConstraintDTO; +import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.numeric.LessThanOrEqualToConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.temporal.AfterConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.temporal.AfterOrAtConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.temporal.BeforeConstraintDTO; @@ -37,7 +40,6 @@ import com.scottlogic.datahelix.generator.profile.dtos.constraints.atomic.textual.MatchesRegexConstraintDTO; import com.scottlogic.datahelix.generator.profile.dtos.constraints.relations.*; import com.scottlogic.datahelix.generator.profile.reader.FileReader; -import com.scottlogic.datahelix.generator.profile.serialisation.ConstraintDeserializer; import org.junit.Assert; import org.junit.Rule; import org.junit.jupiter.api.Test; @@ -45,11 +47,11 @@ import java.io.IOException; import java.nio.file.Paths; -import java.util.Arrays; -import java.util.Collections; import static com.shazam.shazamcrest.MatcherAssert.assertThat; import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs; +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; public class AtomicConstraintDeserialiserTests { @Rule @@ -96,7 +98,10 @@ public void shouldDeserialiseInSetWithoutException() throws IOException { // Assert InSetConstraintDTO expected = new InSetConstraintDTO(); expected.field = "type"; - expected.values = Arrays.asList("X_092", "0001-01-01T00:00:00.000Z"); + expected.values = asList( + new InSetRecord("X_092"), + new InSetRecord("0001-01-01T00:00:00.000Z") + ); assertThat(actual, sameBeanAs(expected)); } @@ -111,7 +116,7 @@ public void shouldDeserialiseInSetCsvFileWithoutException() throws IOException { // Assert InSetConstraintDTO expected = new InSetConstraintDTO(); expected.field = "country"; - expected.values = Collections.singletonList(new WeightedElement<>("test", 1.0)); + expected.values = singletonList(new InSetRecord("test")); assertThat(actual, sameBeanAs(expected)); } @@ -126,9 +131,9 @@ public void shouldDeserialiseWeightedInSetCsvFile() throws IOException { // Assert InSetConstraintDTO expected = new InSetConstraintDTO(); expected.field = "country"; - expected.values = Arrays.asList( - new WeightedElement<>("test1", 0.2), - new WeightedElement<>("test2", 0.8) + expected.values = asList( + new InSetRecord("test1", 20.0), + new InSetRecord("test2", 80.0) ); assertThat(actual, sameBeanAs(expected)); @@ -146,7 +151,7 @@ public void shouldDeserialiseInMapWithoutException() throws IOException { InMapConstraintDTO expected = new InMapConstraintDTO(); expected.field = "country"; expected.otherField = "countries.csv"; - expected.values = Collections.singletonList("test"); + expected.values = singletonList("test"); assertThat(actual, sameBeanAs(expected)); } diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/FieldDeserialiserTests.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/FieldDeserialiserTests.java similarity index 98% rename from profile/src/test/java/com/scottlogic/datahelix/generator/profile/FieldDeserialiserTests.java rename to profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/FieldDeserialiserTests.java index 6f8e573b7..d5b56abb8 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/FieldDeserialiserTests.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/FieldDeserialiserTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.profile; +package com.scottlogic.datahelix.generator.profile.serialisation; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/GrammaticalConstraintDeserialiserTests.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/GrammaticalConstraintDeserialiserTests.java similarity index 98% rename from profile/src/test/java/com/scottlogic/datahelix/generator/profile/GrammaticalConstraintDeserialiserTests.java rename to profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/GrammaticalConstraintDeserialiserTests.java index 163d755d9..0b20155dd 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/GrammaticalConstraintDeserialiserTests.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/GrammaticalConstraintDeserialiserTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package com.scottlogic.datahelix.generator.profile; +package com.scottlogic.datahelix.generator.profile.serialisation; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.databind.ObjectMapper; @@ -32,7 +32,6 @@ import com.scottlogic.datahelix.generator.profile.dtos.constraints.grammatical.NotConstraintDTO; import com.scottlogic.datahelix.generator.profile.reader.CsvInputStreamReaderFactory; import com.scottlogic.datahelix.generator.profile.reader.FileReader; -import com.scottlogic.datahelix.generator.profile.serialisation.ConstraintDeserializer; import org.junit.Assert; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/TestFileReader.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/TestFileReader.java similarity index 59% rename from profile/src/test/java/com/scottlogic/datahelix/generator/profile/TestFileReader.java rename to profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/TestFileReader.java index f163df6d3..87a747ec5 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/TestFileReader.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/serialisation/TestFileReader.java @@ -13,17 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.scottlogic.datahelix.generator.profile; +package com.scottlogic.datahelix.generator.profile.serialisation; -import com.scottlogic.datahelix.generator.common.whitelist.DistributedList; -import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; +import com.scottlogic.datahelix.generator.common.profile.InSetRecord; import com.scottlogic.datahelix.generator.profile.reader.FileReader; import java.io.File; import java.util.Arrays; -import java.util.Collections; import java.util.List; +import static java.util.Collections.singletonList; + public class TestFileReader extends FileReader { private final boolean weighted; @@ -38,22 +38,21 @@ public TestFileReader(boolean weighted) { } @Override - public DistributedList setFromFile(File file) { + public List setFromFile(File file) { return weighted ? this.getDistributedListWithWeights() - : DistributedList.uniform(Collections.singleton("test")); + : singletonList(new InSetRecord("test")); } @Override - public DistributedList listFromMapFile(File file, String key) { - return DistributedList.uniform(Collections.singleton("test")); + public List listFromMapFile(File file, String key) { + return singletonList("test"); } - private static DistributedList getDistributedListWithWeights() { - List elements = Arrays.asList( - new WeightedElement<>("test1", 20), - new WeightedElement<>("test2", 80) + private static List getDistributedListWithWeights() { + return Arrays.asList( + new InSetRecord("test1", 20), + new InSetRecord("test2", 80) ); - return DistributedList.weightedOrDefault(elements); } } diff --git a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidatorTests.java b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidatorTests.java index 577368201..1e3b08a94 100644 --- a/profile/src/test/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidatorTests.java +++ b/profile/src/test/java/com/scottlogic/datahelix/generator/profile/validators/profile/constraints/atomic/InSetConstraintValidatorTests.java @@ -29,9 +29,7 @@ import static java.util.Collections.emptyList; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.iterableWithSize; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; public class InSetConstraintValidatorTests { @@ -56,7 +54,7 @@ public class InSetConstraintValidatorTests public void validateInSetConstraint_withValidField_succeeds() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSet(stringValues); + InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSetFromList(stringValues); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto); @@ -69,7 +67,7 @@ public void validateInSetConstraint_withValidField_succeeds() public void validateInSetConstraint_withNullField_fails() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO(null).buildInSet(stringValues); + InSetConstraintDTO dto = atomicConstraintDTO(null).buildInSetFromList(stringValues); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto); @@ -84,7 +82,7 @@ public void validateInSetConstraint_withNullField_fails() public void validateInSetConstraint_withEmptyField_fails() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO("").buildInSet(stringValues); + InSetConstraintDTO dto = atomicConstraintDTO("").buildInSetFromList(stringValues); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto); @@ -99,7 +97,7 @@ public void validateInSetConstraint_withEmptyField_fails() public void validateInSetConstraint_withUndefinedField_fails() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO("unknown").buildInSet(stringValues); + InSetConstraintDTO dto = atomicConstraintDTO("unknown").buildInSetFromList(stringValues); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto); @@ -114,7 +112,7 @@ public void validateInSetConstraint_withUndefinedField_fails() public void validateInSetConstraint_withEmptyValue_fails() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSet(emptyList()); + InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSetFromList(emptyList()); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto); @@ -129,7 +127,7 @@ public void validateInSetConstraint_withEmptyValue_fails() public void validateInSetConstraint_withNullValue_fails() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSet(null); + InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSetFromList(null); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto); @@ -144,7 +142,7 @@ public void validateInSetConstraint_withNullValue_fails() public void validateInSetConstraint_withInvalidData_fails() { // Arrange - InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSet(valuesOfAllTypes); + InSetConstraintDTO dto = atomicConstraintDTO("text").buildInSetFromList(valuesOfAllTypes); // Act ValidationResult validationResult = new InSetConstraintValidator(fields).validate(dto);