PageRenderTime 624ms CodeModel.GetById 431ms app.highlight 7ms RepoModel.GetById 104ms app.codeStats 0ms

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

http://thoughtsite.googlecode.com/
Java | 88 lines | 61 code | 15 blank | 12 comment | 0 complexity | 9bffaadc2010836414e2b05db2a46067 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.mockito.Mockito.mock;
 8import static org.mockito.Mockito.when;
 9
10import com.google.ie.business.dao.impl.IdeaCategoryDaoImpl;
11import com.google.ie.business.domain.IdeaCategory;
12import com.google.ie.common.cache.CacheConstants;
13import com.google.ie.common.cache.CacheHelper;
14import com.google.ie.test.ServiceTest;
15
16import org.junit.Before;
17import org.junit.Test;
18
19import java.util.LinkedList;
20import java.util.List;
21
22/**
23 * Test case for IdeaCategoryServiceImpl class
24 * 
25 * @author Sachneet
26 * 
27 */
28public class IdeaCategoryServiceImplTest extends ServiceTest {
29    IdeaCategoryServiceImpl categoryService;
30
31    @Before
32    public void setUp() {
33        super.setUp();
34        categoryService = new IdeaCategoryServiceImpl();
35        categoryService.setIdeaCategoryDao(mock(IdeaCategoryDaoImpl.class));
36    }
37
38    @Test
39    public void addCategory() {
40        IdeaCategory category = new IdeaCategory();
41        category.setName("testCategory1");
42        category.setDescription("testCategory1 description");
43        when(categoryService.getIdeaCategoryDao().saveIdeaCategory(category))
44                        .thenReturn(category);
45
46        assertNotNull(categoryService.addIdeaCategory(category));
47    }
48
49    @Test
50    public void getAllCategories_fromCache() {
51        CacheHelper.putObject(CacheConstants.CATEGORY_NAMESPACE, CacheConstants.CATEGORIES, this
52                        .getMockCategoriesList());
53        List<IdeaCategory> listOfCategoryObjects = categoryService.getAllIdeaCategories();
54
55        assertNotNull(listOfCategoryObjects);
56    }
57
58    @Test
59    public void getAllCategories_fromDatastore() {
60        when(categoryService.getIdeaCategoryDao().getIdeaCategories())
61                        .thenReturn(this.getMockCategoriesList());
62        List<IdeaCategory> listOfCategoryObjects = categoryService.getAllIdeaCategories();
63        assertNotNull(listOfCategoryObjects);
64    }
65
66    @Test
67    public void getCategoryByName() {
68        when(categoryService.getIdeaCategoryDao().getCategoryByName("testCategory")).thenReturn(
69                        this.getMockCategoriesList().get(0));
70        IdeaCategory category = categoryService.getCategoryByName("testCategory");
71        assertNotNull(category);
72        assertEquals("testCategory", category.getName());
73    }
74
75    /**
76     * Creates sample category list
77     * 
78     * @return a sample list of {@link IdeaCategory} objects
79     */
80    private LinkedList<IdeaCategory> getMockCategoriesList() {
81        IdeaCategory category = new IdeaCategory();
82        category.setName("testCategory");
83        LinkedList<IdeaCategory> categories = new LinkedList<IdeaCategory>();
84        categories.add(category);
85
86        return categories;
87    }
88}