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