use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getRepliesOfComment.
@Override
public Response getRepliesOfComment(String commentId, String apiId, String xWSO2Tenant, Integer limit, Integer offset, String ifNoneMatch, Boolean includeCommenterInfo, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
CommentList comments = apiConsumer.getComments(apiTypeWrapper, commentId, limit, offset);
CommentListDTO commentDTO = CommentMappingUtil.fromCommentListToDTO(comments, includeCommenterInfo);
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS;
URI uri = new URI(uriString);
return Response.ok(uri).entity(commentDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Failed to get comments of API " + apiId, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving comments content location for API " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method addCommentToAPI.
@Override
public Response addCommentToAPI(String apiId, PostRequestBodyDTO postRequestBodyDTO, String replyTo, MessageContext messageContext) throws APIManagementException {
String username = RestApiCommonUtil.getLoggedInUsername();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = new Comment();
comment.setText(postRequestBodyDTO.getContent());
comment.setCategory(postRequestBodyDTO.getCategory());
comment.setParentCommentID(replyTo);
comment.setEntryPoint("DEVPORTAL");
comment.setUser(username);
comment.setApiId(apiId);
String createdCommentId = apiConsumer.addComment(apiId, comment, username);
Comment createdComment = apiConsumer.getComment(apiTypeWrapper, createdCommentId, 0, 0);
CommentDTO commentDTO = CommentMappingUtil.fromCommentToDTO(createdComment);
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + createdCommentId;
URI uri = new URI(uriString);
return Response.created(uri).entity(commentDTO).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
RestApiUtil.handleInternalServerError("Failed to add comment to the API " + apiId, e, log);
}
} catch (URISyntaxException e) {
throw new APIManagementException("Error while retrieving comment content location for API " + apiId);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getCommentOfAPI.
@Override
public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = apiConsumer.getComment(apiTypeWrapper, commentId, replyLimit, replyOffset);
if (comment != null) {
CommentDTO commentDTO;
if (includeCommenterInfo) {
Map<String, Map<String, String>> userClaimsMap = CommentMappingUtil.retrieveUserClaims(comment.getUser(), new HashMap<>());
commentDTO = CommentMappingUtil.fromCommentToDTOWithUserInfo(comment, userClaimsMap);
} else {
commentDTO = CommentMappingUtil.fromCommentToDTO(comment);
}
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + commentId;
URI uri = new URI(uriString);
return Response.ok(uri).entity(commentDTO).build();
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving comment for API : " + apiId + "with comment ID " + commentId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving comment content location : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO 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());
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO 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;
}
Aggregations