package org.bench4q.master.unitTest.datastatistics;

import static org.junit.Assert.*;

import org.bench4q.master.domain.valueobject.datastatistics.ScriptBriefStatistics;
import org.bench4q.share.models.agent.statistics.AgentBriefStatusModel;
import org.bench4q.share.models.master.statistics.ScriptBriefResultModel;
import org.junit.Test;

public class ScriptBriefStatisticsTest {
	private ScriptBriefStatistics briefStatistics;
	private static int TIME_UNIT_MS = 1000;
	private static int FAIL_COUNT = 1;

	public ScriptBriefStatistics getBriefStatistics() {
		return briefStatistics;
	}

	public void setBriefStatistics(ScriptBriefStatistics briefStatistics) {
		this.briefStatistics = briefStatistics;
	}

	public ScriptBriefStatisticsTest() {
		this.setBriefStatistics(new ScriptBriefStatistics());
	}

	@Test
	public void addNullTest() {
		this.getBriefStatistics().add(null);
	}

	@Test
	public void addZeroTest() {
		briefStatistics.add(new AgentBriefStatusModel());
		ScriptBriefResultModel actualResult = briefStatistics.getStatistics();
		ScriptBriefResultModel expectedResult = makeExpectedModelForZeroAgent();
		assertTrue(actualResult.equals(expectedResult));
		assertEquals(0, actualResult.getvUserCount());
	}

	private ScriptBriefResultModel makeExpectedModelForZeroAgent() {
		ScriptBriefResultModel result = new ScriptBriefResultModel();
		result.setAverageElapsedTime(0);
		result.setAverageResponseTime(0);
		result.setFailRateThisTime(0);
		result.setFailThroughputThisTime(0);
		result.setMaxResponseTime(0);
		result.setMinResponseTime(0);
		result.setResponseTimeDeviationThisTime(0);
		result.setSuccessThroughputThisTime(0);
		result.setTotalFailCountFromBegin(0);
		result.setTotalSuccessCountFromBegin(0);
		result.setvUserCount(0);
		return result;
	}

	@Test
	public void addOneTest() {
		long timeFrame = 200;
		this.getBriefStatistics().add(
				buildOneInputModel(timeFrame, 200, 200, 200, 40000, 1, 10));
		ScriptBriefResultModel actualModel = this.getBriefStatistics()
				.getStatistics();
		ScriptBriefResultModel expectedModel = this
				.makeExepectedModelForOneAgent();
		assertTrue(actualModel.equals(expectedModel));
	}

	@Test
	public void addOneTestWithNegativeVUser() {
		long timeFrame = 200;
		this.getBriefStatistics().add(
				buildOneInputModel(timeFrame, 200, 200, 200, 40000, 1, -2));
		ScriptBriefResultModel actualModel = this.getBriefStatistics()
				.getStatistics();
		ScriptBriefResultModel expectedBriefResultModel = this
				.makeExepectedModelForOneAgent();
		expectedBriefResultModel.setvUserCount(-2);
		assertTrue(actualModel.equals(expectedBriefResultModel));
	}

	public static AgentBriefStatusModel buildOneInputModel(long timeFrame,
			long minRT, long maxRT, long totalRT, long totalSqureRT,
			long successCount, long vUserCount) {
		AgentBriefStatusModel model = new AgentBriefStatusModel();
		model.setTimeFrame(timeFrame);
		model.setSuccessCountFromBegin(successCount);
		model.setFailCountFromBegin(FAIL_COUNT);
		model.setSuccessThroughputThisTime(successCount * TIME_UNIT_MS
				/ timeFrame);
		model.setFailThroughputThisTime(FAIL_COUNT * TIME_UNIT_MS / timeFrame);
		model.setMaxResponseTime(maxRT);
		model.setMinResponseTime(minRT);
		model.setSuccessCountThisTime(successCount);
		model.setFailCountThisTime(FAIL_COUNT);
		model.setTotalResponseTimeThisTime(totalRT);
		model.setTotalSqureResponseTimeThisTime(totalSqureRT);
		model.setvUserCount(vUserCount);
		return model;
	}

	private ScriptBriefResultModel makeExepectedModelForOneAgent() {
		ScriptBriefResultModel result = new ScriptBriefResultModel();
		result.setAverageResponseTime(200);
		result.setFailRateThisTime(50);
		result.setFailThroughputThisTime(1 * TIME_UNIT_MS / 200);
		result.setMaxResponseTime(200);
		result.setMinResponseTime(200);
		result.setResponseTimeDeviationThisTime(0);
		result.setSuccessThroughputThisTime(1 * TIME_UNIT_MS / 200);
		result.setTotalFailCountFromBegin(1);
		result.setTotalSuccessCountFromBegin(1);
		result.setvUserCount(10);
		return result;
	}

	@Test
	public void addTwoTest() {
		long timeFrame1 = 200, timeFrame2 = 500;
		this.briefStatistics.add(buildOneInputModel(timeFrame1, 200, 200, 200,
				40000, 1, 10));
		this.briefStatistics.add(buildOneInputModel(timeFrame2, 190, 210, 400,
				80200, 2, 20));
		ScriptBriefResultModel actualModel = this.briefStatistics
				.getStatistics();
		ScriptBriefResultModel expectedModel = makeExpectedModelForTwoAgent();
		assertTrue(actualModel.equals(expectedModel));
	}

	private ScriptBriefResultModel makeExpectedModelForTwoAgent() {
		ScriptBriefResultModel result = new ScriptBriefResultModel();
		result.setAverageResponseTime(200);
		result.setFailRateThisTime(40);
		result.setFailThroughputThisTime(7);
		result.setMaxResponseTime(210);
		result.setMinResponseTime(190);
		result.setResponseTimeDeviationThisTime(8);
		result.setSuccessThroughputThisTime(9);
		result.setTotalFailCountFromBegin(2);
		result.setTotalSuccessCountFromBegin(3);
		result.setvUserCount(30);
		return result;
	}

}
