Added big decimal validation (#7379)
* 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
This commit is contained in:
parent
493e435ff7
commit
c4110e5fe8
|
@ -0,0 +1,19 @@
|
|||
package org.baeldung.javabeanconstraints.bigdecimal;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import javax.validation.constraints.DecimalMin;
|
||||
import javax.validation.constraints.Digits;
|
||||
|
||||
public class Invoice {
|
||||
|
||||
@DecimalMin(value = "0.0", inclusive = false)
|
||||
@Digits(integer=3, fraction=2)
|
||||
private BigDecimal price;
|
||||
private String description;
|
||||
|
||||
public Invoice(BigDecimal price, String description) {
|
||||
this.price = price;
|
||||
this.description = description;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package org.baeldung.javabeanconstraints.bigdecimal;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
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 InvoiceUnitTest {
|
||||
|
||||
private static Validator validator;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupValidatorInstance() {
|
||||
validator = Validation.buildDefaultValidatorFactory().getValidator();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPriceIntegerDigitLessThanThreeWithDecimalValue_thenShouldGiveConstraintViolations() {
|
||||
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)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPriceIntegerDigitLessThanThreeWithIntegerValue_thenShouldNotGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(10), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPriceIntegerDigitGreaterThanThree_thenShouldGiveConstraintViolations() {
|
||||
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)"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPriceIsZero_thenShouldGiveConstraintViolations() {
|
||||
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"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenPriceIsGreaterThanZero_thenShouldNotGiveConstraintViolations() {
|
||||
Invoice invoice = new Invoice(new BigDecimal(100.50), "Book purchased");
|
||||
Set<ConstraintViolation<Invoice>> violations = validator.validate(invoice);
|
||||
assertThat(violations.size()).isEqualTo(0);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,10 +2,6 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<artifactId>jpa-hibernate-cascade-type</artifactId>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
|
@ -13,6 +9,10 @@
|
|||
<relativePath>../../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>jpa-hibernate-cascade-type</artifactId>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.hibernate</groupId>
|
||||
|
|
Loading…
Reference in New Issue