package com.devproserv.courses.dao;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;

import static com.devproserv.courses.config.MainConfig.SELECT_LOGIN_SQL;
import static com.devproserv.courses.config.MainConfig.INSERT_USER_SQL;
import static com.devproserv.courses.config.MainConfig.INSERT_STUDENT_SQL;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.devproserv.courses.controller.AppContext;
import com.devproserv.courses.model.Student;
import com.devproserv.courses.model.User;

/**
 * Contains unit-tests to check functionality of {@link UserDao} class
 * 
 * @author vovas11
 *
 */
public class UserDaoTest {
    // dependencies to be mocked
    @Mock
    private AppContext appContext;
    @Mock
    private Connection connection;
    @Mock
    private PreparedStatement prepStmt;
    @Mock
    private PreparedStatement prepStmt1;
    @Mock
    private PreparedStatement prepStmt2;
    @Mock
    private ResultSet resultSet;
    @Mock
    private ResultSet resultSet1;
    @Mock
    private User user;
    @Mock
    private Student student;
    
    private UserDao userDao;
    
    // prepare dependencies
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        userDao = new UserDao(connection);
        // mocks methods of Connection
        when(connection.prepareStatement(SELECT_LOGIN_SQL)).thenReturn(prepStmt);
        when(connection.prepareStatement(INSERT_USER_SQL, Statement.RETURN_GENERATED_KEYS)).thenReturn(prepStmt1);
        when(connection.prepareStatement(INSERT_STUDENT_SQL)).thenReturn(prepStmt2);
        // mocks methods of PreparedStatement
        doNothing().when(prepStmt).setString(anyInt(), anyString());
        doNothing().when(prepStmt1).setString(anyInt(), anyString());
        doNothing().when(prepStmt2).setString(anyInt(), anyString());
        when(prepStmt.executeQuery()).thenReturn(resultSet);
        when(prepStmt1.executeUpdate()).thenReturn(Integer.valueOf(1));
        when(prepStmt1.getGeneratedKeys()).thenReturn(resultSet1);
    }
    
    @Test
    public void testLoginExistsOk() throws SQLException {
        // mocks methods of ResultSet
        when(resultSet.next()).thenReturn(Boolean.valueOf(false));
        
        Boolean loginExists = userDao.loginExists("Login");
        assertFalse("Should be false",loginExists);
    }
    
    @Test
    public void testLoginExistsNotOk() throws SQLException {
        // mocks methods of ResultSet
        when(resultSet.next()).thenReturn(Boolean.valueOf(true));
        
        Boolean loginExists = userDao.loginExists("Login");
        assertTrue("Should be false",loginExists);
    }
    
    @Test
    public void testCreateUserOk() throws SQLException {
        // mocks methods of ResultSet
        when(resultSet1.next()).thenReturn(Boolean.valueOf(false));
        
        // TODO continue with student.setId(generatedKey.getInt(1));
        Boolean loginExists = userDao.loginExists("Login");
        assertFalse("Should be false",loginExists);
    }
}

@@ -43,43 +43,141 @@
 ort 
-static org.junit.Assert.assertFal
+com.devproserv.courses.controller.AppContext;%0D%0Aimport com.devproserv.courses.model.Student;%0D%0Aimport com.devproserv.courses.model.U
 se
+r
 ;%0D%0Ai
@@ -178,31 +178,24 @@
 er;%0D%0Aimport 
-static 
 org.junit.As
@@ -196,25 +196,38 @@
 nit.
-Assert.assertTrue
+Before;%0D%0Aimport org.junit.Test
 ;%0D%0Ai
@@ -224,39 +224,32 @@
 t.Test;%0D%0Aimport 
-static 
 org.mockito.Mock
@@ -252,21 +252,8 @@
 Mock
-ito.doNothing
 ;%0D%0Ai
@@ -250,39 +250,32 @@
 o.Mock;%0D%0Aimport 
-static 
 org.mockito.Mock
@@ -281,120 +281,203 @@
 kito
-.when
+Annotations
 ;%0D%0A
+%0D%0A
 import 
-static org.mockito.ArgumentMatchers.anyInt;%0D%0Aimport static org.mockito.ArgumentMatchers.anyString
+javax.sql.DataSource;%0D%0Aimport java.sql.Connection;%0D%0Aimport java.sql.PreparedStatement;%0D%0Aimport java.sql.ResultSet;%0D%0Aimport java.sql.SQLException;%0D%0Aimport java.sql.Statement
 ;%0D%0A%0D
@@ -532,28 +532,30 @@
 nConfig.
-SELECT_LOGIN
+INSERT_STUDENT
 _SQL;%0D%0Ai
@@ -685,232 +685,113 @@
 fig.
-INSERT_STUDENT
+SELECT_LOGIN
 _SQL;%0D%0A
-%0D%0A
 import 
-java.sql.Connection;%0D%0Aimport java.sql.PreparedStatement;%0D%0Aimport java.sql.ResultSet;%0D%0Aimport java.sql.SQLException;%0D%0Aimport java.sql.Statement;%0D%0A%0D%0Aimport org.junit.Before;%0D%0Aimport org.junit.Test
+static org.junit.Assert.assertFalse;%0D%0Aimport static org.junit.Assert.assertTrue
 ;%0D%0Ai
@@ -788,32 +788,39 @@
 rtTrue;%0D%0Aimport 
+static 
 org.mockito.Mock
@@ -807,36 +807,55 @@
 tic org.mockito.
-Mock
+ArgumentMatchers.anyInt
 ;%0D%0Aimport org.mo
@@ -840,32 +840,39 @@
 anyInt;%0D%0Aimport 
+static 
 org.mockito.Mock
@@ -871,171 +871,121 @@
 ito.
-MockitoAnnotations;%0D%0A%0D%0Aimport com.devproserv.courses.controller.AppContext;%0D%0Aimport com.devproserv.courses.model.Student;%0D%0Aimport com.devproserv.courses.model.User
+ArgumentMatchers.anyString;%0D%0Aimport static org.mockito.Mockito.doNothing;%0D%0Aimport static org.mockito.Mockito.when
 ;%0D%0A%0D
@@ -1208,32 +1208,79 @@
 xt;%0D%0A    @Mock%0D%0A
+    private DataSource dataSource;%0D%0A    @Mock%0D%0A
     private Conn
@@ -1827,16 +1827,82 @@
 UserDao(
+dataSource);%0D%0A        when(dataSource.getConnection()).thenReturn(
 connecti

Update tests with connection