package security;

import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationCredentialsNotFoundException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.yang.young.common.sso.Application;
import com.yang.young.common.sso.persistance.jpa.entity.CustomerEntity;
import com.yang.young.common.sso.persistance.jpa.entity.GroupEntity;
import com.yang.young.common.sso.persistance.jpa.entity.RoleEntity;
import com.yang.young.common.sso.service.TestAuthorityService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = Application.class)
public class WithMockUserTest {
	
	//@Before
	public void setup() {
		//save customer
				RoleEntity role1 = new RoleEntity("Seller");
				RoleEntity role2 = new RoleEntity("Customer");
	
				GroupEntity group = new GroupEntity("customer_group");
				group.getRoles().add(role1);
				group.getRoles().add(role2);
		
				CustomerEntity user = new CustomerEntity("yyang","password","yyang@salmon.com", -1, "USD");
				user.getGroups().add(group);
				

		List<GrantedAuthority> authorities = AuthorityUtils
				.createAuthorityList("ROLE_USER");
		UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
				user, "notused", authorities);
		SecurityContextHolder.getContext().setAuthentication(authentication);
	}

	//@After
	public void cleanup() {
		SecurityContextHolder.clearContext();
	}
	
	@Autowired
	TestAuthorityService service ;
	
	@Test(expected=AuthenticationCredentialsNotFoundException.class)
	public void getMessageUnauthenticated() {
		service.getMessage();
	}
	
	@Test
	@WithMockUser
	public void getMessageWithMockUser() {
		String message = service.getMessage();
		System.out.println(message);
	}
	
	@Test
	@WithMockUser("customUsername")
	public void getMessageWithMockUserCustomerUsername() {
		String message = service.getMessage();
		System.out.println(message);
	}
	
	@Test
	@WithMockUser(username="admin",roles={"USER","ADMIN"})
	public void getMessageWithMockUserCustomerUser() {
		String message = service.getMessage();
		System.out.println(message);
	}
	
	/*@Test
	@WithUserDetails
	public void getMesageWithUserDetails() {
		String message = service.getMessage();
		System.out.println(message);
	}*/

}

@@ -36,16 +36,65 @@
 .List;%0A%0A
+import org.junit.After;%0Aimport org.junit.Before;%0A
 import o
@@ -108,16 +108,16 @@
 t.Test;%0A
-
 import o
@@ -660,82 +660,8 @@
 er;%0A
-import org.springframework.security.test.context.support.WithUserDetails;%0A
 impo
@@ -1236,18 +1236,16 @@
 st %7B%0A%09%0A%09
-//
 @Before%0A
@@ -1951,18 +1951,16 @@
 );%0A%09%7D%0A%0A%09
-//
 @After%0A%09
@@ -2157,33 +2157,33 @@
 oid 
-getMessageUnauthenticated
+GETMESSAGEUNAUTHENTICATED
 () %7B

add database insert