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

http://thoughtsite.googlecode.com/ · Java · 140 lines · 101 code · 22 blank · 17 comment · 0 complexity · 6bf11ea8c65d1fcf4eb633b7ecdaf238 MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. package com.google.ie.business.service.impl;
  3. import static org.mockito.Mockito.mock;
  4. import static org.mockito.Mockito.when;
  5. import com.google.ie.business.dao.VoteDao;
  6. import com.google.ie.business.dao.impl.VoteDaoImpl;
  7. import com.google.ie.business.domain.Idea;
  8. import com.google.ie.business.domain.IdeaVote;
  9. import com.google.ie.business.domain.User;
  10. import com.google.ie.common.audit.AuditManager;
  11. import com.google.ie.common.exception.IdeasExchangeException;
  12. import com.google.ie.test.ServiceTest;
  13. import org.junit.Before;
  14. import org.junit.Test;
  15. import junit.framework.Assert;
  16. /**
  17. * Test case for IdeaVoteServiceImpl class
  18. *
  19. * @author gmaurya
  20. *
  21. */
  22. public class IdeaVoteServiceImplTest extends ServiceTest {
  23. private IdeaServiceImpl ideaService;
  24. private IdeaVoteServiceImpl ideaVoteService;
  25. private VoteDao voteDao = mock(VoteDaoImpl.class);
  26. private ShardedCounterServiceImpl shardedCounterService;
  27. private AuditManager mockAuditManager = mock(AuditManager.class);
  28. @Before
  29. public void setUp() {
  30. super.setUp();
  31. ideaService = mock(IdeaServiceImpl.class);
  32. shardedCounterService = mock(ShardedCounterServiceImpl.class);
  33. ideaService.setAuditManager(mockAuditManager);
  34. ideaVoteService = new IdeaVoteServiceImpl();
  35. ideaVoteService.setAuditManager(mockAuditManager);
  36. ideaVoteService.setIdeaService(ideaService);
  37. ideaVoteService.setShardedCounterService(shardedCounterService);
  38. ideaVoteService.setVoteDao(voteDao);
  39. }
  40. @Test
  41. public void addVote() {
  42. User user = new User();
  43. user.setDisplayName("Test User");
  44. user.setRoleName(User.ROLE_USER);
  45. user.setUserKey("userKey");
  46. // when(userService.addOrUpdateUser(user)).thenReturn(user);
  47. Idea idea = new Idea();
  48. idea.setTitle("ideaTitle");
  49. idea.setKey("ideaKey");
  50. idea.setDescription("Idea Description");
  51. idea.setCreatorKey("anyUserKey");
  52. when(ideaService.getIdeaByKey(idea.getKey())).thenReturn(idea);
  53. IdeaVote vote = new IdeaVote();
  54. vote.setKey("voteKey");
  55. vote.setCreatorKey(user.getUserKey());
  56. vote.setIdeaKey(idea.getKey());
  57. vote.setVotePoints(15);
  58. vote.setPositiveVote(true);
  59. when(voteDao.saveVote(vote)).thenReturn(vote);
  60. try {
  61. vote = (IdeaVote) ideaVoteService.addVote(vote, user);
  62. } catch (IdeasExchangeException e) {
  63. Assert.fail();
  64. }
  65. Assert.assertNotNull(vote.getKey());
  66. vote.setPositiveVote(false);
  67. try {
  68. vote = (IdeaVote) ideaVoteService.addVote(vote, user);
  69. } catch (IdeasExchangeException e) {
  70. Assert.fail();
  71. }
  72. Assert.assertNotNull(vote.getKey());
  73. }
  74. @Test
  75. public void isUserAllowedToVote() {
  76. String userKey = "userKey";
  77. Idea idea = new Idea();
  78. idea.setTitle("ideaTitle");
  79. idea.setKey("ideaKey");
  80. idea.setDescription("Idea Description");
  81. idea.setCreatorKey("anyUserKey");
  82. Idea ownIdea = new Idea();
  83. ownIdea.setTitle("ideaTitle");
  84. ownIdea.setKey("ideaKey");
  85. ownIdea.setDescription("Idea Description");
  86. ownIdea.setCreatorKey(userKey);
  87. /** Checking if voting is successful when user is allowed to vote */
  88. when(voteDao.isIdeaAlreadyVotedByUser(userKey, idea.getKey())).thenReturn(false);
  89. boolean actual = false;
  90. try {
  91. actual = ideaVoteService.isUserAllowedToVote(userKey, idea);
  92. } catch (IdeasExchangeException e) {
  93. actual = false;
  94. }
  95. Assert.assertEquals(true, actual);
  96. /**
  97. * Checking if voting is unsuccessful when user is voting on his own
  98. * Idea
  99. */
  100. try {
  101. actual = ideaVoteService.isUserAllowedToVote(userKey, ownIdea);
  102. } catch (IdeasExchangeException e) {
  103. actual = false;
  104. }
  105. Assert.assertEquals(false, actual);
  106. when(voteDao.isIdeaAlreadyVotedByUser(userKey, idea.getKey())).thenReturn(true);
  107. /**
  108. * Checking if voting is unsuccessful when user has already voted on
  109. * this Idea
  110. */
  111. try {
  112. actual = ideaVoteService.isUserAllowedToVote(userKey, idea);
  113. } catch (IdeasExchangeException e) {
  114. actual = false;
  115. }
  116. Assert.assertEquals(false, actual);
  117. }
  118. }