PageRenderTime 28ms CodeModel.GetById 14ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 200 lines | 152 code | 28 blank | 20 comment | 14 complexity | b38ef88556848217a29c4aeb6d82d80b 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.doNothing;
 10import static org.mockito.Mockito.mock;
 11import static org.mockito.Mockito.when;
 12
 13import com.google.ie.business.dao.AdminRequestDao;
 14import com.google.ie.business.dao.CommentDao;
 15import com.google.ie.business.dao.impl.AdminRequestDaoImpl;
 16import com.google.ie.business.dao.impl.CommentDaoImpl;
 17import com.google.ie.business.dao.impl.DaoConstants;
 18import com.google.ie.business.domain.AdminRequest;
 19import com.google.ie.business.domain.Comment;
 20import com.google.ie.business.domain.EntityIndex;
 21import com.google.ie.business.domain.ProjectComment;
 22import com.google.ie.business.domain.User;
 23import com.google.ie.business.service.EntityIndexService;
 24import com.google.ie.business.service.ServiceConstants;
 25import com.google.ie.common.audit.AuditManager;
 26import com.google.ie.common.taskqueue.IndexQueueUpdater;
 27import com.google.ie.dto.RetrievalInfo;
 28import com.google.ie.test.ServiceTest;
 29
 30import org.junit.Before;
 31import org.junit.Test;
 32
 33import java.util.ArrayList;
 34import java.util.Date;
 35import java.util.List;
 36
 37/**
 38 * @author asirohi
 39 * 
 40 */
 41public class ProjectCommentServiceImplTest extends ServiceTest {
 42
 43    ProjectCommentServiceImpl projectCommentServiceImpl;
 44    private CommentDao commentDao = mock(CommentDaoImpl.class);
 45    private AuditManager auditmanager = mock(AuditManager.class);
 46    private AdminRequestDao mockAdminRequestDao = mock(AdminRequestDaoImpl.class);
 47
 48    private EntityIndexService entityIndexService = mock(EntityIndexServiceImpl.class);
 49    private IndexQueueUpdater indexQueueUpdater = mock(IndexQueueUpdater.class);
 50
 51    /**
 52     */
 53    @Before
 54    public void setUp() {
 55        if (projectCommentServiceImpl == null) {
 56            projectCommentServiceImpl = new ProjectCommentServiceImpl();
 57        }
 58
 59        projectCommentServiceImpl.setCommentDao(commentDao);
 60        projectCommentServiceImpl.setAuditManager(auditmanager);
 61        projectCommentServiceImpl.setAdminRequestDao(mockAdminRequestDao);
 62        projectCommentServiceImpl.setEntityIndexService(entityIndexService);
 63        projectCommentServiceImpl.setIndexQueueUpdater(indexQueueUpdater);
 64    }
 65
 66    /**
 67     * Test method for
 68     * {@link com.google.ie.business.service.impl.ProjectCommentServiceImpl#addComment(com.google.ie.business.domain.Comment, com.google.ie.business.domain.User)}
 69     * .
 70     */
 71    @Test
 72    public void addComment() {
 73        ProjectComment projectComment = new ProjectComment();
 74        projectComment.setText("Nice project");
 75        projectComment.setProjectKey("projectKey");
 76        projectComment.setKey("key");
 77
 78        User user = new User();
 79        user.setUserKey("userKey");
 80
 81        EntityIndex index = new EntityIndex();
 82        // index.setKey(KeyFactory.createKey("projectComment", 12345));
 83        // index.setIndexed(0);
 84
 85        when(projectCommentServiceImpl.getCommentDao().saveComment(projectComment))
 86                        .thenReturn(projectComment);
 87
 88        when(projectCommentServiceImpl.getEntityIndexService().createEntityIndex(
 89                        projectComment.getKey()))
 90                        .thenReturn(index);
 91
 92        doNothing().when(projectCommentServiceImpl.getIndexQueueUpdater()).indexEntity(
 93                        index.getKey());
 94        assertNotNull(projectCommentServiceImpl.addComment(projectComment, user));
 95        assertEquals(projectComment,
 96                        projectCommentServiceImpl.addComment(projectComment,
 97                        user));
 98
 99    }
100
101    @Test
102    public void getComments() {
103        String projKey = "testKey";
104        List<Comment> comments = new ArrayList<Comment>();
105
106        ProjectComment projComment = new ProjectComment();
107        projComment.setKey("testCommentKey");
108        comments.add(projComment);
109
110        RetrievalInfo retrievalInfo = prepareRetrievalInfo(null);
111        when(commentDao.getComments(projKey, retrievalInfo,
112                        "projectKey")).thenReturn(comments);
113        assertNotNull(projectCommentServiceImpl.getComments(projKey, retrievalInfo));
114        assertEquals("testCommentKey", projectCommentServiceImpl.getComments(projKey,
115                        retrievalInfo)
116                        .get(0).getKey());
117    }
118
119    private RetrievalInfo prepareRetrievalInfo(RetrievalInfo retrievalInfo) {
120        if (retrievalInfo == null) {
121            retrievalInfo = new RetrievalInfo();
122            retrievalInfo.setStartIndex(ServiceConstants.ZERO);
123            retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
124            retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
125            retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
126        } else {
127            // Handle garbage values if any.
128            String orderOn = retrievalInfo.getOrderBy();
129            String orderByParam = retrievalInfo.getOrderType();
130            if (retrievalInfo.getStartIndex() < ServiceConstants.ZERO)
131                retrievalInfo.setStartIndex(ServiceConstants.ZERO);
132            if (retrievalInfo.getNoOfRecords() <= ServiceConstants.ZERO)
133                retrievalInfo.setNoOfRecords(ServiceConstants.IDEA_COMMENT_LIST_DEFAULT_SIZE);
134            if (orderByParam == null || !((orderByParam.equals(DaoConstants.ORDERING_ASCENDING)
135                            || orderByParam.equals(DaoConstants.ORDERING_DESCENDING))))
136                retrievalInfo.setOrderType(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_TYPE);
137            if (orderOn == null
138                            || !(orderOn
139                            .equals(ServiceConstants.IDEA_COMMENT_ORDERING_FIELD_CREATED_ON)
140                            )) {
141                retrievalInfo.setOrderBy(ServiceConstants.DEFAULT_IDEA_COMMENT_ORDERING_FIELD);
142            }
143        }
144        return retrievalInfo;
145    }
146
147    @Test
148    public void flagComment() {
149        ProjectComment ProjectComment = new ProjectComment();
150        ProjectComment.setKey("testCommentKey");
151        ProjectComment.setText("He this is an awesome project. Keep it up and do " +
152                        "let us know about the latest advancements in the project");
153        ProjectComment.setStatus("Saved");
154
155        ProjectComment saveProjectComment = new ProjectComment();
156        saveProjectComment.setKey("testCommentKey");
157        saveProjectComment.setText("He this is an awesome project. Keep it up and do " +
158                        "let us know about the latest advancements in the project");
159        saveProjectComment.setStatus("Flagged");
160
161        User user = new User();
162        user.setUserKey("key");
163        user.setEmailId("surabhi@gmail.com");
164
165        AdminRequest adminRequest = new AdminRequest();
166        adminRequest.setEntityKey("projectCommentKey");
167        adminRequest.setEntityType(ProjectComment.class.getSimpleName());
168        adminRequest.setRequesterkey("UserKey");
169        adminRequest.setRequestType(AdminRequest.REQUEST_OBJECTIONABLE);
170        adminRequest.setCreatedOn(new Date());
171        adminRequest.setStatus(AdminRequest.STATUS_PENDING);
172        adminRequest.setEntityTitle(projectCommentServiceImpl.getTrimmedComment(ProjectComment
173                        .getText()));
174
175        when(projectCommentServiceImpl.getCommentById(ProjectComment.getKey()))
176                        .thenReturn(ProjectComment);
177        when(projectCommentServiceImpl.getCommentDao().saveComment(ProjectComment)).thenReturn(
178                        saveProjectComment);
179        when(mockAdminRequestDao.saveRequest(adminRequest)).thenReturn(true);
180
181        projectCommentServiceImpl.flagComment(ProjectComment.getKey(), user);
182        assertEquals("Flagged", saveProjectComment.getStatus());
183    }
184
185    @Test
186    public void getTrimmedComment() {
187        /* checking if comment text is greater than 40 */
188        String commentText = "Hey this is an awesome project. Keep it up and do " +
189                        "let us know about the latest advancements in the project";
190        String trimmedComment = projectCommentServiceImpl
191                        .getTrimmedComment(commentText);
192        assertEquals(42, trimmedComment.length());
193
194        /* checking if comment text is smaller than 40 */
195        commentText = "Hey this is an awesome project";
196        trimmedComment =
197                        projectCommentServiceImpl.getTrimmedComment(commentText);
198        assertEquals(commentText.length(), trimmedComment.length());
199    }
200}