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

http://thoughtsite.googlecode.com/ · Java · 135 lines · 103 code · 24 blank · 8 comment · 0 complexity · 99e2ec4dcdce25557f91240424d7c514 MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. package com.google.ie.business.service.impl;
  3. import static org.junit.Assert.assertEquals;
  4. import static org.mockito.Mockito.mock;
  5. import static org.mockito.Mockito.when;
  6. import com.google.ie.business.dao.ShardedCounterDao;
  7. import com.google.ie.business.dao.VoteDao;
  8. import com.google.ie.business.dao.impl.ShardedCounterDaoImpl;
  9. import com.google.ie.business.dao.impl.VoteDaoImpl;
  10. import com.google.ie.business.domain.Idea;
  11. import com.google.ie.business.domain.ShardedCounter;
  12. import com.google.ie.business.domain.User;
  13. import com.google.ie.common.audit.AuditManager;
  14. import com.google.ie.test.ServiceTest;
  15. import org.junit.Before;
  16. import org.junit.Test;
  17. import java.util.ArrayList;
  18. import java.util.List;
  19. /**
  20. * Test case for ShardedCounterServiceImpl class
  21. *
  22. * @author gmaurya
  23. *
  24. */
  25. public class ShardedCounterServiceImplTest extends ServiceTest {
  26. private IdeaServiceImpl ideaService;
  27. private IdeaVoteServiceImpl ideaVoteService;
  28. private VoteDao voteDao = mock(VoteDaoImpl.class);
  29. private ShardedCounterServiceImpl shardedCounterService;
  30. private ShardedCounterDao shardedCounterDao = mock(ShardedCounterDaoImpl.class);;
  31. private AuditManager mockAuditManager = mock(AuditManager.class);
  32. private ShardedCounter counter1;
  33. private ShardedCounter counter2;
  34. private Idea idea;
  35. private User user;
  36. private List<ShardedCounter> shardListTotal;
  37. private List<ShardedCounter> shardListByShardNum;
  38. @Before
  39. public void setUp() {
  40. super.setUp();
  41. ideaService = mock(IdeaServiceImpl.class);
  42. shardedCounterService = new ShardedCounterServiceImpl();
  43. ideaService.setAuditManager(mockAuditManager);
  44. shardedCounterService.setShardedCounterDao(shardedCounterDao);
  45. shardedCounterService.setIdeaService(ideaService);
  46. ideaVoteService = new IdeaVoteServiceImpl();
  47. ideaVoteService.setAuditManager(mockAuditManager);
  48. ideaVoteService.setIdeaService(ideaService);
  49. ideaVoteService.setShardedCounterService(shardedCounterService);
  50. ideaVoteService.setVoteDao(voteDao);
  51. user = new User();
  52. user.setUserKey("userKey");
  53. // when(userService.addOrUpdateUser(user)).thenReturn(user);
  54. idea = new Idea();
  55. idea.setTitle("ideaTitle");
  56. idea.setKey("ideaKey");
  57. idea.setDescription("Idea Description");
  58. idea.setCreatorKey(user.getUserKey());
  59. when(ideaService.getIdeaByKey(idea.getKey())).thenReturn(idea);
  60. counter1 = new ShardedCounter(idea.getKey());
  61. counter1.setPositivePoint(0);
  62. counter1.setTotalPoint(15);
  63. counter1.setShardNumber(3);
  64. counter2 = new ShardedCounter(idea.getKey());
  65. counter2.setPositivePoint(0);
  66. counter2.setTotalPoint(15);
  67. counter2.setShardNumber(2);
  68. shardListTotal = new ArrayList<ShardedCounter>();
  69. shardListTotal.add(counter1);
  70. shardListTotal.add(counter2);
  71. shardListByShardNum = new ArrayList<ShardedCounter>();
  72. shardListByShardNum.add(counter2);
  73. when(shardedCounterDao.getShardsByParentKey(idea.getKey())).thenReturn(shardListTotal);
  74. when(shardedCounterDao.getShardsByParentKeyAndShardNum(idea.getKey(), 2)).thenReturn(
  75. shardListByShardNum);
  76. when(shardedCounterDao.createOrUpdateShardedCounter(counter1)).thenReturn(
  77. counter1);
  78. }
  79. @Test
  80. public void getMergedCounter() {
  81. ShardedCounter mergedCounter = shardedCounterService.getMergedShardedCounter(idea.getKey());
  82. assertEquals(counter1.getPositivePoint() + counter2.getPositivePoint(), mergedCounter
  83. .getPositivePoint());
  84. assertEquals(counter1.getNegativePoint() + counter2.getNegativePoint(), mergedCounter
  85. .getNegativePoint());
  86. assertEquals(counter1.getTotalPoint() + counter2.getTotalPoint(), mergedCounter
  87. .getTotalPoint());
  88. }
  89. @Test
  90. public void getNegativePoint() {
  91. int negative = shardedCounterService.getNegativePoint(idea.getKey());
  92. assertEquals(counter1.getNegativePoint() + counter2.getNegativePoint(), negative);
  93. }
  94. @Test
  95. public void getPositivePoint() {
  96. int positivePoint = shardedCounterService.getNegativePoint(idea.getKey());
  97. assertEquals(counter1.getPositivePoint() + counter2.getPositivePoint(), positivePoint);
  98. }
  99. @Test
  100. public void incrementPositivePoints() {
  101. shardedCounterService.incrementPositivePoints(idea.getKey());
  102. }
  103. @Test
  104. public void incrementNegativePoints() {
  105. shardedCounterService.incrementNegativePoints(idea.getKey());
  106. }
  107. @Test
  108. public void updateTotalPoints() {
  109. shardedCounterService.updateTotalPoints(idea.getKey(), 89);
  110. }
  111. }