/src/test/java/com/google/ie/business/service/impl/IdeaCommentServiceImplTest.java

http://thoughtsite.googlecode.com/ · Java · 168 lines · 134 code · 20 blank · 14 comment · 14 complexity · 2134f72747fdcdde6d9b1822d0cc3a1b MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. /**
  3. *
  4. */
  5. package com.google.ie.business.service.impl;
  6. import static org.junit.Assert.assertEquals;
  7. import static org.junit.Assert.assertNotNull;
  8. import static org.mockito.Mockito.mock;
  9. import static org.mockito.Mockito.when;
  10. import com.google.ie.business.dao.AdminRequestDao;
  11. import com.google.ie.business.dao.CommentDao;
  12. import com.google.ie.business.dao.impl.AdminRequestDaoImpl;
  13. import com.google.ie.business.dao.impl.CommentDaoImpl;
  14. import com.google.ie.business.dao.impl.DaoConstants;
  15. import com.google.ie.business.domain.AdminRequest;
  16. import com.google.ie.business.domain.Comment;
  17. import com.google.ie.business.domain.IdeaComment;
  18. import com.google.ie.business.domain.User;
  19. import com.google.ie.business.service.ServiceConstants;
  20. import com.google.ie.common.audit.AuditManager;
  21. import com.google.ie.dto.RetrievalInfo;
  22. import com.google.ie.test.ServiceTest;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import java.util.ArrayList;
  26. import java.util.Date;
  27. import java.util.List;
  28. /**
  29. * Test case for IdeaCommentServiceImpl class
  30. *
  31. * @author Charanjeet singh
  32. */
  33. public class IdeaCommentServiceImplTest extends ServiceTest {
  34. private IdeaCommentServiceImpl ideaCommentService;
  35. private CommentDao commentDao = mock(CommentDaoImpl.class);
  36. private AuditManager auditmanager = mock(AuditManager.class);
  37. private AdminRequestDao mockAdminRequestDao = mock(AdminRequestDaoImpl.class);
  38. @Before
  39. public void setUp() {
  40. super.setUp();
  41. if (ideaCommentService == null) {
  42. ideaCommentService = new IdeaCommentServiceImpl();
  43. }
  44. ideaCommentService.setCommentDao(commentDao);
  45. ideaCommentService.setAuditManager(auditmanager);
  46. ideaCommentService.setAdminRequestDao(mockAdminRequestDao);
  47. }
  48. @Test
  49. public void addComment() {
  50. User user = new User();
  51. user.setUserKey("key");
  52. IdeaComment comment = new IdeaComment();
  53. comment.setText("TestComment");
  54. comment.setIdeaKey("Ideakey");
  55. Comment comment1 = new IdeaComment();
  56. comment1.setText("TestComment");
  57. comment1.setKey("testKey");
  58. when(commentDao.saveComment(comment)).thenReturn(comment);
  59. assertNotNull(ideaCommentService.addComment(comment, user));
  60. assertNotNull(Comment.STATUS_SAVED, ideaCommentService.addComment(comment, user)
  61. .getStatus());
  62. }
  63. @Test
  64. public void getComments() {
  65. String ideaKey = "testKey";
  66. // Idea idea = new Idea();
  67. // idea.setKey("testKey");
  68. List<Comment> comments = new ArrayList<Comment>();
  69. IdeaComment ideaComment = new IdeaComment();
  70. ideaComment.setKey("testCommentKey");
  71. comments.add(ideaComment);
  72. RetrievalInfo retrievalInfo = prepareRetrievalInfo(null);
  73. when(commentDao.getComments(ideaKey, retrievalInfo, "ideaKey")).thenReturn(comments);
  74. assertNotNull(ideaCommentService.getComments(ideaKey, retrievalInfo));
  75. assertEquals("testCommentKey", ideaCommentService.getComments(ideaKey, retrievalInfo)
  76. .get(0).getKey());
  77. }
  78. @Test
  79. public void flagComment() {
  80. IdeaComment ideaComment = new IdeaComment();
  81. ideaComment.setKey("testCommentKey");
  82. ideaComment.setText("He this is an awesome idea.Keep it up and do " +
  83. "let us know about the latest advancements in the idea");
  84. ideaComment.setStatus("Saved");
  85. IdeaComment saveIdeaComment = new IdeaComment();
  86. saveIdeaComment.setKey("testCommentKey");
  87. saveIdeaComment.setText("He this is an awesome idea.Keep it up and do " +
  88. "let us know about the latest advancements in the idea");
  89. saveIdeaComment.setStatus("Flagged");
  90. User user = new User();
  91. user.setUserKey("key");
  92. user.setEmailId("anujsiroh@gmail.com");
  93. AdminRequest adminRequest = new AdminRequest();
  94. adminRequest.setEntityKey("ideaCommentKey");
  95. adminRequest.setEntityType(IdeaComment.class.getSimpleName());
  96. adminRequest.setRequesterkey("UserKey");
  97. adminRequest.setRequestType(AdminRequest.REQUEST_OBJECTIONABLE);
  98. adminRequest.setCreatedOn(new Date());
  99. adminRequest.setStatus(AdminRequest.STATUS_PENDING);
  100. adminRequest.setEntityTitle(ideaCommentService.getTrimmedComment(ideaComment.getText()));
  101. when(ideaCommentService.getCommentById(ideaComment.getKey())).thenReturn(ideaComment);
  102. when(ideaCommentService.getCommentDao().saveComment(ideaComment)).thenReturn(
  103. saveIdeaComment);
  104. when(mockAdminRequestDao.saveRequest(adminRequest)).thenReturn(true);
  105. ideaCommentService.flagComment(ideaComment.getKey(), user);
  106. assertEquals("Flagged", saveIdeaComment.getStatus());
  107. }
  108. @Test
  109. public void getTrimmedComment() {
  110. /* checking if comment text is greater than 40 */
  111. String commentText = "Hey this is an awesome idea.Keep it up and do " +
  112. "let us know about the latest advancements in the idea";
  113. String trimmedComment = ideaCommentService
  114. .getTrimmedComment(commentText);
  115. assertEquals(42, trimmedComment.length());
  116. /* checking if comment text is smaller than 40 */
  117. commentText = "Hey this is an awesome idea";
  118. trimmedComment = ideaCommentService.getTrimmedComment(commentText);
  119. assertEquals(commentText.length(), trimmedComment.length());
  120. }
  121. private RetrievalInfo prepareRetrievalInfo(RetrievalInfo retrievalInfo) {
  122. if (retrievalInfo == null) {
  123. retrievalInfo = new RetrievalInfo();
  124. retrievalInfo.setStartIndex(ServiceConstants.ZERO);
  125. retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
  126. retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
  127. retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
  128. } else {
  129. // Handle garbage values if any.
  130. String orderOn = retrievalInfo.getOrderBy();
  131. String orderByParam = retrievalInfo.getOrderType();
  132. if (retrievalInfo.getStartIndex() < ServiceConstants.ZERO)
  133. retrievalInfo.setStartIndex(ServiceConstants.ZERO);
  134. if (retrievalInfo.getNoOfRecords() <= ServiceConstants.ZERO)
  135. retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
  136. if (orderByParam == null || !((orderByParam.equals(DaoConstants.ORDERING_ASCENDING)
  137. || orderByParam.equals(DaoConstants.ORDERING_DESCENDING))))
  138. retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
  139. if (orderOn == null
  140. || !(orderOn
  141. .equals(ServiceConstants.IDEA_COMMENT_ORDERING_FIELD_CREATED_ON)
  142. )) {
  143. retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
  144. }
  145. }
  146. return retrievalInfo;
  147. }
  148. }