com.wickettasks.business.services.task.TestTaskService.java Source code

Java tutorial

Introduction

Here is the source code for com.wickettasks.business.services.task.TestTaskService.java

Source

/*
 * TestTaskService.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.services.task;

import static org.junit.Assert.assertEquals;

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

import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.wickettasks.business.AbstractWicketTasksTests;
import com.wickettasks.business.entities.task.Task;
import com.wickettasks.business.entities.tasklist.TaskList;
import com.wickettasks.business.entities.user.User;
import com.wickettasks.business.exceptions.user.AccessRestrictionException;
import com.wickettasks.business.exceptions.user.ExistingUserException;
import com.wickettasks.business.services.tasklist.TaskListService;
import com.wickettasks.business.services.user.UserService;

public class TestTaskService extends AbstractWicketTasksTests {

    @Autowired
    private UserService userService;

    @Autowired
    private TaskListService taskListService;

    @Autowired
    private TaskService taskService;

    private User user;
    private TaskList taskList;

    @Before
    public void addUserAndTaskList() {
        try {
            this.user = this.userService.add("test@email.com", "password");
        } catch (ExistingUserException e) {
            throw new InternalError(e.toString());
        }
        this.taskList = this.taskListService.add("testTaskList", this.user.getId());
    }

    @Test
    public void testAddTask() {
        try {
            Task task = this.taskService.add("adadadadad", this.taskList.getId(), this.user.getId());
            Task foundTask = this.taskService.findByTaskIdAndUserId(task.getId(), this.user.getId());
            assertEquals(task, foundTask);
        } catch (AccessRestrictionException e) {
            throw new AssertionError(e);
        }
    }

    @Test
    public void testCompleteTask() {
        try {
            Task task = this.taskService.add("adadadadad", this.taskList.getId(), this.user.getId());
            this.taskService.complete(task.getId(), this.user.getId());
            Task foundTask = this.taskService.findByTaskIdAndUserId(task.getId(), this.user.getId());
            assertEquals(task, foundTask);
            assertEquals(Boolean.TRUE, foundTask.getCompleted());
            assertEquals(Boolean.TRUE, task.getCompleted());
        } catch (AccessRestrictionException e) {
            throw new AssertionError(e);
        }
    }

    @Test
    public void testDeleteTask() {
        try {
            Task task = this.taskService.add("adadadadad", this.taskList.getId(), this.user.getId());
            Integer taskId = task.getId();
            this.taskService.delete(taskId, this.user.getId());
            List<Task> userTasks = this.taskService.findByTaskListIdAndUserId(this.taskList.getId(),
                    this.user.getId());
            assertEquals(Boolean.TRUE, Boolean.valueOf(userTasks.isEmpty()));
        } catch (AccessRestrictionException e) {
            throw new AssertionError(e);
        }
    }

    @Test
    public void testFindTasksByTaskListIdAndUserId() {
        try {
            for (int i = 0; i < 100; i++) {
                this.taskService.add(String.valueOf(i), this.taskList.getId(), this.user.getId());
            }
            List<Task> task = this.taskService.findByTaskListIdAndUserId(this.taskList.getId(), this.user.getId());
            assertEquals(Integer.valueOf(100), Integer.valueOf(task.size()));
        } catch (AccessRestrictionException e) {
            throw new AssertionError(e);
        }
    }

    @Test
    public void testFindTaskByTaskIdAndUserId() {
        try {
            List<Integer> taskIdList = new ArrayList<Integer>();
            for (int i = 0; i < 100; i++) {
                Task task = this.taskService.add(String.valueOf(i), this.taskList.getId(), this.user.getId());
                taskIdList.add(task.getId());
            }

            for (Integer taskId : taskIdList) {
                Task task = this.taskService.findByTaskIdAndUserId(taskId, this.user.getId());
                assertEquals(taskId, task.getId());
                assertEquals(task.getTaskList(), this.taskList);
                assertEquals(task.getTaskList().getUser(), this.user);
            }
        } catch (AccessRestrictionException e) {
            throw new AssertionError(e);
        }
    }

}