com.wickettasks.business.entities.user.repository.TestUserRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.wickettasks.business.entities.user.repository.TestUserRepository.java

Source

/*
 * TaskRepositoryTests.java
 * Copyright (c) 2009 Felix Cachaldora Sanchez
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
     
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
     
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
 */
package com.wickettasks.business.entities.user.repository;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;

import com.wickettasks.business.AbstractWicketTasksTests;
import com.wickettasks.business.entities.user.User;

public class TestUserRepository extends AbstractWicketTasksTests {

    @Autowired
    private UserRepository userRepository;

    @Test
    public void testSaveUser() {
        User user = new User();
        user.setEmail("test@email");
        user.setPassword("password");
        this.userRepository.save(user);
        assertEquals(Boolean.TRUE, Boolean.valueOf(user.getId() != null));
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void emailAndPasswordMustBeSetBeforeSavinganUser() {
        User user = new User();
        this.userRepository.save(user);
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void aPasswordMustBeSetBeforeSavingAnUser() {
        User user = new User();
        user.setEmail("test@email");
        this.userRepository.save(user);
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void anEmailMustBeSetBeforeSavingAnUser() {
        User user = new User();
        user.setPassword("password");
        this.userRepository.save(user);
    }

    @Test
    public void anUserMustExistBeforeGettingIt() {
        User nonExistingUser = this.userRepository.get(Integer.valueOf(1000));
        assertEquals(null, nonExistingUser);
        User user = new User();
        user.setPassword("password");
        user.setEmail("test@email.com");
        this.userRepository.save(user);
        User existingUser = this.userRepository.get(user.getId());
        assertEquals(user, existingUser);
    }

    @Test
    public void anExistingUserCanBeRemoved() {
        User user = new User();
        user.setPassword("password");
        user.setEmail("test@email.com");
        this.userRepository.save(user);
        Integer userId = user.getId();
        this.userRepository.remove(user);
        User removedUser = this.userRepository.get(userId);
        assertEquals(null, removedUser);
    }

    @Test
    public void anExistingUserCanBeFoundByEmail() {
        User user = new User();
        user.setPassword("password");
        user.setEmail("test@email.com");
        this.userRepository.save(user);
        User sameUser = this.userRepository.findByEmail(user.getEmail());
        assertEquals(user, sameUser);
    }

    @Test
    public void findUsersByEmailReturnsNullForNonExistingUsers() {
        assertEquals(null, this.userRepository.findByEmail("test@email.com"));
    }

}