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

http://thoughtsite.googlecode.com/ · Java · 141 lines · 91 code · 26 blank · 24 comment · 2 complexity · 848f746e6eb29c55919799cd30a37c43 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.junit.Assert.fail;
  9. import static org.mockito.Mockito.mock;
  10. import static org.mockito.Mockito.when;
  11. import com.google.ie.business.dao.impl.VoteDaoImpl;
  12. import com.google.ie.business.domain.CommentVote;
  13. import com.google.ie.business.domain.IdeaComment;
  14. import com.google.ie.business.domain.User;
  15. import com.google.ie.common.audit.AuditManager;
  16. import com.google.ie.common.exception.IdeasExchangeException;
  17. import com.google.ie.test.ServiceTest;
  18. import org.junit.Before;
  19. import org.junit.Test;
  20. import junit.framework.Assert;
  21. import java.util.Date;
  22. /**
  23. * Test case of {@link CommentVoteServiceImpl}
  24. *
  25. * @author asirohi
  26. *
  27. */
  28. public class CommentVoteServiceImplTest extends ServiceTest {
  29. private CommentVoteServiceImpl commentVoteService;
  30. @Before
  31. public void setUp() {
  32. if (commentVoteService == null) {
  33. commentVoteService = new CommentVoteServiceImpl();
  34. commentVoteService.setAuditManager(mock(AuditManager.class));
  35. commentVoteService.setCommentService(mock(IdeaCommentServiceImpl.class));
  36. commentVoteService.setShardedCounterService(mock(ShardedCounterServiceImpl.class));
  37. commentVoteService.setVoteDao(mock(VoteDaoImpl.class));
  38. }
  39. }
  40. /**
  41. * Test method for
  42. * {@link com.google.ie.business.service.impl.CommentVoteServiceImpl#addVote(java.lang.String, com.google.ie.business.domain.Vote, com.google.ie.business.domain.User)}
  43. * .
  44. */
  45. @Test
  46. public void testAddVote() {
  47. User user = new User();
  48. user.setDisplayName("Test User");
  49. user.setRoleName(User.ROLE_USER);
  50. user.setUserKey("userKey");
  51. IdeaComment ideaComment = new IdeaComment();
  52. ideaComment.setKey("comment key");
  53. CommentVote commentVote = new CommentVote();
  54. commentVote.setPositiveVote(true);
  55. commentVote.setVotePoints(10);
  56. commentVote.setVotingDate(new Date());
  57. commentVote.setCreatorKey("creatorKey");
  58. commentVote.setCommentKey("comment key");
  59. when(commentVoteService.getCommentService().getCommentById(commentVote.getCommentKey()))
  60. .thenReturn(ideaComment);
  61. when(commentVoteService.getVoteDao().saveVote(commentVote)).thenReturn(commentVote);
  62. try {
  63. assertNotNull(commentVoteService.addVote(commentVote, user));
  64. } catch (IdeasExchangeException e) {
  65. fail(e.getMessage());
  66. }
  67. try {
  68. assertEquals(commentVote, commentVoteService.addVote(commentVote, user));
  69. } catch (IdeasExchangeException e) {
  70. fail(e.getMessage());
  71. }
  72. }
  73. @Test
  74. public void isUserAllowedToVote() {
  75. String userKey = "userKey";
  76. IdeaComment ideaComment = new IdeaComment();
  77. ideaComment.setKey("comment key");
  78. ideaComment.setCreatorKey("anyUserKey");
  79. IdeaComment ownComment = new IdeaComment();
  80. ownComment.setKey("comment key");
  81. ownComment.setCreatorKey(userKey);
  82. /* Checking if voting is successful when user is allowed to vote */
  83. when(commentVoteService.getVoteDao().isCommentAlreadyVotedByUser(userKey,
  84. ideaComment.getKey())).thenReturn(false);
  85. try {
  86. Assert.assertEquals(true, commentVoteService.isUserAllowedToVote(userKey, ideaComment));
  87. } catch (IdeasExchangeException e) {
  88. fail(e.getMessage());
  89. }
  90. /*
  91. * Checking if voting is unsuccessful when user is voting on his own
  92. * Idea
  93. */
  94. try {
  95. commentVoteService.isUserAllowedToVote(userKey, ownComment);
  96. fail("IdeasExchangeException expected : User should not be allowed on his own idea");
  97. } catch (IdeasExchangeException e) {
  98. assert (true);
  99. }
  100. /*
  101. * Checking if voting is unsuccessful when user has already voted on
  102. * this Idea
  103. */
  104. when(commentVoteService.getVoteDao().isCommentAlreadyVotedByUser(userKey,
  105. ideaComment.getKey())).thenReturn(true);
  106. try {
  107. Assert.assertEquals(false, commentVoteService.isUserAllowedToVote(userKey,
  108. ideaComment));
  109. fail("IdeasExchangeException expected : User should not be allowed to vote on " +
  110. "idea which he/she has already voted");
  111. } catch (IdeasExchangeException e) {
  112. assert (true);
  113. }
  114. }
  115. }