package com.atlassian.stash.internal.concurrent;

import com.atlassian.bitbucket.concurrent.RepositoryLock;
import com.atlassian.bitbucket.repository.Repository;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class ClusterRepositoryLockTest extends AbstractLockTest {
    @Mock
    private Repository repository;
    private RepositoryLock repositoryLock;
    @Captor
    private ArgumentCaptor<String> lockNameCaptor;

    @Test
    public void testLockNameForRepoId() {
        repositoryLock = new ClusterRepositoryLock(lockService, "testing");

        assertEquals("test", repositoryLock.withLock(9, TEST_OP));
        verify(lockService).getLockForName(lockNameCaptor.capture());

        String lockName = lockNameCaptor.getValue();
        assertTrue(lockName.endsWith("testing#9"));
    }

    @Test
    public void testLockNameForRepository() {
        when(repository.getId()).thenReturn(11);
        repositoryLock = new ClusterRepositoryLock(lockService, "testing");

        assertEquals("test", repositoryLock.withLock(repository, TEST_OP));
        verify(lockService).getLockForName(lockNameCaptor.capture());

        String lockName = lockNameCaptor.getValue();
        assertTrue(lockName.endsWith("testing#11"));
    }
}
