Search in sources :

Example 6 with Rating

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());
}
Also used : RatingAnswer(io.gravitee.repository.management.model.RatingAnswer) Rating(io.gravitee.repository.management.model.Rating) Date(java.util.Date) Test(org.junit.Test)

Example 7 with Rating

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);
}
Also used : Rating(io.gravitee.repository.management.model.Rating) Test(org.junit.Test)

Example 8 with Rating

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());
}
Also used : Pageable(io.gravitee.repository.management.api.search.Pageable) Rating(io.gravitee.repository.management.model.Rating) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) Test(org.junit.Test)

Example 9 with Rating

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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) Page(io.gravitee.common.data.domain.Page) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) PageableBuilder(io.gravitee.repository.management.api.search.builder.PageableBuilder) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Example 10 with Rating

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);
    }
}
Also used : TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Rating(io.gravitee.repository.management.model.Rating) ApiRatingUnavailableException(io.gravitee.management.service.exceptions.ApiRatingUnavailableException) Date(java.util.Date) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

Rating (io.gravitee.repository.management.model.Rating)12 ApiRatingUnavailableException (io.gravitee.management.service.exceptions.ApiRatingUnavailableException)8 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)8 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)8 Date (java.util.Date)7 PageableBuilder (io.gravitee.repository.management.api.search.builder.PageableBuilder)3 RatingAnswer (io.gravitee.repository.management.model.RatingAnswer)3 Test (org.junit.Test)3 Page (io.gravitee.common.data.domain.Page)2 RatingAlreadyExistsException (io.gravitee.management.service.exceptions.RatingAlreadyExistsException)2 RatingNotFoundException (io.gravitee.management.service.exceptions.RatingNotFoundException)2 Pageable (io.gravitee.repository.management.api.search.Pageable)2 OptionalDouble (java.util.OptionalDouble)2 UUID (io.gravitee.common.utils.UUID)1 io.gravitee.management.model (io.gravitee.management.model)1 AuditService (io.gravitee.management.service.AuditService)1 RatingService (io.gravitee.management.service.RatingService)1 UserService (io.gravitee.management.service.UserService)1 RatingAnswerRepository (io.gravitee.repository.management.api.RatingAnswerRepository)1 RatingRepository (io.gravitee.repository.management.api.RatingRepository)1