PageRenderTime 558ms CodeModel.GetById 172ms app.highlight 15ms RepoModel.GetById 254ms app.codeStats 0ms

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