PageRenderTime 100ms CodeModel.GetById 22ms app.highlight 12ms RepoModel.GetById 26ms app.codeStats 0ms

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