Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1528 from cdowding-sl/fix/1361
Browse files Browse the repository at this point in the history
Implemented string length validation for textual constraints
  • Loading branch information
cdowding-sl authored Nov 11, 2019
2 parents 402c219 + cf725f9 commit b22e440
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ Feature: User can specify that a string length is longer than, a specified numbe
| null |
| "a" |

Scenario: 'longerThan' a negative number should fail with an error
Given foo is longer than -5
Then the profile is invalid because "String length must have a value >= 0, currently is -5"
And no data is created

Scenario: Multiple non-contradictory 'longerThan' requests should be successful
Given foo is longer than 2
And foo is longer than 1
Expand Down Expand Up @@ -255,3 +250,9 @@ Feature: User can specify that a string length is longer than, a specified numbe
And foo is longer than 999
And the generator can generate at most 20 rows
Then foo contains strings of length between 1000 and 1000 inclusively

Scenario: longerThan with less than minimum permitted value should be successful
Given foo is longer than -1
And the generation strategy is random
And the generator can generate at most 1 rows
Then foo contains strings of length between 0 and 1000 inclusively
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,15 @@ Feature: User can specify that a string length is lower than, a specified number
| foo |
| null |

@ignore #1361 shorter than 1001 not accepted
Scenario: shorterThan with maximum permitted value should be successful
Scenario: shorterThan with more than maximum permitted value should be successful
Given foo is shorter than 1001
And the generation strategy is random
And the generator can generate at most 1 rows
Then foo contains strings of length between 0 and 1000 inclusively

Scenario: shorterThan with value larger than maximum permitted should fail with an error message
Given foo is shorter than 1002
Then the profile is invalid because "String length must have a value <= 1000, currently is 1002"
Scenario: shorterThan with value less than minimum permitted should fail with an error message
Given foo is shorter than -1
Then the profile is invalid because "String length must have a value >= 0, currently is -1"

Scenario: Running a 'shorterThan' request with a value less than implicit max (255) should generate data of length between 0 and value
Given foo has type "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,29 @@
import com.scottlogic.deg.profile.dtos.FieldDTO;
import com.scottlogic.deg.profile.dtos.constraints.ConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.InvalidConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.grammatical.NotConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.EqualToConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.GranularToConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.InSetConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.IsNullConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.StringLengthConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.LongerThanConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.OfLengthConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.ShorterThanConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.numeric.NumericConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.temporal.TemporalConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.textual.RegexConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.grammatical.AllOfConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.grammatical.AnyOfConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.grammatical.ConditionalConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.grammatical.NotConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.relations.InMapConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.EqualToConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.GranularToConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.numeric.NumericConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.temporal.*;
import com.scottlogic.deg.profile.dtos.constraints.atomic.textual.RegexConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.relations.RelationalConstraintDTO;
import com.scottlogic.deg.profile.validators.profile.constraints.InMapConstraintValidator;
import com.scottlogic.deg.profile.validators.profile.constraints.NotConstraintValidator;
import com.scottlogic.deg.profile.validators.profile.constraints.RelationalConstraintValidator;
import com.scottlogic.deg.profile.validators.profile.constraints.atomic.*;
import com.scottlogic.deg.profile.validators.profile.constraints.grammatical.AllOfConstraintValidator;
import com.scottlogic.deg.profile.validators.profile.constraints.grammatical.AnyOfConstraintValidator;
import com.scottlogic.deg.profile.validators.profile.constraints.grammatical.ConditionalConstraintValidator;
import com.scottlogic.deg.profile.validators.profile.constraints.RelationalConstraintValidator;

import java.util.List;

