use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdCommentsCommentIdPut.
/**
* @param commentId Comment ID
* @param apiId API ID
* @param body comment body
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return comment update response
* @throws NotFoundException if this method is not defined in ApisApiServiceImpl
*/
@Override
public Response apisApiIdCommentsCommentIdPut(String commentId, String apiId, CommentDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = apisApiIdCommentsCommentIdPutFingerprint(commentId, apiId, body, ifMatch, ifUnmodifiedSince, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
Comment comment = CommentMappingUtil.fromDTOToComment(body, username);
apiStore.updateComment(comment, commentId, apiId, username);
Comment updatedComment = apiStore.getCommentByUUID(commentId, apiId);
CommentDTO updatedCommentDTO = CommentMappingUtil.fromCommentToDTO(updatedComment);
String newFingerprint = getEtag(commentId, request.getProperty("LOGGED_IN_USER").toString());
return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(updatedCommentDTO).build();
} catch (APIManagementException e) {
String errorMessage = "Error while updating comment : " + commentId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, body.getApiId());
paramList.put(APIMgtConstants.ExceptionsConstants.COMMENT_ID, commentId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdCommentsCommentIdPut.
@Test
public void testApisApiIdCommentsCommentIdPut() 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.doNothing().doThrow(new IllegalArgumentException()).when(apiStore).updateComment(comment, commentId, apiId, USER);
Mockito.when(apiStore.getCommentByUUID(commentId, apiId)).thenReturn(comment);
Response response = apisApiService.apisApiIdCommentsCommentIdPut(commentId, apiId, commentDTO, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class CommentMappingUtil method fromDTOToComment.
/**
* Converts a CommentDTO to a Comment object
*
* @param body commentDTO body
* @param username username of the consumer
* @return Comment object
*/
public static Comment fromDTOToComment(CommentDTO body, String username) {
Comment comment = new Comment();
comment.setCommentText(body.getCommentText());
comment.setCommentedUser(username);
comment.setApiId(body.getApiId());
comment.setCreatedUser(username);
comment.setUpdatedUser(username);
return comment;
}
use of org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO 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.rest.api.store.dto.CommentDTO in project carbon-apimgt by wso2.
the class CommentMappingUtil method fromCommentToDTOWithUserInfo.
/**
* Converts a Comment object into corresponding REST API CommentDTO object with User Info.
*
* @param comment comment object
* @return CommentDTO
*/
public static CommentDTO fromCommentToDTOWithUserInfo(Comment comment, Map<String, Map<String, String>> userClaimsMap) throws APIManagementException {
CommentDTO commentDTO = fromCommentToDTO(comment);
if (userClaimsMap.get(comment.getUser()) != null) {
Map userClaims = userClaimsMap.get(comment.getUser());
CommenterInfoDTO commenterInfoDTO = new CommenterInfoDTO();
commenterInfoDTO.setFullName((String) userClaims.get(APIConstants.FULL_NAME));
commenterInfoDTO.setFirstName((String) userClaims.get(APIConstants.FIRST_NAME));
commenterInfoDTO.setLastName((String) userClaims.get(APIConstants.LAST_NAME));
commentDTO.setCommenterInfo(commenterInfoDTO);
}
return commentDTO;
}
Aggregations