PageRenderTime 344ms CodeModel.GetById 161ms app.highlight 10ms RepoModel.GetById 171ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 141 lines | 91 code | 26 blank | 24 comment | 2 complexity | 848f746e6eb29c55919799cd30a37c43 MD5 | raw file
  1// Copyright 2009 Google Inc. All Rights Reserved.
  2/**
  3 * 
  4 */
  5package com.google.ie.business.service.impl;
  6
  7import static org.junit.Assert.assertEquals;
  8import static org.junit.Assert.assertNotNull;
  9import static org.junit.Assert.fail;
 10import static org.mockito.Mockito.mock;
 11import static org.mockito.Mockito.when;
 12
 13import com.google.ie.business.dao.impl.VoteDaoImpl;
 14import com.google.ie.business.domain.CommentVote;
 15import com.google.ie.business.domain.IdeaComment;
 16import com.google.ie.business.domain.User;
 17import com.google.ie.common.audit.AuditManager;
 18import com.google.ie.common.exception.IdeasExchangeException;
 19import com.google.ie.test.ServiceTest;
 20
 21import org.junit.Before;
 22import org.junit.Test;
 23
 24import junit.framework.Assert;
 25
 26import java.util.Date;
 27
 28/**
 29 * Test case of {@link CommentVoteServiceImpl}
 30 * 
 31 * @author asirohi
 32 * 
 33 */
 34public class CommentVoteServiceImplTest extends ServiceTest {
 35
 36    private CommentVoteServiceImpl commentVoteService;
 37
 38    @Before
 39    public void setUp() {
 40
 41        if (commentVoteService == null) {
 42            commentVoteService = new CommentVoteServiceImpl();
 43            commentVoteService.setAuditManager(mock(AuditManager.class));
 44            commentVoteService.setCommentService(mock(IdeaCommentServiceImpl.class));
 45            commentVoteService.setShardedCounterService(mock(ShardedCounterServiceImpl.class));
 46            commentVoteService.setVoteDao(mock(VoteDaoImpl.class));
 47        }
 48    }
 49
 50    /**
 51     * Test method for
 52     * {@link com.google.ie.business.service.impl.CommentVoteServiceImpl#addVote(java.lang.String, com.google.ie.business.domain.Vote, com.google.ie.business.domain.User)}
 53     * .
 54     */
 55    @Test
 56    public void testAddVote() {
 57
 58        User user = new User();
 59        user.setDisplayName("Test User");
 60        user.setRoleName(User.ROLE_USER);
 61        user.setUserKey("userKey");
 62
 63        IdeaComment ideaComment = new IdeaComment();
 64        ideaComment.setKey("comment key");
 65
 66        CommentVote commentVote = new CommentVote();
 67        commentVote.setPositiveVote(true);
 68        commentVote.setVotePoints(10);
 69        commentVote.setVotingDate(new Date());
 70        commentVote.setCreatorKey("creatorKey");
 71        commentVote.setCommentKey("comment key");
 72
 73        when(commentVoteService.getCommentService().getCommentById(commentVote.getCommentKey()))
 74                        .thenReturn(ideaComment);
 75        when(commentVoteService.getVoteDao().saveVote(commentVote)).thenReturn(commentVote);
 76
 77        try {
 78            assertNotNull(commentVoteService.addVote(commentVote, user));
 79        } catch (IdeasExchangeException e) {
 80            fail(e.getMessage());
 81        }
 82
 83        try {
 84            assertEquals(commentVote, commentVoteService.addVote(commentVote, user));
 85        } catch (IdeasExchangeException e) {
 86            fail(e.getMessage());
 87        }
 88    }
 89
 90    @Test
 91    public void isUserAllowedToVote() {
 92        String userKey = "userKey";
 93
 94        IdeaComment ideaComment = new IdeaComment();
 95        ideaComment.setKey("comment key");
 96        ideaComment.setCreatorKey("anyUserKey");
 97
 98        IdeaComment ownComment = new IdeaComment();
 99        ownComment.setKey("comment key");
100        ownComment.setCreatorKey(userKey);
101
102        /* Checking if voting is successful when user is allowed to vote */
103
104        when(commentVoteService.getVoteDao().isCommentAlreadyVotedByUser(userKey,
105                        ideaComment.getKey())).thenReturn(false);
106
107        try {
108            Assert.assertEquals(true, commentVoteService.isUserAllowedToVote(userKey, ideaComment));
109        } catch (IdeasExchangeException e) {
110            fail(e.getMessage());
111        }
112
113        /*
114         * Checking if voting is unsuccessful when user is voting on his own
115         * Idea
116         */
117        try {
118            commentVoteService.isUserAllowedToVote(userKey, ownComment);
119            fail("IdeasExchangeException expected : User should not be allowed on his own idea");
120        } catch (IdeasExchangeException e) {
121            assert (true);
122        }
123
124        /*
125         * Checking if voting is unsuccessful when user has already voted on
126         * this Idea
127         */
128        when(commentVoteService.getVoteDao().isCommentAlreadyVotedByUser(userKey,
129                        ideaComment.getKey())).thenReturn(true);
130
131        try {
132            Assert.assertEquals(false, commentVoteService.isUserAllowedToVote(userKey,
133                            ideaComment));
134            fail("IdeasExchangeException expected : User should not be allowed to vote on " +
135                            "idea which he/she has already voted");
136        } catch (IdeasExchangeException e) {
137            assert (true);
138        }
139
140    }
141}