import net.pterodactylus.fcp.AddPeer.Trust;
import net.pterodactylus.fcp.AddPeer.Visibility;
import net.pterodactylus.fcp.AllData;
+import net.pterodactylus.fcp.EndListPeerNotes;
import net.pterodactylus.fcp.EndListPeers;
import net.pterodactylus.fcp.FcpConnection;
import net.pterodactylus.fcp.FcpListener;
import net.pterodactylus.fcp.NodeHello;
import net.pterodactylus.fcp.NodeRef;
import net.pterodactylus.fcp.Peer;
+import net.pterodactylus.fcp.PeerNote;
import net.pterodactylus.fcp.PeerRemoved;
import net.pterodactylus.fcp.ProtocolError;
import net.pterodactylus.fcp.SSKKeypair;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
+import java.util.Base64;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.BiFunction;
import java.util.function.Function;
+import static java.nio.charset.StandardCharsets.UTF_8;
import static net.pterodactylus.fcp.AddPeer.Trust.HIGH;
import static net.pterodactylus.fcp.AddPeer.Trust.LOW;
import static net.pterodactylus.fcp.AddPeer.Trust.NORMAL;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
+import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThrows;
public class FcpClientTest {
}
@Test
+ public void getPeerNoteReturnsPeerNote() throws Exception {
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("ListPeerNotes")) {
+ return (listener, connection) -> {
+ FcpMessage receivedMessage = new FcpMessage("PeerNote");
+ receivedMessage.setField("NodeIdentifier", message.getField("NodeIdentifier"));
+ receivedMessage.setField("PeerNoteType", "1");
+ receivedMessage.setField("NoteText", Base64.getEncoder().encodeToString("Peer Note".getBytes(UTF_8)));
+ listener.receivedPeerNote(connection, new PeerNote(new FcpMessage("PeerNote").put("NodeIdentifier", "different-node")));
+ listener.receivedPeerNote(connection, new PeerNote(receivedMessage));
+ listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes")));
+ };
+ }
+ return FcpClientTest::doNothing;
+ });
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ PeerNote peerNote = fcpClient.getPeerNote(createPeer());
+ assertThat(peerNote.getNodeIdentifier(), equalTo("identity"));
+ assertThat(peerNote.getPeerNoteType(), equalTo(1));
+ assertThat(peerNote.getNoteText(), equalTo("Peer Note"));
+ }
+ }
+
+ @Test
+ public void getPeerNoteWithInvalidNodeIdentifierReturnsNull() throws Exception {
+ FcpConnection fcpConnection = createFcpConnection(message -> {
+ if (message.getName().equals("ListPeerNotes")) {
+ return (listener, connection) -> listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes")));
+ }
+ return FcpClientTest::doNothing;
+ });
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ PeerNote peerNote = fcpClient.getPeerNote(createPeer());
+ assertThat(peerNote, nullValue());
+ }
+ }
+
+ @Test
public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException {
FcpConnection fcpConnection = createFcpConnection(message -> {
if (message.getName().equals("GenerateSSK")) {