package de.dhbw.humbuch.viewmodel;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

import javax.persistence.EntityManager;

import org.hibernate.criterion.Restrictions;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.inject.Inject;
import com.google.inject.Provider;

import de.dhbw.humbuch.guice.GuiceJUnitRunner;
import de.dhbw.humbuch.guice.TestModule;
import de.dhbw.humbuch.guice.GuiceJUnitRunner.GuiceModules;
import de.dhbw.humbuch.model.DAO;
import de.dhbw.humbuch.model.entity.TestPersistenceInitialiser;
import de.dhbw.humbuch.model.entity.User;
import de.dhbw.humbuch.util.PasswordHash;

@RunWith(GuiceJUnitRunner.class)
@GuiceModules({ TestModule.class })
public class LoginViewModelTest extends BaseTest {

	private LoginViewModel vm;
	private DAO<User> daoUser;
	
	public static final String USERNAME = "USERNAME";
	public static final String PASSWORD = "PASSWORD";

	@Inject
	public void setInjected(TestPersistenceInitialiser persistenceInitialiser,
			Provider<EntityManager> emProvider,
			LoginViewModel loginViewModel,
			DAO<User> daoUser) {
		this.daoUser = daoUser;
		super.setInjected(persistenceInitialiser, emProvider);
		
		this.vm = loginViewModel;
	}
	
	private void insertUser() {
		try {
			daoUser.insert(new User.Builder(USERNAME, PasswordHash.createHash(PASSWORD)).build());
		} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void testStateInitialisation() {
		assertNotNull(vm.isLoggedIn.get());
	}
	
	@Test
	public void testUserInsert() throws NoSuchAlgorithmException, InvalidKeySpecException {
		insertUser();
		User userInDB = daoUser.findSingleWithCriteria(Restrictions.eq("username", USERNAME));
		assertNotNull(userInDB);
		assertEquals(true, PasswordHash.validatePassword(PASSWORD, userInDB.getPassword()));
	}
	
	@Test
	public void testStateAfterVMInitialisation() {
		assertEquals(false, vm.isLoggedIn.get());
	}
	
	@Test
	public void testStateAfterSuccessfulLogin() {
		insertUser();
		vm.doLogin(USERNAME, PASSWORD);
		assertEquals(true, vm.isLoggedIn.get());
	}
	
	@Test
	public void testStateAfterUnsuccessfulLoginWithWrongPassword() {
		insertUser();
		vm.doLogin(USERNAME, "wrongPassword");
		assertEquals(false, vm.isLoggedIn.get());
	}
	
	@Test
	public void testStateAfterUnsuccessfulLoginWithWrongUsername() {
		insertUser();
		vm.doLogin("wrongUser", PASSWORD);
		assertEquals(false, vm.isLoggedIn.get());
	}
	
	@Test
	public void testStateAfterUnsuccessfulLoginWithAwfullyLongUsername()	{
		insertUser();
		vm.doLogin(new String(new char[1025]).replace('\0', 'X'), PASSWORD);
		assertEquals(false, vm.isLoggedIn.get());
	}
	
	@Test
	public void testStateAfterUnsuccessfulLoginWithAwfullyLongPassword()	{
		insertUser();
		vm.doLogin(USERNAME, new String(new char[1025]).replace('\0', 'X'));
		assertEquals(false, vm.isLoggedIn.get());
	}
	
	@Test
	public void testStateAfterSuccessfulLoginAndLogout() {
		insertUser();
		vm.doLogin(USERNAME, PASSWORD);
		vm.doLogout(null);
		assertEquals(false, vm.isLoggedIn.get());
	}
}

@@ -30,16 +30,185 @@
 odel;%0A%0A%0A
+import static de.dhbw.humbuch.test.TestUtils.PASSWORD;%0Aimport static de.dhbw.humbuch.test.TestUtils.USERNAME;%0Aimport static de.dhbw.humbuch.test.TestUtils.standardUser;%0A
 import s
@@ -244,16 +244,16 @@
 Equals;%0A
-
 import s
@@ -653,49 +653,8 @@
 er;%0A
-import de.dhbw.humbuch.guice.TestModule;%0A
 impo
@@ -709,16 +709,57 @@
 odules;%0A
+import de.dhbw.humbuch.guice.TestModule;%0A
 import d
@@ -1113,112 +1113,8 @@
 ser;
-%0A%09%0A%09public static final String USERNAME = %22USERNAME%22;%0A%09public static final String PASSWORD = %22PASSWORD%22;
 %0A%0A%09@
@@ -1416,235 +1416,8 @@
 %7D%0A%09%0A
-%09private void insertUser() %7B%0A%09%09try %7B%0A%09%09%09daoUser.insert(new User.Builder(USERNAME, PasswordHash.createHash(PASSWORD)).build());%0A%09%09%7D catch (NoSuchAlgorithmException %7C InvalidKeySpecException e) %7B%0A%09%09%09e.printStackTrace();%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09@Te
@@ -1597,36 +1597,54 @@
 ception %7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09User use
@@ -1997,36 +1997,54 @@
 Login() %7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09vm.doLog
@@ -2186,36 +2186,54 @@
 sword() %7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09vm.doLog
@@ -2383,36 +2383,54 @@
 rname() %7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09vm.doLog
@@ -2582,36 +2582,54 @@
 rname()%09%7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09vm.doLog
@@ -2815,36 +2815,54 @@
 sword()%09%7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09vm.doLog
@@ -3040,20 +3040,38 @@
 %7B%0A%09%09
-insert
+daoUser.insert(standard
 User()
+)
 ;%0A%09%09

use new helper methods in TestUtils.java