/**
 * 
 */
package com.tandbergtv.spec;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.tandbergtv.metadatamanager.model.Field;
import com.tandbergtv.metadatamanager.model.Item;
import com.tandbergtv.metadatamanager.model.NextRevision;
import com.tandbergtv.metadatamanager.specimpl.ttv.TTVSpecHandler;

/**
 * @author vgoyal
 * 
 */
public class MergeUnitTest extends TestCase {

	private ApplicationContext context;
	private NextRevision nextRevision = new NextRevision();

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		super.setUp();
		this.context = new ClassPathXmlApplicationContext(new String[] {
				"file:tests/MetadataBeansContext.xml",
				"file:tests/MetadataManager_JTA_DBContext_UnitTest.xml",
				"file:tests/DataSource_UnitTest.xml" });

	}

	/**
	 * @throws java.lang.Exception
	 */
	@After
	public void tearDown() throws Exception {
		super.tearDown();
	}


	public void testTTVmerge() {
		List<Field> oldFields = new ArrayList<Field>();

		oldFields.add(createField("/tns:Fields/tns:AssetName",
				"CaptainCorellisMandolinpackage", "1,1"));
		//cl1.1 field
		oldFields.add(createField("/tns:Fields/tns:AssetClass", "package",
				"1,1"));

		Item oldItem = new Item();
		oldItem.setFields(oldFields);

		List<Field> newFields = new ArrayList<Field>();
		newFields.add(createField("/tns:Fields/tns:AssetName",
				"CaptainCorellisMandolinpackageNEW", "1,1"));

		Item newItem = new Item();
		newItem.setFields(newFields);
		
		TTVSpecHandler handler = (TTVSpecHandler)context.getBean("TTVSpecHandler");
		handler.mergeFields(newItem, oldItem, "title", nextRevision);

		System.out.println("TTV Fields Merge:");
		for (Field f : oldFields) {
			System.out.println(f.getTtvXPath() + " - " + f.getValue());
		}
		
	}

	private Field createField(String xpath, String value, String indices) {
		Field f = new Field();
		f.setTtvXPath(xpath);
		f.setValue(value);

		String[] i = indices.split(",");
		for (String index : i) {
			f.getIndices().add(Integer.parseInt(index));
		}

		return f;
	}

}
