package org.bench4q.master.unitTest.repository;

import static org.junit.Assert.*;

import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.repository.UserRepository;
import org.bench4q.master.helper.HashHelper;
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_UserRepository {
	private UserRepository userRepository;
	private HashHelper hashHelper;
	private User userForTest;

	private UserRepository getUserRepository() {
		return userRepository;
	}

	@Autowired
	private void setUserRepository(UserRepository userRepository) {
		this.userRepository = userRepository;
	}

	private HashHelper getHashHelper() {
		return hashHelper;
	}

	@Autowired
	private void setHashHelper(HashHelper hashHelper) {
		this.hashHelper = hashHelper;
		init();
	}

	private User getUserForTest() {
		return userForTest;
	}

	private void setUserForTest(User userForTest) {
		this.userForTest = userForTest;
	}

	public Test_UserRepository() {
	}

	@Before
	public void init() {
		User user = new User();
		user.setPassword(this.getHashHelper().sha1Hash("test1"));
		user.setUserName("test1");
		user.setScope((byte) 0);
		this.setUserForTest(user);
	}

	@Test
	public void initTest() {
		assertNotNull(this.getUserRepository());
	}

	@Test
	public void testAttach() {
		assertTrue(this.getUserRepository().attatch(this.getUserForTest()));
	}

	@After
	public void cleanUp() {
		this.getUserRepository().detach(this.getUserForTest().getUserName());
	}

	@Test
	public void testIsExist() {
		this.getUserRepository().attatch(this.getUserForTest());
		assertTrue(this.getUserRepository().isExist(this.getUserForTest()));
	}

	@Test
	public void testGetUserByName() {
		this.getUserRepository().attatch(this.getUserForTest());
		assertNotNull(this.getUserRepository().getUser("test1"));
	}

	@Test
	public void testUpdateEntity() {
		// This test can reference to the test Of the
		// Test_UserService.testAddScript
	}
}
