It seems in my previous post the way I was sending error response was not the way UI was expecting it so I had to refactor code and used hibernate validator but it posed some issues in exception validation.
The code in REST api changed to
public Response search(@Context User user, FolderSearchRequest folderSearchRequest) throws ApplicationException {
Set<ConstraintViolation<FolderSearchRequest>> violationResult = validator.validate(folderSearchRequest);
if (!violationResult.isEmpty()) {
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>(violationResult);
throw new ValidationException(violations);
}
and the pojo changed to
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FolderSearchRequest {
public static final Integer DEFAULT_LIMIT = 30;
public static final Integer DEFAULT_OFFSET = 0;
@NotBlank
@Size(min = 3)
private String query;
@Range(min = 0)
private Integer offset = DEFAULT_OFFSET;
@Range(min = 1)
private Integer limit = DEFAULT_LIMIT;
}
Only issue is the ValidationException had a constant message so my expectMessage wont work. I had to now really get the real exception out of ExpectedException and match it, for this I had to write an Exception Matcher (not the most beautiful code but for the temporary api its ok).
@AllArgsConstructor
private static class ValidationExceptionMatcher extends TypeSafeMatcher<ValidationException> {
private String descriptionText;
private String msgPrefix;
@Override
public boolean matchesSafely(ValidationException item) {
Set<ConstraintViolation<?>> violations = item.getConstraintViolations();
if (violations.size() == 1) {
ConstraintViolation<?> violation = violations.iterator().next();
return violation.getMessage().startsWith(msgPrefix);
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText(descriptionText);
}
}
@Test
public void testQueryMinimumChars() throws ApplicationException {
expectedException.expect(ValidationException.class);
expectedException.expectMessage("constraint violations");
expectedException.expect(new ValidationExceptionMatcher("Size", "Size must be between 3 and"));
sut.search(null, new FolderSearchRequest("te", 0, 20));
}
The code in REST api changed to
public Response search(@Context User user, FolderSearchRequest folderSearchRequest) throws ApplicationException {
Set<ConstraintViolation<FolderSearchRequest>> violationResult = validator.validate(folderSearchRequest);
if (!violationResult.isEmpty()) {
Set<ConstraintViolation<?>> violations = new HashSet<ConstraintViolation<?>>(violationResult);
throw new ValidationException(violations);
}
and the pojo changed to
@Data
@AllArgsConstructor
@NoArgsConstructor
public class FolderSearchRequest {
public static final Integer DEFAULT_LIMIT = 30;
public static final Integer DEFAULT_OFFSET = 0;
@NotBlank
@Size(min = 3)
private String query;
@Range(min = 0)
private Integer offset = DEFAULT_OFFSET;
@Range(min = 1)
private Integer limit = DEFAULT_LIMIT;
}
Only issue is the ValidationException had a constant message so my expectMessage wont work. I had to now really get the real exception out of ExpectedException and match it, for this I had to write an Exception Matcher (not the most beautiful code but for the temporary api its ok).
@AllArgsConstructor
private static class ValidationExceptionMatcher extends TypeSafeMatcher<ValidationException> {
private String descriptionText;
private String msgPrefix;
@Override
public boolean matchesSafely(ValidationException item) {
Set<ConstraintViolation<?>> violations = item.getConstraintViolations();
if (violations.size() == 1) {
ConstraintViolation<?> violation = violations.iterator().next();
return violation.getMessage().startsWith(msgPrefix);
}
return false;
}
@Override
public void describeTo(Description description) {
description.appendText(descriptionText);
}
}
@Test
public void testQueryMinimumChars() throws ApplicationException {
expectedException.expect(ValidationException.class);
expectedException.expectMessage("constraint violations");
expectedException.expect(new ValidationExceptionMatcher("Size", "Size must be between 3 and"));
sut.search(null, new FolderSearchRequest("te", 0, 20));
}
Comments
Post a Comment