package org.bench4q.master.unitTest.repository;

import static org.junit.Assert.*;

import java.util.UUID;

import org.bench4q.master.domain.entity.TestPlan;
import org.bench4q.master.unitTest.TestBase_MakeUpTestPlan;
import org.bench4q.share.enums.master.TestPlanStatus;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:service-test-context.xml" })
public class Test_TestPlanRepository extends TestBase_MakeUpTestPlan {
	@Test
	public void testGetTestPlan() {
		TestPlan testPlan = this.getTestPlanRepository().getTestPlanInDomainBy(
				getTestPlanRunIdUuid());
		assertNotNull(testPlan);
		assertEquals(getTestPlanRunIdUuid(),
				UUID.fromString(testPlan.getTestPlanRunId()));
	}

	@Test
	public void testLoadEntities() {
		int sizeBefore = this.getTestPlanRepository()
				.loadEntities(this.getUserRepository().getUser("admin")).size();
		assertTrue(sizeBefore > 0);
		System.out.println(sizeBefore);
		submitATestPlanWithOneScript();
		int sizeAfter = this.getTestPlanRepository()
				.loadEntities(this.getUserRepository().getUser("admin")).size();
		assertEquals(sizeBefore + 1, sizeAfter);
	}

	@Test
	public void testUpdateEntity() {
		TestPlan testPlanDB = this.getTestPlanRepository()
				.getTestPlanInDomainBy(getTestPlanRunIdUuid());
		assertNotNull(testPlanDB);
		assertEquals(TestPlanStatus.NotStart.name(),
				testPlanDB.getCurrentStatus());
		testPlanDB.setCurrentStatus(TestPlanStatus.Complete.name());
		this.getTestPlanRepository().updateEntity(testPlanDB);
		TestPlan afterModify = this.getTestPlanRepository()
				.getTestPlanInDomainBy(getTestPlanRunIdUuid());
		assertEquals(TestPlanStatus.Complete.name(),
				afterModify.getCurrentStatus());
	}

	@Before
	public void prepare() {
		this.submitATestPlanWithOneScript();
	}

	@After
	public void restoreStatus() {
		this.getTestPlanRepository().detach(
				this.getTestPlanRepository()
						.getTestPlanInDomainBy(getTestPlanRunIdUuid()).getId());
	}
}
