package cz.muni.fi.pa165.soccermanager.repository;

import cz.muni.fi.pa165.soccermanager.PersistenceApplication;
import cz.muni.fi.pa165.soccermanager.entity.User;
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.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import static org.assertj.core.api.Assertions.assertThat;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertTrue;

/**
 * Team entity class
 *
 * @author Petr Adamec (uco: 485584)
 */
@ContextConfiguration(classes = PersistenceApplication.class)
@TestExecutionListeners(TransactionalTestExecutionListener.class)
@Transactional
public class UserRepositoryTest extends AbstractTestNGSpringContextTests {

    @PersistenceContext
    protected EntityManager em;

    @Autowired
    private UserRepository userRepository;

    private User userNick;
    private User userIan;

    @BeforeMethod
    public void setUp() {
        userNick = new User();
        userNick.setNickName("nick");
        userNick.setPassword("1234");
        userIan = new User();
        userIan.setNickName("ian");
        userIan.setPassword("strongpass");

        em.persist(userNick);
        em.persist(userIan);
    }

    @Test
    public void createTest() {
        final var u = new User();
        u.setPassword("pass");
        u.setNickName("big U");
        userRepository.save(u);
        final var createdUser = em.find(User.class, u.getId());
        assertThat(createdUser).isEqualTo(u);
    }

    @Test
    public void findByIdTest() {
        final var u = userRepository.findById(userNick.getId());
        assertTrue(u.isPresent());
        assertThat(u.get()).isEqualTo(userNick);

        final var u2 = userRepository.findById(100L);
        assertFalse(u2.isPresent());
    }

    @Test
    public void findAllTest() {
        final var users = userRepository.findAll();
        assertThat(users).hasSize(2).containsExactly(userNick, userIan);
    }

    @Test
    public void findByNickTest() {
        final var user = userRepository.findByNickname(userIan.getNickName());
        assertTrue(user.isPresent());
        assertThat(user.get()).isEqualTo(userIan);
    }


    @Test
    public void removeTest() {
        final var tmpUser = new User();
        tmpUser.setNickName("tmp");
        tmpUser.setPassword("tmp");
        em.persist(tmpUser);
        final var tmpUserFound = userRepository.findById(tmpUser.getId());
        assertTrue(tmpUserFound.isPresent());
        assertThat(tmpUserFound.get()).isEqualTo(tmpUser);

        userRepository.delete(tmpUser);
        final var none = userRepository.findById(tmpUser.getId());
        assertFalse(none.isPresent());
    }
}


