This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1398 from finos/1390-create-in-map-constraint
chore(#1390): added inMap constraint to the generation engine
- Loading branch information
Showing
7 changed files
with
188 additions
and
8 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
...src/main/java/com/scottlogic/deg/common/profile/constraints/atomic/IsInMapConstraint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* 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.common.profile.constraints.atomic; | ||
|
||
import com.scottlogic.deg.common.profile.Field; | ||
import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
public class IsInMapConstraint implements AtomicConstraint { | ||
public final Field field; | ||
public final DistributedList<Object> legalValues; | ||
|
||
public IsInMapConstraint(Field field, DistributedList<Object> legalValues) { | ||
this.field = field; | ||
this.legalValues = legalValues; | ||
|
||
if (legalValues.distributedList().isEmpty()) { | ||
throw new IllegalArgumentException("Cannot create an IsInMapConstraint for field '" + | ||
field.name + "' with an empty set."); | ||
} | ||
|
||
if (legalValues.list().contains(null)) { | ||
throw new IllegalArgumentException("Cannot create an IsInMapConstraint for field '" + | ||
field.name + "' with a list containing null."); | ||
} | ||
} | ||
|
||
@Override | ||
public Field getField() { | ||
return field; | ||
} | ||
|
||
public String toString(){ | ||
boolean overLimit = legalValues.list().size() > 3; | ||
return String.format("%s in [%s%s](%d values)", | ||
field.name, | ||
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 instanceof ViolatedAtomicConstraint) { | ||
return o.equals(this); | ||
} | ||
if (o == null || getClass() != o.getClass()) return false; | ||
IsInMapConstraint constraint = (IsInMapConstraint) o; | ||
return Objects.equals(field, constraint.field) && Objects.equals(legalValues, constraint.legalValues); | ||
} | ||
|
||
@Override | ||
public int hashCode(){ | ||
return Objects.hash(field, legalValues); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...est/java/com/scottlogic/deg/common/profile/constraints/atomic/IsInMapConstraintTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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.common.profile.constraints.atomic; | ||
|
||
import com.scottlogic.deg.common.profile.Field; | ||
import com.scottlogic.deg.generator.fieldspecs.whitelist.DistributedList; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static com.scottlogic.deg.common.profile.FieldBuilder.createField; | ||
|
||
public class IsInMapConstraintTests { | ||
|
||
@Test | ||
public void testConstraintThrowsIfGivenEmptySet(){ | ||
Field field1 = createField("TestField"); | ||
|
||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> new IsInMapConstraint(field1, DistributedList.empty())); | ||
} | ||
|
||
@Test | ||
public void testConstraintThrowsIfGivenNullInASet(){ | ||
Field field1 = createField("TestField"); | ||
|
||
Assertions.assertThrows( | ||
IllegalArgumentException.class, | ||
() -> new IsInMapConstraint(field1, DistributedList.singleton(null))); | ||
} | ||
|
||
@Test | ||
public void testConstraintThrowsNothingIfGivenAValidSet(){ | ||
Field field1 = createField("TestField"); | ||
Assertions.assertDoesNotThrow( | ||
() -> new IsInMapConstraint(field1, DistributedList.singleton("foo"))); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters