package org.bench4q.agent.test.scenario.engine;

import static org.junit.Assert.*;

import java.util.Date;

import org.bench4q.agent.scenario.engine.Schedule;
import org.bench4q.agent.scenario.engine.Schedule.Segment;
import org.bench4q.agent.test.TestBase;
import org.bench4q.share.exception.Bench4QRunTimeException;
import org.bench4q.share.models.agent.scriptrecord.ScheduleModel;
import org.bench4q.share.models.agent.scriptrecord.ScheduleModel.PointModel;
import org.junit.Test;

public class Test_Shedule {

	@Test(expected = Bench4QRunTimeException.class)
	public void test_InitWithZeroPointModels() {
		Schedule.build(new ScheduleModel());
	}

	@Test(expected = Bench4QRunTimeException.class)
	public void test_InitWithOnePointModels() {
		ScheduleModel model = new ScheduleModel();
		model.getPoints().add(new PointModel(new Date().getTime(), 100));
		Schedule.build(model);
	}

	@Test
	public void test_InitWithTwoPoint_WhereSecondTime_LT_FirstTime() {
		ScheduleModel model = new ScheduleModel();
		long firstTime = new Date().getTime();
		model.getPoints().add(new PointModel(firstTime, 100));
		model.getPoints().add(new PointModel(firstTime - 1000000, 50));
		Schedule schedule = Schedule.build(model);
		assertEquals(1, schedule.getSegments().size());
	}

	@Test
	public void test_InitWith5Point() {
		long firstTime = new Date().getTime();
		ScheduleModel model = new ScheduleModel();
		for (int i = 0; i < 5; i++) {
			model.getPoints().add(
					new PointModel(firstTime - 10000 * i, 190 + 10 * i));
		}
		model.getPoints().add(new PointModel(new Date().getTime(), 600));
		Schedule schedule = Schedule.build(model);
		assertEquals(5, schedule.getSegments().size());
	}

	@Test(expected = Bench4QRunTimeException.class)
	public void test_InitWith_MinusTime() {
		long firstTime = -1000000000;
		ScheduleModel model = new ScheduleModel();
		model.getPoints().add(new PointModel(firstTime, 100));
		Schedule.build(model);
	}

	@Test(expected = IllegalArgumentException.class)
	public void test_LoadFor_WithIlegalArgument() {
		ScheduleModel model = new ScheduleModel();
		long firstTime = new Date().getTime();
		model.getPoints().add(new PointModel(firstTime, 100));
		model.getPoints().add(new PointModel(firstTime - 1000000, 50));
		Schedule schedule = Schedule.build(model);
		schedule.getSegments().get(0).loadFor(-1000);
	}

	@Test
	public void test_loadFor() {
		ScheduleModel model = new ScheduleModel();
		model.getPoints().add(new PointModel(1000000, 100));
		model.getPoints().add(new PointModel(0, 50));
		Schedule schedule = Schedule.build(model);
		int load = schedule.getSegments().get(0).loadFor(500 * 1000);
		assertEquals(75, load);
	}

	@Test
	public void test_getSegment() {
		ScheduleModel model = new ScheduleModel();
		model.getPoints().add(new PointModel(1000000, 100));
		model.getPoints().add(new PointModel(0, 50));
		Schedule schedule = Schedule.build(model);
		Segment segment = schedule.getSegment(500 * 1000);
		assertNotNull(segment);
	}

	// TODO : add more test about getSegment, you will got some error

	@Test
	public void test_getScheduleRange() {
		Schedule schedule = Schedule.build(TestBase.buildScheduleModel());
		assertEquals(60 * 1000, schedule.getScheduleRange());
	}
}
