Search in sources :

Example 1 with CommentList

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

the class ApisApiServiceImplTestCase method testApisApiIdCommentsGet.

@Test
public void testApisApiIdCommentsGet() 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);
    Comment comment1 = new Comment();
    comment1.setUuid(UUID.randomUUID().toString());
    comment1.setCommentedUser("commentedUser1");
    comment1.setCommentText("this is a comment 1");
    comment1.setCreatedUser("createdUser1");
    comment1.setUpdatedUser("updatedUser1");
    comment1.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment1.setUpdatedTime(LocalDateTime.now());
    Comment comment2 = new Comment();
    comment2.setUuid(UUID.randomUUID().toString());
    comment2.setCommentedUser("commentedUser2");
    comment2.setCommentText("this is a comment 2");
    comment2.setCreatedUser("createdUser2");
    comment2.setUpdatedUser("updatedUser2");
    comment2.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment2.setUpdatedTime(LocalDateTime.now());
    List<Comment> commentList = new ArrayList<>();
    commentList.add(comment1);
    commentList.add(comment2);
    Mockito.when(apiStore.getCommentsForApi(apiId)).thenReturn(commentList);
    Response response = apisApiService.apisApiIdCommentsGet(apiId, 3, 0, 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) ArrayList(java.util.ArrayList) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with CommentList

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

the class CommentMappingUtil method fromCommentListToDTO.

/**
 * Wraps a List of Comments to a CommentListDTO
 *
 * @param commentList list of comments
 * @param limit maximum comments to return
 * @param offset  starting position of the pagination
 * @return CommentListDTO
 */
public static CommentListDTO fromCommentListToDTO(List<Comment> commentList, int limit, int offset) {
    CommentListDTO commentListDTO = new CommentListDTO();
    List<CommentDTO> listOfCommentDTOs = new ArrayList<>();
    commentListDTO.setCount(commentList.size());
    int start = offset < commentList.size() && offset >= 0 ? offset : Integer.MAX_VALUE;
    int end = offset + limit - 1 <= commentList.size() - 1 ? offset + limit - 1 : commentList.size() - 1;
    for (int i = start; i <= end; i++) {
        listOfCommentDTOs.add(fromCommentToDTO(commentList.get(i)));
    }
    commentListDTO.setList(listOfCommentDTOs);
    return commentListDTO;
}
Also used : ArrayList(java.util.ArrayList) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO)

Example 3 with CommentList

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

the class CommentMappingUtilTestCase method testFromCommentListToDTO.

@Test
public void testFromCommentListToDTO() {
    Comment comment1 = new Comment();
    comment1.setUuid(UUID.randomUUID().toString());
    comment1.setCommentedUser("commentedUser1");
    comment1.setCommentText("this is a comment 1");
    comment1.setCreatedUser("createdUser1");
    comment1.setUpdatedUser("updatedUser1");
    comment1.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment1.setUpdatedTime(LocalDateTime.now());
    Comment comment2 = new Comment();
    comment2.setUuid(UUID.randomUUID().toString());
    comment2.setCommentedUser("commentedUser2");
    comment2.setCommentText("this is a comment 2");
    comment2.setCreatedUser("createdUser2");
    comment2.setUpdatedUser("updatedUser2");
    comment2.setCreatedTime(LocalDateTime.now().minusHours(1));
    comment2.setUpdatedTime(LocalDateTime.now());
    List<Comment> commentList = new ArrayList<>();
    commentList.add(comment1);
    commentList.add(comment2);
    CommentListDTO commentListDTO = commentMappingUtil.fromCommentListToDTO(commentList, 10, 0);
    Assert.assertEquals(commentListDTO.getList().get(0).getUsername().toString(), "commentedUser1");
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) ArrayList(java.util.ArrayList) CommentListDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO) Test(org.junit.Test)

Example 4 with CommentList

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

the class ApiMgtDAO method getComments.

/**
 * Returns all the Comments on an API
 *
 * @param uuid      API uuid
 * @param parentCommentID Parent Comment ID
 * @return Comment Array
 * @throws APIManagementException
 */
