cz.swi2.mendeluis.service.FavouriteItemServiceTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.swi2.mendeluis.service.FavouriteItemServiceTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.swi2.mendeluis.service;

import cz.swi2.mendeluis.dataaccesslayer.domain.FavouriteItem;
import cz.swi2.mendeluis.service.config.ServiceConfiguration;
import cz.swi2.mendeluis.service.exception.UniqueViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.log4j.varia.NullAppender;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Michal
 */
@ContextConfiguration(classes = ServiceConfiguration.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class FavouriteItemServiceTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private FavouriteItemService favService;

    public FavouriteItemServiceTest() {
        org.apache.log4j.BasicConfigurator.configure(new NullAppender());
    }

    /**
     * Create test
     */
    @org.testng.annotations.Test
    public void testCreate() {
        FavouriteItem favi;
        try {
            // Create FavouriteItem
            favi = this.favService.createNewFavouriteItem("Test1");

            // Find FavouriteItem
            FavouriteItem result = this.favService.getFavouriteItemById(favi.getId());

            // assert equal
            assertEquals(favi, result);

        } catch (UniqueViolationException ex) {
            Logger.getLogger(FavouriteItemServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    /**
     * Get all test
     */
    @org.testng.annotations.Test
    public void testGetAll() {
        FavouriteItem fav1, fav2;
        try {
            // Create FavouriteItem
            fav1 = this.favService.createNewFavouriteItem("TestA");
            fav2 = this.favService.createNewFavouriteItem("TestB");

            // Find FavouriteItem
            List<FavouriteItem> results = this.favService.getAllFavouriteItems();

            // assert equal
            List<FavouriteItem> favis = new ArrayList();
            favis.add(fav1);
            favis.add(fav2);

            // We do not use a different db for testing. Thefore we must test only matching items, not all. 
            assertTrue(results.containsAll(favis));

        } catch (UniqueViolationException ex) {
            Logger.getLogger(FavouriteItemServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }

    /**
     * Delete test
     */
    @org.testng.annotations.Test
    public void testDelete() {
        FavouriteItem favi;
        try {
            // Create FavouriteItem
            favi = this.favService.createNewFavouriteItem("test");

            // Delete FavouriteItem
            int id = favi.getId();
            this.favService.removeFavouriteItem(favi);

            // Find FavouriteItem
            FavouriteItem result = this.favService.getFavouriteItemById(id);

            // assert equal
            assertEquals(result, null);

        } catch (UniqueViolationException ex) {
            Logger.getLogger(FavouriteItemServiceTest.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
        }
    }
}