package com.tandbergtv.metadatamanager;

import static org.springframework.transaction.TransactionDefinition.PROPAGATION_REQUIRED;

import java.util.List;

import junit.framework.TestCase;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import com.tandbergtv.metadatamanager.exception.MetadataException;
import com.tandbergtv.metadatamanager.exception.SearchException;
import com.tandbergtv.metadatamanager.model.Rule;
import com.tandbergtv.metadatamanager.model.RuleSet;
import com.tandbergtv.metadatamanager.search.AssetSearchService;

/**
 * Test cases to unit test rule manager methods.
 * 
 * @author nicholas
 * 
 */
public class RuleManagerUnitTest extends TestCase {

	private static final String JPF_PLUGIN_REPOSITORY_PROPERTY = "org.java.plugin.boot.pluginsRepositories";
	public static final String EXTERNAL_REVISION_TEST_EXTERNAL = "testExternal";
	public static String textFileExtension = ".xml";
	protected ApplicationContext context;
	protected AssetSearchService searchService;
	protected PlatformTransactionManager txmgr;
	private RuleManagerDAOImpl daoImpl;
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		
		this.context = new ClassPathXmlApplicationContext(new String[] {
				"MetadataBeansContext.xml",
				"file:tests/MetadataManager_JTA_DBContext_UnitTest.xml",
				"file:tests/DataSource_UnitTest.xml" });
		searchService = (AssetSearchService) context.getBean("assetSearch");
		txmgr = (PlatformTransactionManager) context
				.getBean("transactionManager");
		
		daoImpl = (RuleManagerDAOImpl) context.getBean("ruleManagerDAOImpl");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		daoImpl.getTemplate().getSessionFactory().close();
		super.tearDown();
	}

	public void testSaveRuleSet() {
		try {
			TransactionStatus status = txmgr
			.getTransaction(new DefaultTransactionDefinition(
					PROPAGATION_REQUIRED));
			
			RuleSet ruleSet = new RuleSet();
			ruleSet.setName("new rule set");
			ruleSet.setSpec("MYSPEC");
			Rule rule = new Rule();
			rule.setXpath("/a/b/c");
			rule.setIsRequired(true);
			ruleSet.addRule(rule);
			System.out.println(ruleSet);
			daoImpl.saveRuleSet(ruleSet);

			txmgr.commit(status);
			
			System.out.println("Created single rule set");
		} catch (MetadataException e) {
			e.printStackTrace();
		}
	}

	public void testGetAllRuleSetNames() {
		try {
			TransactionStatus status = txmgr
			.getTransaction(new DefaultTransactionDefinition(
					PROPAGATION_REQUIRED));
			
			List<String> ruleSets = daoImpl.getAllRuleSetNames("MYSPEC");
			
			txmgr.commit(status);
			
			
			for(String ruleSet : ruleSets)
				System.out.println("Name: " + ruleSet + "\n");

			System.out.println("Got list of rule set names");
		} catch (MetadataException e) {
			e.printStackTrace();
		}
	}

	public void testGetRuleSet() {
		try {
			TransactionStatus status = txmgr
			.getTransaction(new DefaultTransactionDefinition(
					PROPAGATION_REQUIRED));
			
			RuleSet ruleSet = daoImpl.getRuleSet(1);
			
			txmgr.commit(status);
			
			
			System.out.println(ruleSet);
			System.out.println("Got rule set");
		} catch (SearchException e) {
			e.printStackTrace();
		}
	}

	public void testLookupRuleSet() {
		try {
			TransactionStatus status = txmgr
			.getTransaction(new DefaultTransactionDefinition(
					PROPAGATION_REQUIRED));
			
			RuleSet ruleSet = daoImpl.lookupRuleSet("new rule set", "MYSPEC");
			
			txmgr.commit(status);
			
			
			System.out.println(ruleSet);
			System.out.println("Looked up rule set");
		} catch (SearchException e) {
			e.printStackTrace();
		}
	}

	public void testDeleteRuleSet() {
		try {
			TransactionStatus status = txmgr
			.getTransaction(new DefaultTransactionDefinition(
					PROPAGATION_REQUIRED));
			
			daoImpl.deleteRuleSet(1);
			
			txmgr.commit(status);
			
			
			System.out.println("Deleted rule set");
		} catch (SearchException e) {
			e.printStackTrace();
		}
	}
}
