use of org.summerb.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.validation.FieldValidationException in project summerb by skarpushin.
the class AttachmentValidationStrategyImpl method validateForCreate.
@Override
public void validateForCreate(Attachment dto) throws FieldValidationException {
ValidationContext ctx = new ValidationContext();
if (ctx.validateNotEmpty(dto.getName(), Attachment.FN_NAME)) {
ctx.validateDataLengthLessOrEqual(dto.getName(), Attachment.FN_NAME_MAXSIZE, Attachment.FN_NAME);
}
ctx.validateNotEmpty(dto.getArticleId(), Attachment.FN_ARTICLE_ID);
ctx.validateNotEmpty(dto.getSize(), Attachment.FN_SIZE);
ctx.validateNotNull(dto.getContents(), Attachment.FN_CONTENTS);
if (ctx.getHasErrors()) {
throw new FieldValidationException(ctx.getErrors());
}
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class UserServiceImpl method createUser.
@Override
@Transactional(rollbackFor = Throwable.class)
public User createUser(User userTemplate) throws FieldValidationException {
Preconditions.checkArgument(userTemplate != null, "userTemplate is required");
validateUser(userTemplate);
User userToCreate = new User();
userToCreate.setDisplayName(userTemplate.getDisplayName());
userToCreate.setEmail(userTemplate.getEmail());
userToCreate.setIntegrationData(userTemplate.getIntegrationData());
userToCreate.setIsBlocked(userTemplate.getIsBlocked());
userToCreate.setLocale(userTemplate.getLocale());
userToCreate.setRegisteredAt(userTemplate.getRegisteredAt());
userToCreate.setTimeZone(userTemplate.getTimeZone());
// Patch data
if (userTemplate.getUuid() != null) {
Preconditions.checkArgument(stringIdGenerator.isValidId(userTemplate.getUuid()), "User id is invalid");
userToCreate.setUuid(userTemplate.getUuid());
} else {
userToCreate.setUuid(stringIdGenerator.generateNewId(userTemplate));
}
if (userToCreate.getRegisteredAt() == 0) {
userToCreate.setRegisteredAt(new Date().getTime());
}
if (userToCreate.getLocale() == null) {
userToCreate.setLocale(LocaleContextHolder.getLocale().toString());
}
if (userToCreate.getTimeZone() == null) {
userToCreate.setTimeZone(Calendar.getInstance().getTimeZone().getID());
}
// Create
try {
userDao.createUser(userToCreate);
eventBus.post(EntityChangedEvent.added(userToCreate));
} catch (DuplicateKeyException dke) {
throw new FieldValidationException(new DuplicateUserValidationError(User.FN_EMAIL));
} catch (Throwable t) {
String msg = String.format("Failed to create user with email '%s'", userToCreate.getEmail());
throw new UserServiceUnexpectedException(msg, t);
}
return userToCreate;
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class AuthTokenServiceImpl method authenticate.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken authenticate(String userEmail, String passwordPlain, String clientIp) throws UserNotFoundException, FieldValidationException, InvalidPasswordException {
Preconditions.checkArgument(userEmail != null);
Preconditions.checkArgument(passwordPlain != null);
Preconditions.checkArgument(clientIp != null);
try {
User user = validateAndGetUser(userEmail, passwordPlain);
return createAuthToken(user.getEmail(), clientIp, UUID.randomUUID().toString(), UUID.randomUUID().toString());
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
Throwables.throwIfInstanceOf(t, InvalidPasswordException.class);
String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
throw new UserServiceUnexpectedException(msg, t);
}
}
use of org.summerb.validation.FieldValidationException in project summerb by skarpushin.
the class AuthTokenServiceImpl method createAuthToken.
@Override
@Transactional(rollbackFor = Throwable.class)
public AuthToken createAuthToken(String userEmail, String clientIp, String tokenUuid, String tokenValueUuid) throws UserNotFoundException, FieldValidationException {
Preconditions.checkArgument(userEmail != null);
Preconditions.checkArgument(clientIp != null);
Preconditions.checkArgument(StringUtils.hasText(tokenUuid));
Preconditions.checkArgument(StringUtils.hasText(tokenValueUuid));
try {
User user = userService.getUserByEmail(userEmail);
AuthToken authToken = buildNewAuthToken(user, clientIp, tokenUuid, tokenValueUuid);
authTokenDao.createAuthToken(authToken);
return authToken;
} catch (Throwable t) {
Throwables.throwIfInstanceOf(t, UserNotFoundException.class);
Throwables.throwIfInstanceOf(t, FieldValidationException.class);
String msg = String.format("Failed to create auth otken for user '%s'", userEmail);
throw new UserServiceUnexpectedException(msg, t);
}
}
Aggregations