package com.projectning.manager;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.projectning.service.exceptions.NotFoundException;
import com.projectning.service.manager.UserManager;
import com.projectning.util.ErrorMessage;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="/pnServiceTesting.xml")
public class UserManagerTests {
	
	@Autowired private UserManager userManager;
	
	@Test
	public void testRegister(){
		try {
			userManager.registerForSalt("TestUser@fmning.com", 0);
			fail(ErrorMessage.SHOULD_NOT_PASS_ERROR.getMsg());
		} catch (IllegalStateException e) {
			assertEquals(e.getMessage(), ErrorMessage.USERNAME_UNAVAILABLE.getMsg());
		}
	}
	
	@Test
	public void testLoginForSalt()
	{
		try {
			assertEquals(userManager.loginForSalt("TestUser@fmning.com"), "testUserSalt");
		} catch (NotFoundException e) {
			fail(e.toString());
		}
		try {
			userManager.loginForSalt("wrongUser");
			fail(ErrorMessage.SHOULD_NOT_PASS_ERROR.getMsg());
		} catch (NotFoundException e) {
			return;
		}
	}
	
	@Test
	public void testLogin()
	  {
		try {
			userManager.login("TestUser@fmning.com", "testUserPassword", "");
		} catch (NotFoundException e) {
			fail(e.toString());
		}
		try{
			userManager.login("TestUser@fmning.com", "WRONG", "");
			fail(ErrorMessage.SHOULD_NOT_PASS_ERROR.getMsg());
		} catch (NotFoundException e){
		}
		try{
			userManager.login("WRONG", "testUserPassword", "");
			fail(ErrorMessage.SHOULD_NOT_PASS_ERROR.getMsg());
		} catch (NotFoundException e){
			return;
		}
	  }
	
//	@Test
//	public void createUser()
//	  {
//		final Random r = new SecureRandom();
//		byte[] salt = new byte[32];
//		r.nextBytes(salt);
//		String encodedSalt = Base64.encodeBase64String(salt);
//		assertEquals("", encodedSalt.substring(0, 32));
//	  }

}

@@ -971,12 +971,10 @@
 lt()
-%0A%09
 %7B%0A
+
 %09%09tr
@@ -1309,20 +1309,16 @@
 tLogin()
-%0A%09  
 %7B%0A%09%09try 
@@ -1770,18 +1770,702 @@
 n;%0A%09%09%7D%0A%09
-  
+%7D%0A%09%0A%09@Test%0A%09public void testCheckUsername()%7B%0A%09%09assertTrue(userManager.checkUsername(%22TestUser@fmning.com%22));%0A%09%09assertFalse(userManager.checkUsername(%22WRONG%22));%0A%09%7D%0A%09%0A%09@Test%0A%09public void testUpdateVerificationCode()%7B%0A%09%09try%7B%0A%09%09%09userManager.updateVeriCode(%22WRONG%22, %22%22);%0A%09%09%7Dcatch(NotFoundException e)%7B%0A%09%09%09assertEquals(e.getMessage(), ErrorMessage.USER_NOT_FOUND.getMsg());%0A%09%09%7D%0A%09%7D%0A%09%0A%09@Test%0A%09public void testCheckVerificationCode()%7B%0A%09%09try%7B%0A%09%09%09userManager.checkVeriCode(%22WRONG%22,%22%22);%0A%09%09%09fail(ErrorMessage.SHOULD_NOT_PASS_ERROR.getMsg());%0A%09%09%7Dcatch(NotFoundException e)%7B%0A%09%09%7D%0A%09%09try%7B%0A%09%09%09userManager.checkVeriCode(%22TestUser@fmning.com%22,%22%22);%0A%09%09%7Dcatch(NotFoundException e)%7B%0A%09%09%09fail(e.toString());%0A%09%09%7D%0A%09
 %7D%0A%09%0A//%09@

Update test cases for user manager