[JavaEE] Data Validation
Data validation JavaEE
2023-09-14 08:59:17 时间
When we create Entity and Respority, we also need to do validations to protect our data.
In Java, validations are built-in, using decorators. For Typescript, found a useful libaray to do the similar validation as well. Checkout class-validator
Entity:
@Entity public class Book { // ====================================== // = Attributes = // ====================================== @Id @GeneratedValue private Long id; @Column(length = 200) @NotNull @Size(min = 1, max = 200) private String title; @Column(length = 10000) @Size(min = 1, max = 10000) private String description; @Column(name = "unit_cost") @Min(1) private Float unitCost; @Column(length = 50) @NotNull @Size(min = 1, max = 50) private String isbn; @Column(name = "publication_date") @Temporal(TemporalType.DATE) @Past private Date publicationDate; .... }
Testing:
We want to test, if we give title as null, it should throw exception.
@RunWith(Arquillian.class) public class BookRepositoryTest { @Inject private BookRepository bookRepository; // We want the test throw exception @Test(expected = Exception.class) public void createInvalidBook() { Book book = new Book("isbn", null, 12F, 123, Language.ENGLISH, new Date(), "imageURL", "description"); bookRepository.create(book); } @Deployment public static JavaArchive createDeployment() { return ShrinkWrap.create(JavaArchive.class) .addClass(BookRepository.class) .addClass(Book.class) .addClass(Language.class) .addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml") .addAsManifestResource("META-INF/test-persistence.xml", "persistence.xml"); } @org.junit.Test public void create() { } }
Respority:
@Transactional(SUPPORTS) public class BookRepository { // ====================================== // = Injection Points = // ====================================== @PersistenceContext(unitName = "bookStorePU") private EntityManager em; // ====================================== // = Business methods = // ====================================== public Book find(@NotNull Long id) { return em.find(Book.class, id); } // For creating and deleting methods, we want to use REQUIRED @Transactional(REQUIRED) public Book create(@NotNull Book book) { em.persist(book); return book; } }
Testing:
@Test(expected = Exception.class) public void findWithInvalidId() { bookRepository.find(null); }
相关文章
- DDN - Digital Data Network
- 泛函编程(5)-数据结构(Functional Data Structures)
- Data - 大数据生态圈
- [Angular] Data Resolver
- [Big Data] Week 4A (Basic)
- 116、Android获取Manifest中<meta-data>元素的值
- [JavaEE] Data Validation
- [Python] Slice the data with pandas
- [Big Data] Week 4A (Basic)
- 快被Gartner捧上天的Data Fabric,数据经纬到底是个啥?
- System.Data.SqlClient.SqlException 超时时间已到
- CL_FXS_URL_DATA_FETCHER - a good utility to fetch picture binary data according to url
- 成功解决pandas.errors.ParserError: Error tokenizing data. C error: Expected 1 fields in line 48, saw 2
- 【MongoDB】mongoimport and mongoexport of data (一)
- 论文解读(GraphDA)《Data Augmentation for Deep Graph Learning: A Survey》
- 不同长度的向量组成数据框 vector dataframe data.frame 不同长度的列表构建DataFrame 列表变成数据框
- Allegro删除Out of data shape铜皮框