Search in sources :

Example 1 with UpdateComment

use of org.wso2.carbon.humantask.core.engine.commands.UpdateComment in project carbon-apimgt by wso2.

the class ApiDAOImpl method updateComment.

@Override
public void updateComment(Comment comment, String commentId, String apiId) throws APIMgtDAOException {
    final String updateCommentQuery = "UPDATE AM_API_COMMENTS SET COMMENT_TEXT = ? " + ", UPDATED_BY = ? , LAST_UPDATED_TIME = ?" + " WHERE UUID = ? AND API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(updateCommentQuery)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, comment.getCommentText());
            statement.setString(2, comment.getUpdatedUser());
            statement.setTimestamp(3, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(4, commentId);
            statement.setString(5, apiId);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "updating comment for API " + apiId + ", Comment " + commentId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMessage = "updating comment for API " + apiId + ", Comment " + commentId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with UpdateComment

use of org.wso2.carbon.humantask.core.engine.commands.UpdateComment 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 3 with UpdateComment

use of org.wso2.carbon.humantask.core.engine.commands.UpdateComment 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 UpdateComment

use of org.wso2.carbon.humantask.core.engine.commands.UpdateComment in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testUpdateCommentDaoException.

@Test(description = "Exception in dao when updating a specific comment", expectedExceptions = APIMgtResourceNotFoundException.class)
public void testUpdateCommentDaoException() throws APIManagementException {
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    APIStore apiStore = getApiStoreImpl(apiDAO);
    API api = SampleTestObjectCreator.createDefaultAPI().build();
    Mockito.when(apiDAO.getAPI(api.getId())).thenReturn(api);
    Comment comment = SampleTestObjectCreator.createDefaultComment(api.getId());
    Mockito.when(apiDAO.getCommentByUUID(comment.getUuid(), api.getId())).thenReturn(comment);
    Mockito.doThrow(new APIMgtDAOException("Error occurred while updating comment " + comment.getUuid(), new SQLException())).when(apiDAO).updateComment(comment, comment.getUuid(), api.getId());
    apiStore.updateComment(comment, comment.getUuid(), api.getId(), "admin");
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with UpdateComment

use of org.wso2.carbon.humantask.core.engine.commands.UpdateComment in project carbon-apimgt by wso2.

the class ApisApiServiceImplTestCase method testApisApiIdCommentsCommentIdPutErrorCase.

@Test
public void testApisApiIdCommentsCommentIdPutErrorCase() 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.doThrow(new APICommentException("Error occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).updateComment(comment, commentId, apiId, USER);
    Mockito.doThrow(new APICommentException("Error occurred", ExceptionCodes.INTERNAL_ERROR)).when(apiStore).getCommentByUUID(commentId, apiId);
    Response response = apisApiService.apisApiIdCommentsCommentIdPut(commentId, apiId, commentDTO, IF_MATCH, IF_UNMODIFIED_SINCE, request);
    Assert.assertEquals(ExceptionCodes.INTERNAL_ERROR.getHttpStatusCode(), 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) 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)

Aggregations

Comment (org.wso2.carbon.apimgt.core.models.Comment)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)4 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)3 SQLException (java.sql.SQLException)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)2 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)2 APICommentException (org.wso2.carbon.apimgt.core.exception.APICommentException)2 API (org.wso2.carbon.apimgt.core.models.API)2 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)2 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)2 Request (org.wso2.msf4j.Request)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)1 HumanTaskException (org.wso2.carbon.humantask.core.engine.HumanTaskException)1 UpdateComment (org.wso2.carbon.humantask.core.engine.commands.UpdateComment)1