/**
 * ScheduleHandlerTest.java
 * Created on Jun 6, 2008
 * (C) Copyright TANDBERG Television Ltd.
 */
package com.tandbergtv.watchpoint.pmm.job.unitTests;

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 com.tandbergtv.watchpoint.pmm.dao.hibernate.HibernateContext;
import com.tandbergtv.watchpoint.pmm.entities.JobParameter;
import com.tandbergtv.watchpoint.pmm.entities.RuleParameter;
import com.tandbergtv.watchpoint.pmm.job.handler.ScheduleAssociatedHandler;
import com.tandbergtv.watchpoint.pmm.job.util.JobScheduleInfoConstants;
import com.tandbergtv.watchpoint.pmm.schedule.ScheduleActivator;
import com.tandbergtv.watchpoint.pmm.title.TitleActivator;
import com.tandbergtv.watchpoint.pmm.util.ContainerCacheActivator;

/**
 * To run this test comment 'checkLicense' in WatchPointPluginManager, set the right
 * connection properties in hibernate.cfg.xml and hibernate.properties  
 * 
 * @author spuranik
 */
public class ScheduleHandlerTest extends TestCase {

	TitleActivator titleActivator;
	ScheduleActivator scheduleActivator;
	ContainerCacheActivator containerCacheActivator;
	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		try {
			HibernateContext.getContext().initializeContext();

			titleActivator = new TitleActivator();
			titleActivator.start();
			
			scheduleActivator = new ScheduleActivator();
			scheduleActivator.start();
			
			containerCacheActivator = new ContainerCacheActivator();
			containerCacheActivator.start();
			// wait for some time before the cache gets populated
			Thread.sleep(20000);			
		} catch (RuntimeException e) {
			System.out.println(e.getMessage());
		}
		super.setUp();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see junit.framework.TestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		try {
			HibernateContext.getContext().closeContext();

			titleActivator.stop();
			scheduleActivator.stop();
			containerCacheActivator.stop();
		} catch (RuntimeException e) {
			System.out.println(e.getMessage());
		}
		super.tearDown();
	}

	public void testJobExecution() {
		try {
			List<RuleParameter> ruleParams = new ArrayList<RuleParameter>();
			RuleParameter p1 = new RuleParameter();
			p1.setValue("6");
			RuleParameter p2 = new RuleParameter();
			p2.setValue("before");
			RuleParameter p3 = new RuleParameter();
			p3.setValue("$Schedule.PlannerDate");
			ruleParams.add(p1);
			ruleParams.add(p2);
			ruleParams.add(p3);

			List<JobParameter> jobParams = new ArrayList<JobParameter>();
			JobParameter j1 = new JobParameter();
			j1.setName("Variable");
			j1.setValue("Sometext");
			jobParams.add(j1);

			JobParameter j2 = new JobParameter();
			j2.setName("Title License Start Date");
			j2.setValue("$Title.CableLabsVOD1.1.Title License Start Date");
			jobParams.add(j2);

			JobParameter j3 = new JobParameter();
			j3.setName("Schedule Id");
			j3.setValue("$Schedule.Id");
			jobParams.add(j3);

			JobParameter j4 = new JobParameter();
			j4.setName("Planner Date");
			j4.setValue("$Schedule.PlannerDate");
			jobParams.add(j4);
			
			Map<String, Object> callbackInfo = new HashMap<String, Object>();
			callbackInfo.put(JobScheduleInfoConstants.CONTEXTID, 42L);
			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");

			ScheduleAssociatedHandler handler = new ScheduleAssociatedHandler();
			handler.executeJob(callbackInfo, new Date());
		} catch (RuntimeException e) {
			System.out.println("Error: " + e.toString());
		}
	}
}
