use of org.wso2.carbon.apimgt.core.models.Comment 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.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdCommentsCommentIdDelete.
/**
* Deletes a comment
*
* @param commentId Comment ID
* @param apiId API ID
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param request msf4j request object
* @return 200 response if the deletion was successful
* @throws NotFoundException if this method is not defined in ApisApiServiceImpl
*/
@Override
public Response apisApiIdCommentsCommentIdDelete(String commentId, String apiId, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
String existingFingerprint = apisApiIdCommentsCommentIdDeleteFingerprint(commentId, apiId, ifMatch, ifUnmodifiedSince, request);
if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
return Response.status(Response.Status.PRECONDITION_FAILED).build();
}
apiStore.deleteComment(commentId, apiId, username);
} catch (APIManagementException e) {
String errorMessage = "Error while deleting comment with commentId: " + commentId + " of apiID :" + apiId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
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();
}
return Response.ok().build();
}
use of org.wso2.carbon.apimgt.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdUserRatingPut.
/**
* Add or update raating to an API
*
* @param apiId APIID
* @param body RatingDTO object
* @param request msf4j request
* @return 201 response if successful
* @throws NotFoundException if failed to find method implementation
*/
@Override
public Response apisApiIdUserRatingPut(String apiId, RatingDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
String ratingId;
try {
APIStore apiStore = RestApiUtil.getConsumer(username);
Rating ratingFromPayload = RatingMappingUtil.fromDTOToRating(username, apiId, body);
Rating existingRating = apiStore.getRatingForApiFromUser(apiId, username);
if (existingRating != null) {
String existingRatingUUID = existingRating.getUuid();
apiStore.updateRating(apiId, existingRatingUUID, ratingFromPayload);
Rating updatedRating = apiStore.getRatingByUUID(apiId, existingRatingUUID);
RatingDTO updatedRatingDTO = RatingMappingUtil.fromRatingToDTO(updatedRating);
return Response.ok().entity(updatedRatingDTO).build();
} else {
ratingId = apiStore.addRating(apiId, ratingFromPayload);
Rating createdRating = apiStore.getRatingByUUID(apiId, ratingId);
RatingDTO createdRatingDTO = RatingMappingUtil.fromRatingToDTO(createdRating);
URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.SUBRESOURCE_PATH_RATINGS + "/" + ratingId);
return Response.status(Response.Status.CREATED).header(RestApiConstants.LOCATION_HEADER, location).entity(createdRatingDTO).build();
}
} catch (APIManagementException e) {
String errorMessage = "Error while adding/updating rating for user " + username + " for given API " + apiId;
Map<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (URISyntaxException e) {
String errorMessage = "Error while adding location header in response for comment";
ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler);
log.error(errorMessage, e);
return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
}
}
use of org.wso2.carbon.apimgt.core.models.Comment 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.core.models.Comment in project carbon-apimgt by wso2.
the class ApisApiServiceImplTestCase method testApisApiIdCommentsCommentIdGet.
@Test
public void testApisApiIdCommentsCommentIdGet() 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);
Comment comment = new Comment();
comment.setUuid(commentId);
comment.setApiId(apiId);
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.when(apiStore.getCommentByUUID(commentId, apiId)).thenReturn(comment);
Response response = apisApiService.apisApiIdCommentsCommentIdGet(commentId, apiId, null, null, request);
Assert.assertEquals(200, response.getStatus());
}
Aggregations