package org.bench4q.master.unitTest.datastatistics;

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

import org.bench4q.master.domain.valueobject.datastatistics.PagesBriefStatistics;
import org.bench4q.share.models.agent.statistics.AgentPageBriefModel;
import org.bench4q.share.models.agent.statistics.AgentPagesBriefModel;
import org.bench4q.share.models.master.statistics.ScriptPagesBriefModel;
import org.junit.Test;

import static org.junit.Assert.*;

public class Test_PagesBrief {
	private PagesBriefStatistics pagesBriefStatistics = new PagesBriefStatistics();

	@Test
	public void testAddNull() {
		pagesBriefStatistics.add(null);
		ScriptPagesBriefModel scriptPagesBriefModel = (ScriptPagesBriefModel) pagesBriefStatistics
				.getStatistics();
		assertEquals(0, scriptPagesBriefModel.getScriptPageBriefModels().size());
	}

	@Test
	public void testAddOneWithOnePage() {
		pagesBriefStatistics.add(createAPagesBriefModel(1));
		ScriptPagesBriefModel scriptPagesBriefModel = (ScriptPagesBriefModel) pagesBriefStatistics
				.getStatistics();
		assertNotNull(scriptPagesBriefModel);
		assertEquals(220,
				scriptPagesBriefModel.getScriptPageBriefModels().get(0)
						.getMaxResponseTimeFromBegin());
	}

	private AgentPagesBriefModel createAPagesBriefModel(int size) {
		AgentPagesBriefModel agentPagesBriefModel = new AgentPagesBriefModel();
		List<AgentPageBriefModel> pageBriefModels = new ArrayList<AgentPageBriefModel>();
		for (int i = 0; i < size; i++) {
			AgentPageBriefModel pageBriefModel = new AgentPageBriefModel();
			pageBriefModel.setCountFromBegin(100);
			pageBriefModel.setCountThisTime(20);
			pageBriefModel.setLatestResponseTime(200);
			pageBriefModel.setMaxResponseTimeFromBegin(220);
			pageBriefModel.setMinResponseTimeFromBegin(200);
			pageBriefModel.setPageId(i);
			pageBriefModel.setTimeFrame(100);
			pageBriefModel.setTotalResponseTimeThisTime(20000);
			pageBriefModels.add(pageBriefModel);
		}
		agentPagesBriefModel.setPageBriefModels(pageBriefModels);
		return agentPagesBriefModel;
	}

	@Test
	public void testAddOneWithTwoPages() {
		this.pagesBriefStatistics.add(createAPagesBriefModel(2));
		ScriptPagesBriefModel scriptPagesBriefModel = (ScriptPagesBriefModel) this.pagesBriefStatistics
				.getStatistics();
		assertEquals(2, scriptPagesBriefModel.getScriptPageBriefModels().size());
		assertNotNull(scriptPagesBriefModel.getScriptPageBriefModels().get(0));
		assertEquals(220,
				scriptPagesBriefModel.getScriptPageBriefModels().get(0)
						.getMaxResponseTimeFromBegin());
		assertNotNull(scriptPagesBriefModel.getScriptPageBriefModels().get(1));
		assertEquals(220,
				scriptPagesBriefModel.getScriptPageBriefModels().get(1)
						.getMaxResponseTimeFromBegin());
	}

	@Test
	public void testAddTwoWithOnePage() {
		this.pagesBriefStatistics.add(createAPagesBriefModel(1));
		this.pagesBriefStatistics.add(createAPagesBriefModel(1));
		ScriptPagesBriefModel scriptPagesBriefModel = (ScriptPagesBriefModel) this.pagesBriefStatistics
				.getStatistics();
		assertNotNull(scriptPagesBriefModel.getScriptPageBriefModels().get(0));
		assertEquals(220,
				scriptPagesBriefModel.getScriptPageBriefModels().get(0)
						.getMaxResponseTimeFromBegin());
		assertEquals(1, scriptPagesBriefModel.getScriptPageBriefModels().size());
	}

	@Test
	public void testAddTwoWithTwoPages() {
		this.pagesBriefStatistics.add(createAPagesBriefModel(2));
		this.pagesBriefStatistics.add(createAPagesBriefModel(2));
		ScriptPagesBriefModel scriptPagesBriefModel = (ScriptPagesBriefModel) this.pagesBriefStatistics
				.getStatistics();
		assertNotNull(scriptPagesBriefModel.getScriptPageBriefModels().get(0));
		assertEquals(220,
				scriptPagesBriefModel.getScriptPageBriefModels().get(0)
						.getMaxResponseTimeFromBegin());
		assertEquals(2, scriptPagesBriefModel.getScriptPageBriefModels().size());
	}
}
