package cz.muni.fi.rtc.teacherWorkbench.tests.importer;


import static org.junit.Assert.assertNotSame;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import cz.muni.fi.rtc.teacherWorkbench.importer.PasswordGenerator;
import cz.muni.fi.rtc.teacherWorkbench.importer.exceptions.EmptyPossibleCharsException;
import cz.muni.fi.rtc.teacherWorkbench.model.ImportedPerson;
import cz.muni.fi.rtc.teacherWorkbench.model.ImportedPersonImpl;

public class PasswordGeneratorTest {

	
	@Before
	public void setUp() throws Exception {
		
	}
	
	@Test
	public void testDefaultLength() throws Exception {
		PasswordGenerator generator = new PasswordGenerator();
		int expectedLength = PasswordGenerator.DEFAULT_LENGTH;
		String password = generator.generatePassword();
		Assert.assertEquals(expectedLength, password.length());
	}
	
	@Test
	public void testLength() throws Exception {
		PasswordGenerator generator = new PasswordGenerator();
		int expectedLength = 423;
		String password = generator.generatePassword(expectedLength);
		Assert.assertEquals(expectedLength, password.length());
	}
	
	
	@Test(expected=EmptyPossibleCharsException.class)
	public void testEmptyPossibleChars() throws Exception {
		PasswordGenerator generator = new PasswordGenerator(new char[]{});
		generator.generatePassword();
	}
	
	@Test
	public void testIllegalChar() throws Exception {
		char[] chars = new char[]{'a'};
		PasswordGenerator generator = new PasswordGenerator(chars);
		String expectedPassword = "aaaaa";
		String password = generator.generatePassword(expectedPassword.length());
		if(! password.equals(expectedPassword)) {
			Assert.fail();
		}
	}
	
	
	@Test
	public void testSetPeoplePasswords() {
		ImportedPerson p1 = new ImportedPersonImpl();
		ImportedPerson p2 = new ImportedPersonImpl();
		Set people = new HashSet();
		people.add(p1);
		people.add(p2);
		
		Collection peopleWithPassword = PasswordGenerator.generatePasswordsForPeople(people);
		for(ImportedPerson p : peopleWithPassword) {
			assertNotSame("Password should not be empty", "", p.getRawPassword());
		}
		
	}
	

}