PageRenderTime 326ms CodeModel.GetById 161ms app.highlight 65ms RepoModel.GetById 96ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 259 lines | 199 code | 41 blank | 19 comment | 0 complexity | ba8ce788b73922859228d12c475e89a0 MD5 | raw file
  1// Copyright 2009 Google Inc. All Rights Reserved.
  2
  3package com.google.ie.business.service.impl;
  4
  5import static org.junit.Assert.assertEquals;
  6import static org.junit.Assert.assertNotNull;
  7import static org.junit.Assert.assertNull;
  8import static org.mockito.Mockito.mock;
  9import static org.mockito.Mockito.when;
 10
 11import com.google.appengine.api.datastore.KeyFactory;
 12import com.google.ie.business.dao.IdeaDao;
 13import com.google.ie.business.dao.impl.IdeaDaoImpl;
 14import com.google.ie.business.domain.EntityIndex;
 15import com.google.ie.business.domain.Idea;
 16import com.google.ie.business.domain.Tag;
 17import com.google.ie.business.domain.User;
 18import com.google.ie.business.service.EntityIndexService;
 19import com.google.ie.business.service.ShardedCounterService;
 20import com.google.ie.business.service.UserService;
 21import com.google.ie.common.audit.AuditManager;
 22import com.google.ie.common.exception.SystemException;
 23import com.google.ie.common.taskqueue.IndexQueueUpdater;
 24import com.google.ie.dto.RetrievalInfo;
 25import com.google.ie.test.ServiceTest;
 26
 27import org.junit.Assert;
 28import org.junit.Before;
 29import org.junit.Test;
 30
 31import java.util.ArrayList;
 32import java.util.Date;
 33import java.util.HashSet;
 34import java.util.List;
 35import java.util.Set;
 36
 37/**
 38 * Test case for IdeaServiceImpl class
 39 * 
 40 * @author Charanjeet singh
 41 * 
 42 */
 43public class IdeaServiceImplTest extends ServiceTest {
 44    private IdeaServiceImpl ideaService;
 45    private IdeaDao mockIdeaDao = mock(IdeaDaoImpl.class);
 46    private AuditManager mockAuditManager = mock(AuditManager.class);
 47    private TagServiceImpl tagService = mock(TagServiceImpl.class);
 48    private UserService userService = mock(UserServiceImpl.class);
 49    private ShardedCounterService shardedCounterService = mock(ShardedCounterServiceImpl.class);
 50    private EntityIndexService entityIndexService = mock(EntityIndexServiceImpl.class);
 51    private IndexQueueUpdater indexQueueUpdater = mock(IndexQueueUpdater.class);
 52
 53    @Before
 54    public void setUp() {
 55        super.setUp();
 56        ideaService = new IdeaServiceImpl();
 57        ideaService.setIdeaDao(mockIdeaDao);
 58        ideaService.setAuditManager(mockAuditManager);
 59        ideaService.setTagService(tagService);
 60        ideaService.setUserService(userService);
 61        ideaService.setEntityIndexService(entityIndexService);
 62        ideaService.setIndexQueueUpdater(indexQueueUpdater);
 63        ideaService.setShardedCounterService(shardedCounterService);
 64    }
 65
 66    @Test
 67    public void addSummary() {
 68
 69        Idea idea = new Idea();
 70        idea.setKey("key");
 71        idea.setCreatorKey("userKey");
 72        idea.setStatus("Published");
 73        idea.setIdeaSummary("Test Summary");
 74
 75        Idea ideaTmp = new Idea();
 76        ideaTmp.setKey("key");
 77        ideaTmp.setCreatorKey("userKey");
 78        ideaTmp.setStatus("Published");
 79
 80        Idea updatredIdea = new Idea();
 81        updatredIdea.setKey("key");
 82        updatredIdea.setCreatorKey("userKey");
 83        updatredIdea.setStatus("Published");
 84        updatredIdea.setIdeaSummary("Test Summary");
 85
 86        User user = new User();
 87        user.setUserKey("userKey");
 88        when(mockIdeaDao.findEntityByPrimaryKey(Idea.class, idea.getKey()))
 89                        .thenReturn(ideaTmp);
 90        when(mockIdeaDao.saveIdea(idea)).thenReturn(updatredIdea);
 91        Assert.assertTrue(ideaService.addSummary(idea.getKey(), idea.getIdeaSummary(), user));
 92    }
 93
 94    @Test(expected = SystemException.class)
 95    public void addSummary_withIdeaStatusNotPublish() {
 96        Idea idea = new Idea();
 97        idea.setKey("key");
 98        idea.setCreatorKey("userKey");
 99        idea.setStatus("Saved");
100        idea.setIdeaSummary("Test Summary");
101        User user = new User();
102        user.setUserKey("userKey");
103
104        when(mockIdeaDao.saveIdea(idea)).thenReturn(idea);
105        ideaService.addSummary(idea.getKey(), idea.getIdeaSummary(), user);
106    }
107
108    @Test(expected = SystemException.class)
109    public void addSummary_withWrongIdeaCreator() {
110        Idea idea = new Idea();
111        idea.setKey("key");
112        idea.setCreatorKey("userKey");
113        idea.setStatus("Published");
114        idea.setIdeaSummary("Test Summary");
115
116        User user = new User();
117        user.setUserKey("userKey1");
118
119        when(mockIdeaDao.saveIdea(idea)).thenReturn(idea);
120        ideaService.addSummary(idea.getKey(), idea.getIdeaSummary(), user);
121    }
122
123    @Test
124    public void saveNewIdea() {
125        Idea idea = new Idea();
126        idea.setKey(null);
127        idea.setTitle("ideaTitle");
128
129        User user = new User();
130        user.setUserKey("key");
131        user.setId("id");
132
133        Idea savedIdea = new Idea();
134        savedIdea.setKey("MyKey");
135        savedIdea.setTitle("ideaTitle");
136        savedIdea.setCreatorKey(user.getUserKey());
137        savedIdea.setStatus(Idea.STATUS_SAVED);
138        savedIdea.setLastUpdated(new Date(System.currentTimeMillis()));
139
140        when(mockIdeaDao.saveIdea(idea)).thenReturn(savedIdea);
141        when(userService.saveUser(user)).thenReturn(user);
142        when(userService.getUserById(user.getId())).thenReturn(user);
143        assertNotNull(ideaService.saveIdea(idea, user));
144        assertEquals(idea.getTitle(), ideaService.saveIdea(idea, user).getTitle());
145    }
146
147    @Test
148    public void saveNullIdea() {
149        User user = new User();
150        assertNull(ideaService.saveIdea(null, user));
151    }
152
153    @Test
154    public void formatTagString() {
155        String tagStr = "TaG1,TAg2  Tag3 \n tag4";
156        assertEquals("tag1,tag2,tag3,tag4", ideaService.formatTagString(tagStr));
157    }
158
159    @Test
160    public void getIdeaDetails() {
161        Idea actualIdea = new Idea();
162        actualIdea.setTitle("Test_Idea");
163        actualIdea.setKey("TestKey");
164        when(mockIdeaDao.getIdea(actualIdea)).thenReturn(actualIdea);
165        actualIdea = ideaService.getIdeaDetails(actualIdea);
166        assertEquals("Test_Idea", actualIdea.getTitle());
167    }
168
169    @Test
170    public void getIdeaDetails_withNullKeys() {
171        Idea actualIdea = new Idea();
172        actualIdea.setTitle("Test_Idea");
173        actualIdea.setKey(null);
174        when(mockIdeaDao.getIdea(actualIdea)).thenReturn(actualIdea);
175        ideaService.getIdeaDetails(actualIdea);
176        assertNull(ideaService.getIdeaDetails(actualIdea));
177    }
178
179    @Test
180    public void publishIdea() {
181        ideaService = mock(IdeaServiceImpl.class);
182        int points = 5;
183        User user = new User();
184        user.setUserKey("userKey");
185
186        Idea idea = new Idea();
187        idea.setKey(null);
188        idea.setTitle("ideaTitle");
189
190        Idea savedIdea = new Idea();
191        savedIdea.setKey("key");
192        savedIdea.setTitle("ideaTitle");
193        savedIdea.setCreatorKey(user.getUserKey());
194        savedIdea.setStatus(Idea.STATUS_PUBLISHED);
195        savedIdea.setLastUpdated(new Date(System.currentTimeMillis()));
196        savedIdea.setPublishDate(new Date(System.currentTimeMillis()));
197
198        EntityIndex index = new EntityIndex();
199        index.setKey(KeyFactory.createKey("index", "idea"));
200        index.setIndexed(0);
201
202        // when(ideaService.getEntityIndexService().createEntityIndex(
203        // savedIdea.getKey()))
204        // .thenReturn(index);
205        //
206        // doNothing().when(ideaService.getIndexQueueUpdater()).indexEntity(
207        // index.getKey());
208        //
209        // doNothing().when(ideaService.getShardedCounterService()).updateTotalPoints(
210        // user.getUserKey(), points);
211        // doNothing().when(ideaService.getObjectionableManager()).checkObjectionable(
212        // idea.getKey());
213        when(mockIdeaDao.saveIdea(idea)).thenReturn(savedIdea);
214        when(ideaService.publishIdea(idea, user)).thenReturn(savedIdea);
215        assertEquals("Published", savedIdea.getStatus());
216    }
217
218    @Test
219    public void getIdeasForUser_forNullUser() {
220        User user = null;
221        RetrievalInfo retrievalInfo = null;
222
223        // when(mockIdeaDao.getUserIdeas(user, retrievalInfo)).thenReturn(null);
224        assertNull(ideaService.getIdeasForUser(user, retrievalInfo));
225    }
226
227    @Test
228    public void getIdeasByTagName() {
229        String tagKey = "testTagKey1234";
230        Tag tag = new Tag();
231        tag.setTitle("testTag");
232        tag.setKey(tagKey);
233
234        Idea expectedIdea = new Idea();
235        expectedIdea.setTitle("Test Idea");
236        expectedIdea.setStatus(Idea.STATUS_PUBLISHED);
237
238        Set<String> tagKeys = new HashSet<String>();
239        tagKeys.add(tagKey);
240        expectedIdea.setTagKeys(tagKeys);
241
242        List<Idea> expectedIdeasList = new ArrayList<Idea>();
243        expectedIdeasList.add(expectedIdea);
244        RetrievalInfo info = new RetrievalInfo();
245
246        when(tagService.getTagByName("testTag")).thenReturn(tag);
247
248        Set<String> setOfStatus = new HashSet<String>();
249        setOfStatus.add(Idea.STATUS_PUBLISHED);
250        setOfStatus.add(Idea.STATUS_DUPLICATE);
251
252        when(mockIdeaDao.getIdeasByTagKey(tagKey, setOfStatus, info)).thenReturn(
253                        expectedIdeasList);
254
255        ideaService.getIdeasByTagName(tag.getTitle(), info);
256
257        assertEquals(expectedIdea.getTitle(), expectedIdeasList.get(0).getTitle());
258    }
259}