package org.hive2hive.core.processes.logout;

import java.io.IOException;
import java.util.List;

import net.tomp2p.dht.FutureGet;

import org.hive2hive.core.H2HConstants;
import org.hive2hive.core.H2HJUnitTest;
import org.hive2hive.core.exceptions.NoPeerConnectionException;
import org.hive2hive.core.exceptions.NoSessionException;
import org.hive2hive.core.model.versioned.Locations;
import org.hive2hive.core.network.NetworkManager;
import org.hive2hive.core.network.data.parameters.Parameters;
import org.hive2hive.core.processes.ProcessFactory;
import org.hive2hive.core.security.UserCredentials;
import org.hive2hive.core.utils.FileTestUtil;
import org.hive2hive.core.utils.NetworkTestUtil;
import org.hive2hive.core.utils.TestExecutionUtil;
import org.hive2hive.core.utils.UseCaseTestUtil;
import org.hive2hive.processframework.interfaces.IProcessComponent;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests the logout procedure.
 * 
 * @author Christian, Seppi
 */
public class LogoutTest extends H2HJUnitTest {

	private static final int networkSize = 10;
	private static List<NetworkManager> network;
	private static UserCredentials userCredentials;

	@BeforeClass
	public static void initTest() throws Exception {
		testClass = LogoutTest.class;
		beforeClass();

		network = NetworkTestUtil.createNetwork(networkSize);
		userCredentials = generateRandomCredentials();

		UseCaseTestUtil.registerAndLogin(userCredentials, network.get(0), FileTestUtil.getTempDirectory());
	}

	@Test
	public void testLogout() throws ClassNotFoundException, IOException, NoSessionException, NoPeerConnectionException {
		NetworkManager client = network.get(0);

		// verify the locations map before logout
		FutureGet futureGet = client.getDataManager().getUnblocked(
				new Parameters().setLocationKey(userCredentials.getUserId()).setContentKey(H2HConstants.USER_LOCATIONS));
		futureGet.awaitUninterruptibly();
		futureGet.futureRequests().awaitUninterruptibly();
		Locations locations = (Locations) futureGet.data().object();

		Assert.assertEquals(1, locations.getPeerAddresses().size());

		// logout
		IProcessComponent<Void> process = ProcessFactory.instance().createLogoutProcess(client);
		TestExecutionUtil.executeProcessTillSucceded(process);

		// verify the locations map after logout
		FutureGet futureGet2 = client.getDataManager().getUnblocked(
				new Parameters().setLocationKey(userCredentials.getUserId()).setContentKey(H2HConstants.USER_LOCATIONS));
		futureGet2.awaitUninterruptibly();
		futureGet2.futureRequests().awaitUninterruptibly();
		Locations locations2 = (Locations) futureGet2.data().object();

		Assert.assertEquals(0, locations2.getPeerAddresses().size());
	}

	@AfterClass
	public static void endTest() {
		NetworkTestUtil.shutdownNetwork(network);
		afterClass();
	}
}

@@ -595,16 +595,68 @@
 ntials;%0A
+import org.hive2hive.core.serializer.IH2HSerialize;%0A
 import o
@@ -685,32 +685,32 @@
 s.FileTestUtil;%0A
-
 import org.hive2
@@ -1769,16 +1769,86 @@
 .get(0);
+%0A%09%09IH2HSerialize serializer = client.getDataManager().getSerializer();
 %0A%0A%09%09// v
@@ -2174,32 +2174,55 @@
 s = (Locations) 
+serializer.deserialize(
 futureGet.data()
@@ -2214,39 +2214,41 @@
 utureGet.data().
-object(
+toBytes()
 );%0A%0A%09%09Assert.ass
@@ -2804,16 +2804,39 @@
 ations) 
+serializer.deserialize(
 futureGe
@@ -2849,15 +2849,17 @@
 a().
-object(
+toBytes()
 );%0A%0A

Fix LogoutTest