Merge pull request #1199 from NishadiPrasangini/AppManager

App manager
feature/appm-store/pbac
Dharmakeerhi Lasantha 7 years ago committed by GitHub
commit bc162d05fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -35,12 +35,11 @@ public interface CommentsManager {
* To add a comment to a application * To add a comment to a application
* *
* @param comment comment of the application. * @param comment comment of the application.
* @param uuid uuid of the application release * @param uuid uuid of the application release\
* @param tenantId tenant id of the application
* @return {@link Comment} Comment added * @return {@link Comment} Comment added
* @throws CommentManagementException Exceptions of the comment management. * @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 * To validate the pre-request of the comment

@ -56,11 +56,12 @@ public class CommentsManagerImpl implements CommentsManager {
} }
@Override @Override
public Comment addComment(Comment comment, String uuid, int tenantId) throws CommentManagementException { public Comment addComment(Comment comment, String uuid) throws CommentManagementException {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Request for comment is received for uuid" + uuid); log.debug("Request for comment is received for uuid" + uuid);
} }
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
comment.setCreatedAt(Timestamp.from(Instant.now())); comment.setCreatedAt(Timestamp.from(Instant.now()));
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
@ -140,7 +141,7 @@ public class CommentsManagerImpl implements CommentsManager {
public Comment getComment(int commentId) throws CommentManagementException { public Comment getComment(int commentId) throws CommentManagementException {
PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true); PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
Comment comment = null; Comment comment;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Comment retrieval request is received for the comment id " + commentId); log.debug("Comment retrieval request is received for the comment id " + commentId);
} }
@ -214,7 +215,7 @@ public class CommentsManagerImpl implements CommentsManager {
@Override @Override
public int getRatedUser(String uuid) throws ApplicationManagementException { public int getRatedUser(String uuid) throws ApplicationManagementException {
int ratedUsers = 0; int ratedUsers;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get the rated users for the application release number" + uuid); log.debug("Get the rated users for the application release number" + uuid);
} }
@ -233,7 +234,7 @@ public class CommentsManagerImpl implements CommentsManager {
@Override @Override
public int getStars(String uuid) throws ApplicationManagementException { public int getStars(String uuid) throws ApplicationManagementException {
int stars = 0; int stars;
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Get the average of rated stars for the application " + uuid); log.debug("Get the average of rated stars for the application " + uuid);
} }
@ -255,7 +256,7 @@ public class CommentsManagerImpl implements CommentsManager {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Stars are received for the application " + uuid); log.debug("Stars are received for the application " + uuid);
} }
int newStars = 0; int newStars;
try { try {
ConnectionManagerUtil.beginDBTransaction(); ConnectionManagerUtil.beginDBTransaction();
int ratedUsers = commentDAO.getRatedUser(uuid); int ratedUsers = commentDAO.getRatedUser(uuid);

@ -157,6 +157,16 @@
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
<artifactId>org.wso2.carbon.device.application.mgt.core</artifactId> <artifactId>org.wso2.carbon.device.application.mgt.core</artifactId>
<scope>provided</scope> <scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
</exclusion>
<exclusion>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.wso2.carbon.devicemgt</groupId> <groupId>org.wso2.carbon.devicemgt</groupId>
@ -229,5 +239,15 @@
<groupId>javax.ws.rs</groupId> <groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId> <artifactId>javax.ws.rs-api</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-testng</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>

@ -16,7 +16,7 @@
* under the License. * 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.ApiParam;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@ -30,7 +30,7 @@ import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses; import io.swagger.annotations.ApiResponses;
import org.wso2.carbon.apimgt.annotations.api.Scope; import org.wso2.carbon.apimgt.annotations.api.Scope;
import org.wso2.carbon.apimgt.annotations.api.Scopes; 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 org.wso2.carbon.device.application.mgt.common.Comment;
import javax.validation.Valid; import javax.validation.Valid;
import javax.ws.rs.Path; import javax.ws.rs.Path;

@ -16,14 +16,13 @@
* under the License. * 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 io.swagger.annotations.ApiParam;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.application.mgt.store.api.APIUtil;
import org.wso2.carbon.device.application.mgt.api.APIUtil; import org.wso2.carbon.device.application.mgt.store.api.services.CommentManagementAPI;
import org.wso2.carbon.device.application.mgt.api.services.CommentManagementAPI;
import org.wso2.carbon.device.application.mgt.common.Comment; 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.PaginationRequest;
import org.wso2.carbon.device.application.mgt.common.exception.ApplicationManagementException; 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.GET;
import javax.ws.rs.POST; import javax.ws.rs.POST;
import javax.ws.rs.DELETE; import javax.ws.rs.DELETE;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -63,19 +61,17 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
List<Comment> comments = new ArrayList<>(); List<Comment> comments = new ArrayList<>();
try { try {
PaginationRequest request = new PaginationRequest(offSet, limit); 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); commentsManager.getAllComments(request, uuid);
return Response.status(Response.Status.OK).entity(comments).build(); 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) { } catch (CommentManagementException e) {
String msg = "Error occurred while retrieving comments."; String msg = "Error occurred while retrieving comments.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg)
.entity(" Internal server error occurs").build(); .build();
} }
return Response.status(Response.Status.OK).entity(comments).build(); return Response.status(Response.Status.OK).entity(comments).build();
} }
@ -89,9 +85,8 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
@PathParam("uuid") String uuid) { @PathParam("uuid") String uuid) {
CommentsManager commentsManager = APIUtil.getCommentsManager(); CommentsManager commentsManager = APIUtil.getCommentsManager();
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(true);
try { try {
Comment newComment = commentsManager.addComment(comment, uuid, tenantId); Comment newComment = commentsManager.addComment(comment, uuid);
if (comment != null) { if (comment != null) {
return Response.status(Response.Status.CREATED).entity(newComment).build(); return Response.status(Response.Status.CREATED).entity(newComment).build();
} else { } else {
@ -103,7 +98,7 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
String msg = "Error occurred while creating the comment"; String msg = "Error occurred while creating the comment";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) 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."; String msg = "Error occurred while retrieving comments.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) 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(); CommentsManager commentsManager = APIUtil.getCommentsManager();
try { 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) { } catch (CommentManagementException e) {
String msg = "Error occurred while deleting the comment."; String msg = "Error occurred while deleting the comment.";
log.error(msg, e); log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR) return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg)
.entity("Internal server error occurs").build(); .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.OK).entity("Comment is deleted successfully.").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) { @PathParam("uuid") String uuid) {
CommentsManager commentsManager = APIUtil.getCommentsManager(); CommentsManager commentsManager = APIUtil.getCommentsManager();
int Stars = 0; int Stars;
try { try {
Stars = commentsManager.getStars(uuid); Stars = commentsManager.getStars(uuid);
} catch (CommentManagementException e) { } catch (CommentManagementException e) {
log.error("Comment Management Exception occurs", e); log.error("Comment Management Exception occurs", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build(); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
} catch (ApplicationManagementException e) { } 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) 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(); return Response.status(Response.Status.OK).entity(Stars).build();
} }
@ -185,15 +182,17 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
@PathParam("uuid") String uuid) { @PathParam("uuid") String uuid) {
CommentsManager commentsManager = APIUtil.getCommentsManager(); CommentsManager commentsManager = APIUtil.getCommentsManager();
int ratedUsers = 0; int ratedUsers;
try { try {
ratedUsers = commentsManager.getRatedUser(uuid); ratedUsers = commentsManager.getRatedUser(uuid);
} catch (CommentManagementException e) { } 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) return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Internal server error occurs").build(); .entity(msg).build();
} catch (ApplicationManagementException e) { } 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) return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity("Application with UUID" + uuid + " Internal server error occurs").build(); .entity("Application with UUID" + uuid + " Internal server error occurs").build();
} }
@ -208,10 +207,9 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
@PathParam("uuid") String uuid) { @PathParam("uuid") String uuid) {
CommentsManager commentsManager = APIUtil.getCommentsManager(); CommentsManager commentsManager = APIUtil.getCommentsManager();
int newStars = 0; int newStars;
try { try {
newStars = commentsManager.updateStars(stars, uuid); newStars = commentsManager.updateStars(stars, uuid);
if (stars != 0) { if (stars != 0) {
return Response.status(Response.Status.CREATED).entity(newStars).build(); return Response.status(Response.Status.CREATED).entity(newStars).build();
} else { } else {
@ -220,9 +218,10 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
return Response.status(Response.Status.BAD_REQUEST).build(); return Response.status(Response.Status.BAD_REQUEST).build();
} }
} catch (ApplicationManagementException e) { } 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) return Response.status(Response.Status.INTERNAL_SERVER_ERROR)
.entity(" Internal server error occurs").build(); .entity(msg).build();
} }
} }
} }

@ -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);
}
}

@ -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;
}
}

@ -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

@ -0,0 +1,29 @@
<!--
~ 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.
-->
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="CommentManagementAPI">
<parameter name="useDefaultListeners" value="false"/>
<test name="API Unit Tests" preserve-order="true">
<classes>
<class name="org.wso2.carbon.device.application.mgt.store.api.services.CommentManagementAPITest"/>
</classes>
</test>
</suite>
Loading…
Cancel
Save