be.bittich.quote.service.impl.AuthorServiceImplTest.java Source code

Java tutorial

Introduction

Here is the source code for be.bittich.quote.service.impl.AuthorServiceImplTest.java

Source

/*
 * Copyright 2014 nateriver.
 *
 * 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 be.bittich.quote.service.impl;

import be.bittich.quote.config.SpringConfigForTest;
import be.bittich.quote.dao.AuthorDAO;
import be.bittich.quote.model.Author;
import be.bittich.quote.model.Quote;
import be.bittich.quote.model.User;
import be.bittich.quote.service.AuthorService;
import be.bittich.quote.service.QuoteService;
import be.bittich.quote.service.UserService;
import static be.bittich.quote.service.impl.QuoteServiceImplTest.createQuote;
import static be.bittich.quote.service.impl.UserDetailsServiceImplTest.createUser;
import be.bittich.quote.vo.LastnameFirstnameVO;
import java.util.List;
import javax.transaction.Transactional;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

//import org.hsqldb.jdbc.;
/**
 *
 * @author nateriver
 */
@ContextHierarchy({ @ContextConfiguration(classes = { SpringConfigForTest.class }) })
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class AuthorServiceImplTest {

    @Autowired
    private QuoteService quoteService;

    @Autowired
    private AuthorService authorService;

    @Autowired
    private UserService userService;

    private Author author;

    protected static Author createAuthor() {
        Author aut = new Author();
        aut.setFirstname("jean");
        aut.setLastname("pierre");
        return aut;
    }

    @Before
    public void setUp() {
        author = createAuthor();
        authorService.saveOrUpdate(author);
        assertNotNull(author.getIdauthor());
        System.out.println(author.getIdauthor());
        User user = createUser("kikoo");
        userService.insert(user);
        assertNotNull(user.getIduser());
        Quote quote = createQuote(author, user);
        quoteService.saveAndFlush(quote);
        assertNotNull(quote.getIdQuote());
    }

    @Test
    public void findByFirstnameLastname() {
        Author aut = authorService.findByFirstnameLastname("jean", "pierre");
        assertNotNull(aut);
    }

    @Test
    public void findByLastname() {
        List<Author> list = authorService.findByLastname("pierre");
        assertNotNull(list);
        assertEquals(list.size(), 1);
    }

    @Test
    public void autoCompleteLastNameFirstName() {
        List<LastnameFirstnameVO> vo = authorService.autoCompleteLastNameFirstName();
        assertNotNull(vo);
        System.out.println(vo.toString());
    }

    @Test
    public void delete() {
        this.authorService.delete(author);
        assertNotNull(author);
        System.out.println(author.getIdauthor());
        author = authorService.findOneById(author.getIdauthor());
        assertNull(author);
    }

    /**
     * Test of getRepository method, of class AuthorServiceImpl.
     */
    @Test
    public void testGetRepository() {
        assertNotNull(authorService.getRepository());
        assertThat(authorService.getRepository(), instanceOf(AuthorDAO.class));
    }

}