/**
 * TitleHandlerTest.java
 * Created on Jun 5, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.unitTests;

import static com.tandbergtv.metadatamanager.model.FieldName.FILE_URL;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;

import com.tandbergtv.watchpoint.pmm.boot.Platform;
import com.tandbergtv.watchpoint.pmm.entities.JobParameter;
import com.tandbergtv.watchpoint.pmm.entities.RuleParameter;
import com.tandbergtv.watchpoint.pmm.job.callback.TitleCallback;
import com.tandbergtv.watchpoint.pmm.job.util.JobScheduleInfoConstants;
import com.tandbergtv.watchpoint.pmm.title.TitleActivator;

/**
 * to run this test the spec manager's path needs to be set right.
 * 
 * @author spuranik
 *
 */
public class TitleHandlerTest extends TestCase {
	
	private static final Logger logger = Logger.getLogger(TitleHandlerTest.class);

	TitleActivator titleActivator = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		try {
			Platform.getPlatform().start();
		} catch (Throwable e) {
			logger.error("Oh noes!", e);
			throw (Exception) e;
		}
		
		super.setUp();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		try {
			Platform.getPlatform().stop();
		} catch (RuntimeException e) {
			logger.error("Oh noes!", e);
			throw e;
		}
		
		super.tearDown();
	}

	public void testJobExecution() {
		try {
			List<RuleParameter> ruleParams = new ArrayList<RuleParameter>();
			RuleParameter p1 = new RuleParameter();
			p1.setValue("1");
			RuleParameter p2 = new RuleParameter();
			p2.setValue("after");
			RuleParameter p3 = new RuleParameter();
			p3.setValue("TITLE./tns:Fields/tns:Rights/tns:LicensingWindowEnd");
			ruleParams.add(p1);
			ruleParams.add(p2);
			ruleParams.add(p3);

			List<JobParameter> jobParams = new ArrayList<JobParameter>();
			JobParameter j1 = new JobParameter();
			j1.setName("Movie ID");
			j1.setValue("MOVIE.Id");
			jobParams.add(j1);

			JobParameter j2 = new JobParameter();
			j2.setName("Movie License Start Date");
			j2.setValue("TITLE./tns:Fields/tns:Rights/tns:LicensingWindowStart");
			jobParams.add(j2);

			JobParameter j3 = new JobParameter();
			j3.setName("Movie Runtime");
			j3.setValue("MOVIE-FILE." + FILE_URL.toString());
			jobParams.add(j3);
			
			JobParameter j4 = new JobParameter();
			j4.setName("Title ID");
			j4.setValue("/usr/bin");
			jobParams.add(j4);

			Map<String, Object> callbackInfo = new HashMap<String, Object>();
			callbackInfo.put(JobScheduleInfoConstants.JOB_RULE_PARAMETERS, ruleParams);
			callbackInfo.put(JobScheduleInfoConstants.JOB_RULE_NAME, "NDaysFromMetadata");
			callbackInfo.put(JobScheduleInfoConstants.JOB_PARAMETERS, jobParams);
			callbackInfo.put(JobScheduleInfoConstants.JOB_SELECTED_TEMPLATE_NAME, "TestTemplate1");
			callbackInfo.put(JobScheduleInfoConstants.JOB_PRIORITY, "Normal");

			new TitleCallback().executeJob(callbackInfo, new Date());
		} catch (Throwable t) {
			logger.error("Crap", t);
			throw new RuntimeException(t);
		}
	}
}
