package org.bench4q.master.unitTest.entity;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.factory.BusinessModelMapFactory;
import org.bench4q.share.enums.master.AgentStatus;
import org.bench4q.share.helper.MarshalHelper;
import org.bench4q.share.models.master.AgentModel;
import org.junit.Test;

public class Test_Agent {
	private Agent agent = Agent.createAgentWithoutId("127.0.0.1", 6565, 1000);

	public Agent getAgentForTest() {
		return agent;
	}

	public void setAgent(Agent agent) {
		this.agent = agent;
	}

	@Test
	public void testBackLoadFromAgentWhereBackLoad_Plus_RemainLoad__LE_MaxLoad() {
		Agent agent = getAgentForTest();
		int initialDifferenceBetweenMaxLoadAndRemainLoad = agent.getMaxLoad()
				- agent.getRemainLoad();
		assertTrue(agent.backLoad(initialDifferenceBetweenMaxLoadAndRemainLoad));
		assertEquals(getAgentForTest().getMaxLoad(), getAgentForTest()
				.getRemainLoad());
	}

	@Test
	public void testBackLoadToAgentWhereBackLoad_Plus_RemainLoad_GE_MaxLoad() {
		Agent agent = getAgentForTest();
		int initialDifferenceBetweenMaxLoadAndRemainLoad = agent.getMaxLoad()
				- agent.getRemainLoad();
		assertTrue(initialDifferenceBetweenMaxLoadAndRemainLoad >= 0);
		assertFalse(agent
				.backLoad(initialDifferenceBetweenMaxLoadAndRemainLoad + 100));
		assertEquals(initialDifferenceBetweenMaxLoadAndRemainLoad,
				getAgentForTest().getMaxLoad()
						- getAgentForTest().getRemainLoad());
	}

	@Test
	public void testBackLoadToAgentWhereBackLoad_LT_Zero() {
		int initialDifferenceBetweenMaxLoadAndRemainLoad = getAgentForTest()
				.getMaxLoad() - getAgentForTest().getRemainLoad();
		assertFalse(getAgentForTest().backLoad(-100));
		assertEquals(initialDifferenceBetweenMaxLoadAndRemainLoad,
				getAgentForTest().getMaxLoad()
						- getAgentForTest().getRemainLoad());
	}

	@Test
	public void testGetLoadFromAgentWhereRequireLoad_LT_RemainLoad() {
		this.getAgentForTest().bookTest(100);
		assertEquals(400, getAgentForTest().getRemainLoad());
	}

	@Test
	public void testGetLoadFromAgentWhereRequireLoad_GT_RemainLoad() {
		int initialRemainLoad = getAgentForTest().getRemainLoad();
		this.getAgentForTest().bookTest(initialRemainLoad + 100);
		assertEquals(initialRemainLoad, getAgentForTest().getRemainLoad());
	}

	@Test
	public void testGetLoadFromAgentWhereRequireLoad_LT_Zero() {
		this.getAgentForTest().bookTest(-100);
		assertEquals(getAgentForTest().getMaxLoad(), getAgentForTest()
				.getRemainLoad());
	}

	@Test
	public void testMarshal() {
		AgentModel agentModel = BusinessModelMapFactory
				.toModel(getAgentForTest());
		assertEquals(AgentStatus.InIdle, agentModel.getCurrentEnumStatus());
		String content = MarshalHelper.tryMarshal(agentModel);
		System.out.println(content);
		AgentModel modelAfterUnMarshal = (AgentModel) MarshalHelper
				.tryUnmarshal(AgentModel.class, content);
		assertEquals(modelAfterUnMarshal.getCurrentEnumStatus(),
				AgentStatus.InIdle);
	}
}
