BaseServiceImpl implements BaseService<ID, T> { @Override public void validate(T t) { Validator validator = factory.getValidator(); Set<ConstraintViolation<T>> constraintViolations = validator.validate(t); if (!constraintViolations.isEmpty()) { List<String> errors = new ArrayList<>(); for (ConstraintViolation<T> c : constraintViolations) { Class<?> annotationType = c.getConstraintDescriptor().getAnnotation().annotationType(); if (annotationType.equals(AssertTrue.class) || annotationType.equals(AssertFalse.class)) { errors.add(c.getMessage()); } else { errors.add(c.getPropertyPath() + " " + c.getMessage()); } } for (String error : errors) { LOGGER.warn(error); } throw new OCSValidationException(errors); } if (identicalEntityExists(t)) { throw new OCSNonUniqueException(messageService.getMessage(getEntityClass().getSimpleName() + ".not.unique", new Locale(SystemPropertyUtils.getDefaultLocale()))); } }  @Override long count(); @Override long count(Filter filter, boolean distinct); @Override T createNewEntity(); @Override @Transactional void delete(List<T> list); @Override @Transactional void delete(T t); @Override List<T> fetch(Filter filter, FetchJoinInformation... joins); @Override List<T> fetch(Filter filter, int pageNumber, int pageSize, FetchJoinInformation... joins); @Override List<T> fetch(Filter filter, int pageNumber, int pageSize, SortOrders sortOrders, FetchJoinInformation... joins); @Override List<T> fetch(Filter filter, SortOrders orders, FetchJoinInformation... joins); @Override T fetchById(ID id, FetchJoinInformation... joins); @Override List<T> fetchByIds(List<ID> ids, FetchJoinInformation... joins); @Override List<T> fetchByIds(List<ID> ids, SortOrders sortOrders, FetchJoinInformation... joins); @Override T fetchByUniqueProperty(String propertyName, Object value, boolean caseSensitive, FetchJoinInformation... joins); @Override List<T> find(Filter filter); @Override List<T> find(Filter filter, SortOrder... orders); @Override List<T> findAll(); @Override List<T> findAll(SortOrder... orders); @Override T findById(ID id); @Override T findByUniqueProperty(String propertyName, Object value, boolean caseSensitive); @Override List<S> findDistinct(Filter filter, String distinctField, Class<S> elementType, SortOrder... orders); @Override List<S> findDistinctInCollectionTable(String tableName, String distinctField, Class<S> elementType); @Override List<ID> findIds(Filter filter, Integer maxResults, SortOrder... orders); @Override List<ID> findIds(Filter filter, SortOrder... orders); @Override List<?> findSelect(Filter filter, String[] selectProperties, int pageNumber, int pageSize, SortOrders sortOrders); @Override List<?> findSelect(Filter filter, String[] selectProperties, SortOrders sortOrders); @Override Class<T> getEntityClass(); MessageService getMessageService(); @Override @Transactional List<T> save(List<T> list); @Override @Transactional T save(T t); @Override void validate(T t);  }
@Test public void testValidate() { TestEntity entity = new TestEntity("name1", 15L); service.validate(entity); }
@Test public void testValidate_AssertTrue() { TestEntity entity = new TestEntity("bogus", 15L); assertThrows(OCSValidationException.class, () -> service.validate(entity)); }
@Test public void testValidate_Error() { TestEntity entity = new TestEntity(null, 15L); assertThrows(OCSValidationException.class, () -> service.validate(entity)); }