Search in sources :

Example 1 with RatingAnswerEntity

use of io.gravitee.rest.api.model.RatingAnswerEntity in project gravitee-management-rest-api by gravitee-io.

the class RatingMapperTest method testConvert.

@Test
public void testConvert() {
    Instant now = Instant.now();
    Date nowDate = Date.from(now);
    // init
    RatingEntity ratingEntity = new RatingEntity();
    RatingAnswerEntity ratingAnswerEntity = new RatingAnswerEntity();
    ratingAnswerEntity.setComment(RATING_RESPONSE_COMMENT);
    ratingAnswerEntity.setCreatedAt(nowDate);
    ratingAnswerEntity.setId(RATING_RESPONSE_ID);
    ratingAnswerEntity.setUser(RATING_RESPONSE_AUTHOR);
    ratingAnswerEntity.setUserDisplayName(RATING_RESPONSE_AUTHOR_DISPLAY_NAME);
    ratingEntity.setAnswers(Arrays.asList(ratingAnswerEntity));
    ratingEntity.setApi(RATING_API);
    ratingEntity.setComment(RATING_COMMENT);
    ratingEntity.setCreatedAt(nowDate);
    ratingEntity.setId(RATING_ID);
    ratingEntity.setRate((byte) 1);
    ratingEntity.setTitle(RATING_TITLE);
    ratingEntity.setUpdatedAt(nowDate);
    ratingEntity.setUser(RATING_AUTHOR);
    ratingEntity.setUserDisplayName(RATING_AUTHOR_DISPLAY_NAME);
    UserEntity authorEntity = new UserEntity();
    authorEntity.setId(RATING_AUTHOR);
    UserEntity responseAuthorEntity = new UserEntity();
    responseAuthorEntity.setId(RATING_RESPONSE_AUTHOR);
    User author = new User();
    author.setId(RATING_AUTHOR);
    User responseAuthor = new User();
    responseAuthor.setId(RATING_RESPONSE_AUTHOR);
    doReturn(authorEntity).when(userService).findById(RATING_AUTHOR);
    doReturn(responseAuthorEntity).when(userService).findById(RATING_RESPONSE_AUTHOR);
    doReturn(author).when(userMapper).convert(authorEntity);
    doReturn(responseAuthor).when(userMapper).convert(responseAuthorEntity);
    Rating responseRating = ratingMapper.convert(ratingEntity, uriInfo);
    assertNotNull(responseRating);
    List<RatingAnswer> answers = responseRating.getAnswers();
    assertNotNull(answers);
    assertEquals(1, answers.size());
    RatingAnswer ratingAnswer = answers.get(0);
    assertNotNull(ratingAnswer);
    assertEquals(RATING_RESPONSE_COMMENT, ratingAnswer.getComment());
    assertEquals(now.toEpochMilli(), ratingAnswer.getDate().toInstant().toEpochMilli());
    assertEquals(responseAuthor, ratingAnswer.getAuthor());
    assertEquals(author, responseRating.getAuthor());
    assertEquals(RATING_COMMENT, responseRating.getComment());
    assertEquals(RATING_TITLE, responseRating.getTitle());
    assertEquals(now.toEpochMilli(), responseRating.getDate().toInstant().toEpochMilli());
    assertEquals(RATING_ID, responseRating.getId());
    assertEquals(Integer.valueOf(1), responseRating.getValue());
}
Also used : RatingAnswer(io.gravitee.rest.api.portal.rest.model.RatingAnswer) User(io.gravitee.rest.api.portal.rest.model.User) Instant(java.time.Instant) Rating(io.gravitee.rest.api.portal.rest.model.Rating) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Date(java.util.Date) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 2 with RatingAnswerEntity

use of io.gravitee.rest.api.model.RatingAnswerEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiRatingAnswerResourceTest method init.

