use of io.gravitee.rest.api.model.RatingEntity in project gravitee-api-management 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());
}
use of io.gravitee.rest.api.model.RatingEntity in project gravitee-api-management by gravitee-io.
the class ApiRatingResourceTest 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());
doReturn(ratingEntity).when(ratingService).findById(eq(RATING));
doReturn(ratingEntity).when(ratingService).update(any());
}
use of io.gravitee.rest.api.model.RatingEntity in project gravitee-api-management by gravitee-io.
the class ApiRatingAnswerResource method deleteApiRatingAnswer.
@DELETE
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING_ANSWER, acls = RolePermissionAction.DELETE) })
public Response deleteApiRatingAnswer(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @PathParam("answerId") String answerId) {
final ApiQuery apiQuery = new ApiQuery();
apiQuery.setIds(Collections.singletonList(apiId));
Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
RatingEntity ratingEntity = ratingService.findById(ratingId);
if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
if (ratingEntity.getAnswers().stream().anyMatch(answer -> answer.getId().equals(answerId))) {
ratingService.deleteAnswer(ratingId, answerId);
return Response.status(Status.NO_CONTENT).build();
}
throw new RatingAnswerNotFoundException(answerId, ratingId, apiId);
}
throw new RatingNotFoundException(ratingId, apiId);
}
throw new ApiNotFoundException(apiId);
}
use of io.gravitee.rest.api.model.RatingEntity in project gravitee-api-management by gravitee-io.
the class ApiRatingAnswersResource method createApiRatingAnswer.
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING_ANSWER, acls = RolePermissionAction.CREATE) })
public Response createApiRatingAnswer(@PathParam("apiId") String apiId, @PathParam("ratingId") String ratingId, @Valid RatingAnswerInput ratingAnswerInput) {
if (ratingAnswerInput == null) {
throw new BadRequestException("Input must not be null.");
}
final ApiQuery apiQuery = new ApiQuery();
apiQuery.setIds(Collections.singletonList(apiId));
Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
RatingEntity ratingEntity = ratingService.findById(ratingId);
if (ratingEntity != null && ratingEntity.getApi().equals(apiId)) {
NewRatingAnswerEntity ratingAnswerEntity = new NewRatingAnswerEntity();
ratingAnswerEntity.setComment(ratingAnswerInput.getComment());
ratingAnswerEntity.setRatingId(ratingId);
RatingEntity updatedRating = ratingService.createAnswer(ratingAnswerEntity);
return Response.status(Status.CREATED).entity(ratingMapper.convert(updatedRating, uriInfo)).build();
}
throw new RatingNotFoundException(ratingId, apiId);
}
throw new ApiNotFoundException(apiId);
}
use of io.gravitee.rest.api.model.RatingEntity in project gravitee-api-management by gravitee-io.
the class ApiRatingsResource method createApiRating.
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Permissions({ @Permission(value = RolePermission.API_RATING, acls = RolePermissionAction.CREATE) })
public Response createApiRating(@PathParam("apiId") String apiId, @Valid RatingInput ratingInput) {
if (ratingInput == null) {
throw new BadRequestException("Input must not be null.");
}
final ApiQuery apiQuery = new ApiQuery();
apiQuery.setIds(Collections.singletonList(apiId));
Collection<ApiEntity> userApis = apiService.findPublishedByUser(getAuthenticatedUserOrNull(), apiQuery);
if (userApis.stream().anyMatch(a -> a.getId().equals(apiId))) {
NewRatingEntity rating = new NewRatingEntity();
rating.setApi(apiId);
rating.setComment(ratingInput.getComment());
rating.setTitle(ratingInput.getTitle());
rating.setRate(ratingInput.getValue().byteValue());
RatingEntity createdRating = ratingService.create(rating);
return Response.status(Status.CREATED).entity(ratingMapper.convert(createdRating, uriInfo)).build();
}
throw new ApiNotFoundException(apiId);
}
Aggregations