public Comment[] getComments(String uuid, String parentCommentID) throws APIManagementException {
    List<Comment> commentList = new ArrayList<Comment>();
    Connection connection = null;
    ResultSet resultSet = null;
    PreparedStatement prepStmt = null;
    int id = -1;
    String sqlQuery;
    if (parentCommentID == null) {
        sqlQuery = SQLConstantManagerFactory.getSQlString("GET_ROOT_COMMENTS_SQL");
    } else {
        sqlQuery = SQLConstantManagerFactory.getSQlString("GET_REPLIES_SQL");
    }
    try {
        connection = APIMgtDBUtil.getConnection();
        id = getAPIID(uuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for API with UUID: " + uuid;
            throw new APIManagementException(msg);
        }
        prepStmt = connection.prepareStatement(sqlQuery);
        prepStmt.setString(1, uuid);
        if (parentCommentID != null) {
            prepStmt.setString(2, parentCommentID);
        }
        resultSet = prepStmt.executeQuery();
        while (resultSet.next()) {
            Comment comment = new Comment();
            comment.setId(resultSet.getString("COMMENT_ID"));
            comment.setText(resultSet.getString("COMMENT_TEXT"));
            comment.setUser(resultSet.getString("CREATED_BY"));
            comment.setCreatedTime(resultSet.getTimestamp("CREATED_TIME"));
            comment.setUpdatedTime(resultSet.getTimestamp("UPDATED_TIME"));
            comment.setApiId(resultSet.getString("API_ID"));
            comment.setParentCommentID(resultSet.getString("PARENT_COMMENT_ID"));
            comment.setEntryPoint(resultSet.getString("ENTRY_POINT"));
            comment.setCategory(resultSet.getString("CATEGORY"));
            commentList.add(comment);
        }
    } catch (SQLException e) {
        try {
            if (connection != null) {
                connection.rollback();
            }
        } catch (SQLException e1) {
            log.error("Failed to retrieve comments ", e1);
        }
        handleException("Failed to retrieve comments for API with UUID " + uuid, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, resultSet);
    }
    return commentList.toArray(new Comment[commentList.size()]);
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 5 with CommentList

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

the class ApiMgtDAO method getComments.

/**
 **************************************
 * Returns all the Comments on an API
 *
 * @param apiTypeWrapper API type Wrapper
 * @param parentCommentID Parent Comment ID
 * @return Comment Array
 * @throws APIManagementException
 */
public CommentList getComments(ApiTypeWrapper apiTypeWrapper, String parentCommentID, Integer limit, Integer offset) throws APIManagementException {
    CommentList commentList = null;
    try (Connection connection = APIMgtDBUtil.getConnection()) {
        int id = -1;
        String uuid;
        Identifier identifier;
        String currentApiUuid;
        if (apiTypeWrapper.isAPIProduct()) {
            identifier = apiTypeWrapper.getApiProduct().getId();
            uuid = apiTypeWrapper.getApiProduct().getUuid();
            APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                currentApiUuid = apiRevision.getApiUUID();
            } else {
                currentApiUuid = uuid;
            }
        } else {
            identifier = apiTypeWrapper.getApi().getId();
            uuid = apiTypeWrapper.getApi().getUuid();
            APIRevision apiRevision = checkAPIUUIDIsARevisionUUID(uuid);
            if (apiRevision != null && apiRevision.getApiUUID() != null) {
                currentApiUuid = apiRevision.getApiUUID();
            } else {
                currentApiUuid = uuid;
            }
        }
        id = getAPIID(currentApiUuid, connection);
        if (id == -1) {
            String msg = "Could not load API record for: " + identifier.getName();
            throw new APIManagementException(msg);
        }
        commentList = getComments(currentApiUuid, parentCommentID, limit, offset, connection);
    } catch (SQLException e) {
        handleException("Failed to retrieve comments for  " + apiTypeWrapper.getName(), e);
    }
    return commentList;
}
Also used : DeployedAPIRevision(org.wso2.carbon.apimgt.api.model.DeployedAPIRevision) APIRevision(org.wso2.carbon.apimgt.api.model.APIRevision) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) APIProductIdentifier(org.wso2.carbon.apimgt.api.model.APIProductIdentifier) Identifier(org.wso2.carbon.apimgt.api.model.Identifier) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CommentList(org.wso2.carbon.apimgt.api.model.CommentList)

Aggregations

ArrayList (java.util.ArrayList)9 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)8 CommentList (org.wso2.carbon.apimgt.api.model.CommentList)6 Comment (org.wso2.carbon.apimgt.core.models.Comment)6 URI (java.net.URI)4 URISyntaxException (java.net.URISyntaxException)4 SQLException (java.sql.SQLException)4 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)4 Comment (org.wso2.carbon.apimgt.api.model.Comment)4 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 CommentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO)3 CommentListDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentListDTO)3 Test (org.junit.Test)2 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)2 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)2