Search in sources :

Example 46 with Comment

use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.

the class ApiDAOImpl method getCommentsForApi.

@Override
public List<Comment> getCommentsForApi(String apiId) throws APIMgtDAOException {
    List<Comment> commentList = new ArrayList<>();
    final String getCommentsQuery = "SELECT UUID, COMMENT_TEXT, USER_IDENTIFIER, API_ID, " + "CREATED_BY, CREATED_TIME, UPDATED_BY, LAST_UPDATED_TIME " + "FROM AM_API_COMMENTS WHERE API_ID = ?";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(getCommentsQuery)) {
        try {
            statement.setString(1, apiId);
            statement.execute();
            try (ResultSet rs = statement.getResultSet()) {
                while (rs.next()) {
                    commentList.add(constructCommentFromResultSet(rs));
                }
            }
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "getting all comments for API " + apiId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        }
    } catch (SQLException e) {
        String errorMessage = "getting all comments for API " + apiId;
        throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
    }
    return commentList;
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 47 with Comment

use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.

the class ApiDAOImpl method addComment.

@Override
public void addComment(Comment comment, String apiId) throws APIMgtDAOException {
    final String addCommentQuery = "INSERT INTO AM_API_COMMENTS (UUID, COMMENT_TEXT, USER_IDENTIFIER, API_ID, " + "CREATED_BY, CREATED_TIME, UPDATED_BY, LAST_UPDATED_TIME" + ") VALUES (?,?,?,?,?,?,?,?)";
    try (Connection connection = DAOUtil.getConnection();
        PreparedStatement statement = connection.prepareStatement(addCommentQuery)) {
        try {
            connection.setAutoCommit(false);
            statement.setString(1, comment.getUuid());
            statement.setString(2, comment.getCommentText());
            statement.setString(3, comment.getCommentedUser());
            statement.setString(4, apiId);
            statement.setString(5, comment.getCreatedUser());
            statement.setTimestamp(6, Timestamp.valueOf(LocalDateTime.now()));
            statement.setString(7, comment.getUpdatedUser());
            statement.setTimestamp(8, Timestamp.valueOf(LocalDateTime.now()));
            statement.execute();
            connection.commit();
        } catch (SQLException e) {
            connection.rollback();
            String errorMessage = "adding comment for API " + apiId;
            throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + errorMessage, e);
        } finally {
            connection.setAutoCommit(DAOUtil.isAutoCommit());
        }
    } catch (SQLException e) {
        String errorMessage = "adding comment for API " + apiId;
        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 48 with Comment

use of org.wso2.carbon.apimgt.api.model.Comment 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)

Example 49 with Comment

use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.

the class CommentMappingUtil method fromCommentToDTO.

/**
 * Converts a Comment object into corresponding REST API CommentDTO object
 *
 * @param comment comment object
 * @return CommentDTO
 */
public static CommentDTO fromCommentToDTO(Comment comment) {
    CommentDTO commentDTO = new CommentDTO();
    commentDTO.setCommentId(comment.getUuid());
    commentDTO.setUsername(comment.getCommentedUser());
    commentDTO.setCommentText(comment.getCommentText());
    commentDTO.setCreatedBy(comment.getCreatedUser());
    commentDTO.setLastUpdatedBy(comment.getUpdatedUser());
    commentDTO.setCreatedTime(comment.getCreatedTime().toString());
    commentDTO.setLastUpdatedTime(comment.getUpdatedTime().toString());
    return commentDTO;
}
Also used : CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)

Example 50 with Comment

use of org.wso2.carbon.apimgt.api.model.Comment in project carbon-apimgt by wso2.

the class SampleTestObjectCreator method createDefaultComment.

public static Comment createDefaultComment(String apiId) {
    Comment comment = new Comment();
    comment.setUuid(UUID.randomUUID().toString());
    comment.setApiId(apiId);
    comment.setCommentText("this is a sample comment");
    comment.setCommentedUser("admin");
    comment.setUpdatedUser("admin");
    comment.setCreatedTime(LocalDateTime.now());
    comment.setUpdatedTime(LocalDateTime.now());
    return comment;
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment)

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