package org.sagebionetworks.bridge.redis;

import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.when;

import org.junit.Test;
import org.mockito.InOrder;
import org.sagebionetworks.bridge.lock.LockNotAvailableException;

public class RedisLockTest {

    @Test
    public void testAcquireLock() {
        JedisOps ops = mock(JedisOps.class);
        // 1 -- indicates lock acquired
        when(ops.setnx(eq("key"), anyString())).thenReturn(1L);
        RedisLock lock = new RedisLock(ops);
        lock.acquireLock("key", 10);
        InOrder inOrder = inOrder(ops);
        inOrder.verify(ops, times(1)).setnx(eq("key"), anyString());
        inOrder.verify(ops, times(1)).expire("key", 10);
    }

    @Test
    public void testAcquireLockWithLockNotAvailableException() {
        JedisOps ops = mock(JedisOps.class);
        // 0 -- indicates lock acquiring failed
        when(ops.setnx(eq("key"), anyString())).thenReturn(0L);
        when(ops.ttl("key")).thenReturn(30L);
        RedisLock lock = new RedisLock(ops);
        try {
            lock.acquireLock("key", 10);
        } catch (LockNotAvailableException e) {
            assertTrue("LockNotAvailableException expected", true);
        }
        InOrder inOrder = inOrder(ops);
        inOrder.verify(ops, times(1)).setnx(eq("key"), anyString());
        inOrder.verify(ops, times(1)).expire("key", 10);
    }
}

@@ -36,16 +36,60 @@
 redis;%0A%0A
+import static org.junit.Assert.assertFalse;%0A
 import s
@@ -325,21 +325,104 @@
 Mockito.
-times
+never;%0Aimport static org.mockito.Mockito.times;%0Aimport static org.mockito.Mockito.verify
 ;%0Aimport
@@ -1729,11 +1729,1129 @@
 );%0A    %7D
+%0A%0A    @Test%0A    public void testReleaseLockSucceeded() %7B%0A        JedisOps ops = mock(JedisOps.class);%0A        when(ops.get(%22key%22)).thenReturn(%22lock%22);%0A        when(ops.del(%22key%22)).thenReturn(1L);%0A        RedisLock lock = new RedisLock(ops);%0A        assertTrue(lock.releaseLock(%22key%22, %22lock%22));%0A        InOrder inOrder = inOrder(ops);%0A        inOrder.verify(ops, times(1)).get(%22key%22);%0A        inOrder.verify(ops, times(1)).del(%22key%22);%0A    %7D%0A%0A    @Test%0A    public void testReleaseLockFailed() %7B%0A        JedisOps ops = mock(JedisOps.class);%0A        when(ops.get(%22key%22)).thenReturn(%22lock%22);%0A        when(ops.del(%22key%22)).thenReturn(1L);%0A        RedisLock lock = new RedisLock(ops);%0A        assertFalse(lock.releaseLock(%22key%22, %22lockNotOwnedByMe%22));%0A        verify(ops, never()).del(%22key%22);%0A    %7D%0A%0A    @Test(expected = RedisException.class)%0A    public void testReleaseLockWithRedisException() %7B%0A        JedisOps ops = mock(JedisOps.class);%0A        when(ops.get(%22key%22)).thenReturn(%22lock%22);%0A        when(ops.del(%22key%22)).thenReturn(0L);%0A        RedisLock lock = new RedisLock(ops);%0A        lock.releaseLock(%22key%22, %22lock%22);%0A    %7D
 %0A%7D%0A

Add test for releasing lock