diff --git a/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/gen/DictionaryAttrGen.java b/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/gen/DictionaryAttrGen.java index 1336b4e..141b7d6 100644 --- a/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/gen/DictionaryAttrGen.java +++ b/tdrules-store-loader/src/main/java/giis/tdrules/store/loader/gen/DictionaryAttrGen.java @@ -34,6 +34,8 @@ public class DictionaryContainer { private char padChar = ' '; private int padSize = 0; + ConstraintInteger intConstraint = null; + public void reset() { lastIndex = -1; lastCycle = 0; @@ -112,6 +114,17 @@ public DictionaryAttrGen padLeft(char padChar, int padSize) { return this; } + /** + * Sets an interval to generated values (applicable to numbers), + * even if the schema does not specify min/max + */ + public DictionaryAttrGen setInterval(int min, int max) { + currentConfiguringContainer.intConstraint = new ConstraintInteger(); + currentConfiguringContainer.intConstraint.add(">=", String.valueOf(min)); + currentConfiguringContainer.intConstraint.add("<=", String.valueOf(max)); + return this; + } + // Internal methods to manage the dictionary // returns null if no dictionary container found for the coordinates @@ -163,6 +176,9 @@ public String generateNumber(IConstraint constraints, String entityName, String DictionaryContainer container = getDictionary(entityName, attrName); String value = super.generateNumber(constraints, entityName, attrName); + if (container != null && container.intConstraint != null) + value = container.intConstraint.apply(value); + if (container != null) value = container.padValue(value); diff --git a/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/TestGenerationSettings.java b/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/TestGenerationSettings.java index 966baf6..9a45f16 100644 --- a/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/TestGenerationSettings.java +++ b/tdrules-store-loader/src/test/java/test4giis/tdrules/store/loader/TestGenerationSettings.java @@ -6,6 +6,7 @@ import giis.tdrules.store.loader.IAttrGen; import giis.tdrules.store.loader.gen.DeterministicAttrGen; +import giis.tdrules.store.loader.gen.DictionaryAttrGen; /** * How optional settings of the Attribute Generators influence the generated values @@ -37,5 +38,24 @@ public void testGenerateDatesMinYear() { gen.incrementAttrCount(); assertEquals("2054-01-04", gen.generateDate()); } + + // Limits to int values (when the schema does not specify any) must + // be set through a dictionary because are attribute specific + @Test + public void testGenerateNumberInInterval() { + IAttrGen gen= new DictionaryAttrGen().with("entity", "attr").setInterval(2, 4); + assertEquals("4", gen.generateNumber(null, "entity", "attr")); // first is 1, out of rang, produces 4 + gen.incrementAttrCount(); + assertEquals("2", gen.generateNumber(null, "entity", "attr")); //2 + gen.incrementAttrCount(); + assertEquals("3", gen.generateNumber(null, "entity", "attr")); //3 + gen.incrementAttrCount(); + assertEquals("4", gen.generateNumber(null, "entity", "attr")); //4 + gen.incrementAttrCount(); + assertEquals("2", gen.generateNumber(null, "entity", "attr")); //5 out of range, produces 2 + gen.incrementAttrCount(); + assertEquals("3", gen.generateNumber(null, "entity", "attr")); + + } }