package org.bench4q.master.unitTest.datastatistics;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.xml.bind.JAXBException;

import org.bench4q.master.domain.RunningAgentInterface;
import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.factory.RunningAgentFactory;
import org.bench4q.master.domain.valueobject.datastatistics.RunningScriptSampler;
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:mockHttpMesseger-context.xml" })
public class Test_ScriptSample {
	@Autowired
	private RunningAgentFactory runningAgentFactory;
	private List<Agent> testcase2 = new ArrayList<Agent>();
	{
		testcase2.add(Agent.createAgentWithoutId("127.0.0.1", 6565, 500));
		testcase2.add(Agent.createAgentWithoutId("127.0.0.2", 6565, 500));
	}

	/**
	 * The destiny is to justify if a runningAgent is stopped, if it will
	 * decrease the totalSuccessCount
	 * 
	 * @throws JAXBException
	 */
	@Test
	public void testForStopOneRunningAgentWhenSample() throws JAXBException {
		ArrayList<RunningAgentInterface> runningAgents = new ArrayList<RunningAgentInterface>();
		runningAgents.add(this.runningAgentFactory
				.buildRunningAgentWithOutIdAndRunningScript(
						this.testcase2.get(0), 10, UUID.randomUUID()));
		runningAgents.add(this.runningAgentFactory
				.buildRunningAgentWithOutIdAndRunningScript(
						this.testcase2.get(1), 20, UUID.randomUUID()));
		RunningScriptSampler sampler = new RunningScriptSampler(runningAgents);
		long totalSuccessCountBeforeStop = sampler.getResultModelFromAgent()
				.getScriptBriefResultModel().getTotalSuccessCountFromBegin();

		runningAgents.get(0).stop();

		long totalSuccessCountAfterStop = sampler.getResultModelFromAgent()
				.getScriptBriefResultModel().getTotalSuccessCountFromBegin();
		assertTrue(totalSuccessCountAfterStop >= totalSuccessCountBeforeStop);
	}
}
