Added constraint group sequence test cases (#7763)
This commit is contained in:
parent
dc1cd98cd0
commit
f4ec48e0b8
|
@ -0,0 +1,8 @@
|
|||
package org.baeldung.javabeanconstraints.validationgroup;
|
||||
|
||||
import javax.validation.GroupSequence;
|
||||
|
||||
@GroupSequence({BasicInfo.class, AdvanceInfo.class})
|
||||
public interface CompleteInfo {
|
||||
|
||||
}
|
|
@ -13,6 +13,9 @@ public class RegistrationForm {
|
|||
@NotBlank(groups = BasicInfo.class)
|
||||
private String phone;
|
||||
|
||||
@NotBlank(groups = { BasicInfo.class, AdvanceInfo.class })
|
||||
private String captcha;
|
||||
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
private String street;
|
||||
@NotBlank(groups = AdvanceInfo.class)
|
||||
|
@ -96,4 +99,12 @@ public class RegistrationForm {
|
|||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getCaptcha() {
|
||||
return captcha;
|
||||
}
|
||||
|
||||
public void setCaptcha(String captcha) {
|
||||
this.captcha = captcha;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -43,6 +43,61 @@ public class RegistrationFormUnitTest {
|
|||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCaptchaIsBlank_thenShouldGiveConstraintViolationsForBasicInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setCaptcha("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, BasicInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("captcha");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCaptchaIsBlank_thenShouldGiveConstraintViolationsForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithAdvanceInfo();
|
||||
form.setCaptcha("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, AdvanceInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("captcha");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsNotComplete_thenShouldGiveConstraintViolationsForBasicInfoOnly() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicInfo();
|
||||
form.setFirstName("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("firstName");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicInfoIsCompleteAndAdvanceInfoIsNotComplete_thenShouldGiveConstraintViolationsForAdvanceInfo() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
form.setZipCode("");
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(1);
|
||||
violations.forEach(action -> {
|
||||
assertThat(action.getMessage()).isEqualTo("must not be blank");
|
||||
assertThat(action.getPropertyPath().toString()).isEqualTo("zipCode");
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolationsWithCompleteInfoValidationGroup() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form, CompleteInfo.class);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolations() {
|
||||
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
|
||||
|
@ -56,6 +111,7 @@ public class RegistrationFormUnitTest {
|
|||
form.setLastName("kumar");
|
||||
form.setEmail("anyemail@yopmail.com");
|
||||
form.setPhone("12345");
|
||||
form.setCaptcha("Y2HAhU5T");
|
||||
return form;
|
||||
}
|
||||
|
||||
|
@ -70,6 +126,7 @@ public class RegistrationFormUnitTest {
|
|||
form.setStreet("alexa str.");
|
||||
form.setZipCode("19923");
|
||||
form.setHouseNumber("2a");
|
||||
form.setCaptcha("Y2HAhU5T");
|
||||
return form;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue