package org.bench4q.agent.test.datastatistics;

import org.bench4q.agent.datacollector.DataCollector;
import org.bench4q.agent.datacollector.impl.PageResultCollector;
import org.bench4q.agent.scenario.engine.PageResult;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.junit.Assert.*;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:test-storage-context.xml",
		"classpath*:/org/bench4q/agent/config/application-context.xml" })
public class PageResultStatisticsTest {
	private DataCollector dataCollector;

	private DataCollector getDataCollector() {
		return dataCollector;
	}

	private void setDataCollector(DataCollector dataCollector) {
		this.dataCollector = dataCollector;
	}

	@SuppressWarnings("resource")
	public PageResultStatisticsTest() {
		new ClassPathXmlApplicationContext(
				"classpath*:/org/bench4q/agent/config/application-context.xml");
		this.setDataCollector(new PageResultCollector());
	}

	@Test
	public void pageResultTest() {
		PageResult pageResult = PageResult.buildPageResult(2,
				ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(1));
		assertEquals(2, pageResult.getPageId());
		assertEquals(200, pageResult.getExecuteRange());
	}

	@Test
	public void pageResultWithTwoBehaviorTest() {
		PageResult pageResult = PageResult.buildPageResult(2,
				ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2));
		assertEquals(2, pageResult.getPageId());
		assertEquals(220, pageResult.getExecuteRange());
		assertEquals(420, pageResult.getPageEndTime());
	}

	@Test
	public void testNull() {
		this.getDataCollector().add((PageResult) null);
		assertNotNull(dataCollector.getPageBriefStatistics(0));
	}

	@Test
	public void testOnePaegWithOneBehaviorResult() {
		this.getDataCollector().add(
				PageResult.buildPageResult(2,
						ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(1)));
		AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this
				.getDataCollector().getPageBriefStatistics(2);
		assertEquals(2, pageBriefModel.getPageId());
		assertEquals(1, pageBriefModel.getCountFromBegin());
		assertEquals(1, pageBriefModel.getCountThisTime());
		assertEquals(200, pageBriefModel.getMaxResponseTimeFromBegin());
		assertEquals(200, pageBriefModel.getMinResponseTimeFromBegin());
		assertTrue(pageBriefModel.getTimeFrame() >= 0);
		assertEquals(200, pageBriefModel.getTotalResponseTimeThisTime());
		assertEquals(200, pageBriefModel.getLatestResponseTime());
	}

	@Test
	public void testOnePageWithTwoBehaviorResult() {
		this.getDataCollector().add(
				PageResult.buildPageResult(2,
						ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2)));
		AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this
				.getDataCollector().getPageBriefStatistics(2);
		System.out.println(pageBriefModel.getCountFromBegin());
		assertEquals(2, pageBriefModel.getPageId());
		assertEquals(1, pageBriefModel.getCountFromBegin());
		assertEquals(1, pageBriefModel.getCountThisTime());
		assertEquals(220, pageBriefModel.getMaxResponseTimeFromBegin());
		assertEquals(220, pageBriefModel.getMinResponseTimeFromBegin());
		assertTrue(pageBriefModel.getTimeFrame() >= 0);
		assertEquals(220, pageBriefModel.getTotalResponseTimeThisTime());
		assertEquals(220, pageBriefModel.getLatestResponseTime());
	}

	@Test
	public void testTwoPageWithStatisticsAtLast() {
		this.getDataCollector().add(
				PageResult.buildPageResult(2,
						ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(1)));
		this.getDataCollector().add(
				PageResult.buildPageResult(2,
						ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2)));
		AgentPageBriefModel pageBriefModel = (AgentPageBriefModel) this
				.getDataCollector().getPageBriefStatistics(2);
		assertEquals(2, pageBriefModel.getPageId());
		assertEquals(2, pageBriefModel.getCountFromBegin());
		assertEquals(2, pageBriefModel.getCountThisTime());
		assertEquals(220, pageBriefModel.getMaxResponseTimeFromBegin());
		assertEquals(200, pageBriefModel.getMinResponseTimeFromBegin());
		assertEquals(420, pageBriefModel.getTotalResponseTimeThisTime());
		assertTrue(pageBriefModel.getTimeFrame() >= 0);
		assertEquals(220, pageBriefModel.getLatestResponseTime());
	}

	@Test
	public void testTwoPageWithStatisticsIndividually() {
		testOnePaegWithOneBehaviorResult();
		this.getDataCollector().add(
				PageResult.buildPageResult(2,
						ScenarioStatisticsTest.makeBehaviorListWhoseBehaviorIdIsOne(2)));
		AgentPageBriefModel model = (AgentPageBriefModel) this
				.getDataCollector().getPageBriefStatistics(2);
		assertEquals(2, model.getPageId());
		assertEquals(2, model.getCountFromBegin());
		assertEquals(1, model.getCountThisTime());
		assertEquals(220, model.getMaxResponseTimeFromBegin());
		assertEquals(200, model.getMinResponseTimeFromBegin());
		assertEquals(220, model.getTotalResponseTimeThisTime());
		assertTrue(model.getTimeFrame() >= -0);
		assertEquals(220, model.getLatestResponseTime());
	}
}
