diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/CommentManagementAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/CommentManagementAPIImpl.java index 2ae8cb8b7e2..7c378162196 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/CommentManagementAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/impl/CommentManagementAPIImpl.java @@ -62,19 +62,17 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { List comments = new ArrayList<>(); try { PaginationRequest request = new PaginationRequest(offSet, limit); - if (request.validatePaginationRequest(offSet, limit)) { + if (uuid == null) { + return Response.status(Response.Status.NOT_FOUND).entity("Comments not found").build(); + } else if (request.validatePaginationRequest(offSet, limit)) { commentsManager.getAllComments(request, uuid); return Response.status(Response.Status.OK).entity(comments).build(); } - } catch (NotFoundException e) { - log.error("Not found exception occurs to uuid " + uuid + " .", e); - return Response.status(Response.Status.NOT_FOUND).entity("Application with UUID " + uuid + " not found") - .build(); } catch (CommentManagementException e) { String msg = "Error occurred while retrieving comments."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(" Internal server error occurs").build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(" Internal server error occurs") + .build(); } return null; } @@ -142,15 +140,16 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { CommentsManager commentsManager = APIUtil.getCommentsManager(); try { - commentsManager.deleteComment(commentId); + if (commentId == 0) { + return Response.status(Response.Status.NOT_FOUND).entity("Comment not found").build(); + } else { + commentsManager.deleteComment(commentId); + } } catch (CommentManagementException e) { String msg = "Error occurred while deleting the comment."; log.error(msg, e); - return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity("Internal server error occurs").build(); - } catch (NotFoundException e) { - log.error("Not found exception occurs to comment id " + commentId + " .", e); - return Response.status(Response.Status.NOT_FOUND).entity("Comment not found").build(); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Internal server error occurs") + .build(); } return Response.status(Response.Status.OK).entity("Comment is deleted successfully.").build(); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java index cd795abe091..e48346c9ae7 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPITest.java @@ -80,12 +80,23 @@ public class CommentManagementAPITest { @Test public void testGetAllCommentsInternalError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.doThrow(new CommentManagementException()).when(this.commentsManager).getAllComments(Mockito.any(),Mockito.anyString()); + Mockito.doThrow(new CommentManagementException()).when(this.commentsManager) + .getAllComments(Mockito.any(), Mockito.anyString()); Response response = this.commentManagementAPI.getAllComments("a", 1, 4); + Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); Mockito.reset(commentsManager); + } + + @Test + public void testGetAllCommentsNotFoundError() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.getAllComments(null, 1, 3); Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), + "The response status should be 404."); + Mockito.reset(commentsManager); } @Test @@ -174,14 +185,22 @@ public class CommentManagementAPITest { @Test public void testDeleteCommentInternalError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.deleteComment(Mockito.anyInt())) - .thenThrow(new CommentManagementException()); + Mockito.when(this.commentManagementAPI.deleteComment(1)).thenThrow(new CommentManagementException()); Response response = this.commentManagementAPI.deleteComment(1); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), "The response status should be 500."); } + @Test + public void testDeleteCommentNotFoundError() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.deleteComment(0); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.NOT_FOUND.getStatusCode(), + "The response status should be 404."); + } + @Test public void testGetStars() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); @@ -195,7 +214,8 @@ public class CommentManagementAPITest { @Test public void testGetStarsCommentError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.getStars(Mockito.anyString())).thenThrow(new CommentManagementException()); + Mockito.when(this.commentManagementAPI.getStars(Mockito.anyString())) + .thenThrow(new CommentManagementException()); Response response = this.commentManagementAPI.getStars("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -255,7 +275,7 @@ public class CommentManagementAPITest { Response response = this.commentManagementAPI.updateStars(3, "a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), - "The response status should be 200."); + "The response status should be 201."); Mockito.reset(commentsManager); }