use of io.gravitee.repository.management.model.Rating 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.Rating in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceTest method shouldFindSummaryByApi.
@Test
public void shouldFindSummaryByApi() throws TechnicalException {
final Rating r = new Rating();
r.setRate(new Byte("4"));
when(ratingRepository.findByApi(API_ID)).thenReturn(asList(rating, r));
final RatingSummaryEntity ratingSummary = ratingService.findSummaryByApi(API_ID);
assertEquals(API_ID, ratingSummary.getApi());
assertEquals(2, ratingSummary.getNumberOfRatings());
assertEquals(3.5, ratingSummary.getAverageRate(), 0);
assertFalse(ratingSummary.getNumberOfRatingsByRate().isEmpty());
assertEquals(1, ratingSummary.getNumberOfRatingsByRate().get(new Byte("3")), 0);
assertEquals(1, ratingSummary.getNumberOfRatingsByRate().get(new Byte("4")), 0);
}
use of io.gravitee.repository.management.model.Rating in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceTest method shouldFindByApi.
@Test
public void shouldFindByApi() throws TechnicalException {
final Pageable pageable = mock(Pageable.class);
when(pageable.pageNumber()).thenReturn(1);
when(pageable.pageSize()).thenReturn(1);
final Page<Rating> pageRating = mock(Page.class);
when(pageRating.getPageNumber()).thenReturn(1);
when(pageRating.getPageElements()).thenReturn(10L);
when(pageRating.getTotalElements()).thenReturn(100L);
when(pageRating.getContent()).thenReturn(singletonList(rating));
when(ratingRepository.findByApiPageable(eq(API_ID), eq(new PageableBuilder().pageNumber(0).pageSize(1).build()))).thenReturn(pageRating);
final Page<RatingEntity> pageRatingEntity = ratingService.findByApi(API_ID, pageable);
assertEquals(1, pageRatingEntity.getPageNumber());
assertEquals(10, pageRatingEntity.getPageElements());
assertEquals(100, pageRatingEntity.getTotalElements());
final RatingEntity ratingEntity = pageRatingEntity.getContent().get(0);
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());
}
use of io.gravitee.repository.management.model.Rating in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method findByApi.
@Override
public Page<RatingEntity> findByApi(final String api, final Pageable pageable) {
if (!enabled) {
throw new ApiRatingUnavailableException();
}
try {
final Page<Rating> pageRating = ratingRepository.findByApiPageable(api, new PageableBuilder().pageNumber(pageable.pageNumber() - 1).pageSize(pageable.pageSize()).build());
final List<RatingEntity> ratingEntities = pageRating.getContent().stream().map(this::convert).collect(toList());
return new Page<>(ratingEntities, pageRating.getPageNumber(), (int) pageRating.getPageElements(), pageRating.getTotalElements());
} catch (TechnicalException ex) {
LOGGER.error("An error occurred while trying to find ratings for api {}", api, ex);
throw new TechnicalManagementException("An error occurred while trying to find ratings for api " + api, ex);
}
}
use of io.gravitee.repository.management.model.Rating in project gravitee-management-rest-api by gravitee-io.
the class RatingServiceImpl method deleteAnswer.
@Override
public void deleteAnswer(final String ratingId, final String answerId) {
if (!enabled) {
throw new ApiRatingUnavailableException();
}
try {
Rating rating = findById(ratingId);
ratingAnswerRepository.delete(answerId);
auditService.createApiAuditLog(rating.getApi(), null, RatingAnswer.RatingAnswerEvent.RATING_ANSWER_DELETED, new Date(), rating, null);
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to delete rating answer {}", answerId, ex);
throw new TechnicalManagementException("An error occurs while trying to delete rating answer " + answerId, ex);
}
}
Aggregations