import net.pterodactylus.fcp.NodeRef;
import net.pterodactylus.fcp.Peer;
import net.pterodactylus.fcp.PeerNote;
-import net.pterodactylus.fcp.PeerNoteType;
import net.pterodactylus.fcp.PeerRemoved;
import net.pterodactylus.fcp.PersistentGet;
import net.pterodactylus.fcp.PersistentPut;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Function;
+import java.util.function.Predicate;
import static java.nio.charset.StandardCharsets.UTF_8;
import static net.pterodactylus.fcp.AddPeer.Trust.HIGH;
}
private static void removePeerAndVerifyThatNodeIdentifierIsNotBeingIgnored(BiConsumer<FcpListener, FcpConnection> responseGenerator) {
- FcpConnection fcpConnection = createFcpConnection(message -> {
- if (message.getName().equals("RemovePeer")) {
- return responseGenerator;
- }
- return FcpClientTest::doNothing;
- });
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("RemovePeer"), responseGenerator);
try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
FcpProtocolException fcpProtocolException = assertThrows(FcpProtocolException.class, () -> fcpClient.removePeer(createPeer()));
assertThat(fcpProtocolException.getCode(), equalTo(123));
@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;
- });
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPeerNotes"), (listener, connection) -> listener.receivedEndListPeerNotes(connection, new EndListPeerNotes(new FcpMessage("EndListPeerNotes"))));
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")) {
- return ((listener, connection) -> listener.receivedSSKKeypair(connection, new SSKKeypair(new FcpMessage("SSKKeypair").put("InsertURI", "insert-uri").put("RequestURI", "request-uri"))));
- }
- return FcpClientTest::doNothing;
- });
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("GenerateSSK"), (listener, connection) -> listener.receivedSSKKeypair(connection, new SSKKeypair(new FcpMessage("SSKKeypair").put("InsertURI", "insert-uri").put("RequestURI", "request-uri"))));
try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
SSKKeypair keypair = fcpClient.generateKeyPair();
assertThat(keypair.getInsertURI(), equalTo("insert-uri"));
@Test
public void getGetRequestsReturnsGetRequests() throws IOException, FcpException {
- FcpConnection fcpConnection = createFcpConnection(message -> {
- if (message.getName().equals("ListPersistentRequests")) {
- return (listener, connection) -> {
- listener.receivedPersistentGet(connection, new PersistentGet(new FcpMessage("PersistentGet").put("Identifier", "get1")));
- listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put1")));
- listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put2")));
- listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir1")));
- listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir2")));
- listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir3")));
- listener.receivedEndListPersistentRequests(connection, new EndListPersistentRequests(new FcpMessage("EndListPersistentRequests")));
- };
- }
- return FcpClientTest::doNothing;
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), (listener, connection) -> {
+ listener.receivedPersistentGet(connection, new PersistentGet(new FcpMessage("PersistentGet").put("Identifier", "get1")));
+ listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put1")));
+ listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put2")));
+ listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir1")));
+ listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir2")));
+ listener.receivedPersistentPutDir(connection, new PersistentPutDir(new FcpMessage("PersistentPutDir").put("Identifier", "putdir3")));
+ listener.receivedEndListPersistentRequests(connection, new EndListPersistentRequests(new FcpMessage("EndListPersistentRequests")));
});
try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
Collection<Request> getRequests = fcpClient.getGetRequests(false);
return createFcpConnection(m -> FcpClientTest::doNothing);
}
+ private static FcpConnection createFcpConnectionReactingToSingleMessage(Predicate<FcpMessage> messageFilter, BiConsumer<FcpListener, FcpConnection> messageConsumer) {
+ return createFcpConnection(message -> {
+ if (messageFilter.test(message)) {
+ return messageConsumer;
+ }
+ return FcpClientTest::doNothing;
+ });
+ }
+
private static FcpConnection createFcpConnection(Function<FcpMessage, BiConsumer<FcpListener, FcpConnection>> messageConsumer) {
return new FcpConnection() {
private final List<FcpListener> listeners = new ArrayList<>();
};
}
+ private static Predicate<FcpMessage> named(String name) {
+ return message -> message.getName().equals(name);
+ }
+
@Rule
public final Timeout timeout = Timeout.seconds(5);