package org.hive2hive.core.processes.notify;

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

import net.tomp2p.dht.FutureGet;
import net.tomp2p.peers.PeerAddress;

import org.hive2hive.core.H2HJUnitTest;
import org.hive2hive.core.H2HTestData;
import org.hive2hive.core.exceptions.NoPeerConnectionException;
import org.hive2hive.core.network.NetworkManager;
import org.hive2hive.core.network.data.parameters.Parameters;
import org.hive2hive.core.network.messages.direct.BaseDirectMessage;
import org.hive2hive.core.network.userprofiletask.TestUserProfileTask;
import org.hive2hive.core.network.userprofiletask.UserProfileTask;
import org.junit.Assert;

/**
 * Simple message factory for testing. It counts the received messages by checking whether the
 * {@link TestDirectNotificationMessage} has put the ordered content (indicating that it arrived)
 * 
 * @author Nico
 */
public class CountingNotificationMessageFactory extends BaseNotificationMessageFactory {

	private final NetworkManager sender;
	private final List<String> testContentKeys;
	private final H2HTestData data = new H2HTestData(H2HJUnitTest.randomString());

	public CountingNotificationMessageFactory(NetworkManager sender) {
		super(sender.getEncryption());
		this.sender = sender;
		testContentKeys = new ArrayList<String>();
	}

	public boolean allMsgsArrived() {
		return getSentMessageCount() == getArrivedMessageCount();
	}

	public int getSentMessageCount() {
		return testContentKeys.size();
	}

	public int getArrivedMessageCount() {
		int counter = 0;
		for (String contentKey : testContentKeys) {
			FutureGet futureGet = null;
			try {
				futureGet = sender.getDataManager().getUnblocked(
						new Parameters().setLocationKey(sender.getNodeId()).setContentKey(contentKey));
				futureGet.awaitUninterruptibly();
			} catch (NoPeerConnectionException e1) {
				Assert.fail();
			}

			if (futureGet.data() == null) {
				continue;
			}

			try {
				H2HTestData gotData = (H2HTestData) futureGet.data().object();
				if (gotData.getTestString().equalsIgnoreCase(data.getTestString())) {
					counter++;
				}
			} catch (ClassNotFoundException | IOException e) {
				Assert.fail();
			}
		}

		return counter;
	}

	@Override
	public BaseDirectMessage createPrivateNotificationMessage(PeerAddress receiver) {
		String contentKey = H2HJUnitTest.randomString();
		testContentKeys.add(contentKey);
		return new TestDirectNotificationMessage(receiver, sender.getNodeId(), contentKey, data);
	}

	@Override
	public UserProfileTask createUserProfileTask(String sender) {
		return new TestUserProfileTask(generateProtectionKeys());
	}

	@Override
	public BaseDirectMessage createHintNotificationMessage(PeerAddress receiver, String userId) {
		// trick a little bit: send the same message to other users. We are able to control the arrival.
		return createPrivateNotificationMessage(receiver);
	}

}

@@ -1994,16 +1994,75 @@
 stData) 
+sender.getDataManager().getSerializer()%0A%09%09%09%09%09%09.deserialize(
 futureGe
@@ -2074,15 +2074,17 @@
 a().
-object(
+toBytes()
 );%0A%09
@@ -2227,16 +2227,44 @@
 ception 
+%7C NoPeerConnectionException 
 e) %7B%0A%09%09%09
@@ -2268,32 +2268,46 @@
 %09%09%09%09Assert.fail(
+e.getMessage()
 );%0A%09%09%09%7D%0A%09%09%7D%0A%0A%09%09r

Fix notification test