package org.bench4q.master.unitTest.datastatistics;

import static org.junit.Assert.*;

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

import org.bench4q.master.domain.valueobject.datastatistics.PageBriefStatistics;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
import org.bench4q.share.models.master.statistics.ScriptPageBriefModel;
import org.junit.Test;

public class TestPageBrief {
	private PageBriefStatistics statistics;

	private PageBriefStatistics getStatistics() {
		return statistics;
	}

	private void setStatistics(PageBriefStatistics statistics) {
		this.statistics = statistics;
	}

	public TestPageBrief() {
		this.setStatistics(new PageBriefStatistics());
	}

	@Test
	public void testNull() {
		this.getStatistics().add(null);
		ScriptPageBriefModel scriptPageBriefModel = (ScriptPageBriefModel) this
				.getStatistics().getStatistics();
		assertTrue(scriptPageBriefModel != null);
		assertEquals(0, scriptPageBriefModel.getCountFromBegin());
		assertEquals(Long.MIN_VALUE,
				scriptPageBriefModel.getMaxResponseTimeFromBegin());
		assertEquals(Long.MAX_VALUE,
				scriptPageBriefModel.getMinResponseTimeFromBegin());
		assertEquals(0, scriptPageBriefModel.getAverageResponseTimeThisTime());
		assertEquals(0, scriptPageBriefModel.getLatestResponseTime());
	}

	@Test
	public void testOneWithZeroCountThisTimeForAverage() {
		this.getStatistics().add(
				buildAgentPageBriefModel(1, 1, 0, 200, 200, 200, 190, 0));
		ScriptPageBriefModel pageBriefModel = (ScriptPageBriefModel) this
				.getStatistics().getStatistics();
		assertEquals(0, pageBriefModel.getAverageResponseTimeThisTime());
		assertEquals(1, pageBriefModel.getCountFromBegin());
		assertEquals(200, pageBriefModel.getLatestResponseTime());
		assertEquals(200, pageBriefModel.getMaxResponseTimeFromBegin());
		assertEquals(200, pageBriefModel.getMinResponseTimeFromBegin());
		assertEquals(0, pageBriefModel.getThroughputThisTime());
	}

	@Test
	public void testTwoForCountFromBegin() {
		this.testOneWithZeroCountThisTimeForAverage();
		this.getStatistics().add(
				buildAgentPageBriefModel(1, 2, 0, 200, 200, 200, 190, 200));
		ScriptPageBriefModel pageBriefModel = (ScriptPageBriefModel) this
				.getStatistics().getStatistics();
		assertEquals(0, pageBriefModel.getAverageResponseTimeThisTime());
		assertEquals(2, pageBriefModel.getCountFromBegin());
	}

	@Test
	public void testThree() {
		for (AgentPageBriefModel agentPageBriefModel : makePageBriefModels(3)) {
			this.getStatistics().add(agentPageBriefModel);
		}
		ScriptPageBriefModel scriptPageBriefModel = (ScriptPageBriefModel) this
				.getStatistics().getStatistics();
		assertEquals(9, scriptPageBriefModel.getCountFromBegin());
		assertEquals(220, scriptPageBriefModel.getLatestResponseTime());
		assertEquals(220, scriptPageBriefModel.getMaxResponseTimeFromBegin());
		assertEquals(180, scriptPageBriefModel.getMinResponseTimeFromBegin());
		assertEquals(0, scriptPageBriefModel.getThroughputThisTime());
		assertEquals(1230 / 6,
				scriptPageBriefModel.getAverageResponseTimeThisTime());
	}

	private List<AgentPageBriefModel> makePageBriefModels(int count) {
		List<AgentPageBriefModel> result = new ArrayList<AgentPageBriefModel>();
		for (int i = 0; i < count; i++) {
			result.add(buildAgentPageBriefModel(2, i + 2, i + 1, 200 + 10 * i,
					200 + 10 * i, 200 - 10 * i, 200 + 20 * i, 400 + 10 * i));
		}
		return result;
	}

	private AgentPageBriefModel buildAgentPageBriefModel(int pageId,
			long countFromBegin, long countThisTime,
			long latestTimeResponseTime, long maxResponseTimeFromBegin,
			long minResponseTimeFromBegin, long timeFrame,
			long totalResponseTimeThisTime) {
		AgentPageBriefModel result = new AgentPageBriefModel();
		result.setPageId(pageId);
		result.setCountFromBegin(countFromBegin);
		result.setCountThisTime(countThisTime);
		result.setLatestResponseTime(latestTimeResponseTime);
		result.setMaxResponseTimeFromBegin(maxResponseTimeFromBegin);
		result.setMinResponseTimeFromBegin(minResponseTimeFromBegin);
		result.setTimeFrame(timeFrame);
		result.setTotalResponseTimeThisTime(totalResponseTimeThisTime);
		return result;
	}
}
