PageRenderTime 55ms CodeModel.GetById 38ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://thoughtsite.googlecode.com/
Java | 137 lines | 109 code | 18 blank | 10 comment | 2 complexity | 57650cecb8d69ece3a72187fa444cd9a 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.appengine.api.datastore.KeyFactory;
 14import com.google.ie.business.dao.impl.ProjectDaoImpl;
 15import com.google.ie.business.domain.EntityIndex;
 16import com.google.ie.business.domain.Idea;
 17import com.google.ie.business.domain.Project;
 18import com.google.ie.business.domain.User;
 19import com.google.ie.business.service.EntityIndexService;
 20import com.google.ie.common.audit.AuditManager;
 21import com.google.ie.common.exception.SystemException;
 22import com.google.ie.common.taskqueue.IndexQueueUpdater;
 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.HashSet;
 31import java.util.List;
 32import java.util.Set;
 33
 34/**
 35 * Test case for ProjectServiceImpl class
 36 * 
 37 * @author Charanjeet singh
 38 * 
 39 */
 40public class ProjectServiceImplTest extends ServiceTest {
 41    private ProjectServiceImpl projectService;
 42    private ProjectDaoImpl projectDao = mock(ProjectDaoImpl.class);
 43    private AuditManager mockAuditManager = mock(AuditManager.class);
 44    private IdeaServiceImpl ideaService = mock(IdeaServiceImpl.class);
 45    private EntityIndexService entityIndexService = mock(EntityIndexServiceImpl.class);
 46    private IndexQueueUpdater indexQueueUpdater = mock(IndexQueueUpdater.class);
 47
 48    @Before
 49    public void setUp() {
 50        super.setUp();
 51        if (projectService == null)
 52            projectService = new ProjectServiceImpl();
 53        projectService.setAuditManager(mockAuditManager);
 54        projectService.setProjectDao(projectDao);
 55        projectService.setIdeaService(ideaService);
 56        projectService.setEntityIndexService(entityIndexService);
 57        projectService.setIndexQueueUpdater(indexQueueUpdater);
 58    }
 59
 60    @Test
 61    public final void createProject() {
 62        User user = new User();
 63        user.setUserKey("userKey");
 64
 65        Idea idea = new Idea();
 66        idea.setKey("ideaKey");
 67        idea.setStatus(Idea.STATUS_PUBLISHED);
 68
 69        Project project = new Project();
 70        project.setIdeaKey("ideaKey");
 71        project.setName("Project Name");
 72        project.setKey("projectkey");
 73        project.setStatus(Project.STATUS_CREATED);
 74
 75        EntityIndex index = new EntityIndex();
 76        index.setKey(KeyFactory.createKey("index", "project"));
 77        index.setIndexed(0);
 78
 79        when(projectService.getIdeaService().getIdeaByKey(project.getIdeaKey())).thenReturn(idea);
 80        when(projectService.getProjectDao().saveProject(project)).thenReturn(project);
 81        when(projectService.getEntityIndexService().createEntityIndex(project.getKey()))
 82                        .thenReturn(index);
 83        when(projectService.getProjectDao().getProject(project.getKey())).thenReturn(project);
 84        doNothing().when(projectService.getIndexQueueUpdater()).indexEntity(index.getKey());
 85        assertNotNull(projectService.createOrUpdateProject(project, user));
 86        assertEquals(projectService.createOrUpdateProject(project, user).getStatus(),
 87                        Project.STATUS_CREATED);
 88    }
 89
 90    @Test(expected = SystemException.class)
 91    public final void createProject_withIdeaNotPublished() {
 92        User user = new User();
 93        user.setUserKey("userKey");
 94
 95        Idea idea = new Idea();
 96        idea.setKey("ideaKey");
 97        idea.setStatus(Idea.STATUS_OBJECTIONABLE);
 98
 99        Project project = new Project();
100        project.setIdeaKey("ideaKey");
101        project.setName("Project Name");
102
103        when(projectService.getIdeaService().getIdeaByKey(project.getIdeaKey())).thenReturn(idea);
104        when(projectService.getProjectDao().saveProject(project)).thenReturn(project);
105        assertNotNull(projectService.createOrUpdateProject(project, user));
106    }
107
108    @Test
109    public final void listProjects() {
110        Set<String> statusOfProject = new HashSet<String>();
111        RetrievalInfo retrievalInfo = new RetrievalInfo();
112        statusOfProject.add(Project.STATUS_CREATED);
113        List<Project> projects = new ArrayList<Project>();
114        projects.add(new Project());
115        when(projectDao.getProjects(null, statusOfProject)).thenReturn(projects);
116        assertNotNull(projectService.listProjects(retrievalInfo));
117    }
118
119    @Test
120    public final void getDetails() {
121        Project project = new Project();
122        project.setKey("testKey");
123        project.setName("testName");
124        when(projectDao.getProject(project.getKey())).thenReturn(project);
125        assertNotNull(projectService.getDetails(project));
126        assertEquals(project.getKey(), projectService.getDetails(project).getKey());
127    }
128
129    @Test(expected = SystemException.class)
130    public final void getDetails_invalidArgument() {
131        Project project = new Project();
132        project.setName("testName");
133        when(projectDao.getProject(project.getKey())).thenReturn(project);
134        assertNotNull(projectService.getDetails(project));
135        assertEquals(project.getKey(), projectService.getDetails(project).getKey());
136    }
137}