From 6b76d5ed0e7bb3eecea0bb3c564f0159353b1c48 Mon Sep 17 00:00:00 2001 From: Devender Kumar <47500074+kumar-devender@users.noreply.github.com> Date: Thu, 29 Aug 2019 10:36:53 +0200 Subject: [PATCH] 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 --- .../validationgroup/AdvanceInfo.java | 5 + .../validationgroup/BasicInfo.java | 5 + .../validationgroup/RegistrationForm.java | 99 +++++++++++++++++++ .../bigdecimal/InvoiceUnitTest.java | 9 +- .../RegistrationFormUnitTest.java | 80 +++++++++++++++ 5 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/AdvanceInfo.java create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/BasicInfo.java create mode 100644 javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationForm.java create mode 100644 javaxval/src/test/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationFormUnitTest.java diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/AdvanceInfo.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/AdvanceInfo.java new file mode 100644 index 0000000000..4d5df052c2 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/AdvanceInfo.java @@ -0,0 +1,5 @@ +package org.baeldung.javabeanconstraints.validationgroup; + +public interface AdvanceInfo { + +} diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/BasicInfo.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/BasicInfo.java new file mode 100644 index 0000000000..4564a7a702 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/BasicInfo.java @@ -0,0 +1,5 @@ +package org.baeldung.javabeanconstraints.validationgroup; + +public interface BasicInfo { + +} diff --git a/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationForm.java b/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationForm.java new file mode 100644 index 0000000000..57c06648a7 --- /dev/null +++ b/javaxval/src/main/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationForm.java @@ -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; + } + +} diff --git a/javaxval/src/test/java/org/baeldung/javabeanconstraints/bigdecimal/InvoiceUnitTest.java b/javaxval/src/test/java/org/baeldung/javabeanconstraints/bigdecimal/InvoiceUnitTest.java index 525dd7d1ad..860177f4c9 100644 --- a/javaxval/src/test/java/org/baeldung/javabeanconstraints/bigdecimal/InvoiceUnitTest.java +++ b/javaxval/src/test/java/org/baeldung/javabeanconstraints/bigdecimal/InvoiceUnitTest.java @@ -26,7 +26,8 @@ public class InvoiceUnitTest { Invoice invoice = new Invoice(new BigDecimal(10.21), "Book purchased"); Set> 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> 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> 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 diff --git a/javaxval/src/test/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationFormUnitTest.java b/javaxval/src/test/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationFormUnitTest.java new file mode 100644 index 0000000000..91792a0c7e --- /dev/null +++ b/javaxval/src/test/java/org/baeldung/javabeanconstraints/validationgroup/RegistrationFormUnitTest.java @@ -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> 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> 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> 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); + } +}