adde validation group code (#7646)

* Added cascading type mudule

* fix compile error

* updated dependency version in pom

* Added BigDecimal validation classes

* Updated test cases

* Remove syso from test cases

* Updated test cases

* Added validation group code

* Added validation group code

* Added validation group code
This commit is contained in:
Devender Kumar 2019-08-29 10:36:53 +02:00 committed by Grzegorz Piwowarek
parent 5d57a6f11f
commit 6b76d5ed0e
5 changed files with 195 additions and 3 deletions

View File

@ -0,0 +1,5 @@
package org.baeldung.javabeanconstraints.validationgroup;
public interface AdvanceInfo {
}

View File

@ -0,0 +1,5 @@
package org.baeldung.javabeanconstraints.validationgroup;
public interface BasicInfo {
}

View File

@ -0,0 +1,99 @@
package org.baeldung.javabeanconstraints.validationgroup;
import javax.validation.constraints.Email;
import javax.validation.constraints.NotBlank;
public class RegistrationForm {
@NotBlank(groups=BasicInfo.class)
private String firstName;
@NotBlank(groups=BasicInfo.class)
private String lastName;
@Email(groups=BasicInfo.class)
private String email;
@NotBlank(groups=BasicInfo.class)
private String phone;
@NotBlank(groups=AdvanceInfo.class)
private String street;
@NotBlank(groups=AdvanceInfo.class)
private String houseNumber;
@NotBlank(groups=AdvanceInfo.class)
private String zipCode;
@NotBlank(groups=AdvanceInfo.class)
private String city;
@NotBlank(groups=AdvanceInfo.class)
private String contry;
public String getStreet() {
return street;
}
public void setStreet(String street) {
this.street = street;
}
public String getHouseNumber() {
return houseNumber;
}
public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getContry() {
return contry;
}
public void setContry(String contry) {
this.contry = contry;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}

View File

@ -26,7 +26,8 @@ public class InvoiceUnitTest {
Invoice invoice = new Invoice(new BigDecimal(10.21), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(1);
violations.forEach(action-> assertThat(action.getMessage()).isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
violations.forEach(action-> assertThat(action.getMessage())
.isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
}
@Test
@ -41,7 +42,8 @@ public class InvoiceUnitTest {
Invoice invoice = new Invoice(new BigDecimal(1021.21), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(1);
violations.forEach(action-> assertThat(action.getMessage()).isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
violations.forEach(action-> assertThat(action.getMessage())
.isEqualTo("numeric value out of bounds (<3 digits>.<2 digits> expected)"));
}
@Test
@ -49,7 +51,8 @@ public class InvoiceUnitTest {
Invoice invoice = new Invoice(new BigDecimal(000.00), "Book purchased");
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
assertThat(violations.size()).isEqualTo(1);
violations.forEach(action-> assertThat(action.getMessage()).isEqualTo("must be greater than 0.0"));
violations.forEach(action-> assertThat(action.getMessage())
.isEqualTo("must be greater than 0.0"));
}
@Test

View File

@ -0,0 +1,80 @@
package org.baeldung.javabeanconstraints.validationgroup;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import org.junit.BeforeClass;
import org.junit.Test;
public class RegistrationFormUnitTest {
private static Validator validator;
@BeforeClass
public static void setupValidatorInstance() {
validator = Validation.buildDefaultValidatorFactory().getValidator();
}
@Test
public void whenBasicInfoIsNotComplete_thenShouldGiveConstraintViolationsOnlyForBasicInfo() {
RegistrationForm form = buildRegistrationFormWithBasicInfo();
form.setFirstName("");
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("firstName");
});
}
@Test
public void whenAdvanceInfoIsNotComplete_thenShouldGiveConstraintViolationsOnlyForAdvanceInfo() {
RegistrationForm form = buildRegistrationFormWithAdvanceInfo();
form.setZipCode("");
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("zipCode");
});
}
@Test
public void whenBasicAndAdvanceInfoIsComplete_thenShouldNotGiveConstraintViolations() {
RegistrationForm form = buildRegistrationFormWithBasicAndAdvanceInfo();
Set<ConstraintViolation<RegistrationForm>> violations = validator.validate(form);
assertThat(violations.size()).isEqualTo(0);
}
private RegistrationForm buildRegistrationFormWithBasicInfo() {
RegistrationForm form = new RegistrationForm();
form.setFirstName("devender");
form.setLastName("kumar");
form.setEmail("anyemail@yopmail.com");
form.setPhone("12345");
return form;
}
private RegistrationForm buildRegistrationFormWithAdvanceInfo() {
RegistrationForm form = new RegistrationForm();
return popultaeAdvanceInfo(form);
}
private RegistrationForm popultaeAdvanceInfo(RegistrationForm form) {
form.setCity("Berlin");
form.setContry("DE");
form.setStreet("alexa str.");
form.setZipCode("19923");
form.setHouseNumber("2a");
return form;
}
private RegistrationForm buildRegistrationFormWithBasicAndAdvanceInfo() {
RegistrationForm form = buildRegistrationFormWithBasicInfo();
return popultaeAdvanceInfo(form);
}
}