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);
}
}
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());
}
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());
}
}
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");
}
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());
}
Aggregations