package com.atlassian.stash.internal.activity;

import org.hibernate.dialect.Dialect;
import org.hibernate.dialect.MySQLDialect;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ActivityLockFactoryBeanTest {

    @InjectMocks
    private ActivityLockFactoryBean factoryBean;
    @Mock
    private SessionFactoryImplementor sessionFactory;

    @Test
    public void testCreateInstance() {
        when(sessionFactory.getDialect()).thenReturn(mock(Dialect.class));

        ActivityLock lock = factoryBean.createInstance();
        assertNotNull(lock);
        assertTrue(lock instanceof NoOpActivityLock);
    }

    @Test
    public void testCreateForMySql() {
        when(sessionFactory.getDialect()).thenReturn(mock(MySQLDialect.class));

        ActivityLock lock = factoryBean.createInstance();
        assertNotNull(lock);
        assertTrue(lock instanceof MySqlActivityLock);
    }
}