RatingMapper { 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; }  Rating convert(RatingEntity ratingEntity, UriInfo uriInfo);  }
@Test public void testConvert() { Instant now = Instant.now(); Date nowDate = Date.from(now); 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()); }