use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class ArticleServiceImplTest method testCreateArticleContent_testDataTruncationError.
@Test
public void testCreateArticleContent_testDataTruncationError() throws Exception {
Article a = buildTestDto();
a.setArticleKey(generateLongString(Article.FN_KEY_SIZE, "Z"));
a.setLang("ZZ");
a.setTitle(generateLongString(Article.FN_TITLE_SIZE, "Ф"));
a.setAnnotation(generateLongString(Article.FN_ANNOTATION_SIZE, "Ф"));
a.setContent(generateLongString(Article.FN_ANNOTATION_SIZE, "Ф"));
a.setArticleGroup(generateLongString(Article.FN_GROUP_SIZE, "Z"));
articleService.create(a);
// Update data with wrong length text
try {
a = buildTestDto();
a.setArticleKey(generateLongString(Article.FN_KEY_SIZE + 1, "ф"));
a.setLang("ZZZ");
a.setTitle(generateLongString(Article.FN_TITLE_SIZE + 1, "Ф"));
a.setAnnotation(generateLongString(Article.FN_ANNOTATION_SIZE + 1, "ф"));
a.setContent(generateLongString(Article.FN_ANNOTATION_SIZE + 1, "ф"));
a.setArticleGroup(generateLongString(Article.FN_GROUP_SIZE + 1, "Z"));
articleService.create(a);
fail("FVE expected");
} catch (FieldValidationException fve) {
assertEquals(5, fve.getErrors().size());
assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_KEY));
assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_LANG));
assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_TITLE));
assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_ANNOTATION));
assertNotNull(fve.findErrorOfTypeForField(DataTooLongValidationError.class, Article.FN_GROUP));
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class EasyCrudValidationStrategyAbstract method validateForUpdate.
@Override
public void validateForUpdate(TDto existingVersion, TDto newVersion) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
doValidateForUpdate(existingVersion, newVersion, ctx);
if (ctx.getHasErrors()) {
throw new FieldValidationException(ctx.getErrors());
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class EasyCrudValidationStrategyAbstract method validateForCreate.
@Override
public void validateForCreate(TDto dto) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
doValidateForCreate(dto, ctx);
if (ctx.getHasErrors()) {
throw new FieldValidationException(ctx.getErrors());
}
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class EasyCrudDaoMySqlImpl method processDuplicateEntryException.
private void processDuplicateEntryException(Throwable t) throws FieldValidationException {
DuplicateKeyException dke = ExceptionUtils.findExceptionOfType(t, DuplicateKeyException.class);
if (dke == null) {
return;
}
String constraint = DaoExceptionUtils.findViolatedConstraintName(dke);
// Handle case when uuid is duplicated
if (DaoExceptionUtils.MYSQL_CONSTRAINT_PRIMARY.equals(constraint)) {
throw new IllegalArgumentException("Row with same primary key already exists", dke);
}
if (!constraint.contains(DaoExceptionUtils.MYSQL_CONSTRAINT_UNIQUE)) {
throw new IllegalArgumentException("Constraint violation " + constraint, dke);
}
String fieldName = constraint.substring(0, constraint.indexOf(DaoExceptionUtils.MYSQL_CONSTRAINT_UNIQUE));
if (fieldName.contains("_")) {
fieldName = JdbcUtils.convertUnderscoreNameToPropertyName(fieldName);
}
throw new FieldValidationException(new DuplicateRecordValidationError(fieldName));
}
use of org.summerb.approaches.validation.FieldValidationException in project summerb by skarpushin.
the class DaoExceptionUtils method propagateIfTruncationError.
/**
* @deprecated This feature will not actually work at least with mysql.
* MySql doesn't send the numbers - it sends only field name
* which was truncated.
*/
@Deprecated
public static void propagateIfTruncationError(Throwable t) throws FieldValidationException {
DataTruncation exc = ExceptionUtils.findExceptionOfType(t, DataTruncation.class);
if (exc == null) {
return;
}
String fieldName = findTruncatedFieldNameIfAny(t);
if (fieldName == null) {
return;
}
DataTooLongValidationError dataTooLongValidationError = new DataTooLongValidationError(exc.getDataSize(), exc.getTransferSize(), fieldName);
throw new FieldValidationException(dataTooLongValidationError);
}
Aggregations