<commit_before>package org.jtalks.jcommune.service.transactional;

import org.jtalks.jcommune.model.dao.UserDao;
import org.jtalks.jcommune.model.entity.User;
import org.jtalks.jcommune.service.UserService;
import org.jtalks.jcommune.service.exceptions.DuplicateException;
import org.mockito.Matchers;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.*;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class TransactionalUserServiceTest {
    private static final String USERNAME = "username";
    private static final String EMAIL = "<EMAIL>";
    private static final String PASSWORD = "password";
    private static final String FIRST_NAME = "first name";
    private static final String LAST_NAME = "last name";
    private static final Long USER_ID = 999L;


    private UserService userService;
    private UserDao userDao;

    @BeforeMethod
    public void setUp() throws Exception {
        userDao = mock(UserDao.class);
        userService = new TransactionalUserService(userDao);
    }

    @Test
    public void testGetByUsername() throws Exception {

        User user = new User();
        user.setUsername(USERNAME);
        when(userDao.getByUsername(USERNAME)).thenReturn(user);

        User result = userService.getByUsername(USERNAME);

        Assert.assertEquals(USERNAME, result.getUsername(), "Username not equals");
        verify(userDao, times(1)).getByUsername(USERNAME);
    }

    @Test(expectedExceptions = UsernameNotFoundException.class)
    public void testGetByUsernameNotFound() throws Exception {
        when(userDao.getByUsername(USERNAME)).thenReturn(null);

        userService.getByUsername(USERNAME);
    }

    @Test
    public void testRegisterUser() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(false);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(false);

        userService.registerUser(USERNAME, EMAIL, FIRST_NAME, LAST_NAME,
                PASSWORD);

        verify(userDao, times(1)).isUserWithEmailExist(EMAIL);
        verify(userDao, times(1)).isUserWithUsernameExist(USERNAME);
        verify(userDao, times(1)).saveOrUpdate(Matchers.<User>any());
    }

    @Test(expectedExceptions = {DuplicateException.class})
    public void testRegisterUserUsernameExist() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(false);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(true);

        userService.registerUser(USERNAME, EMAIL, FIRST_NAME, LAST_NAME,
                PASSWORD);
    }

    @Test(expectedExceptions = {DuplicateException.class})
    public void testRegisterUserEmailExist() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(true);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(false);

        userService.registerUser(USERNAME, EMAIL, FIRST_NAME, LAST_NAME,
                PASSWORD);
    }

    @Test(expectedExceptions = {DuplicateException.class})
    public void testRegisterUserBothExist() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(true);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(true);

        userService.registerUser(USERNAME, EMAIL, FIRST_NAME, LAST_NAME,
                PASSWORD);
    }

    @Test
    public void deleteByIdTest() {
        userService.delete(USER_ID);
        verify(userDao, times(1)).delete(Matchers.anyLong());
    }

    @Test
    public void getByIdTest() {
        when(userDao.get(USER_ID)).thenReturn(getUser());
        User user = userService.get(USER_ID);
        Assert.assertEquals(user, getUser(), "Users aren't equals");
        verify(userDao, times(1)).get(Matchers.anyLong());
    }

    @Test
    public void getAllTest() {
        List<User> expectedUserList = new ArrayList<User>();
        expectedUserList.add(getUser());
        when(userDao.getAll()).thenReturn(expectedUserList);
        List<User> actualUserList = userService.getAll();
        Assert.assertEquals(actualUserList, expectedUserList, "User lists aren't equals");
        verify(userDao, times(1)).getAll();
    }

    private User getUser() {<commit_msg>#JC-29 tests for registerUser changed after refactoring<commit_after>package org.jtalks.jcommune.service.transactional;

import org.jtalks.jcommune.model.dao.UserDao;
import org.jtalks.jcommune.model.entity.User;
import org.jtalks.jcommune.service.UserService;
import org.jtalks.jcommune.service.exceptions.DuplicateException;
import org.mockito.Matchers;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.*;

/**
 * @author <NAME>
 * @author <NAME>
 */
public class TransactionalUserServiceTest {
    private static final String USERNAME = "username";
    private static final String EMAIL = "<EMAIL>";
    private static final String PASSWORD = "password";
    private static final String FIRST_NAME = "first name";
    private static final String LAST_NAME = "last name";
    private static final Long USER_ID = 999L;


    private UserService userService;
    private UserDao userDao;

    @BeforeMethod
    public void setUp() throws Exception {
        userDao = mock(UserDao.class);
        userService = new TransactionalUserService(userDao);
    }

    @Test
    public void testGetByUsername() throws Exception {

        User user = new User();
        user.setUsername(USERNAME);
        when(userDao.getByUsername(USERNAME)).thenReturn(user);

        User result = userService.getByUsername(USERNAME);

        Assert.assertEquals(USERNAME, result.getUsername(), "Username not equals");
        verify(userDao, times(1)).getByUsername(USERNAME);
    }

    @Test(expectedExceptions = UsernameNotFoundException.class)
    public void testGetByUsernameNotFound() throws Exception {
        when(userDao.getByUsername(USERNAME)).thenReturn(null);

        userService.getByUsername(USERNAME);
    }

    @Test
    public void testRegisterUser() throws Exception {
        User user = getUser();
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(false);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(false);

        userService.registerUser(user);

        verify(userDao, times(1)).isUserWithEmailExist(EMAIL);
        verify(userDao, times(1)).isUserWithUsernameExist(USERNAME);
        verify(userDao, times(1)).saveOrUpdate(user);
    }

    @Test(expectedExceptions = {DuplicateException.class})
    public void testRegisterUserUsernameExist() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(false);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(true);

        userService.registerUser(getUser());
    }

    @Test(expectedExceptions = {DuplicateException.class})
    public void testRegisterUserEmailExist() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(true);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(false);

        userService.registerUser(getUser());
    }

    @Test(expectedExceptions = {DuplicateException.class})
    public void testRegisterUserBothExist() throws Exception {
        when(userDao.isUserWithEmailExist(EMAIL)).thenReturn(true);
        when(userDao.isUserWithUsernameExist(USERNAME)).thenReturn(true);

        userService.registerUser(getUser());
    }

    @Test
    public void deleteByIdTest() {
        userService.delete(USER_ID);
        verify(userDao, times(1)).delete(Matchers.anyLong());
    }

    @Test
    public void getByIdTest() {
        when(userDao.get(USER_ID)).thenReturn(getUser());
        User user = userService.get(USER_ID);
        Assert.assertEquals(user, getUser(), "Users aren't equals");
        verify(userDao, times(1)).get(Matchers.anyLong());
    }

    @Test
    public void getAllTest() {
        List<User> expectedUserList = new ArrayList<User>();
        expectedUserList.add(getUser());
        when(userDao.getAll()).thenReturn(expectedUserList);
        List<User> actualUserList = userService.getAll();
        Assert.assertEquals(actualUserList, expectedUserList, "User lists aren't equals");
        verify(userDao, times(1)).getAll();
    }

    private User getUser() {
{'fraction_non_alphanumeric': 0.11022253129346314, 'fraction_numerical': 0.0025498377375985167, 'mean_word_length': 3.8778971170152627, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4447590', 'n_tokens_mistral': 2519, 'n_tokens_neox': 2498, 'n_words': 447}