From e70899a19315848e203b0766433fe59d7a7b489a Mon Sep 17 00:00:00 2001 From: nishadi Date: Tue, 27 Feb 2018 16:47:12 +0530 Subject: [PATCH] Add new methods Exception handling test methods for rating methods --- .../services/CommentManagementAPITest.java | 45 ++++++++++++++++--- 1 file changed, 39 insertions(+), 6 deletions(-) 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 42ccb831498..cd795abe091 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 @@ -31,7 +31,6 @@ import org.testng.annotations.BeforeClass; import org.testng.annotations.ObjectFactory; import org.testng.annotations.Test; import org.wso2.carbon.device.application.mgt.common.Comment; -import org.wso2.carbon.device.application.mgt.common.PaginationRequest; import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; @@ -41,8 +40,6 @@ import org.wso2.carbon.device.application.mgt.store.api.services.util.CommentMgt import javax.ws.rs.core.Response; -import java.util.List; - import static org.mockito.MockitoAnnotations.initMocks; @PowerMockIgnore("javax.ws.rs.*") @@ -80,6 +77,17 @@ public class CommentManagementAPITest { Mockito.reset(commentsManager); } + @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()); + Response response = this.commentManagementAPI.getAllComments("a", 1, 4); + Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), + "The response status should be 500."); + Mockito.reset(commentsManager); + Assert.assertNotNull(response, "The response object is null."); + } + @Test public void testAddComments() throws Exception { Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); @@ -143,6 +151,17 @@ public class CommentManagementAPITest { "The response status should be 404."); } + @Test + public void testUpdateCommentInternalServerError() throws Exception { + Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Mockito.doThrow(new CommentManagementException()).when(this.commentsManager).updateComment(comment, 9); + Response response = this.commentManagementAPI.updateComment(comment, 9); + 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 testDeleteComment() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); @@ -187,7 +206,8 @@ public class CommentManagementAPITest { @Test public void testGetStarsApplicationError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.getStars(Mockito.anyString())).thenThrow(new ApplicationManagementException()); + Mockito.when(this.commentManagementAPI.getStars(Mockito.anyString())) + .thenThrow(new ApplicationManagementException()); Response response = this.commentManagementAPI.getStars("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -208,7 +228,8 @@ public class CommentManagementAPITest { @Test public void testGetRatedUserCommentError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.getRatedUser(Mockito.anyString())).thenThrow(new CommentManagementException()); + Mockito.when(this.commentManagementAPI.getRatedUser(Mockito.anyString())) + .thenThrow(new CommentManagementException()); Response response = this.commentManagementAPI.getRatedUser("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -219,7 +240,8 @@ public class CommentManagementAPITest { @Test public void testGetRatedUserApplicationError() throws Exception { PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); - Mockito.when(this.commentManagementAPI.getRatedUser(Mockito.anyString())).thenThrow(new ApplicationManagementException()); + Mockito.when(this.commentManagementAPI.getRatedUser(Mockito.anyString())) + .thenThrow(new ApplicationManagementException()); Response response = this.commentManagementAPI.getRatedUser("a"); Assert.assertNotNull(response, "The response object is null."); Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), @@ -247,4 +269,15 @@ public class CommentManagementAPITest { Mockito.reset(commentsManager); } + @Test + public void testUpdateStarsApplicationError() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Mockito.doThrow(new ApplicationManagementException()).when(this.commentsManager) + .updateStars(Mockito.anyInt(), Mockito.anyString()); + Response response = this.commentManagementAPI.updateStars(3, "a"); + 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); + } } \ No newline at end of file