diff --git a/README.md b/README.md index 34b4d0f2..8e026c4d 100644 --- a/README.md +++ b/README.md @@ -215,6 +215,7 @@ If you believe you found a bug or have any question, please use the [issue track * [Adriano Machado](https://github.com/ammachado) * [Alberto Lagna](https://github.com/alagna) * [Andrew Neal](https://github.com/aeneal) +* [Aurélien Mino](https://github.com/murdos) * [Arne Zelasko](https://github.com/arnzel) * [dadiyang](https://github.com/dadiyang) * [Dovid Kopel](https://github.com/dovidkopel) diff --git a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationMethodAnnotatedBean.java b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationMethodAnnotatedBean.java index 80792e05..917d3c65 100644 --- a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationMethodAnnotatedBean.java +++ b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationMethodAnnotatedBean.java @@ -87,6 +87,8 @@ class BeanValidationMethodAnnotatedBean { private List sizedList; + private List sizedListEmbeddedBean; + private Set sizedSet; private Map sizedMap; @@ -369,6 +371,15 @@ public void setSizedList(List sizedList) { this.sizedList = sizedList; } + @Size(min = 2, max = 10) + public List getSizedListEmbeddedBean() { + return sizedListEmbeddedBean; + } + + public void setSizedListEmbeddedBean(List sizedListEmbeddedBean) { + this.sizedListEmbeddedBean = sizedListEmbeddedBean; + } + @Size(min=2, max=10) public Set getSizedSet() { return sizedSet; diff --git a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java index 338af3e1..a3933ca6 100644 --- a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java +++ b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/BeanValidationTest.java @@ -74,6 +74,23 @@ void sizeConstraintShouldNotBePropagatedToEmbeddedBeans() { }); } + @Test + void sizeConstraintShouldTakePrecedenceOverCollectionSizeRangeInEmbeddedBeans() { + EasyRandomParameters parameters = new EasyRandomParameters() + .collectionSizeRange(11, 15) + .stringLengthRange(16, 20); + easyRandom = new EasyRandom(parameters); + BeanValidationAnnotatedBean bean = easyRandom.nextObject(BeanValidationAnnotatedBean.class); + + assertThat(bean.getSizedListEmbeddedBean().size()).isBetween(2, 10); // @Size constraint + assertThat(bean.getSizedListEmbeddedBean()).allSatisfy(embeddedBean -> { + assertThat(embeddedBean.getItems().size()).isBetween(11, 15); + assertThat(embeddedBean.getOtherItems().size()).isBetween(3, 5); // @Size(min = 3, max = 5) constraint should take precedence over collectionSizeRange(11, 15) + assertThat(embeddedBean.getItems()).allSatisfy(stringItem -> assertThat(stringItem.length()).isBetween(16, 20)); + assertThat(embeddedBean.getOtherItems()).allSatisfy(stringItem -> assertThat(stringItem.length()).isBetween(16, 20)); + }); + } + @Test void generatedValuesShouldBeValidAccordingToValidationConstraints() { BeanValidationAnnotatedBean bean = easyRandom.nextObject(BeanValidationAnnotatedBean.class); @@ -136,6 +153,7 @@ void generatedValuesShouldBeValidAccordingToValidationConstraints() { assertThat(bean.getBriefMessage().length()).isBetween(2, 10);// @Size(min=2, max=10) String briefMessage; assertThat(bean.getSizedCollection().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedCollection; assertThat(bean.getSizedList().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedList; + assertThat(bean.getSizedListEmbeddedBean().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedListEmbeddedBean; assertThat(bean.getSizedSet().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedSet; assertThat(bean.getSizedMap().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedMap; assertThat(bean.getSizedArray().length).isBetween(2, 10);// @Size(min=2, max=10) String sizedArray; @@ -206,6 +224,7 @@ void generatedValuesShouldBeValidAccordingToValidationConstraintsOnMethod() { assertThat(bean.getBriefMessage().length()).isBetween(2, 10);// @Size(min=2, max=10) String briefMessage; assertThat(bean.getSizedCollection().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedCollection; assertThat(bean.getSizedList().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedList; + assertThat(bean.getSizedListEmbeddedBean().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedListEmbeddedBean; assertThat(bean.getSizedSet().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedSet; assertThat(bean.getSizedMap().size()).isBetween(2, 10);// @Size(min=2, max=10) String sizedMap; assertThat(bean.getSizedArray().length).isBetween(2, 10);// @Size(min=2, max=10) String sizedArray; diff --git a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java index ec65b4b9..92aa7520 100644 --- a/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java +++ b/easy-random-bean-validation/src/test/java/org/jeasy/random/validation/EmbeddedBean.java @@ -1,16 +1,52 @@ +/* + * The MIT License + * + * Copyright (c) 2020, Mahmoud Ben Hassine (mahmoud.benhassine@icloud.com) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package org.jeasy.random.validation; import java.util.List; +import javax.validation.constraints.Size; + public class EmbeddedBean { private List items; + @Size(min = 3, max = 5) + private List otherItems; + public List getItems() { return items; } + public List getOtherItems() { + return otherItems; + } + public void setItems(List items) { this.items = items; } + + public void setOtherItems(List otherItems) { + this.otherItems = otherItems; + } }