PageRenderTime 386ms CodeModel.GetById 160ms app.highlight 87ms RepoModel.GetById 135ms app.codeStats 0ms

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