@Before
public void init() {
    resetAllMocks();
    ApiEntity mockApi = new ApiEntity();
    mockApi.setId(API);
    Set<ApiEntity> mockApis = new HashSet<>(Arrays.asList(mockApi));
    doReturn(mockApis).when(apiService).findPublishedByUser(any(), argThat(q -> singletonList(API).equals(q.getIds())));
    RatingEntity ratingEntity = new RatingEntity();
    ratingEntity.setId(RATING);
    ratingEntity.setComment(RATING);
    ratingEntity.setApi(API);
    ratingEntity.setRate(Integer.valueOf(1).byteValue());
    RatingAnswerEntity answer = new RatingAnswerEntity();
    answer.setId(ANSWER);
    ratingEntity.setAnswers(Arrays.asList(answer));
    doReturn(ratingEntity).when(ratingService).findById(eq(RATING));
    doReturn(ratingEntity).when(ratingService).createAnswer(any());
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Assert.assertNotNull(org.junit.Assert.assertNotNull) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Set(java.util.Set) Test(org.junit.Test) Error(io.gravitee.rest.api.portal.rest.model.Error) ErrorResponse(io.gravitee.rest.api.portal.rest.model.ErrorResponse) Entity(javax.ws.rs.client.Entity) Collections.singletonList(java.util.Collections.singletonList) HttpStatusCode(io.gravitee.common.http.HttpStatusCode) HashSet(java.util.HashSet) List(java.util.List) RatingAnswerInput(io.gravitee.rest.api.portal.rest.model.RatingAnswerInput) Response(javax.ws.rs.core.Response) Rating(io.gravitee.rest.api.portal.rest.model.Rating) Mockito.doReturn(org.mockito.Mockito.doReturn) Assert.assertEquals(org.junit.Assert.assertEquals) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 3 with RatingAnswerEntity

use of io.gravitee.rest.api.model.RatingAnswerEntity in project gravitee-management-rest-api by gravitee-io.

the class RatingMapper method convert.

public Rating convert(RatingEntity ratingEntity, UriInfo uriInfo) {
    final Rating rating = new Rating();
    UserEntity authorEntity = userService.findById(ratingEntity.getUser());
    User author = userMapper.convert(authorEntity);
    author.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), authorEntity.getId()), authorEntity.getUpdatedAt()));
    rating.setAuthor(author);
    rating.setTitle(ratingEntity.getTitle());
    rating.setComment(ratingEntity.getComment());
    if (ratingEntity.getCreatedAt() != null) {
        rating.setDate(ratingEntity.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
    }
    rating.setId(ratingEntity.getId());
    rating.setValue(Integer.valueOf(ratingEntity.getRate()));
    if (ratingEntity.getAnswers() != null) {
        List<RatingAnswer> ratingsAnswer = ratingEntity.getAnswers().stream().sorted(Comparator.comparing(RatingAnswerEntity::getCreatedAt)).map(rae -> {
            UserEntity answerAuthorEntity = userService.findById(rae.getUser());
            User answerAuthor = userMapper.convert(answerAuthorEntity);
            answerAuthor.setLinks(userMapper.computeUserLinks(usersURL(uriInfo.getBaseUriBuilder(), answerAuthorEntity.getId()), answerAuthorEntity.getUpdatedAt()));
            return new RatingAnswer().id(rae.getId()).author(answerAuthor).comment(rae.getComment()).date(rae.getCreatedAt().toInstant().atOffset(ZoneOffset.UTC));
        }).collect(Collectors.toList());
        rating.setAnswers(ratingsAnswer);
    }
    return rating;
}
Also used : RatingEntity(io.gravitee.rest.api.model.RatingEntity) Autowired(org.springframework.beans.factory.annotation.Autowired) User(io.gravitee.rest.api.portal.rest.model.User) Collectors(java.util.stream.Collectors) List(java.util.List) Component(org.springframework.stereotype.Component) UserService(io.gravitee.rest.api.service.UserService) Rating(io.gravitee.rest.api.portal.rest.model.Rating) RatingAnswer(io.gravitee.rest.api.portal.rest.model.RatingAnswer) ZoneOffset(java.time.ZoneOffset) UriInfo(javax.ws.rs.core.UriInfo) Comparator(java.util.Comparator) PortalApiLinkHelper.usersURL(io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL) RatingAnswerEntity(io.gravitee.rest.api.model.RatingAnswerEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) RatingAnswer(io.gravitee.rest.api.portal.rest.model.RatingAnswer) User(io.gravitee.rest.api.portal.rest.model.User) Rating(io.gravitee.rest.api.portal.rest.model.Rating) UserEntity(io.gravitee.rest.api.model.UserEntity)

Aggregations

RatingAnswerEntity (io.gravitee.rest.api.model.RatingAnswerEntity)3 RatingEntity (io.gravitee.rest.api.model.RatingEntity)3 Rating (io.gravitee.rest.api.portal.rest.model.Rating)3 UserEntity (io.gravitee.rest.api.model.UserEntity)2 RatingAnswer (io.gravitee.rest.api.portal.rest.model.RatingAnswer)2 User (io.gravitee.rest.api.portal.rest.model.User)2 List (java.util.List)2 Test (org.junit.Test)2 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)1 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)1 Error (io.gravitee.rest.api.portal.rest.model.Error)1 ErrorResponse (io.gravitee.rest.api.portal.rest.model.ErrorResponse)1 RatingAnswerInput (io.gravitee.rest.api.portal.rest.model.RatingAnswerInput)1 PortalApiLinkHelper.usersURL (io.gravitee.rest.api.portal.rest.utils.PortalApiLinkHelper.usersURL)1 UserService (io.gravitee.rest.api.service.UserService)1 Instant (java.time.Instant)1 ZoneOffset (java.time.ZoneOffset)1 Arrays (java.util.Arrays)1 Collections.singletonList (java.util.Collections.singletonList)1 Comparator (java.util.Comparator)1