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