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