package org.bench4q.master.unitTest.repository;

import static org.junit.Assert.*;

import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.repository.AgentRepository;
import org.bench4q.master.helper.SessionHelper;
import org.bench4q.share.enums.master.AgentStatus;
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(classes = { AgentRepository.class, SessionHelper.class })
public class Test_AgentRepository {
	private AgentRepository agentRepoitory;

	private AgentRepository getAgentRepoitory() {
		return agentRepoitory;
	}

	@Autowired
	private void setAgentRepoitory(AgentRepository agentRepoitory) {
		this.agentRepoitory = agentRepoitory;
	}

	@Test
	public void testAddAgent() {
		String hostNameForTest = "133.133.12.95";
		int portForTest = 6565;
		assertTrue(this.getAgentRepoitory().attach(
				Agent.createAgentWithoutId(hostNameForTest, portForTest, 1000)));
		Agent agent = this.getAgentRepoitory().getAgentBy(hostNameForTest);
		assertNotNull(agent);
		assertEquals(hostNameForTest, agent.getHostName());
		assertEquals(portForTest, agent.getPort());
		assertEquals(1000, agent.getMaxLoad());
		assertEquals(AgentStatus.InIdle, agent.getCurrentEnumStatus());
		System.out.println(AgentStatus.InIdle.ordinal());
		assertTrue(this.getAgentRepoitory().detach(agent.getId()));
	}
}
