import java.util.ArrayList;
import org.junit.Test;


/**
 * Test Cases
 */

public class GDI2BTreeTest 
{
	/*
	 * Write your own tests to thoroughly check your implementation.
	 * Passing the following tests does not mean that your implementation is 100% correct.
	 * 
	 */
	@Test(expected=GDI2BTreeFileFormatException.class)
	public void treeTest0()
	{
		// read data
		GDI2BTreeDataReader dataReader = new GDI2BTreeDataReader("test_03");
		// create tree and root
		GDI2BTree tree = new GDI2BTree();
		GDI2BTreeNode root = new GDI2BTreeNode(null);
		// initialise root
		root.setData(new ArrayList<String>());
		root.setChildren(new ArrayList<GDI2BTreeNode>());
		// read order
		int order = dataReader.readOrder();
		// initialise tree
		tree.setOrder(order);
		tree.setRoot(root);
		
		String[] commandKey;
		while ((commandKey = dataReader.getNextLine()) != null)
		{
			if (commandKey[0].equals("add"))
			{
				root.getData().add(commandKey[1]); 	// simulating addData()
				System.out.println("Add: " + commandKey[1]);
				tree.correctnessCheckPublic();		// check the correctness of the B-Tree
			}
			else if (commandKey[0].equals("del"))
			{
				System.out.println("Del: " + commandKey[1]);
			}
		}
		dataReader.closeGDI2BTreeDataReader();	// close the stream
	}
	@Test(expected=GDI2BTreeFileFormatException.class)
	public void treeTest1()
	{
		// read data
		GDI2BTreeDataReader dataReader = new GDI2BTreeDataReader("test_04");
		// create tree and root
		GDI2BTree tree = new GDI2BTree();
		GDI2BTreeNode root = new GDI2BTreeNode(null);
		// initialise root
		root.setData(new ArrayList<String>());
		root.setChildren(new ArrayList<GDI2BTreeNode>());
		// read order
		int order = dataReader.readOrder();
		// initialise tree
		tree.setOrder(order);
		tree.setRoot(root);
		
		String[] commandKey;
		while ((commandKey = dataReader.getNextLine()) != null)
		{
			if (commandKey[0].equals("add"))
			{
				root.getData().add(commandKey[1]); // simulating addData()
				System.out.println("Add: " + commandKey[1]);
				tree.correctnessCheckPublic();	// check the correctness of the B-Tree
			}
			else if (commandKey[0].equals("del"))
			{
				System.out.println("Del: " + commandKey[1]);
			}
		}
		dataReader.closeGDI2BTreeDataReader();	// close the stream
		// until here everything should go well

		// adding incorrectly a child node
		GDI2BTreeNode child = new GDI2BTreeNode(root);
		root.getChildren().add(child);
		// the leaf variable is not set to false
		// hasChildren() has to be correctly implemented to raise the exception here
		tree.correctnessCheckPublic();	// check the correctness of the B-Tree
	}
}