Expand Down Expand Up @@ -92,9 +94,11 @@ protected static ValidationResult validateConstraint(ConstraintDTO dto, String r
case BEFORE_OR_AT_FIELD:
return new RelationalConstraintValidator<>(rule, fields).validate((RelationalConstraintDTO) dto);
case OF_LENGTH:
return new OfLengthConstraintValidator(rule, fields, FieldType.STRING).validate((OfLengthConstraintDTO) dto);
case LONGER_THAN:
return new LongerThanConstraintValidator(rule, fields, FieldType.STRING).validate((LongerThanConstraintDTO) dto);
case SHORTER_THAN:
return new StringLengthConstraintValidator(rule, fields, FieldType.STRING).validate((StringLengthConstraintDTO) dto);
return new ShorterThanConstraintValidator(rule, fields, FieldType.STRING).validate((ShorterThanConstraintDTO) dto);
case GREATER_THAN:
case GREATER_THAN_OR_EQUAL_TO:
case LESS_THAN:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.scottlogic.deg.profile.validators.profile.constraints.atomic;

import com.scottlogic.deg.common.profile.FieldType;
import com.scottlogic.deg.common.util.Defaults;
import com.scottlogic.deg.common.validators.ValidationResult;
import com.scottlogic.deg.profile.dtos.FieldDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.LongerThanConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.StringLengthConstraintDTO;

import java.math.BigDecimal;
import java.util.List;

public class LongerThanConstraintValidator extends AtomicConstraintValidator<LongerThanConstraintDTO>
{
private final FieldType expectedFieldType;

public LongerThanConstraintValidator(String rule, List<FieldDTO> fields, FieldType expectedFieldType)
{
super(rule, fields);
this.expectedFieldType = expectedFieldType;
}

@Override
public final ValidationResult validate(LongerThanConstraintDTO dto)
{
ValidationResult fieldMustBeValid = fieldMustBeValid(dto);
if(!fieldMustBeValid.isSuccess) return fieldMustBeValid;

ValidationResult stringLengthMustBeValid = stringLengthMustBeValid(dto);
if(!stringLengthMustBeValid.isSuccess) return stringLengthMustBeValid;

return fieldTypeMustMatchValueType(dto, expectedFieldType);
}

private ValidationResult stringLengthMustBeValid(StringLengthConstraintDTO dto)
{
BigDecimal integer = BigDecimal.valueOf(dto.stringLength());
BigDecimal max = BigDecimal.valueOf(Defaults.MAX_STRING_LENGTH);
return integer.compareTo(max) <= 0
? ValidationResult.success()
: ValidationResult.failure(String.format("String length must have a value <= %s, currently is %s"
, max.toPlainString(), integer.toPlainString()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,30 @@
import com.scottlogic.deg.common.util.Defaults;
import com.scottlogic.deg.common.validators.ValidationResult;
import com.scottlogic.deg.profile.dtos.FieldDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.OfLengthConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.StringLengthConstraintDTO;

import java.math.BigDecimal;
import java.util.List;

public class StringLengthConstraintValidator extends AtomicConstraintValidator<StringLengthConstraintDTO>
public class OfLengthConstraintValidator extends AtomicConstraintValidator<OfLengthConstraintDTO>
{
private final FieldType expectedFieldType;

public StringLengthConstraintValidator(String rule, List<FieldDTO> fields, FieldType expectedFieldType)
public OfLengthConstraintValidator(String rule, List<FieldDTO> fields, FieldType expectedFieldType)
{
super(rule, fields);
this.expectedFieldType = expectedFieldType;
}

@Override
public final ValidationResult validate(StringLengthConstraintDTO dto)
public final ValidationResult validate(OfLengthConstraintDTO dto)
{
ValidationResult fieldMustBeValid = fieldMustBeValid(dto);
if(!fieldMustBeValid.isSuccess) return fieldMustBeValid;

ValidationResult integerMustBeValid = stringLengthMustBeValid(dto);
if(!integerMustBeValid.isSuccess) return integerMustBeValid;
ValidationResult stringLengthMustBeValid = stringLengthMustBeValid(dto);
if(!stringLengthMustBeValid.isSuccess) return stringLengthMustBeValid;

return fieldTypeMustMatchValueType(dto, expectedFieldType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright 2019 Scott Logic Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.scottlogic.deg.profile.validators.profile.constraints.atomic;

import com.scottlogic.deg.common.profile.FieldType;
import com.scottlogic.deg.common.validators.ValidationResult;
import com.scottlogic.deg.profile.dtos.FieldDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.ShorterThanConstraintDTO;
import com.scottlogic.deg.profile.dtos.constraints.atomic.integer.StringLengthConstraintDTO;

import java.math.BigDecimal;
import java.util.List;

public class ShorterThanConstraintValidator extends AtomicConstraintValidator<ShorterThanConstraintDTO>
{
private final FieldType expectedFieldType;

public ShorterThanConstraintValidator(String rule, List<FieldDTO> fields, FieldType expectedFieldType)
{
super(rule, fields);
this.expectedFieldType = expectedFieldType;
}

@Override
public final ValidationResult validate(ShorterThanConstraintDTO dto)
{
ValidationResult fieldMustBeValid = fieldMustBeValid(dto);
if(!fieldMustBeValid.isSuccess) return fieldMustBeValid;

ValidationResult stringLengthMustBeValid = stringLengthMustBeValid(dto);
if(!stringLengthMustBeValid.isSuccess) return stringLengthMustBeValid;

return fieldTypeMustMatchValueType(dto, expectedFieldType);
}

private ValidationResult stringLengthMustBeValid(StringLengthConstraintDTO dto)
{
BigDecimal integer = BigDecimal.valueOf(dto.stringLength());
BigDecimal min = BigDecimal.ZERO;
return integer.compareTo(min) >= 0
? ValidationResult.success()
: ValidationResult.failure(String.format("String length must have a value >= %s, currently is %s"
, min.toPlainString(), integer.toPlainString()));
}
}

0 comments on commit b22e440

Please sign in to comment.