cz.swi2.mendeluis.dataaccesslayer.domain.UserFavouriteItemTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.swi2.mendeluis.dataaccesslayer.domain.UserFavouriteItemTest.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.dataaccesslayer.domain;

import cz.swi2.mendeluis.dataaccesslayer.core.DatabaseConfig;
import cz.swi2.mendeluis.dataaccesslayer.repository.FavouriteItemRepository;
import cz.swi2.mendeluis.dataaccesslayer.repository.UserFavouriteItemRepository;
import cz.swi2.mendeluis.dataaccesslayer.repository.UserRepository;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.varia.NullAppender;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
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 = DatabaseConfig.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class UserFavouriteItemTest extends AbstractTestNGSpringContextTests {

    @Autowired
    private UserFavouriteItemRepository userFavouriteItemRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private FavouriteItemRepository favouriteItemRepository;

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

    @org.testng.annotations.Test
    public void testCreate() {
        // Create userFavItem
        FavouriteItem favItem = new FavouriteItem("Rozvrh");
        User user = new User("Pepa", "xpepa", "heslo");
        UserFavouriteItem userFavItem = new UserFavouriteItem(user, favItem, 0);

        // Insert favItem
        this.userRepository.insert(user);
        this.favouriteItemRepository.insert(favItem);
        this.userFavouriteItemRepository.insert(userFavItem);

        // Find userFavItem
        UserFavouriteItem result = this.userFavouriteItemRepository.getById(userFavItem.getId());

        // asser equal
        assertEquals(userFavItem, result);
    }

    @org.testng.annotations.Test
    public void testUpdate() {
        // Create userFavItem
        FavouriteItem favItem = new FavouriteItem("Rozvrh");
        User user = new User("Pepa", "xpepa", "heslo");
        UserFavouriteItem userFavItem = new UserFavouriteItem(user, favItem, 0);

        // Insert favItem
        this.userRepository.insert(user);
        this.favouriteItemRepository.insert(favItem);
        this.userFavouriteItemRepository.insert(userFavItem);

        // try to update
        userFavItem.setName("Rozvrh");
        this.userFavouriteItemRepository.update(userFavItem);

        // Find userFavItem again
        UserFavouriteItem result = this.userFavouriteItemRepository.getById(userFavItem.getId());

        // Assert updated value
        assertEquals("Rozvrh", result.getName());
    }

    @org.testng.annotations.Test
    public void testDelete() {
        // Create userFavItem
        FavouriteItem favItem = new FavouriteItem("Rozvrh");
        User user = new User("Pepa", "xpepa", "heslo");
        UserFavouriteItem userFavItem = new UserFavouriteItem(user, favItem, 0);

        // Insert favItem
        this.userRepository.insert(user);
        this.favouriteItemRepository.insert(favItem);
        this.userFavouriteItemRepository.insert(userFavItem);

        // try to delete
        this.userFavouriteItemRepository.delete(userFavItem);

        // Find result
        UserFavouriteItem result = this.userFavouriteItemRepository.getById(userFavItem.getId());

        // Assert updated value
        assertNull(result);
    }

    @org.testng.annotations.Test
    public void testGetAll() {
        // Create entities
        User u = new User("michal", "michal", "pw123456");
        FavouriteItem f1 = new FavouriteItem("Food menu xx");
        FavouriteItem f2 = new FavouriteItem("Food menu x2");
        UserFavouriteItem uf1 = new UserFavouriteItem(u, f1, 0);
        UserFavouriteItem uf2 = new UserFavouriteItem(u, f2, 0);

        // Insert 
        this.userRepository.insert(u);
        this.favouriteItemRepository.insert(f1);
        this.favouriteItemRepository.insert(f2);
        this.userFavouriteItemRepository.insert(uf1);
        this.userFavouriteItemRepository.insert(uf2);

        // Fetch all user userFavouriteItems
        List<UserFavouriteItem> results = this.userFavouriteItemRepository.getAll();

        // Prepare a list to be compared
        List<UserFavouriteItem> favi = new ArrayList();
        favi.add(uf1);
        favi.add(uf2);

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

    @org.testng.annotations.Test
    public void testGetAllByUser() {
        // Create entities
        User u = new User("michal", "michal", "pw123456");
        FavouriteItem f1 = new FavouriteItem("Food menu xx");
        FavouriteItem f2 = new FavouriteItem("Food menu x2");
        UserFavouriteItem uf1 = new UserFavouriteItem(u, f1, 0);
        UserFavouriteItem uf2 = new UserFavouriteItem(u, f2, 0);

        // Insert 
        this.userRepository.insert(u);
        this.favouriteItemRepository.insert(f1);
        this.favouriteItemRepository.insert(f2);
        this.userFavouriteItemRepository.insert(uf1);
        this.userFavouriteItemRepository.insert(uf2);

        // Fetch all user userFavouriteItems
        List<UserFavouriteItem> results = this.userFavouriteItemRepository.getAllByUser(u);

        // Prepare a list to be compared
        List<UserFavouriteItem> favi = new ArrayList();
        favi.add(uf1);
        favi.add(uf2);

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