Search in sources :

Example 1 with DeleteComment

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

the class ApiDAOImpl method deleteComment.

@Override
public void deleteComment(String commentId, String apiId) throws APIMgtDAOException {
    final String deleteCommentQuery = "DELETE FROM AM_API_COMMENTS WHERE UUID = ? AND API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(deleteCommentQuery)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, commentId);
            statement.setString(2, apiId);
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "deleting 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 = "deleting 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 DeleteComment

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

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

the class ApisApiServiceImplTestCase method testApisApiIdCommentsCommentIdDeleteIfMatchStringExistingFingerprintCheck.

@Test
public void testApisApiIdCommentsCommentIdDeleteIfMatchStringExistingFingerprintCheck() 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);
    String existingFingerprint = "existingFingerprint";
    Mockito.when(apisApiService.apisApiIdCommentsCommentIdDeleteFingerprint(commentId, apiId, "test", "test", request)).thenReturn(existingFingerprint);
    Mockito.doNothing().doThrow(new IllegalArgumentException()).when(apiStore).deleteComment(commentId, apiId, USER);
    Response response = apisApiService.apisApiIdCommentsCommentIdDelete(commentId, apiId, "test", "test", request);
    assertEquals(response.getStatus(), 412);
}
Also used : Response(javax.ws.rs.core.Response) Request(org.wso2.msf4j.Request) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 4 with DeleteComment

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

Example 5 with DeleteComment

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

the class APIStoreImplTestCase method testDeleteCommentDaoException.

@Test(description = "Exception in dao when deleting a specific comment", expectedExceptions = APIMgtResourceNotFoundException.class)
public void testDeleteCommentDaoException() 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 deleting comment " + comment.getUuid(), new SQLException())).when(apiDAO).deleteComment(comment.getUuid(), api.getId());
    apiStore.deleteComment(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)

Aggregations

APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 SQLException (java.sql.SQLException)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 PreparedStatement (java.sql.PreparedStatement)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)3 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)3 Comment (org.wso2.carbon.apimgt.core.models.Comment)3 Request (org.wso2.msf4j.Request)3 Connection (java.sql.Connection)2 JSONObject (org.json.simple.JSONObject)2 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)2 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)2 Comment (org.wso2.carbon.apimgt.api.model.Comment)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