diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java index 6428f11ceb..82beeca2c3 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.common/src/main/java/org/wso2/carbon/device/application/mgt/common/services/CommentsManager.java @@ -35,12 +35,11 @@ public interface CommentsManager { * To add a comment to a application * * @param comment comment of the application. - * @param uuid uuid of the application release - * @param tenantId tenant id of the application + * @param uuid uuid of the application release\ * @return {@link Comment} Comment added * @throws CommentManagementException Exceptions of the comment management. */ - Comment addComment(Comment comment, String uuid, int tenantId) throws CommentManagementException; + Comment addComment(Comment comment, String uuid) throws CommentManagementException; /** * To validate the pre-request of the comment diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java index 4a5ab8b1c9..6f8b6a1fa6 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/impl/CommentsManagerImpl.java @@ -56,11 +56,12 @@ public class CommentsManagerImpl implements CommentsManager { } @Override - public Comment addComment(Comment comment, String uuid, int tenantId) throws CommentManagementException { + public Comment addComment(Comment comment, String uuid) throws CommentManagementException { if (log.isDebugEnabled()) { log.debug("Request for comment is received for uuid" + uuid); } + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); comment.setCreatedAt(Timestamp.from(Instant.now())); try { ConnectionManagerUtil.beginDBTransaction(); @@ -140,7 +141,7 @@ public class CommentsManagerImpl implements CommentsManager { public Comment getComment(int commentId) throws CommentManagementException { PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); - Comment comment = null; + Comment comment; if (log.isDebugEnabled()) { log.debug("Comment retrieval request is received for the comment id " + commentId); } @@ -214,7 +215,7 @@ public class CommentsManagerImpl implements CommentsManager { @Override public int getRatedUser(String uuid) throws ApplicationManagementException { - int ratedUsers = 0; + int ratedUsers; if (log.isDebugEnabled()) { log.debug("Get the rated users for the application release number" + uuid); } @@ -233,7 +234,7 @@ public class CommentsManagerImpl implements CommentsManager { @Override public int getStars(String uuid) throws ApplicationManagementException { - int stars = 0; + int stars; if (log.isDebugEnabled()) { log.debug("Get the average of rated stars for the application " + uuid); } @@ -255,7 +256,7 @@ public class CommentsManagerImpl implements CommentsManager { if (log.isDebugEnabled()) { log.debug("Stars are received for the application " + uuid); } - int newStars = 0; + int newStars; try { ConnectionManagerUtil.beginDBTransaction(); int ratedUsers = commentDAO.getRatedUser(uuid); diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/pom.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/pom.xml index 1a7bbecce2..67ce79c8b8 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/pom.xml +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/pom.xml @@ -157,6 +157,16 @@ org.wso2.carbon.devicemgt org.wso2.carbon.device.application.mgt.core provided + + + org.mockito + mockito-core + + + javassist + javassist + + org.wso2.carbon.devicemgt @@ -229,5 +239,15 @@ javax.ws.rs javax.ws.rs-api + + org.powermock + powermock-module-testng + test + + + org.powermock + powermock-api-mockito + test + \ No newline at end of file diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/CommentManagementAPI.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/CommentManagementAPI.java similarity index 99% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/CommentManagementAPI.java rename to components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/main/java/org/wso2/carbon/device/application/mgt/store/api/services/CommentManagementAPI.java index 87a391758a..8fda8cd31d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/CommentManagementAPI.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/CommentManagementAPI.java @@ -16,7 +16,7 @@ * under the License. * */ -package org.wso2.carbon.device.application.mgt.api.services; +package org.wso2.carbon.device.application.mgt.store.api.services; import io.swagger.annotations.ApiParam; import io.swagger.annotations.Api; @@ -30,7 +30,7 @@ import io.swagger.annotations.ApiResponse; import io.swagger.annotations.ApiResponses; import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scopes; -import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; +import org.wso2.carbon.device.application.mgt.publisher.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.common.Comment; import javax.validation.Valid; import javax.ws.rs.Path; diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/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 similarity index 79% rename from components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/CommentManagementAPIImpl.java rename to 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 be1a7cfe85..8f842d3e1a 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/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 @@ -16,14 +16,13 @@ * under the License. * */ -package org.wso2.carbon.device.application.mgt.api.services.impl; +package org.wso2.carbon.device.application.mgt.store.api.services.impl; import io.swagger.annotations.ApiParam; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.application.mgt.api.APIUtil; -import org.wso2.carbon.device.application.mgt.api.services.CommentManagementAPI; +import org.wso2.carbon.device.application.mgt.store.api.APIUtil; +import org.wso2.carbon.device.application.mgt.store.api.services.CommentManagementAPI; 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; @@ -38,7 +37,6 @@ import javax.ws.rs.PUT; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.DELETE; -import javax.ws.rs.NotFoundException; import javax.ws.rs.core.Response; import java.util.ArrayList; import java.util.List; @@ -63,19 +61,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(msg) + .build(); } return Response.status(Response.Status.OK).entity(comments).build(); } @@ -89,9 +85,8 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { @PathParam("uuid") String uuid) { CommentsManager commentsManager = APIUtil.getCommentsManager(); - int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); try { - Comment newComment = commentsManager.addComment(comment, uuid, tenantId); + Comment newComment = commentsManager.addComment(comment, uuid); if (comment != null) { return Response.status(Response.Status.CREATED).entity(newComment).build(); } else { @@ -103,7 +98,7 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { String msg = "Error occurred while creating the comment"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity("Internal server error occurs").build(); + .entity(msg).build(); } } @@ -132,7 +127,7 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { 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(); + .entity(msg).build(); } } @@ -144,15 +139,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(msg) + .build(); } return Response.status(Response.Status.OK).entity("Comment is deleted successfully.").build(); } @@ -164,16 +160,17 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { @PathParam("uuid") String uuid) { CommentsManager commentsManager = APIUtil.getCommentsManager(); - int Stars = 0; + int Stars; try { Stars = commentsManager.getStars(uuid); } catch (CommentManagementException e) { log.error("Comment Management Exception occurs", e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); } catch (ApplicationManagementException e) { - log.error("Application Management Exception occurs", e); + String msg="Application Management Exception occurs"; + log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity("Internal server error occurs").build(); + .entity(msg).build(); } return Response.status(Response.Status.OK).entity(Stars).build(); } @@ -185,15 +182,17 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { @PathParam("uuid") String uuid) { CommentsManager commentsManager = APIUtil.getCommentsManager(); - int ratedUsers = 0; + int ratedUsers; try { ratedUsers = commentsManager.getRatedUser(uuid); } catch (CommentManagementException e) { - log.error("Comment Management Exception occurs", e); + String msg="Comment Management Exception occurs"; + log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity("Internal server error occurs").build(); + .entity(msg).build(); } catch (ApplicationManagementException e) { - log.error("Application Management Exception occurs", e); + String msg="Application Management Exception occurs"; + log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) .entity("Application with UUID" + uuid + " Internal server error occurs").build(); } @@ -208,10 +207,9 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { @PathParam("uuid") String uuid) { CommentsManager commentsManager = APIUtil.getCommentsManager(); - int newStars = 0; + int newStars; try { newStars = commentsManager.updateStars(stars, uuid); - if (stars != 0) { return Response.status(Response.Status.CREATED).entity(newStars).build(); } else { @@ -220,9 +218,10 @@ public class CommentManagementAPIImpl implements CommentManagementAPI { return Response.status(Response.Status.BAD_REQUEST).build(); } } catch (ApplicationManagementException e) { - log.error("Application Management Exception occurs", e); + String msg="Application Management Exception occurs"; + log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR) - .entity(" Internal server error occurs").build(); + .entity(msg).build(); } } } \ No newline at end of file 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 new file mode 100644 index 0000000000..e48346c9ae --- /dev/null +++ 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 @@ -0,0 +1,303 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + */ +package org.wso2.carbon.device.application.mgt.store.api.services; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor; +import org.testng.Assert; +import org.testng.IObjectFactory; +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.exception.ApplicationManagementException; +import org.wso2.carbon.device.application.mgt.common.exception.CommentManagementException; +import org.wso2.carbon.device.application.mgt.common.services.CommentsManager; +import org.wso2.carbon.device.application.mgt.store.api.APIUtil; +import org.wso2.carbon.device.application.mgt.store.api.services.impl.CommentManagementAPIImpl; +import org.wso2.carbon.device.application.mgt.store.api.services.util.CommentMgtTestHelper; + +import javax.ws.rs.core.Response; + +import static org.mockito.MockitoAnnotations.initMocks; + +@PowerMockIgnore("javax.ws.rs.*") +@SuppressStaticInitializationFor({ + "org.wso2.carbon.device.application.mgt.api.APIUtil" }) +@PrepareForTest({ APIUtil.class, CommentsManager.class, + CommentManagementAPITest.class}) +public class CommentManagementAPITest { + private static final Log log = LogFactory.getLog(CommentManagementAPI.class); + + private CommentManagementAPI commentManagementAPI; + private CommentsManager commentsManager; + + @ObjectFactory + public IObjectFactory getObjectFactory() { + return new org.powermock.modules.testng.PowerMockObjectFactory(); + } + + @BeforeClass + void init() throws CommentManagementException { + + log.info("Initializing CommentManagementAPI tests"); + initMocks(this); + this.commentsManager = Mockito.mock(CommentsManager.class, Mockito.RETURNS_DEFAULTS); + this.commentManagementAPI = new CommentManagementAPIImpl(); + } + + @Test + public void testGetAllCommentsWithValidDetails() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.getAllComments("a", 1, 2); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), + "The response status should be 200."); + 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.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 + public void testAddComments() throws Exception { + Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.addComments(comment, "a"); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), + "The response status should be 201."); + Mockito.reset(commentsManager); + } + + @Test + public void testAddNullComment() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.addComments(null, "a"); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), + "The response status should be 400."); + Mockito.reset(commentsManager); + } + + @Test + public void testAddCommentsInternalError() throws Exception { + Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Mockito.when(this.commentManagementAPI.addComments(Mockito.any(), Mockito.anyString())) + .thenThrow(new CommentManagementException()); + Response response = this.commentManagementAPI.addComments(comment, null); + 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 testUpdateComment() throws Exception { + Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.updateComment(comment, 1); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), + "The response status should be 200."); + } + + @Test + public void testUpdateNullComment() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.updateComment(null, 1); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), + "The response status should be 400."); + } + + @Test + public void testUpdateCommentWhenNullCommentId() throws Exception { + Comment comment = CommentMgtTestHelper.getDummyComment("a", "a"); + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.updateComment(comment, 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 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); + Response response = this.commentManagementAPI.deleteComment(1); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), + "The response status should be 200."); + } + + @Test + public void testDeleteCommentInternalError() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + 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); + Response response = this.commentManagementAPI.getStars("a"); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), + "The response status should be 200."); + Mockito.reset(commentsManager); + } + + @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()); + Response response = this.commentManagementAPI.getStars("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); + } + + @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()); + Response response = this.commentManagementAPI.getStars("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); + } + + @Test + public void testGetRatedUser() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.getRatedUser("a"); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.OK.getStatusCode(), + "The response status should be 200."); + Mockito.reset(commentsManager); + } + + @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()); + Response response = this.commentManagementAPI.getRatedUser("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); + } + + @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()); + Response response = this.commentManagementAPI.getRatedUser("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); + } + + @Test + public void testUpdateStars() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + 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 201."); + Mockito.reset(commentsManager); + } + + @Test + public void testUpdateInvalidStars() throws Exception { + PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); + Response response = this.commentManagementAPI.updateStars(0, "a"); + Assert.assertNotNull(response, "The response object is null."); + Assert.assertEquals(response.getStatus(), Response.Status.BAD_REQUEST.getStatusCode(), + "The response status should be 400."); + 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 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/util/CommentMgtTestHelper.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/util/CommentMgtTestHelper.java new file mode 100644 index 0000000000..836d2798ae --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/java/org/wso2/carbon/device/application/mgt/store/api/services/util/CommentMgtTestHelper.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * you may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.wso2.carbon.device.application.mgt.store.api.services.util; + +import org.wso2.carbon.device.application.mgt.common.Comment; + +/** + * Helper class for Comment Management API test cases. + */ + +public class CommentMgtTestHelper { + + private static final String COMMENT_TEXT = "Dummy Comment"; + private static final String CREATED_BY = "TEST_CREATED_BY"; + private static final String MODIFIED_BY = "TEST_MODIFIED_BY"; + private static final int PARENT_ID = 123; + private static final int COMMENT_ID = 1; + + /** + * Creates a Comment with given text and given uuid. + * If the text is null, the COMMENT_TEXT will be used as the Dummy Comment. + * + * @param commentText : Text of the Comment + * @return Comment + */ + public static Comment getDummyComment(String commentText, String uuid) { + Comment comment = new Comment(); + comment.setId(COMMENT_ID); + comment.setCreatedBy(CREATED_BY); + comment.setModifiedBy(MODIFIED_BY); + comment.setParent(PARENT_ID); + comment.setComment(commentText != null ? commentText : COMMENT_TEXT); + + return comment; + } +} + + diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/log4j.properties b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/log4j.properties new file mode 100644 index 0000000000..d2bd747baf --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/log4j.properties @@ -0,0 +1,30 @@ +# Copyright (c) 2018, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +# +# WSO2 Inc. licenses this file to you under the Apache License, +# Version 2.0 (the "License"); you may not use this file except +# in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +# +# +# This is the log4j configuration file used by WSO2 Carbon +# +# IMPORTANT : Please do not remove or change the names of any +# of the Appender defined here. The layout pattern & log file +# can be changed using the WSO2 Carbon Management Console, and those +# settings will override the settings in this file. +# +log4j.rootLogger=DEBUG, STD_OUT +# Redirect log messages to console +log4j.appender.STD_OUT=org.apache.log4j.ConsoleAppender +log4j.appender.STD_OUT.Target=System.out +log4j.appender.STD_OUT.layout=org.apache.log4j.PatternLayout +log4j.appender.STD_OUT.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml new file mode 100644 index 0000000000..ab33c9c410 --- /dev/null +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.store.api/src/test/resources/testng.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + +