package com.github.sbouclier.jaads;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class NodeTest {

	@Test
	public void should_retrieve_node_value() {
		Node<String> node = new Node<>("ABC");
		assertEquals("ABC", node.getValue());
	}

	@Test
	public void should_set_node_max_degree() {
		Node<Integer> node1 = new Node<>(65);
		Node<Integer> node2 = new Node<>(65, 8);

		assertEquals(Node.MAX_DEGREE, node1.getMaxDegree());
		assertEquals(8, node2.getMaxDegree());
	}

	@Test
	public void should_retrieve_node_degree() {
		Node<Integer> node = new Node<>(1);

		assertEquals(0, node.getDegree());

		node.addChild(new Node<Integer>(2));
		node.addChild(new Node<Integer>(3));
		node.addChild(new Node<Integer>(4));
		node.addChild(null);

		assertEquals(3, node.getDegree());
	}

	@Test
	public void should_filter_null_for_degrees() {
		Node<Integer> node = new Node<>(1);
		node.addChild(new Node<Integer>(2));
		node.addChild(null);

		assertEquals(2, node.children.size());
		assertEquals(1, node.getDegree());
	}

	@Test
	public void should_create_child_nodes() {
		Node<Integer> node = new Node<>(1);

		node.addChild(new Node<Integer>(2));
		node.addChild(new Node<Integer>(3));

		assertEquals(2, node.children.size());
		assertEquals(2, node.getChild(0).getValue().intValue());
		assertEquals(3, node.getChild(1).getValue().intValue());
	}

	@Test
	public void node_without_children_is_a_leaf() {
		Node<Integer> node1 = new Node<>(1);
		Node<Integer> node2 = new Node<>(2);
		Node<Integer> node3 = new Node<>(3);
		Node<Integer> node4 = new Node<>(4);

		node1.addChild(node2);
		node2.addChild(node3);
		node2.addChild(node4);
		node4.addChild(null);

		assertFalse(node1.isLeaf());
		assertFalse(node2.isLeaf());
		assertTrue(node3.isLeaf());
		assertTrue(node4.isLeaf());
	}

	@Test(expected = NodeOutOfBoundException.class)
	public void should_throw_exception_when_children_node_max_size_is_reached() {
		Node<Integer> node = new Node<>(1, 2);
		node.addChild(new Node<Integer>(2));
		node.addChild(new Node<Integer>(3));
		
		assertEquals(2, node.children.size());

		node.addChild(new Node<Integer>(4));
	}
}

@@ -187,16 +187,73 @@
 .Test;%0A%0A
+/**%0A * Node test%0A * %0A * @author St%C3%A9phane Bouclier%0A *%0A */%0A
 public c
@@ -2206,18 +2206,16 @@
 r%3E(3));%0A
-%09%09
 %0A%09%09asser
@@ -2291,10 +2291,231 @@
 4));%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void should_get_to_string() %7B%0A%09%09Node%3CString%3E node = new Node%3C%3E(%22ABC%22);%0A%09%09node.addChild(new Node%3CString%3E(%22DEF%22));%0A%0A%09%09assertEquals(%22Node %5BmaxDegree=32767, value=ABC, children.size=1%5D%22, node.toString());%0A%09%7D%0A
 %7D%0A

add toString() test