package nl.erwinvaneyk.core;

import static org.junit.Assert.*;

import java.rmi.RemoteException;

import nl.erwinvaneyk.communication.exceptions.CommunicationException;
import nl.erwinvaneyk.communication.exceptions.PortAlreadyInUseException;
import org.junit.Test;

public class NodeImplTest {

	@Test
	public void startCluster() throws CommunicationException {
		Node headnode = NodeImpl.startCluster(1817, "test-cluster");
		assertEquals(headnode.getState().getAddress().getLocation().getPort(), 1817);
		assertEquals(headnode.getState().getAddress().getIdentifier(), "NodeImpl-0");
		assertEquals(headnode.getState().getClusterId(), "test-cluster");
		headnode.disconnect();
	}

	@Test
	public void shutdownNode() throws CommunicationException {
		Node node = NodeImpl.startCluster(1818, "test-cluster");
		node.disconnect();
		// Check if port has been released
		NodeImpl.startCluster(1818, "test-cluster").disconnect();
	}

	@Test
	public void joinCluster() throws CommunicationException {
		Node node1 = NodeImpl.startCluster(1819, "test-cluster");
		Node node2 = NodeImpl.connectToCluster(1820, node1.getState().getAddress());
		assertEquals(node2.getState().getClusterId(), "test-cluster");
		node1.disconnect();
		node2.disconnect();
	}


}

@@ -1243,11 +1243,650 @@
 ();%0A%09%7D%0A%0A
+%09@Test%0A%09public void joinClusterOnNonStartingNode() throws CommunicationException %7B%0A%09%09Node node1 = NodeImpl.startCluster(1819, %22test-cluster%22);%0A%09%09Node node2 = NodeImpl.connectToCluster(1820, node1.getState().getAddress());%0A%09%09Node node3 = NodeImpl.connectToCluster(1821, node2.getState().getAddress());%0A%09%09Node node4 = NodeImpl.connectToCluster(1822, node3.getState().getAddress());%0A%09%09assertEquals(node4.getState().getClusterId(), %22test-cluster%22);%0A%09%09System.out.println(node4.getState());%0A%09%09assertEquals(3, node4.getState().getConnectedNodes().size());%0A%09%09node1.disconnect();%0A%09%09node2.disconnect();%0A%09%09node3.disconnect();%0A%09%09node4.disconnect();%0A%09%7D
 %0A%7D%0A

Add test for large clusters