package org.bench4q.master.unitTest.repository;

import static org.junit.Assert.*;

import javax.xml.bind.JAXBException;

import org.bench4q.master.domain.factory.PluginEntityFactory;
import org.bench4q.master.domain.repository.PluginRepository;
import org.bench4q.master.exception.Bench4QException;
import org.bench4q.master.unitTest.testHelper.Test_PluginHelper;
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:service-test-context.xml" })
public class Test_PluginRepository extends Test_PluginHelper {
	private PluginRepository pluginRepository;
	private String pluginName;
	private String pluginFile;
	private int behaviorsCount;

	private PluginEntityFactory pluginEntityFactory;

	public PluginRepository getPluginRepository() {
		return pluginRepository;
	}

	@Autowired
	public void setPluginRepository(PluginRepository pluginRepository) {
		this.pluginRepository = pluginRepository;
	}

	public PluginEntityFactory getPluginEntityFactory() {
		return pluginEntityFactory;
	}

	@Autowired
	public void setPluginEntityFactory(PluginEntityFactory pluginEntityFactory) {
		this.pluginEntityFactory = pluginEntityFactory;
	}

	@Before
	public void setUp() throws Bench4QException {
		pluginFile = "hbase.xml";
		this.pluginName = addPlugin(pluginFile, this.pluginRepository);
		this.behaviorsCount = 1;
	}

	@Test
	public void testGetPlugin() throws JAXBException {
		assertNotNull(pluginName);
		assertNotNull(this.getPluginRepository().getPluginUI(pluginName));
		System.out.println(this.getPluginRepository().getPluginUI(pluginName).getPluginInfo().getName());
		assertTrue(this.getPluginRepository().getPluginUI(pluginName)
				.getPluginInfo().getGroups().size() > 0);
	}

	@Test
	public void testLoadPlugins() throws Bench4QException, JAXBException {
		this.clearPlugin(pluginName, this.pluginRepository);
		int countBeforeInsert = this.getPluginRepository().loadPlugins().size();
		System.out.println(countBeforeInsert);
		String name = addPlugin(pluginFile, this.pluginRepository);
		assertEquals(countBeforeInsert + 1, this.getPluginRepository()
				.loadPlugins().size());
		assertTrue(this.getPluginRepository().detach(name));
	}

	@Test
	public void testLoadBehaviores() {
		assertEquals(this.behaviorsCount, this.getPluginRepository()
				.loadBehaviorInfos(this.pluginName).size());
	}

	@Test
	public void testGetPluginUI() {
		assertNotNull(this.getPluginRepository().getPluginUI(pluginName));

	}

	@Test
	public void testDeletePlugin() {
		String name = addPlugin(pluginFile, this.pluginRepository);
		assertTrue(this.getPluginRepository().detach(name));
	}

	@After
	public void clear() throws Bench4QException {
	this.getPluginRepository().detach(pluginName);
	}

}
