Add new methods

Exception handling test methods for rating methods
merge-requests/7/head
nishadi 7 years ago
parent e70899a193
commit 0ac0efcf2e

@ -62,19 +62,17 @@ public class CommentManagementAPIImpl implements CommentManagementAPI {
List<Comment> 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 {
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();
}

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

Loading…
Cancel
Save