Add new methods

Exception handling test methods for rating methods
feature/appm-store/pbac
nishadi 7 years ago
parent e70899a193
commit 0ac0efcf2e

@ -62,19 +62,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(" Internal server error occurs")
.entity(" Internal server error occurs").build(); .build();
} }
return null; return null;
} }
@ -142,15 +140,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("Internal server error occurs")
.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();
} }

@ -80,12 +80,23 @@ public class CommentManagementAPITest {
@Test @Test
public void testGetAllCommentsInternalError() throws Exception { public void testGetAllCommentsInternalError() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); 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); 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(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"The response status should be 500."); "The response status should be 500.");
Mockito.reset(commentsManager); 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.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 @Test
@ -174,14 +185,22 @@ public class CommentManagementAPITest {
@Test @Test
public void testDeleteCommentInternalError() throws Exception { public void testDeleteCommentInternalError() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
Mockito.when(this.commentManagementAPI.deleteComment(Mockito.anyInt())) Mockito.when(this.commentManagementAPI.deleteComment(1)).thenThrow(new CommentManagementException());
.thenThrow(new CommentManagementException());
Response response = this.commentManagementAPI.deleteComment(1); Response response = this.commentManagementAPI.deleteComment(1);
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
"The response status should be 500."); "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 @Test
public void testGetStars() throws Exception { public void testGetStars() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager);
@ -195,7 +214,8 @@ public class CommentManagementAPITest {
@Test @Test
public void testGetStarsCommentError() throws Exception { public void testGetStarsCommentError() throws Exception {
PowerMockito.stub(PowerMockito.method(APIUtil.class, "getCommentsManager")).toReturn(this.commentsManager); 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"); Response response = this.commentManagementAPI.getStars("a");
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
@ -255,7 +275,7 @@ public class CommentManagementAPITest {
Response response = this.commentManagementAPI.updateStars(3, "a"); Response response = this.commentManagementAPI.updateStars(3, "a");
Assert.assertNotNull(response, "The response object is null."); Assert.assertNotNull(response, "The response object is null.");
Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(), Assert.assertEquals(response.getStatus(), Response.Status.CREATED.getStatusCode(),
"The response status should be 200."); "The response status should be 201.");
Mockito.reset(commentsManager); Mockito.reset(commentsManager);
} }

Loading…
Cancel
Save