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

http://thoughtsite.googlecode.com/ · Java · 282 lines · 225 code · 30 blank · 27 comment · 4 complexity · 89bc9019837f11ca3ba9ab2bc897030c MD5 · raw file

  1. // Copyright 2009 Google Inc. All Rights Reserved.
  2. /**
  3. *
  4. */
  5. package com.google.ie.business.service.impl;
  6. import static org.junit.Assert.assertEquals;
  7. import static org.junit.Assert.assertNotNull;
  8. import static org.junit.Assert.assertNull;
  9. import static org.junit.Assert.assertTrue;
  10. import static org.mockito.Mockito.mock;
  11. import static org.mockito.Mockito.when;
  12. import com.google.ie.business.dao.TagDao;
  13. import com.google.ie.business.dao.impl.TagDaoImpl;
  14. import com.google.ie.business.domain.Idea;
  15. import com.google.ie.business.domain.Tag;
  16. import com.google.ie.business.domain.User;
  17. import com.google.ie.common.cache.CacheHelper;
  18. import com.google.ie.dto.RetrievalInfo;
  19. import com.google.ie.test.ServiceTest;
  20. import org.junit.After;
  21. import org.junit.Before;
  22. import org.junit.Test;
  23. import java.io.Serializable;
  24. import java.util.ArrayList;
  25. import java.util.Collection;
  26. import java.util.HashSet;
  27. import java.util.List;
  28. import java.util.Set;
  29. /**
  30. * Test case for TagServiceImpl class
  31. *
  32. * @author Charanjeet singh
  33. */
  34. public class TagServiceImplTest extends ServiceTest {
  35. private static final String TAG = "tag";
  36. private static final String TAG_CLOUD = "tagCloud";
  37. private TagServiceImpl tagService;
  38. @Before
  39. public void setUp() {
  40. super.setUp();
  41. if (tagService == null)
  42. tagService = new TagServiceImpl();
  43. TagDao mockTagDao = mock(TagDaoImpl.class);
  44. tagService.setTagDao(mockTagDao);
  45. tagService.setIdeaService(mock(IdeaServiceImpl.class));
  46. tagService.setEntityIndexService(mock(EntityIndexServiceImpl.class));
  47. }
  48. @After
  49. public void tearDown() {
  50. super.setUp();
  51. tagService.setTagDao(null);
  52. }
  53. @Test
  54. public void saveTags_withNullTagString() {
  55. String tagString = null;
  56. assertNull(tagService.saveTags(tagString));
  57. }
  58. @Test
  59. public void parseTags() {
  60. String tagString = "tag1,tag2";
  61. List<Tag> tags = tagService.parseTags(tagString);
  62. assertNotNull(tags);
  63. assertEquals(2, tags.size());
  64. assertEquals("tag1", tags.get(0).getTitle());
  65. assertEquals("tag2", tags.get(1).getTitle());
  66. }
  67. @Test
  68. public void parseTags_checkTagLowerCase() {
  69. String tagString = "TAg1,TAG2";
  70. List<Tag> tags = tagService.parseTags(tagString);
  71. assertNotNull(tags);
  72. assertEquals(2, tags.size());
  73. assertEquals("tag1", tags.get(0).getTitle());
  74. assertEquals("tag2", tags.get(1).getTitle());
  75. }
  76. @Test
  77. public void parseTags_checkWhiteSpaceHandling() {
  78. String tagString = "TAg1, TAG2 TaG3 \n Tag4";
  79. List<Tag> tags = tagService.parseTags(tagString);
  80. assertNotNull(tags);
  81. assertEquals(4, tags.size());
  82. assertEquals("tag1", tags.get(0).getTitle());
  83. assertEquals("tag2", tags.get(1).getTitle());
  84. assertEquals("tag3", tags.get(2).getTitle());
  85. assertEquals("tag4", tags.get(3).getTitle());
  86. }
  87. @Test
  88. public void saveTags_withBlankTagString() {
  89. String tagString = null;
  90. assertNull(tagService.saveTags(tagString));
  91. }
  92. @Test
  93. public void getTagsByKeys_withNullKeys() {
  94. List<Tag> tagList = tagService.getTagsByKeys(null);
  95. assertNull(tagList);
  96. }
  97. @Test
  98. public void getTagsByKeys() {
  99. Tag expactedTag = new Tag();
  100. expactedTag.setKey("testKey");
  101. List<Tag> tagList = new ArrayList<Tag>();
  102. tagList.add(expactedTag);
  103. Collection<String> keys = new ArrayList<String>();
  104. keys.add("testKey");
  105. when(tagService.getTagDao().getTagsByKeys(keys)).thenReturn(tagList);
  106. assertNotNull(tagService.getTagsByKeys(keys));
  107. assertEquals("testKey", tagService.getTagsByKeys(keys).get(0).getKey());
  108. }
  109. @Test
  110. public void getTagsWithSpecificStartString() {
  111. Tag tag = new Tag();
  112. tag.setTitle("FINANCE");
  113. List<Tag> list = new ArrayList<Tag>();
  114. list.add(tag);
  115. RetrievalInfo info = new RetrievalInfo();
  116. String startString = "fin";
  117. when(tagService.getTagDao().getTagsWithSpecificStartString(startString, info)).thenReturn(
  118. list);
  119. assertNotNull(list);
  120. String titleOfTagRetrieved = (list.get(0)).getTitle().toLowerCase();
  121. assertTrue(titleOfTagRetrieved.startsWith(startString.toLowerCase()));
  122. }
  123. @Test
  124. public void getTagsForAutoSuggestion() {
  125. Tag tag = new Tag();
  126. tag.setTitle("Finance");
  127. List<Tag> list = new ArrayList<Tag>();
  128. list.add(tag);
  129. RetrievalInfo info = new RetrievalInfo();
  130. String startString = "fin";
  131. when(tagService.getTagDao().getTagsWithSpecificStartString(startString, info)).thenReturn(
  132. list);
  133. assertNotNull(list);
  134. String titleOfTagRetrieved = (list.get(0)).getTitle().toLowerCase();
  135. assertTrue(titleOfTagRetrieved.startsWith(startString.toLowerCase()));
  136. }
  137. @Test
  138. public void getTagsForTagCloud_fromCache() {
  139. List<Tag> expectedList = createSampleTagList();
  140. CacheHelper.putObject(TAG, TAG_CLOUD,
  141. (Serializable) expectedList);
  142. int expectedListSize = expectedList.size();
  143. RetrievalInfo info = new RetrievalInfo();
  144. /*
  145. * the when condition below is used so that if the data is not available
  146. * in cache,the call to datastore within the method being tested should
  147. * return null
  148. */
  149. when(tagService.getTagDao().getTagsForTagCloud(info))
  150. .thenReturn(null);
  151. List<Tag> actualList = tagService.getTagsForTagCloud(info);
  152. assertNotNull(actualList);
  153. assertEquals(expectedListSize, actualList.size());
  154. }
  155. @Test
  156. public void getTagsForTagCloud_fromDatastore() {
  157. /* Clear the data from cache if exists */
  158. if (CacheHelper.containsObject(TAG, TAG_CLOUD)) {
  159. CacheHelper.deleteObject(TAG, TAG_CLOUD);
  160. }
  161. RetrievalInfo info = new RetrievalInfo();
  162. when(tagService.getTagDao().getTagsForTagCloud(info))
  163. .thenReturn(createSampleTagList());
  164. List<Tag> tags = tagService.getTagsForTagCloud(info);
  165. assertNotNull(tags);
  166. }
  167. @Test
  168. public void incrementWeights() {
  169. List<Tag> tags = new ArrayList<Tag>();
  170. Tag tmpTag = new Tag();
  171. tmpTag.setTitle("TestTitle");
  172. tmpTag.setWeightage(1);
  173. tags.add(tmpTag);
  174. when(tagService.getTagDao().getTagByTitle(tmpTag.getTitle())).thenReturn(tmpTag);
  175. when(tagService.getTagDao().saveTag(tmpTag)).thenReturn(tmpTag);
  176. tagService.getTagDao().saveTag(tmpTag);
  177. List<Tag> tagList = tagService.incrementWeights("TestTitle");
  178. assertEquals(1, tagList.size());
  179. assertEquals(2, tagList.get(0).getWeightage());
  180. }
  181. @Test
  182. public void getMyTagCloud() {
  183. Set<String> setOfTagKeys = new HashSet<String>();
  184. List<Tag> expectedListOfTags = new ArrayList<Tag>();
  185. /* The user */
  186. User user = new User();
  187. user.setUserKey("sunny");
  188. /* First tag */
  189. Tag tag1 = new Tag();
  190. tag1.setTitle("Tag 1");
  191. tag1.setKey("Tag1key");
  192. /* Added to the expected tag list */
  193. expectedListOfTags.add(tag1);
  194. /* Second tag */
  195. Tag tag2 = new Tag();
  196. tag2.setTitle("Tag 2");
  197. tag2.setKey("Tag2key");
  198. /* Added to the expected tag list */
  199. expectedListOfTags.add(tag2);
  200. /* First idea of the user */
  201. Idea idea1 = new Idea();
  202. idea1.setCreatorKey("sunny");
  203. Set<String> set1 = new HashSet<String>();
  204. /* tag1 and tag2 associated with idea1 */
  205. set1.add(tag1.getKey());
  206. set1.add(tag2.getKey());
  207. idea1.setTagKeys(set1);
  208. /* Second idea of the user */
  209. Idea idea2 = new Idea();
  210. idea2.setCreatorKey("sunny");
  211. Set<String> set2 = new HashSet<String>();
  212. /* tag1 associated with idea2 */
  213. set2.add(tag1.getKey());
  214. idea2.setTagKeys(set2);
  215. /* Combined set of both tag sets */
  216. setOfTagKeys.addAll(set1);
  217. setOfTagKeys.addAll(set2);
  218. /* Create a list containing both ideas */
  219. List<Idea> ideaList = new ArrayList<Idea>();
  220. ideaList.add(idea1);
  221. ideaList.add(idea2);
  222. when(tagService.getIdeaService().getIdeasForUser(user, null)).thenReturn(ideaList);
  223. when(tagService.getTagDao().getTagsByKeys(setOfTagKeys)).thenReturn(expectedListOfTags);
  224. List<Tag> actualTagList = tagService.getMyTagCloud(user);
  225. assertNotNull(actualTagList);
  226. assertEquals(expectedListOfTags.size(), actualTagList.size());
  227. }
  228. @Test
  229. public void saveTag() {
  230. Tag expectedTag = new Tag();
  231. expectedTag.setTitle("Tag 1");
  232. expectedTag.setKey("Tag1key");
  233. when(tagService.getTagDao().saveTag(expectedTag)).thenReturn(expectedTag);
  234. Tag actualTag = tagService.saveTag(expectedTag);
  235. assertEquals(expectedTag, actualTag);
  236. }
  237. private List<Tag> createSampleTagList() {
  238. Tag tag;
  239. String[] titles = { "Finance Tag", "Medicine Tag", "Education Tag", "Finance second Tag",
  240. "Medicine second Tag", "Education second Tag" };
  241. List<Tag> tagList = new ArrayList<Tag>();
  242. int i = 0;
  243. for (String title : titles) {
  244. tag = new Tag();
  245. tag.setTitle(title);
  246. tag.setWeightage(i++);
  247. // tag = mockTagDao.saveTag(tag);
  248. tagList.add(tag);
  249. }
  250. return tagList;
  251. }
  252. }