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

Commit

Permalink
Merge branch 'master' into 1235-generic-dependent-fields
Browse files Browse the repository at this point in the history
  • Loading branch information
pdaulbyscottlogic authored Oct 2, 2019
2 parents f007f84 + cd7a652 commit 01a3be4
Show file tree
Hide file tree
Showing 35 changed files with 308 additions and 180 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,18 @@ public class Field {
public final Types type;
private final boolean unique;
private final String formatting;
private final boolean internal;

public Field(String name, Types type, Boolean unique, String formatting) {
public Field(String name, Types type, boolean unique, String formatting, boolean internal) {
this.name = name;
this.type = type;
this.unique = unique;
this.formatting = formatting;
this.internal = internal;
}

public boolean isInternal() {
return internal;
}

public boolean isUnique() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ public Stream<Field> stream() {
return this.fields.stream();
}

public Stream<Field> getExternalStream() {
return this.stream().filter(f -> !f.isInternal());
}

public List<Field> asList() {
return fields;
}
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public class FieldBuilder {
public static Field createField(String name) {
return createField(name, Types.STRING);
}
public static Field createInternalField(String name) {
return new Field(name, Types.STRING, false, null, true);
}
public static Field createField(String name, Types type) {
return new Field(name, type, false, null);
return new Field(name, type, false, null, false);
}
}
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")));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ private FieldSpec construct(AtomicConstraint constraint, boolean negate) {
return construct(((NotConstraint) constraint).negatedConstraint, !negate);
} else if (constraint instanceof IsInSetConstraint) {
return construct((IsInSetConstraint) constraint, negate);
} else if (constraint instanceof IsInMapConstraint) {
return construct((IsInMapConstraint) constraint, negate);
} else if (constraint instanceof EqualToConstraint) {
return construct((EqualToConstraint) constraint, negate);
} else if (constraint instanceof IsGreaterThanConstantConstraint) {
Expand Down Expand Up @@ -101,6 +103,14 @@ private FieldSpec construct(IsInSetConstraint constraint, boolean negate) {
return FieldSpec.fromList(constraint.legalValues);
}

private FieldSpec construct(IsInMapConstraint constraint, boolean negate) {
if (negate) {
throw new UnsupportedOperationException("negation of inMap not supported");
}

return FieldSpec.fromList(constraint.legalValues);
}

private FieldSpec construct(EqualToConstraint constraint, boolean negate) {
if (negate) {
return FieldSpec.empty().withBlacklist(Collections.singleton(constraint.value));
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ void generateRandom_uniqueFieldSpec_returnsAllValues() {
randomNumberGenerator
);

fieldSpecFulfiller.generate(new Field(null, Types.STRING, true, null), fieldSpec).collect(Collectors.toSet());
fieldSpecFulfiller.generate(new Field(null, Types.STRING, true, null, false), fieldSpec).collect(Collectors.toSet());

verify(fieldValueSource, times(1)).generateAllValues();
verify(fieldValueSource, times(0)).generateInterestingValues();
Expand Down Expand Up @@ -155,7 +155,7 @@ void generateInteresting_uniqueFieldSpec_returnsAllValues() {
randomNumberGenerator
);

fieldSpecFulfiller.generate(new Field(null, STRING, true, null), fieldSpec).collect(Collectors.toSet());
fieldSpecFulfiller.generate(new Field(null, STRING, true, null, false), fieldSpec).collect(Collectors.toSet());

verify(fieldValueSource, times(1)).generateAllValues();
verify(fieldValueSource, times(0)).generateInterestingValues();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public boolean useStdOut() {

@Override
public boolean useNdJson() {
return (ndjson == null && this.useStdOut()) || ndjson;
return ndjson == null ? this.useStdOut() : ndjson;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@
import com.google.inject.Inject;
import com.scottlogic.deg.common.output.GeneratedObject;
import com.scottlogic.deg.common.profile.Profile;
import com.scottlogic.deg.common.profile.ViolatedProfile;
import com.scottlogic.deg.generator.generation.DataGenerator;
import com.scottlogic.deg.generator.inputs.profileviolation.ProfileViolator;
import com.scottlogic.deg.orchestrator.violate.violator.ProfileViolator;
import com.scottlogic.deg.generator.inputs.validation.ProfileValidator;
import com.scottlogic.deg.common.util.FileUtils;
import com.scottlogic.deg.output.manifest.ManifestWriter;
import com.scottlogic.deg.orchestrator.violate.manifest.ManifestWriter;
import com.scottlogic.deg.output.outputtarget.OutputTargetFactory;
import com.scottlogic.deg.output.outputtarget.SingleDatasetOutputTarget;
import com.scottlogic.deg.output.writer.DataSetWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.scottlogic.deg.orchestrator.guice.AllConfigSource;
import com.scottlogic.deg.generator.guice.GeneratorModule;
import com.scottlogic.deg.generator.inputs.profileviolation.IndividualConstraintRuleViolator;
import com.scottlogic.deg.generator.inputs.profileviolation.IndividualRuleProfileViolator;
import com.scottlogic.deg.generator.inputs.profileviolation.ProfileViolator;
import com.scottlogic.deg.generator.inputs.profileviolation.RuleViolator;
import com.scottlogic.deg.orchestrator.violate.violator.RuleViolator;
import com.scottlogic.deg.generator.violations.filters.ViolationFilter;
import com.scottlogic.deg.orchestrator.guice.AllModule;
import com.scottlogic.deg.profile.guice.ProfileModule;

import java.util.List;

Expand All @@ -42,9 +37,6 @@ protected void configure() {
bind(AllConfigSource.class).toInstance(configSource);
bind(ViolateConfigSource.class).toInstance(configSource);

bind(ProfileViolator.class).to(IndividualRuleProfileViolator.class);
bind(RuleViolator.class).to(IndividualConstraintRuleViolator.class);

bind(new TypeLiteral<List<ViolationFilter>>(){}).toProvider(ViolationFiltersProvider.class);

install(new AllModule(configSource));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
* limitations under the License.
*/

package com.scottlogic.deg.common.profile;
package com.scottlogic.deg.orchestrator.violate;

import com.scottlogic.deg.common.profile.Profile;
import com.scottlogic.deg.common.profile.ProfileFields;
import com.scottlogic.deg.common.profile.Rule;

import java.util.Collection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.scottlogic.deg.output.manifest;
package com.scottlogic.deg.orchestrator.violate.manifest;

import java.util.Collection;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
* limitations under the License.
*/

package com.scottlogic.deg.output.manifest;
package com.scottlogic.deg.orchestrator.violate.manifest;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Charsets;
import com.google.inject.Inject;
import com.scottlogic.deg.common.profile.ViolatedProfile;
import com.scottlogic.deg.orchestrator.violate.ViolatedProfile;
import com.scottlogic.deg.common.util.FileUtils;
import com.scottlogic.deg.output.OutputPath;

Expand All @@ -36,11 +36,11 @@
* write out a JSON manifest file during violation.
* This file shows which rule has been violated in which output file.
*/
public class JsonManifestWriter implements ManifestWriter {
public class ManifestWriter {
private final Path outputPath;

@Inject
public JsonManifestWriter(OutputPath outputPath){
public ManifestWriter(OutputPath outputPath){
this.outputPath = outputPath.getPath();
}

Expand Down
Loading

0 comments on commit 01a3be4

Please sign in to comment.