/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
- // Copyright 2009 Google Inc. All Rights Reserved.
-
- package com.google.ie.business.service.impl;
-
- import static org.mockito.Mockito.mock;
- import static org.mockito.Mockito.when;
-
- import com.google.ie.business.dao.VoteDao;
- import com.google.ie.business.dao.impl.VoteDaoImpl;
- import com.google.ie.business.domain.Idea;
- import com.google.ie.business.domain.IdeaVote;
- import com.google.ie.business.domain.User;
- import com.google.ie.common.audit.AuditManager;
- import com.google.ie.common.exception.IdeasExchangeException;
- import com.google.ie.test.ServiceTest;
-
- import org.junit.Before;
- import org.junit.Test;
-
- import junit.framework.Assert;
-
- /**
- * Test case for IdeaVoteServiceImpl class
- *
- * @author gmaurya
- *
- */
- public class IdeaVoteServiceImplTest extends ServiceTest {
- private IdeaServiceImpl ideaService;
- private IdeaVoteServiceImpl ideaVoteService;
- private VoteDao voteDao = mock(VoteDaoImpl.class);
- private ShardedCounterServiceImpl shardedCounterService;
- private AuditManager mockAuditManager = mock(AuditManager.class);
-
- @Before
- public void setUp() {
- super.setUp();
- ideaService = mock(IdeaServiceImpl.class);
-
- shardedCounterService = mock(ShardedCounterServiceImpl.class);
- ideaService.setAuditManager(mockAuditManager);
-
- ideaVoteService = new IdeaVoteServiceImpl();
- ideaVoteService.setAuditManager(mockAuditManager);
- ideaVoteService.setIdeaService(ideaService);
- ideaVoteService.setShardedCounterService(shardedCounterService);
- ideaVoteService.setVoteDao(voteDao);
- }
-
- @Test
- public void addVote() {
- User user = new User();
- user.setDisplayName("Test User");
- user.setRoleName(User.ROLE_USER);
- user.setUserKey("userKey");
- // when(userService.addOrUpdateUser(user)).thenReturn(user);
-
- Idea idea = new Idea();
- idea.setTitle("ideaTitle");
- idea.setKey("ideaKey");
- idea.setDescription("Idea Description");
- idea.setCreatorKey("anyUserKey");
- when(ideaService.getIdeaByKey(idea.getKey())).thenReturn(idea);
-
- IdeaVote vote = new IdeaVote();
- vote.setKey("voteKey");
- vote.setCreatorKey(user.getUserKey());
- vote.setIdeaKey(idea.getKey());
- vote.setVotePoints(15);
- vote.setPositiveVote(true);
- when(voteDao.saveVote(vote)).thenReturn(vote);
- try {
- vote = (IdeaVote) ideaVoteService.addVote(vote, user);
- } catch (IdeasExchangeException e) {
- Assert.fail();
-
- }
- Assert.assertNotNull(vote.getKey());
-
- vote.setPositiveVote(false);
- try {
- vote = (IdeaVote) ideaVoteService.addVote(vote, user);
- } catch (IdeasExchangeException e) {
- Assert.fail();
- }
- Assert.assertNotNull(vote.getKey());
-
- }
-
- @Test
- public void isUserAllowedToVote() {
- String userKey = "userKey";
-
- Idea idea = new Idea();
- idea.setTitle("ideaTitle");
- idea.setKey("ideaKey");
- idea.setDescription("Idea Description");
- idea.setCreatorKey("anyUserKey");
-
- Idea ownIdea = new Idea();
- ownIdea.setTitle("ideaTitle");
- ownIdea.setKey("ideaKey");
- ownIdea.setDescription("Idea Description");
- ownIdea.setCreatorKey(userKey);
-
- /** Checking if voting is successful when user is allowed to vote */
- when(voteDao.isIdeaAlreadyVotedByUser(userKey, idea.getKey())).thenReturn(false);
- boolean actual = false;
- try {
- actual = ideaVoteService.isUserAllowedToVote(userKey, idea);
- } catch (IdeasExchangeException e) {
- actual = false;
- }
- Assert.assertEquals(true, actual);
-
- /**
- * Checking if voting is unsuccessful when user is voting on his own
- * Idea
- */
- try {
- actual = ideaVoteService.isUserAllowedToVote(userKey, ownIdea);
- } catch (IdeasExchangeException e) {
- actual = false;
- }
- Assert.assertEquals(false, actual);
-
- when(voteDao.isIdeaAlreadyVotedByUser(userKey, idea.getKey())).thenReturn(true);
- /**
- * Checking if voting is unsuccessful when user has already voted on
- * this Idea
- */
- try {
- actual = ideaVoteService.isUserAllowedToVote(userKey, idea);
- } catch (IdeasExchangeException e) {
- actual = false;
- }
- Assert.assertEquals(false, actual);
-
- }
- }