<commit_before>package studentcapture.user;

import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;

import studentcapture.config.StudentCaptureApplicationTests;

import java.sql.Types;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * Created by tfy12hsm.
 */
public class UserDAOTest extends StudentCaptureApplicationTests {


    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private JdbcTemplate jdbcMock;

    private User userSetup;

    @Before
    public void setup() {
        //Add one user
        String sql = "INSERT INTO users"
                +" (username, firstname, lastname, email, pswd)"
                +" VALUES (?, ?, ?, ?, ?)";

        userSetup = new User("testUser","testFName","testLName",
                             "<EMAIL>","<PASSWORD>");

        Object[] args = new Object[] {userSetup.getUserName(), userSetup.getfName(),
                            userSetup.getlName(),userSetup.getEmail(),
                            userSetup.getPswd()};

        int[] types = new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
                Types.VARCHAR,Types.VARCHAR};

        try {
            jdbcMock.update(sql,args,types);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Remove all content from the database
     */
    @After
    public void tearDown() {
        String sql1 = "DELETE FROM Users;";
        String sql2 = "ALTER TABLE users ALTER COLUMN userid RESTART WITH 1";
        jdbcMock.update(sql1);
        jdbcMock.update(sql2);
    }

    @Test
    public void testGetUserByID() {
       // printUsersTableTemp();
        User userRes = userDAO.getUser("1",1);
        System.out.println("username:" + userRes.getUserName());
        assertEquals(userSetup,userRes);
    }


    @Test
    public void testAddUser() {

        User user = new User("userPelle","Pelle","Jönsson","<EMAIL>",
                            "<PASSWORD>");

        boolean res = userDAO.addUser(user);
        User userRes = userDAO.getUser("userPelle",0);

        assertTrue(res);
        assertEquals(user,userRes);
    }

    @Test
    public void testGetNonExistingUser() {
        User userRes = userDAO.getUser("notExist",0);
        assertEquals(null,userRes);
    }



    @Test
    public void testUpdateUser() {
        User user = new User("testUser","newFirstName","newLname",
                             "<EMAIL>","new_pswd_1fdsgasda213fC");

        boolean res = userDAO.updateUser(user);
        User userRes = userDAO.getUser("testUser",0);

        assertTrue(res);
        assertEquals(user,userRes);
    }

    @Test
    public void testUpdateNonExistingUser() {
        User user = new User("testUserNotExists","newFirstName","newLname",
                "<EMAIL>","new_pswd_1fdsgasda213fC");
        boolean res = userDAO.updateUser(user);

        assertFalse(res);

    }

    /**
     * Used for debugging purposes.
     */
    private void printUsersTableTemp() {
        System.out.println("!!!!!!!!!!!!!!!!!TABLE!!!!!!!!!!!!!!!!!!!!!!!!!\n");

        String sql = "SELECT * FROM users";
        List<Map<String,Object>> users = jdbcMock.queryForList(sql);

        if(users != null && !users.isEmpty()) {
            for(Map<String,Object> user: users) {
                for(Iterator<Map.Entry<String, Object>> it = user.entrySet().iterator(); it.hasNext();) {
                    Map.Entry<String,Object> entry = it.next();
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    System.out.println(key + " = " + value);
                }
                System.out.println();
            }
        }
    }

    /*
    @Test
    public void getCourseAssignmentHierarchyUserInformationTest() {
    	String getUserStatement = "SELECT * FROM Users WHERE "
        		+ "UserId=?";
    	String getTeacherHierarchyStatement = "SELECT * FROM Participant AS par"
				+ " LEFT JOIN Course AS cou ON par.courseId="
	    		+ "cou.courseId LEFT JOIN Assignment AS ass ON cou.courseId="
	    		+ "ass.courseId LEFT JOIN Submission AS sub ON "
	    		+ "ass.assignmentId=sub.assignmentId WHERE par.userId=? AND "
	    		+ "par.function='Teacher'";
    	String getStudentHierarchyStatement = "SELECT * FROM "
	    		+ "Participant AS par LEFT JOIN Course AS cou ON par.courseId="
	    		+ "cou.courseId LEFT JOIN Assignment AS ass ON cou.courseId="
	    		+ "ass.courseId LEFT JOIN Submission AS sub ON par.userId="
	    		+ "sub.studentId AND ass.assignmentId=sub.assignmentId WHERE "
	    		+ "par.userId=? AND par.function='Student'";

    	Timestamp ts = new Timestamp(System.currentTimeMillis());

    	Map responseFromMockUser = new HashMap();
    	responseFromMockUser.put("UserId", 1);
    	responseFromMockUser.put("FirstName", "nameFirst");
    	responseFromMockUser.put("LastName", "nameLast");
    	when(jdbcMock.queryForMap(getUserStatement, 1)).<commit_msg>Added test supporting error flags<commit_after>package studentcapture.user;

import org.junit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.context.WebApplicationContext;

import studentcapture.config.StudentCaptureApplicationTests;
import studentcapture.login.ErrorFlags;

import java.sql.Types;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.*;

/**
 * Created by tfy12hsm.
 */
public class UserDAOTest extends StudentCaptureApplicationTests {


    @Autowired
    private WebApplicationContext webApplicationContext;

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private JdbcTemplate jdbcMock;

    private User userSetup;

    @Before
    public void setup() {
        //Add one user
        String sql = "INSERT INTO users"
                +" (username, firstname, lastname, email, pswd)"
                +" VALUES (?, ?, ?, ?, ?)";

        userSetup = new User("testUser","testFName","testLName",
                             "<EMAIL>","<PASSWORD>");

        Object[] args = new Object[] {userSetup.getUserName(), userSetup.getfName(),
                            userSetup.getlName(),userSetup.getEmail(),
                            userSetup.getPswd()};

        int[] types = new int[]{Types.VARCHAR,Types.VARCHAR,Types.VARCHAR,
                Types.VARCHAR,Types.VARCHAR};

        try {
            jdbcMock.update(sql,args,types);
        } catch(Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * Remove all content from the database
     */
    @After
    public void tearDown() {
        String sql1 = "DELETE FROM Users;";
        String sql2 = "ALTER TABLE users ALTER COLUMN userid RESTART WITH 1";
        jdbcMock.update(sql1);
        jdbcMock.update(sql2);
    }

    @Test
    public void testGetUserByID() {
       // printUsersTableTemp();
        User userRes = userDAO.getUser("1",1);
        System.out.println("username:" + userRes.getUserName());
        assertEquals(userSetup,userRes);
    }


    @Test
    public void testAddUser() {

        User user = new User("userPelle","Pelle","Jönsson","<EMAIL>",
                            "<PASSWORD>");

        ErrorFlags res = userDAO.addUser(user);
        User userRes = userDAO.getUser("userPelle",0);

        assertEquals(ErrorFlags.NOERROR,res);
        assertEquals(user,userRes);
    }

    @Test
    public void testAddingUserTwice() {
        ErrorFlags errorFlags = userDAO.addUser(userSetup);

        assertEquals(ErrorFlags.USEREXISTS,errorFlags);
    }

    @Test
    public void testGetNonExistingUser() {
        User userRes = userDAO.getUser("notExist",0);
        assertEquals(null,userRes);
    }

    @Test
    public void testUpdateUser() {
        User user = new User("testUser","newFirstName","newLname",
                             "<EMAIL>","<PASSWORD>");

        boolean res = userDAO.updateUser(user);
        User userRes = userDAO.getUser("testUser",0);

        assertTrue(res);
        assertEquals(user,userRes);
    }

    @Test
    public void testUpdateNonExistingUser() {
        User user = new User("testUserNotExists","newFirstName","newLname",
                "<EMAIL>","new_pswd_1fdsgasda213fC");
        boolean res = userDAO.updateUser(user);
        assertFalse(res);
    }

    @Test
    public void testAddingEmailTwice() {
        User user = new User("testNewUser","newFirstName","newLname",
                             userSetup.getEmail(),"newpswd");

        ErrorFlags res = userDAO.addUser(user);
        assertEquals(ErrorFlags.EMAILEXISTS,res);
    }

    /**
     * Used for debugging purposes.
     */
    private void printUsersTableTemp() {
        System.out.println("!!!!!!!!!!!!!!!!!TABLE!!!!!!!!!!!!!!!!!!!!!!!!!\n");

        String sql = "SELECT * FROM users";
        List<Map<String,Object>> users = jdbcMock.queryForList(sql);

        if(users != null && !users.isEmpty()) {
            for(Map<String,Object> user: users) {
                for(Iterator<Map.Entry<String, Object>> it = user.entrySet().iterator(); it.hasNext();) {
                    Map.Entry<String,Object> entry = it.next();
                    String key = entry.getKey();
                    Object value = entry.getValue();
                    System.out.println(key + " = " + value);
                }
                System.out.println();
            }
        }
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");

    }

    /*
    @Test
    public void getCourseAssignmentHierarchyUserInformationTest() {
    	String getUserStatement = "SELECT * FROM Users WHERE "
        		+ "UserId=?";
    	String getTeacherHierarchyStatement = "SELECT * FROM Participant AS par"
				+ " LEFT JOIN Course AS cou ON par.courseId="
	    		+ "cou.courseId LEFT JOIN Assignment AS ass ON cou.courseId="
	    		+ "ass.courseId LEFT JOIN Submission AS sub ON "
	    		+ "ass.assignmentId=sub.assignmentId WHERE par.userId=? AND "
	    		+ "par.function='Teacher'";
    	String getStudentHierarchyStatement = "SELECT * FROM "
	    		+ "Participant AS par LEFT JOIN Course AS cou ON par.courseId="
	    		+ "cou.courseId LEFT JOIN Assignment AS ass ON cou.courseId="
	    		+ "ass.courseId LEFT JOIN Submission AS sub ON par.userId="
	    		+ "sub.studentId AND ass.assignmentId=sub.assignmentId WHERE "
	    		+ "par.userId=? AND par.function='Student'";

    	Timestamp ts = new Timestamp(System.currentTimeMillis());

    	Map responseFromMockUser = new HashMap();
    	responseFromMockUser.put("UserId", 1);
    	responseFromMockUser.put("FirstName", "nameFirst");
    	responseFromMockUser.put("LastName", "nameLast");
    	when(jdbcMock.queryForMap(getUserStatement, 1)).
{'fraction_non_alphanumeric': 0.13058823529411764, 'fraction_numerical': 0.0036199095022624436, 'mean_word_length': 2.4740647595095884, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556881', 'n_tokens_mistral': 3320, 'n_tokens_neox': 3171, 'n_words': 661}