com.bcknds.demo.mongo.TodoServiceTests.java Source code

Java tutorial

Introduction

Here is the source code for com.bcknds.demo.mongo.TodoServiceTests.java

Source

/**
 * Copyright 2014 Michael Brush
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.bcknds.demo.mongo;

import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.bcknds.demo.mongo.model.Todo;
import com.bcknds.demo.mongo.repository.TodoRepository;
import com.bcknds.demo.mongo.service.TodoService;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

/**
 * @author Michael Brush
 *
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Application.class)
public class TodoServiceTests {

    private static final Logger LOG = LoggerFactory.getLogger(TodoServiceTests.class);

    private Todo trash;
    private Todo dishes;

    @Autowired
    private TodoService todoService;

    @Autowired
    private TodoRepository todoRepository;

    @Before
    public void setUp() {
        trash = new Todo("Take out the trash", "Take out the trash", new Date());
        dishes = new Todo("Do the dishes", "Do the dishes", new Date());
        try {
            todoRepository.deleteAll();
        } catch (DataAccessResourceFailureException ex) {
            LOG.info("Unable to connect to Mongo");
            System.exit(1);
        } catch (Exception ex) {
            LOG.info(ex.getMessage());
            System.exit(1);
        }
    }

    @After
    public void tearDown() {
        trash = null;
        dishes = null;
        todoRepository.deleteAll();
    }

    @Test
    public void testCreateTodo() {
        assertNull(trash.getId());
        Todo result = todoService.create(trash);
        assertNotNull("The id was not generated", result.getId());
    }

    @Test
    public void testFindAllTodos() {
        todoRepository.save(trash);
        todoRepository.save(dishes);
        List<Todo> todos = todoService.findAll();
        assertEquals(String.format("Expected 2. Got %s", todos.size()), 2, todos.size());
    }

    @Test
    public void testFindOne() {
        trash = todoRepository.save(trash);
        Todo result = todoService.findOne(trash.getId());
        assertEquals("The result does not match the original", trash, result);
    }

    @Test
    public void testUpdateTodo() {
        trash = todoRepository.save(trash);
        trash.setSummary("I refuse to take out the trash");
        todoService.update(trash);
        Todo result = todoService.findOne(trash.getId());
        assertEquals("The result summary does not match the new summary", "I refuse to take out the trash",
                result.getSummary());
    }

    @Test
    public void testDeleteTodo() {
        trash = todoRepository.save(trash);
        Todo result = todoRepository.findOne(trash.getId());
        assertEquals("The orginal and the result are not equal.", trash, result);
        todoService.remove(trash);
        result = todoRepository.findOne(trash.getId());
        assertNull("The result of the find should be null.", result);
    }

    @Test
    public void testDeleteTodoById() {
        trash = todoRepository.save(trash);
        Todo result = todoRepository.findOne(trash.getId());
        assertEquals("The orginal and the result are not equal.", trash, result);
        todoService.remove(trash.getId());
        result = todoRepository.findOne(trash.getId());
        assertNull("The result of the find should be null.", result);
    }
}