Search in sources :

Example 1 with APICommentException

use of org.wso2.carbon.apimgt.core.exception.APICommentException in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdCommentsCommentIdDeleteNotFound.

@Test
public void testApisApiIdCommentsCommentIdDeleteNotFound() 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);
    Mockito.doThrow(new APICommentException("Error occurred", ExceptionCodes.COMMENT_NOT_FOUND)).when(apiStore).deleteComment(commentId, apiId, USER);
    Response response = apisApiService.apisApiIdCommentsCommentIdDelete(commentId, apiId, IF_MATCH, IF_UNMODIFIED_SINCE, request);
    assertEquals(response.getStatus(), 404);
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) APICommentException(org.wso2.carbon.apimgt.core.exception.APICommentException) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with APICommentException

use of org.wso2.carbon.apimgt.core.exception.APICommentException in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdCommentsGetNotFound.

@Test
public void testApisApiIdCommentsGetNotFound() throws APIManagementException, NotFoundException {
    printTestMethodName();
    String apiId = 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);
    Mockito.doThrow(new APICommentException("Error occurred", ExceptionCodes.COMMENT_NOT_FOUND)).when(apiStore).getCommentsForApi(apiId);
    Response response = apisApiService.apisApiIdCommentsGet(apiId, 3, 0, request);
    Assert.assertEquals(404, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) APICommentException(org.wso2.carbon.apimgt.core.exception.APICommentException) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with APICommentException

use of org.wso2.carbon.apimgt.core.exception.APICommentException 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)

Example 4 with APICommentException

use of org.wso2.carbon.apimgt.core.exception.APICommentException in project carbon-apimgt by wso2.

the class APIStoreImpl method addComment.

@Override
public String addComment(Comment comment, String apiId) throws APICommentException, APIMgtResourceNotFoundException {
    validateCommentMaxCharacterLength(comment.getCommentText());
    String generatedUuid = UUID.randomUUID().toString();
    comment.setUuid(generatedUuid);
    try {
        failIfApiNotExists(apiId);
        getApiDAO().addComment(comment, apiId);
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while adding comment for api - " + apiId;
        log.error(errorMsg, e);
        throw new APICommentException(errorMsg, e, e.getErrorHandler());
    }
    return comment.getUuid();
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APICommentException(org.wso2.carbon.apimgt.core.exception.APICommentException)

Example 5 with APICommentException

use of org.wso2.carbon.apimgt.core.exception.APICommentException in project carbon-apimgt by wso2.

the class APIStoreImpl method deleteComment.

@Override
public void deleteComment(String commentId, String apiId, String username) throws APICommentException, APIMgtResourceNotFoundException {
    try {
        ApiDAO apiDAO = getApiDAO();
        failIfApiNotExists(apiId);
        Comment comment = apiDAO.getCommentByUUID(commentId, apiId);
        if (comment != null) {
            // if the delete operation is done by a user who isn't the owner of the comment
            if (!comment.getCommentedUser().equals(username)) {
                checkIfUserIsCommentModerator(username);
            }
            apiDAO.deleteComment(commentId, apiId);
        } else {
            String errorMsg = "Couldn't find comment with comment_id : " + commentId;
            log.error(errorMsg);
            throw new APIMgtResourceNotFoundException(errorMsg, ExceptionCodes.COMMENT_NOT_FOUND);
        }
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while deleting 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) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO)

Aggregations

APICommentException (org.wso2.carbon.apimgt.core.exception.APICommentException)9 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)5 Comment (org.wso2.carbon.apimgt.core.models.Comment)5 Response (javax.ws.rs.core.Response)4 Test (org.junit.Test)4 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)4 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)4 Request (org.wso2.msf4j.Request)4 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)3 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)1 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)1