Search in sources :

Example 11 with RatingEntity

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());
}
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 12 with RatingEntity

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());
}
Also used : RatingInput(io.gravitee.rest.api.portal.rest.model.RatingInput) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Arrays(java.util.Arrays) ArgumentMatchers(org.mockito.ArgumentMatchers) Assert.assertNotNull(org.junit.Assert.assertNotNull) NOT_FOUND_404(io.gravitee.common.http.HttpStatusCode.NOT_FOUND_404) 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) NO_CONTENT_204(io.gravitee.common.http.HttpStatusCode.NO_CONTENT_204) Entity(javax.ws.rs.client.Entity) Collections.singletonList(java.util.Collections.singletonList) HashSet(java.util.HashSet) List(java.util.List) 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) Before(org.junit.Before) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 13 with RatingEntity

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);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) RatingAnswerNotFoundException(io.gravitee.rest.api.service.exceptions.RatingAnswerNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 14 with RatingEntity

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);
}
Also used : ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) RatingNotFoundException(io.gravitee.rest.api.service.exceptions.RatingNotFoundException) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) NewRatingAnswerEntity(io.gravitee.rest.api.model.NewRatingAnswerEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Example 15 with RatingEntity

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);
}
Also used : NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) ApiQuery(io.gravitee.rest.api.model.api.ApiQuery) ApiNotFoundException(io.gravitee.rest.api.service.exceptions.ApiNotFoundException) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) RatingEntity(io.gravitee.rest.api.model.RatingEntity) NewRatingEntity(io.gravitee.rest.api.model.NewRatingEntity) Permissions(io.gravitee.rest.api.portal.rest.security.Permissions)

Aggregations

RatingEntity (io.gravitee.rest.api.model.RatingEntity)24 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)20 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)12 ApiNotFoundException (io.gravitee.rest.api.service.exceptions.ApiNotFoundException)12 Rating (io.gravitee.rest.api.portal.rest.model.Rating)10 Permissions (io.gravitee.rest.api.portal.rest.security.Permissions)10 Test (org.junit.Test)10 Error (io.gravitee.rest.api.portal.rest.model.Error)8 RatingNotFoundException (io.gravitee.rest.api.service.exceptions.RatingNotFoundException)8 Collections.singletonList (java.util.Collections.singletonList)8 List (java.util.List)8 Entity (javax.ws.rs.client.Entity)8 Response (javax.ws.rs.core.Response)8 Assert.assertEquals (org.junit.Assert.assertEquals)8 Assert.assertNotNull (org.junit.Assert.assertNotNull)8 Before (org.junit.Before)8 ArgumentMatchers (org.mockito.ArgumentMatchers)8 Mockito.doReturn (org.mockito.Mockito.doReturn)8 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)6 RatingAnswerEntity (io.gravitee.rest.api.model.RatingAnswerEntity)6