PageRenderTime 36ms CodeModel.GetById 12ms app.highlight 19ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://thoughtsite.googlecode.com/
Java | 120 lines | 96 code | 17 blank | 7 comment | 0 complexity | e1c08e8ebe22358814a36445f1e963ab 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.UserDao;
 11import com.google.ie.business.dao.impl.UserDaoImpl;
 12import com.google.ie.business.domain.User;
 13import com.google.ie.common.util.SearchUtility;
 14import com.google.ie.dto.RetrievalInfo;
 15import com.google.ie.test.ServiceTest;
 16
 17import org.junit.Before;
 18import org.junit.Test;
 19
 20/**
 21 * Test case for UserServiceImpl class
 22 * 
 23 * @author Surabhi Bhatnagar
 24 * 
 25 */
 26public class UserServiceImplTest extends ServiceTest {
 27    private UserServiceImpl userService;
 28    private UserDao mockUserDao = mock(UserDaoImpl.class);
 29
 30    @Before
 31    public void setUp() {
 32        super.setUp();
 33        userService = new UserServiceImpl();
 34        userService.setUserDao(mockUserDao);
 35    }
 36
 37    @Test
 38    public void saveUser() {
 39        User user = new User();
 40        user.setUserKey("key");
 41        user.setUserKey("Display Name");
 42        user.setId("id");
 43        user.setStatus("active");
 44        when(userService.saveUser(user)).thenReturn(user);
 45        assertNotNull(userService.saveUser(user));
 46    }
 47
 48    @Test
 49    public void addOrUpdateUser() {
 50        User user = new User();
 51        user.setUserKey("key");
 52        user.setDisplayName("Display Name");
 53        user.setId("id");
 54        user.setThumbnailUrl("thumbnailUrl");
 55        user.setEmailId("emailId@google.com");
 56        when(userService.saveUser(user)).thenReturn(user);
 57        when(userService.getUserById("id")).thenReturn(user);
 58        assertNotNull(userService.addOrUpdateUser(user));
 59    }
 60
 61    @Test
 62    public void banUser() {
 63        User user = new User();
 64        user.setUserKey("key");
 65        user.setDisplayName("Display Name");
 66        user.setId("id");
 67        user.setThumbnailUrl("thumbnailUrl");
 68        user.setEmailId("emailId@google.com");
 69        user.setStatus("active");
 70
 71        User banUser = new User();
 72        banUser.setUserKey("key");
 73        banUser.setDisplayName("Display Name");
 74        banUser.setId("id");
 75        banUser.setThumbnailUrl("thumbnailUrl");
 76        banUser.setEmailId("emailId@google.com");
 77        banUser.setStatus("banned");
 78
 79        when(userService.getUserDao().findEntityByPrimaryKey(User.class, user.getUserKey()))
 80                        .thenReturn(user);
 81        when(userService.saveUser(user)).thenReturn(banUser);
 82        SearchUtility.indexEntity(user);
 83
 84        userService.banUser(banUser);
 85        assertEquals("banned", banUser.getStatus());
 86    }
 87
 88    @Test
 89    public void activate() {
 90        User user = new User();
 91        user.setUserKey("key");
 92        user.setDisplayName("Display Name");
 93        user.setId("id");
 94        user.setThumbnailUrl("thumbnailUrl");
 95        user.setEmailId("emailId@google.com");
 96        user.setStatus("banned");
 97
 98        User banUser = new User();
 99        banUser.setUserKey("key");
100        banUser.setDisplayName("Display Name");
101        banUser.setId("id");
102        banUser.setThumbnailUrl("thumbnailUrl");
103        banUser.setEmailId("emailId@google.com");
104        banUser.setStatus("active");
105
106        when(userService.getUserDao().findEntityByPrimaryKey(User.class, user.getUserKey()))
107                        .thenReturn(user);
108        when(userService.saveUser(user)).thenReturn(banUser);
109        userService.activate(banUser);
110        assertEquals("active", banUser.getStatus());
111    }
112
113    @Test
114    public void getUsers() {
115        RetrievalInfo retrievalInfo = new RetrievalInfo();
116        String role = "user";
117        assertNotNull(userService.getUserDao().getUsers(retrievalInfo, role, null));
118    }
119
120}