package org.bench4q.master.unitTest.service;

import static org.junit.Assert.*;

import java.util.List;

import javax.xml.bind.JAXBException;

import org.bench4q.master.domain.entity.Agent;
import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.domain.entity.User;
import org.bench4q.master.domain.factory.TestPlanFactory;
import org.bench4q.master.domain.repository.TestPlanRepository;
import org.bench4q.master.domain.service.AgentService;
import org.bench4q.master.domain.service.MonitorResultService;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.bench4q.share.models.monitor.MemoryModel;
import org.bench4q.share.models.monitor.NetworkInterfaceModel;
import org.bench4q.share.models.monitor.PhysicalDiskModel;
import org.bench4q.share.models.monitor.ProcessorModel;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:mockHttpMesseger-context.xml" })
public class Test_MonitorResultService extends TestBase_MakeUpTestPlan {
	private String monitor = "133.133.12.3";
	private int port = 5556;
	private MonitorResultService monitorResultService;
	private TestPlanRepository testPlanRepository;
	private TestPlanFactory testPlanFactory;
	@Autowired
	private AgentService agentService;

	public TestPlanFactory getTestPlanFactory() {
		return testPlanFactory;
	}

	@Autowired
	public void setTestPlanFactory(TestPlanFactory testPlanFactory) {
		this.testPlanFactory = testPlanFactory;
	}

	private MonitorResultService getMonitorResultService() {
		return monitorResultService;
	}

	@Autowired
	private void setMonitorResultService(
			MonitorResultService monitorResultService) {
		this.monitorResultService = monitorResultService;
	}

	public TestPlanRepository getTestPlanRepository() {
		return testPlanRepository;
	}

	@Autowired
	public void setTestPlanRepository(TestPlanRepository testPlanRepository) {
		this.testPlanRepository = testPlanRepository;
	}

	@Test
	public void test() {
		assertNotNull(this.getMonitorResultService());
	}

	@Before
	public void makeUpTestPlanAndTestPlanScriptAndTestPlanScriptResult()
			throws JAXBException {
		this.agentService.addAgentToPool(Agent.createAgentWithoutId(
				"127.0.0.1", 6565, 500));
		prepareForTestPlanRunning();
	}

	@After
	public void cleanUp() {
		this.getAgentRepository().detach(
				this.getAgentRepository().getAgentBy("127.0.0.1").getId());
	}

	@Test
	public void testLoadMemoryResults() throws InterruptedException,
			Bench4QException {
		assertNotNull(this.getAgentMessenger()
				.getStatus(
						Agent.createAgentWithoutId(Test_AGENT_HOSTNAME,
								TEST_PORT, 500)));
		User user = this.getUserRepository().getUser("admin");
		int scriptId = getUserFirstScript(user);
		this.getHaPool().checkAllHeartBeat();
		this.setTestPlanRunIdUuid(this.getTestPlanEngine().runWith(
				createATestPlanWithOneScript(scriptId), user));
		Thread.sleep(500);
		TestPlan testPlanInDomain = this.getTestPlanFactory().convertToDomain(
				this.getTestPlanRepository().getTestPlanInDomainBy(
						getTestPlanRunIdUuid()));
		assertEquals(TestPlanStatus.InRunning.name(),
				testPlanInDomain.getCurrentStatus());
		Thread.sleep(4000);
		TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
				getTestPlanRunIdUuid());
		assertEquals(TestPlanStatus.InRunning,
				TestPlanStatus.valueOf(testPlan.getCurrentStatus()));
		List<PhysicalDiskModel> logicalDiskModels = this
				.getMonitorResultService()
				.physicalDiskDiskResults(this.getTestPlanRunIdUuid(), monitor,
						port, 0).getPhysicalDiskModels();
		List<MemoryModel> memoryModels = this
				.getMonitorResultService()
				.loadMemoryModels(this.getTestPlanRunIdUuid(), monitor, port, 0)
				.getMemoryModels();
		List<ProcessorModel> processorModels = this
				.getMonitorResultService()
				.loadProcessorModels(this.getTestPlanRunIdUuid(), monitor,
						port, 0).getProcessorModels();
		List<NetworkInterfaceModel> networkInterfaceModels = this
				.getMonitorResultService()
				.loadNetworkInterfaceModels(this.getTestPlanRunIdUuid(),
						monitor, port, 0).getModels();
		assertEquals(1, processorModels.size());
		assertTrue(processorModels.size() >= 1);
		assertTrue(logicalDiskModels.size() > 0);
		assertTrue(memoryModels.size() > 0);

		assertTrue(networkInterfaceModels.size() >= 1);

		cleanUpForTestPlanRunning();
	}
}
