Search in sources :

Example 11 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdCommentsCommentIdPut.

@Test
public void testApisApiIdCommentsCommentIdPut() throws APIManagementException, NotFoundException {
    printTestMethodName();
    String apiId = UUID.randomUUID().toString();
    String commentId = UUID.randomUUID().toString();
    ApisApiServiceImpl apisApiService = new ApisApiServiceImpl();
    APIStore apiStore = Mockito.mock(APIStoreImpl.class);
    PowerMockito.mockStatic(RestApiUtil.class);
    PowerMockito.when(RestApiUtil.getConsumer(USER)).thenReturn(apiStore);
    Request request = getRequest();
    PowerMockito.when(RestApiUtil.getLoggedInUsername(request)).thenReturn(USER);
    CommentDTO commentDTO = new CommentDTO();
    commentDTO.setApiId(apiId);
    commentDTO.setCommentText("comment text");
    commentDTO.setCreatedBy("creater");
    commentDTO.setLastUpdatedBy("updater");
    Comment comment = new Comment();
    comment.setCommentedUser("commentedUser");
    comment.setCommentText("this is a comment");
    comment.setCreatedUser("createdUser");
    comment.setUpdatedUser("updatedUser");
    comment.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment.setUpdatedTime(LocalDateTime.now());
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiStore).updateComment(comment, commentId, apiId, USER);
    Mockito.when(apiStore.getCommentByUUID(commentId, apiId)).thenReturn(comment);
    Response response = apisApiService.apisApiIdCommentsCommentIdPut(commentId, apiId, commentDTO, null, null, request);
    Assert.assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Comment(org.wso2.carbon.apimgt.core.models.Comment) Request(org.wso2.msf4j.Request) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 12 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class RatingMappingUtil method fromRatingToDTO.

/**
 * Converts an Rating object into corresponding REST API RatingDTO object
 *
 * @param rating Comment object
 * @return a new RatingDTO object corresponding to given Rating object
 */
public static RatingDTO fromRatingToDTO(Rating rating) {
    RatingDTO ratingDTO = new RatingDTO();
    ratingDTO.setRatingId(rating.getUuid());
    ratingDTO.setRating(rating.getRating());
    ratingDTO.setUsername(rating.getUsername());
    return ratingDTO;
}
Also used : RatingDTO(org.wso2.carbon.apimgt.rest.api.store.dto.RatingDTO)

Example 13 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromDTOToComment.

/**
 * Converts a CommentDTO to a Comment object
 *
 * @param body commentDTO body
 * @param username username of the consumer
 * @return Comment object
 */
public static Comment fromDTOToComment(CommentDTO body, String username) {
    Comment comment = new Comment();
    comment.setCommentText(body.getCommentText());
    comment.setCommentedUser(username);
    comment.setApiId(body.getApiId());
    comment.setCreatedUser(username);
    comment.setUpdatedUser(username);
    return comment;
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment)

Example 14 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentListToDTO.

/**
 * Wraps a List of Comments to a CommentListDTO
 *
 * @param commentList list of comments
 * @param limit maximum comments to return
 * @param offset  starting position of the pagination
 * @return CommentListDTO
 */
public static CommentListDTO fromCommentListToDTO(List<Comment> commentList, int limit, int offset) {
    CommentListDTO commentListDTO = new CommentListDTO();
    List<CommentDTO> listOfCommentDTOs = new ArrayList<>();
    commentListDTO.setCount(commentList.size());
    int start = offset < commentList.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= commentList.size() - 1 ? offset + limit - 1 : commentList.size() - 1;
    for (int i = start; i <= end; i++) {
        listOfCommentDTOs.add(fromCommentToDTO(commentList.get(i)));
    }
    commentListDTO.setList(listOfCommentDTOs);
    return commentListDTO;
}
Also used : ArrayList(java.util.ArrayList) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO)

Example 15 with Comment

use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.

the class APIStoreImpl method updateComment.

@Override
public void updateComment(Comment comment, String commentId, String apiId, String username) throws APICommentException, APIMgtResourceNotFoundException {
    validateCommentMaxCharacterLength(comment.getCommentText());
    try {
        failIfApiNotExists(apiId);
        Comment oldComment = getApiDAO().getCommentByUUID(commentId, apiId);
        if (oldComment != null) {
            // if the update operation is done by a user who isn't the owner of the comment
            if (!oldComment.getCommentedUser().equals(username)) {
                checkIfUserIsCommentModerator(username);
            }
            getApiDAO().updateComment(comment, commentId, apiId);
        } else {
            String errorMsg = "Couldn't find comment with comment_id : " + commentId + "and api_id : " + apiId;
            log.error(errorMsg);
            throw new APIMgtResourceNotFoundException(errorMsg, ExceptionCodes.COMMENT_NOT_FOUND);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while updating comment " + commentId;
        log.error(errorMsg, e);
        throw new APICommentException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APICommentException(org.wso2.carbon.apimgt.core.exception.APICommentException)

Aggregations

Comment (org.wso2.carbon.apimgt.core.models.Comment)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)28 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)23 Test (org.testng.annotations.Test)22 SQLException (java.sql.SQLException)18 BeforeTest (org.testng.annotations.BeforeTest)18 API (org.wso2.carbon.apimgt.core.models.API)17 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)17 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)16 Comment (org.wso2.carbon.apimgt.api.model.Comment)16 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)15 PreparedStatement (java.sql.PreparedStatement)13 Connection (java.sql.Connection)12 HashMap (java.util.HashMap)12 Map (java.util.Map)12 ArrayList (java.util.ArrayList)10 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)9 URI (java.net.URI)8 URISyntaxException (java.net.URISyntaxException)8 Test (org.junit.Test)8