use of io.gravitee.repository.management.model.RatingAnswer in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method createAnswer.
@Override
public RatingEntity createAnswer(final NewRatingAnswerEntity answerEntity) {
if (!enabled) {
throw new ApiRatingUnavailableException();
}
try {
final Rating rating = findById(answerEntity.getRatingId());
final RatingAnswer ratingAnswer = new RatingAnswer();
ratingAnswer.setId(UUID.toString(UUID.random()));
ratingAnswer.setRating(answerEntity.getRatingId());
ratingAnswer.setUser(getAuthenticatedUsername());
ratingAnswer.setComment(answerEntity.getComment());
ratingAnswer.setCreatedAt(new Date());
ratingAnswerRepository.create(ratingAnswer);
auditService.createApiAuditLog(rating.getApi(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_CREATED, ratingAnswer.getCreatedAt(), null, ratingAnswer);
return convert(rating);
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to create a rating answer on rating {}", answerEntity.getRatingId(), ex);
throw new TechnicalManagementException("An error occurred while trying to create a rating answer on rating" + answerEntity.getRatingId(), ex);
}
}
use of io.gravitee.repository.management.model.RatingAnswer in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceTest method shouldCreateAnswer.
@Test
public void shouldCreateAnswer() throws TechnicalException {
final Rating updatedRating = mock(Rating.class);
when(updatedRating.getApi()).thenReturn(API_ID);
when(updatedRating.getTitle()).thenReturn(TITLE);
when(updatedRating.getComment()).thenReturn(COMMENT);
when(updatedRating.getRate()).thenReturn(RATE);
when(updatedRating.getUser()).thenReturn(USER);
when(ratingAnswer.getUser()).thenReturn(USER);
when(ratingAnswer.getComment()).thenReturn(ANSWER);
when(ratingAnswer.getCreatedAt()).thenReturn(new Date());
when(ratingAnswerRepository.findByRating(RATING_ID)).thenReturn(singletonList(ratingAnswer));
when(newRatingAnswerEntity.getRatingId()).thenReturn(RATING_ID);
when(newRatingAnswerEntity.getComment()).thenReturn(COMMENT);
when(ratingRepository.findById(RATING_ID)).thenReturn(of(rating));
final RatingEntity ratingEntity = ratingService.createAnswer(newRatingAnswerEntity);
verify(ratingAnswerRepository).create(any(RatingAnswer.class));
assertEquals(USER, ratingEntity.getUser());
assertEquals(API_ID, ratingEntity.getApi());
assertEquals(TITLE, ratingEntity.getTitle());
assertEquals(COMMENT, ratingEntity.getComment());
assertEquals(RATE, ratingEntity.getRate(), 0);
assertEquals(ratingEntity.getCreatedAt(), ratingEntity.getUpdatedAt());
assertEquals(ANSWER, ratingEntity.getAnswers().get(0).getComment());
assertEquals(USER, ratingEntity.getAnswers().get(0).getUser());
assertNotNull(ratingEntity.getAnswers().get(0).getCreatedAt());
}
use of io.gravitee.repository.management.model.RatingAnswer in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method convert.
private RatingEntity convert(final Rating rating) {
final RatingEntity ratingEntity = new RatingEntity();
final UserEntity user = userService.findById(rating.getUser());
ratingEntity.setUser(user.getId());
if (user.getFirstname() != null && user.getLastname() != null) {
ratingEntity.setUserDisplayName(user.getFirstname() + ' ' + user.getLastname());
} else {
ratingEntity.setUserDisplayName(user.getUsername());
}
ratingEntity.setId(rating.getId());
ratingEntity.setApi(rating.getApi());
ratingEntity.setTitle(rating.getTitle());
ratingEntity.setComment(rating.getComment());
ratingEntity.setRate(rating.getRate());
ratingEntity.setCreatedAt(rating.getCreatedAt());
ratingEntity.setUpdatedAt(rating.getUpdatedAt());
try {
final List<RatingAnswer> ratingAnswers = ratingAnswerRepository.findByRating(rating.getId());
if (ratingAnswers != null) {
ratingEntity.setAnswers(ratingAnswers.stream().map(ratingAnswer -> {
final RatingAnswerEntity ratingAnswerEntity = new RatingAnswerEntity();
ratingAnswerEntity.setId(ratingAnswer.getId());
final UserEntity userAnswer = userService.findById(ratingAnswer.getUser());
ratingAnswerEntity.setUser(userAnswer.getId());
if (userAnswer.getFirstname() != null && userAnswer.getLastname() != null) {
ratingAnswerEntity.setUserDisplayName(userAnswer.getFirstname() + ' ' + userAnswer.getLastname());
} else {
ratingAnswerEntity.setUserDisplayName(userAnswer.getUsername());
}
ratingAnswerEntity.setComment(ratingAnswer.getComment());
ratingAnswerEntity.setCreatedAt(ratingAnswer.getCreatedAt());
return ratingAnswerEntity;
}).sorted(comparing(RatingAnswerEntity::getCreatedAt, reverseOrder())).collect(toList()));
}
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find rating answers by rating id {}", rating.getId(), ex);
throw new TechnicalManagementException("An error occurred while trying to find rating answers by rating id " + rating.getId(), ex);
}
return ratingEntity;
}
Aggregations