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 com.devproserv.courses.config.MainConfig.SELECT_LOGIN_SQL;

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

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 ResultSet resultSet;
    @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);
        // mocks methods of PreparedStatement
        doNothing().when(prepStmt).setString(1, "Login");
        when(prepStmt.executeQuery()).thenReturn(resultSet);
    }
    
    @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);
    }
}

@@ -212,77 +212,333 @@
 n;%0D%0A
-%0D%0Aimport static com.devproserv.courses.config.MainConfig.SELECT_LOGIN
+import static org.mockito.ArgumentMatchers.anyInt;%0D%0Aimport static org.mockito.ArgumentMatchers.anyString;%0D%0A%0D%0Aimport static com.devproserv.courses.config.MainConfig.SELECT_LOGIN_SQL;%0D%0Aimport static com.devproserv.courses.config.MainConfig.INSERT_USER_SQL;%0D%0Aimport static com.devproserv.courses.config.MainConfig.INSERT_STUDENT
 _SQL
@@ -665,16 +665,44 @@
 eption;%0D
+%0Aimport java.sql.Statement;%0D
 %0A%0D%0Aimpor
@@ -1305,27 +1305,179 @@
 ate 
-ResultSet resultSet
+PreparedStatement prepStmt1;%0D%0A    @Mock%0D%0A    private PreparedStatement prepStmt2;%0D%0A    @Mock%0D%0A    private ResultSet resultSet;%0D%0A    @Mock%0D%0A    private ResultSet resultSet1
 ;%0D%0A 
@@ -1903,164 +1903,660 @@
     
-// mocks methods of PreparedStatement%0D%0A        doNothing().when(prepStmt).setString(1, %22Login%22);%0D%0A        when(prepStmt.executeQuery()).thenReturn(resultSet
+when(connection.prepareStatement(INSERT_USER_SQL, Statement.RETURN_GENERATED_KEYS)).thenReturn(prepStmt1);%0D%0A        when(connection.prepareStatement(INSERT_STUDENT_SQL)).thenReturn(prepStmt2);%0D%0A        // mocks methods of PreparedStatement%0D%0A        doNothing().when(prepStmt).setString(anyInt(), anyString());%0D%0A        doNothing().when(prepStmt1).setString(anyInt(), anyString());%0D%0A        doNothing().when(prepStmt2).setString(anyInt(), anyString());%0D%0A        when(prepStmt.executeQuery()).thenReturn(resultSet);%0D%0A        when(prepStmt1.executeUpdate()).thenReturn(Integer.valueOf(1));%0D%0A        when(prepStmt1.getGeneratedKeys()).thenReturn(resultSet1
 );%0D%0A
@@ -3191,11 +3191,395 @@
 %0A    %7D%0D%0A
+    %0D%0A    @Test%0D%0A    public void testCreateUserOk() throws SQLException %7B%0D%0A        // mocks methods of ResultSet%0D%0A        when(resultSet1.next()).thenReturn(Boolean.valueOf(false));%0D%0A        %0D%0A        // TODO continue with student.setId(generatedKey.getInt(1));%0D%0A        Boolean loginExists = userDao.loginExists(%22Login%22);%0D%0A        assertFalse(%22Should be false%22,loginExists);%0D%0A    %7D%0D%0A
 %7D%0D%0A

Add part of test for createUser() method in UserDao