package org.sagebionetworks.repo.model.dbo.dao;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Matchers;
import org.mockito.Mockito;

import static org.mockito.Mockito.*;

import org.sagebionetworks.repo.model.UserGroupDAO;
import org.sagebionetworks.repo.model.dbo.DBOBasicDao;
import org.sagebionetworks.util.Clock;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.test.util.ReflectionTestUtils;

public class DBOAuthenticationDAOImplUnitTest {

	JdbcTemplate mockJdbcTemplate;
	UserGroupDAO mockUserGroupDao;
	DBOBasicDao mockBasicDao;
	Clock mockClock;
	DBOAuthenticationDAOImpl authDao;
	long principalId;
	
	@Before
	public void before(){
		mockJdbcTemplate = Mockito.mock(JdbcTemplate.class);
		mockUserGroupDao = Mockito.mock(UserGroupDAO.class);
		mockBasicDao = Mockito.mock(DBOBasicDao.class);
		mockClock = Mockito.mock(Clock.class);
		authDao = new DBOAuthenticationDAOImpl();
		ReflectionTestUtils.setField(authDao, "jdbcTemplate", mockJdbcTemplate);
		ReflectionTestUtils.setField(authDao, "userGroupDao", mockUserGroupDao);
		ReflectionTestUtils.setField(authDao, "basicDao", mockBasicDao);
		ReflectionTestUtils.setField(authDao, "clock", mockClock);
		
		principalId = 789;
	}
	
	/**
	 * For this case the token was last updated one millisecond ago, so there is no need to revaliate it again.
	 * See: PLFM-3206 & PLFM-3202
	 */
	@Test
	public void testRevaliateNotNeeded(){
		long lastValidate = 0;
		long now = lastValidate + 1L;
		when(mockClock.currentTimeMillis()).thenReturn(now);
		when(mockJdbcTemplate.queryForObject(any(String.class), Matchers.<RowMapper<Long>>any(),Matchers.<Object>anyVararg())).thenReturn(lastValidate);
		// call under test
		assertFalse("The token did not need to be validated yet",authDao.revalidateSessionTokenIfNeeded(principalId));
		verify(mockUserGroupDao, never()).touch(anyLong());
		verify(mockJdbcTemplate, never()).update(anyString(), any(MapSqlParameterSource.class));
	}
	
	/**
	 * For this case the token was last updated one millisecond past the half-life of a session token, so
	 * we need to revalidate it.
	 */
	@Test
	public void testRevaliateNeeded(){
		long lastValidate = 1;
		long now = lastValidate + DBOAuthenticationDAOImpl.HALF_SESSION_EXPIRATION+1L;
		when(mockClock.currentTimeMillis()).thenReturn(now);
		when(mockJdbcTemplate.queryForObject(any(String.class), Matchers.<RowMapper<Long>>any(),Matchers.<Object>anyVararg())).thenReturn(lastValidate);
		// call under test
		assertTrue("The token needed to be revalidated",authDao.revalidateSessionTokenIfNeeded(principalId));
		verify(mockUserGroupDao).touch(principalId);
		verify(mockJdbcTemplate).update(anyString(), Matchers.<Object>anyVararg());
	}
}

@@ -146,24 +146,117 @@
 org.
-mockito.Matchers
+junit.runner.RunWith;%0D%0Aimport org.mockito.InjectMocks;%0D%0Aimport org.mockito.Matchers;%0D%0Aimport org.mockito.Mock
 ;%0D%0Ai
@@ -321,24 +321,72 @@
 ckito.*;%0D%0A%0D%0A
+import org.mockito.runners.MockitoJUnitRunner;%0D%0A
 import org.s
@@ -756,16 +756,52 @@
 ils;%0D%0A%0D%0A
+@RunWith(MockitoJUnitRunner.class)%0D%0A
 public c
@@ -843,16 +843,24 @@
 st %7B%0D%0A%0D%0A
+%09@Mock%0D%0A
 %09JdbcTem
@@ -884,16 +884,24 @@
 plate;%0D%0A
+%09@Mock%0D%0A
 %09UserGro
@@ -925,16 +925,24 @@
 upDao;%0D%0A
+%09@Mock%0D%0A
 %09DBOBasi
@@ -961,16 +961,24 @@
 icDao;%0D%0A
+%09@Mock%0D%0A
 %09Clock m
@@ -987,16 +987,33 @@
 kClock;%0D
+%0A%0D%0A%09@InjectMocks%0D
 %0A%09DBOAut
@@ -1074,16 +1074,16 @@
 Before%0D%0A
+
 %09public 
@@ -1102,544 +1102,8 @@
 )%7B%0D%0A
-%09%09mockJdbcTemplate = Mockito.mock(JdbcTemplate.class);%0D%0A%09%09mockUserGroupDao = Mockito.mock(UserGroupDAO.class);%0D%0A%09%09mockBasicDao = Mockito.mock(DBOBasicDao.class);%0D%0A%09%09mockClock = Mockito.mock(Clock.class);%0D%0A%09%09authDao = new DBOAuthenticationDAOImpl();%0D%0A%09%09ReflectionTestUtils.setField(authDao, %22jdbcTemplate%22, mockJdbcTemplate);%0D%0A%09%09ReflectionTestUtils.setField(authDao, %22userGroupDao%22, mockUserGroupDao);%0D%0A%09%09ReflectionTestUtils.setField(authDao, %22basicDao%22, mockBasicDao);%0D%0A%09%09ReflectionTestUtils.setField(authDao, %22clock%22, mockClock);%0D%0A%09%09%0D%0A
 %09%09pr

fix broken test caused by IDE variable rename