Search in sources :

Example 1 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.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();
    }
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 2 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.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());
}
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) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.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;
}
Also used : Comment(org.wso2.carbon.apimgt.core.models.Comment)

Example 4 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.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;
}
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 5 with CommentDTO

use of org.wso2.carbon.apimgt.rest.api.publisher.v1.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;
}
Also used : CommenterInfoDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommenterInfoDTO) CommentDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

URI (java.net.URI)11 URISyntaxException (java.net.URISyntaxException)11 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)10 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)10 Comment (org.wso2.carbon.apimgt.api.model.Comment)10 HashMap (java.util.HashMap)9 Map (java.util.Map)9 CommentDTO (org.wso2.carbon.apimgt.rest.api.store.dto.CommentDTO)9 Comment (org.wso2.carbon.apimgt.core.models.Comment)8 CommentDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentDTO)6 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)5 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)5 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)5 Test (org.junit.Test)4 CommentList (org.wso2.carbon.apimgt.api.model.CommentList)4 CommentDTO (com.odysseusinc.arachne.portal.api.v1.dto.CommentDTO)3 ArrayList (java.util.ArrayList)3 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)3 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)3 CommentListDTO (org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.CommentListDTO)3