com.wickettasks.business.entities.tasklist.repository.TestTaskListRepository.java Source code

Java tutorial

Introduction

Here is the source code for com.wickettasks.business.entities.tasklist.repository.TestTaskListRepository.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.tasklist.repository;

import static org.junit.Assert.assertEquals;

import java.util.List;

import org.junit.After;
import org.junit.Before;
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.tasklist.TaskList;
import com.wickettasks.business.entities.user.User;
import com.wickettasks.business.entities.user.repository.UserRepository;

public class TestTaskListRepository extends AbstractWicketTasksTests {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TaskListRepository taskListRepository;

    private User user;

    @Before
    public void createUser() {
        this.user = new User();
        this.user.setPassword("password");
        this.user.setEmail("test@email.com");
        this.userRepository.save(this.user);
    }

    @After
    public void deleteUser() {
        this.userRepository.remove(this.user);
    }

    @Test
    public void testSaveTaskList() {
        TaskList taskList = new TaskList();
        taskList.setName("testTaskList");
        taskList.setUser(this.user);
        this.taskListRepository.save(taskList);
        assertEquals(Boolean.TRUE, Boolean.valueOf(taskList.getId() != null));
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void nameAndUserMustBeSetBeforeSavingATaskList() {
        TaskList taskList = new TaskList();
        this.taskListRepository.save(taskList);
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void anUserMustBeSetBeforeSavingATaskList() {
        TaskList taskList = new TaskList();
        taskList.setName("testTaskList");
        this.taskListRepository.save(taskList);
    }

    @Test(expected = DataIntegrityViolationException.class)
    public void anNameMustBeSetBeforeSavingATaskList() {
        TaskList taskList = new TaskList();
        taskList.setUser(this.user);
        this.taskListRepository.save(taskList);
    }

    @Test
    public void aTaskListMustExistBeforeGettingIt() {
        TaskList nonExistingTaskList = this.taskListRepository.get(Integer.valueOf(1000));
        assertEquals(null, nonExistingTaskList);
        TaskList taskList = new TaskList();
        taskList.setName("testTaskList");
        taskList.setUser(this.user);
        this.taskListRepository.save(taskList);
        TaskList existingTaskList = this.taskListRepository.get(taskList.getId());
        assertEquals(taskList, existingTaskList);
    }

    @Test
    public void anExistingTaskListCanBeRemoved() {
        TaskList taskList = new TaskList();
        taskList.setName("testTaskList");
        taskList.setUser(this.user);
        this.taskListRepository.save(taskList);
        Integer taskListId = taskList.getId();
        this.taskListRepository.remove(taskList);
        TaskList removedTaskList = this.taskListRepository.get(taskListId);
        assertEquals(null, removedTaskList);

    }

    @Test
    public void testFindTaskListsByUserId() {
        TaskList taskList = new TaskList();
        taskList.setName("testTaskList");
        taskList.setUser(this.user);
        this.taskListRepository.save(taskList);
        TaskList taskList2 = new TaskList();
        taskList2.setName("testTaskList2");
        taskList2.setUser(this.user);
        this.taskListRepository.save(taskList2);
        List<TaskList> taskListList = this.taskListRepository.findByUserId(this.user.getId());
        assertEquals(Boolean.TRUE, Boolean.valueOf(taskListList.size() == 2));
    }

    @Test
    public void findTaskListsByUserIdReturnsAnEmptyListForNonExistingUsers() {
        assertEquals(Boolean.TRUE,
                Boolean.valueOf(this.taskListRepository.findByUserId(Integer.valueOf(1000)).isEmpty()));
    }

    @Test
    public void testFindTaskListByTaskListIdAndUserId() {
        TaskList taskList = new TaskList();
        taskList.setName("testTaskList");
        taskList.setUser(this.user);
        this.taskListRepository.save(taskList);
        TaskList foundTaskList = this.taskListRepository.findByTaskListIdAndUserId(taskList.getId(),
                this.user.getId());
        assertEquals(taskList, foundTaskList);
    }

}