import net.pterodactylus.fcp.RemovePeer;
import net.pterodactylus.fcp.SSKKeypair;
import net.pterodactylus.fcp.SimpleProgress;
+import net.pterodactylus.fcp.UnknownNodeIdentifier;
import net.pterodactylus.fcp.WatchGlobal;
import java.io.Closeable;
@Override
@SuppressWarnings("synthetic-access")
public void run() throws IOException {
- sendMessage(new RemovePeer(peer.getIdentity()));
+ sendMessage(new RemovePeer(createIdentifier("remove-peer"), peer.getIdentity()));
}
/**
*/
@Override
public void receivedPeerRemoved(FcpConnection fcpConnection, PeerRemoved peerRemoved) {
- complete();
+ if (peerRemoved.getNodeIdentifier().equals(peer.getIdentity())) {
+ complete();
+ }
+ }
+
+ @Override
+ public void receivedUnknownNodeIdentifier(FcpConnection fcpConnection, UnknownNodeIdentifier unknownNodeIdentifier) {
+ if (unknownNodeIdentifier.getNodeIdentifier().equals(peer.getIdentity())) {
+ complete();
+ }
}
}.execute();
}
import net.pterodactylus.fcp.NodeHello;
import net.pterodactylus.fcp.NodeRef;
import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.PeerRemoved;
+import net.pterodactylus.fcp.ProtocolError;
import net.pterodactylus.fcp.SSKKeypair;
+import net.pterodactylus.fcp.UnknownNodeIdentifier;
import org.hamcrest.Matcher;
import org.junit.Rule;
import org.junit.Test;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
+import static org.junit.Assert.assertThrows;
public class FcpClientTest {
}
@Test
+ public void removePeerSendsRemovePeerMessageWithIdentityAsNodeIdentifier() throws Exception {
+ List<FcpMessage> sentMessages = new ArrayList<>();
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("RemovePeer")) {
+ sentMessages.add(message);
+ FcpMessage peerRemovedMessage = new FcpMessage("PeerRemoved");
+ peerRemovedMessage.put("NodeIdentifier", message.getField("NodeIdentifier"));
+ return (listener, connection) -> listener.receivedPeerRemoved(connection, new PeerRemoved(peerRemovedMessage));
+ }
+ return FcpClientTest::doNothing;
+ });
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ fcpClient.removePeer(createPeer());
+ assertThat(sentMessages, contains(allOf(hasField("NodeIdentifier", equalTo("identity")))));
+ }
+ }
+
+ @Test
+ public void removePeerWithInvalidNodeIdentifierReturns() throws Exception {
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("RemovePeer")) {
+ return (listener, connection) -> listener.receivedUnknownNodeIdentifier(connection, new UnknownNodeIdentifier(message));
+ }
+ return FcpClientTest::doNothing;
+ });
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ fcpClient.removePeer(createPeer());
+ }
+ }
+
+ @Test
+ public void removePeerWithInvalidNodeIdentifierIgnoresPositiveResultForDifferentNodeIdentifier() throws Exception {
+ removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored((listener, connection) -> {
+ listener.receivedPeerRemoved(connection, new PeerRemoved(new FcpMessage("PeerRemoved").put("NodeIdentifier", "different-node")));
+ listener.receivedProtocolError(connection, new ProtocolError(new FcpMessage("ProtocolError").put("Code", "123")));
+ });
+ }
+
+ @Test
+ public void removePeerWithInvalidNodeIdentifierIgnoresNegativeResultForDifferentNodeIdentifier() throws Exception {
+ removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored((listener, connection) -> {
+ listener.receivedUnknownNodeIdentifier(connection, new UnknownNodeIdentifier(new FcpMessage("UnknownNodeIdentifier").put("NodeIdentifier", "different-node")));
+ listener.receivedProtocolError(connection, new ProtocolError(new FcpMessage("ProtocolError").put("Code", "123")));
+ });
+ }
+
+ private static void removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored(BiConsumer<FcpListener, FcpConnection> responseGenerator) {
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("RemovePeer")) {
+ return responseGenerator;
+ }
+ return FcpClientTest::doNothing;
+ });
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ FcpProtocolException fcpProtocolException = assertThrows(FcpProtocolException.class, () -> fcpClient.removePeer(createPeer()));
+ assertThat(fcpProtocolException.getCode(), equalTo(123));
+ }
+ }
+
+ @Test
public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException {
FcpConnection fcpConnection = createFcpConnection(message -> {
if (message.getName().equals("GenerateSSK")) {