package org.bench4q.master.unitTest.service.auth;

import static org.junit.Assert.*;

import org.bench4q.master.domain.repository.UserRepository;
import org.bench4q.master.domain.service.UserService;
import org.bench4q.master.domain.service.auth.AccessToken;
import org.bench4q.master.domain.service.auth.AuthenticationManager;
import org.junit.After;
import org.junit.Before;
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;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_AuthenticationManager {
	private AuthenticationManager authenticationManager;

	@Autowired
	private UserRepository userRepository;
	private UserService userService;

	private AuthenticationManager getAuthenticationManager() {
		return authenticationManager;
	}

	@Autowired
	private void setAuthenticationManager(
			AuthenticationManager authenticationManager) {
		this.authenticationManager = authenticationManager;
	}

	private UserService getUserService() {
		return userService;
	}

	@Autowired
	private void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Before
	public void prepare() {
		assertTrue(this.getUserService().register("test1", "test1", (byte) 1));
		this.userRepository.getUser("test1");
	}

	@After
	public void after() {
		this.userRepository.detach("test1");
	}

	@Test
	public void test() {
		assertNotNull(this.getAuthenticationManager());
	}

	@Test
	public void testGenerateCredential() {
		String generateCredential = this.getAuthenticationManager()
				.generateCredential("test1");
		assertNotNull(generateCredential);
		System.out.println(generateCredential);
	}

	@Test
	public void GenerateAccessToken() {
		AccessToken accessToken = this.getAuthenticationManager()
				.generateAccessToken("test1");
		assertNotNull(accessToken);
		System.out.println(accessToken.getExpiresIn());
	}
}
