PageRenderTime 314ms CodeModel.GetById 181ms app.highlight 16ms RepoModel.GetById 115ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 168 lines | 134 code | 20 blank | 14 comment | 14 complexity | 2134f72747fdcdde6d9b1822d0cc3a1b 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.mockito.Mockito.mock;
 10import static org.mockito.Mockito.when;
 11
 12import com.google.ie.business.dao.AdminRequestDao;
 13import com.google.ie.business.dao.CommentDao;
 14import com.google.ie.business.dao.impl.AdminRequestDaoImpl;
 15import com.google.ie.business.dao.impl.CommentDaoImpl;
 16import com.google.ie.business.dao.impl.DaoConstants;
 17import com.google.ie.business.domain.AdminRequest;
 18import com.google.ie.business.domain.Comment;
 19import com.google.ie.business.domain.IdeaComment;
 20import com.google.ie.business.domain.User;
 21import com.google.ie.business.service.ServiceConstants;
 22import com.google.ie.common.audit.AuditManager;
 23import com.google.ie.dto.RetrievalInfo;
 24import com.google.ie.test.ServiceTest;
 25
 26import org.junit.Before;
 27import org.junit.Test;
 28
 29import java.util.ArrayList;
 30import java.util.Date;
 31import java.util.List;
 32
 33/**
 34 * Test case for IdeaCommentServiceImpl class
 35 * 
 36 * @author Charanjeet singh
 37 */
 38public class IdeaCommentServiceImplTest extends ServiceTest {
 39    private IdeaCommentServiceImpl ideaCommentService;
 40    private CommentDao commentDao = mock(CommentDaoImpl.class);
 41    private AuditManager auditmanager = mock(AuditManager.class);
 42    private AdminRequestDao mockAdminRequestDao = mock(AdminRequestDaoImpl.class);
 43
 44    @Before
 45    public void setUp() {
 46        super.setUp();
 47        if (ideaCommentService == null) {
 48            ideaCommentService = new IdeaCommentServiceImpl();
 49        }
 50        ideaCommentService.setCommentDao(commentDao);
 51        ideaCommentService.setAuditManager(auditmanager);
 52        ideaCommentService.setAdminRequestDao(mockAdminRequestDao);
 53    }
 54
 55    @Test
 56    public void addComment() {
 57        User user = new User();
 58        user.setUserKey("key");
 59
 60        IdeaComment comment = new IdeaComment();
 61        comment.setText("TestComment");
 62        comment.setIdeaKey("Ideakey");
 63
 64        Comment comment1 = new IdeaComment();
 65        comment1.setText("TestComment");
 66        comment1.setKey("testKey");
 67
 68        when(commentDao.saveComment(comment)).thenReturn(comment);
 69        assertNotNull(ideaCommentService.addComment(comment, user));
 70        assertNotNull(Comment.STATUS_SAVED, ideaCommentService.addComment(comment, user)
 71                        .getStatus());
 72    }
 73
 74    @Test
 75    public void getComments() {
 76        String ideaKey = "testKey";
 77        // Idea idea = new Idea();
 78        // idea.setKey("testKey");
 79        List<Comment> comments = new ArrayList<Comment>();
 80        IdeaComment ideaComment = new IdeaComment();
 81        ideaComment.setKey("testCommentKey");
 82        comments.add(ideaComment);
 83        RetrievalInfo retrievalInfo = prepareRetrievalInfo(null);
 84        when(commentDao.getComments(ideaKey, retrievalInfo, "ideaKey")).thenReturn(comments);
 85        assertNotNull(ideaCommentService.getComments(ideaKey, retrievalInfo));
 86        assertEquals("testCommentKey", ideaCommentService.getComments(ideaKey, retrievalInfo)
 87                        .get(0).getKey());
 88    }
 89
 90    @Test
 91    public void flagComment() {
 92        IdeaComment ideaComment = new IdeaComment();
 93        ideaComment.setKey("testCommentKey");
 94        ideaComment.setText("He this is an awesome idea.Keep it up and do " +
 95                        "let us know about the latest advancements in the idea");
 96        ideaComment.setStatus("Saved");
 97
 98        IdeaComment saveIdeaComment = new IdeaComment();
 99        saveIdeaComment.setKey("testCommentKey");
100        saveIdeaComment.setText("He this is an awesome idea.Keep it up and do " +
101                        "let us know about the latest advancements in the idea");
102        saveIdeaComment.setStatus("Flagged");
103
104        User user = new User();
105        user.setUserKey("key");
106        user.setEmailId("anujsiroh@gmail.com");
107
108        AdminRequest adminRequest = new AdminRequest();
109        adminRequest.setEntityKey("ideaCommentKey");
110        adminRequest.setEntityType(IdeaComment.class.getSimpleName());
111        adminRequest.setRequesterkey("UserKey");
112        adminRequest.setRequestType(AdminRequest.REQUEST_OBJECTIONABLE);
113        adminRequest.setCreatedOn(new Date());
114        adminRequest.setStatus(AdminRequest.STATUS_PENDING);
115        adminRequest.setEntityTitle(ideaCommentService.getTrimmedComment(ideaComment.getText()));
116
117        when(ideaCommentService.getCommentById(ideaComment.getKey())).thenReturn(ideaComment);
118        when(ideaCommentService.getCommentDao().saveComment(ideaComment)).thenReturn(
119                        saveIdeaComment);
120        when(mockAdminRequestDao.saveRequest(adminRequest)).thenReturn(true);
121
122        ideaCommentService.flagComment(ideaComment.getKey(), user);
123        assertEquals("Flagged", saveIdeaComment.getStatus());
124    }
125
126    @Test
127    public void getTrimmedComment() {
128        /* checking if comment text is greater than 40 */
129        String commentText = "Hey this is an awesome idea.Keep it up and do " +
130                        "let us know about the latest advancements in the idea";
131        String trimmedComment = ideaCommentService
132                        .getTrimmedComment(commentText);
133        assertEquals(42, trimmedComment.length());
134
135        /* checking if comment text is smaller than 40 */
136        commentText = "Hey this is an awesome idea";
137        trimmedComment = ideaCommentService.getTrimmedComment(commentText);
138        assertEquals(commentText.length(), trimmedComment.length());
139    }
140
141    private RetrievalInfo prepareRetrievalInfo(RetrievalInfo retrievalInfo) {
142        if (retrievalInfo == null) {
143            retrievalInfo = new RetrievalInfo();
144            retrievalInfo.setStartIndex(ServiceConstants.ZERO);
145            retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
146            retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
147            retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
148        } else {
149            // Handle garbage values if any.
150            String orderOn = retrievalInfo.getOrderBy();
151            String orderByParam = retrievalInfo.getOrderType();
152            if (retrievalInfo.getStartIndex() < ServiceConstants.ZERO)
153                retrievalInfo.setStartIndex(ServiceConstants.ZERO);
154            if (retrievalInfo.getNoOfRecords() <= ServiceConstants.ZERO)
155                retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
156            if (orderByParam == null || !((orderByParam.equals(DaoConstants.ORDERING_ASCENDING)
157                            || orderByParam.equals(DaoConstants.ORDERING_DESCENDING))))
158                retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
159            if (orderOn == null
160                            || !(orderOn
161                            .equals(ServiceConstants.IDEA_COMMENT_ORDERING_FIELD_CREATED_ON)
162                            )) {
163                retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
164            }
165        }
166        return retrievalInfo;
167    }
168}