import net.pterodactylus.fcp.AddPeer.Trust;
import net.pterodactylus.fcp.AddPeer.Visibility;
import net.pterodactylus.fcp.AllData;
+import net.pterodactylus.fcp.DataFound;
import net.pterodactylus.fcp.EndListPeerNotes;
import net.pterodactylus.fcp.EndListPeers;
import net.pterodactylus.fcp.EndListPersistentRequests;
import java.util.function.Predicate;
import static java.nio.charset.StandardCharsets.UTF_8;
+import static java.util.Arrays.stream;
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 net.pterodactylus.fcp.AddPeer.Visibility.YES;
import static net.pterodactylus.fcp.test.InputStreamMatchers.streamContaining;
import static net.pterodactylus.fcp.test.Matchers.hasField;
+import static net.pterodactylus.fcp.test.Matchers.matches;
import static net.pterodactylus.fcp.test.NodeRefs.createNodeRef;
import static net.pterodactylus.fcp.test.PeerMatchers.peerWithIdentity;
import static net.pterodactylus.fcp.test.Peers.createPeer;
}
}
+ @Test
+ public void getGetRequestsIsCompleteWhenDataFoundMessageIsReceived() throws IOException, FcpException {
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), sendRequests(this::sendRequests, this::sendDataFound));
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ Collection<Request> requests = fcpClient.getGetRequests(false);
+ assertThat(requests, contains(isGetRequest(
+ equalTo("get1"),
+ matches("complete", Request::isComplete),
+ matches("content type", m -> m.getContentType().equals("application/test")),
+ matches("data length", m -> m.getLength() == 12345L)
+ )));
+ }
+ }
+
+ private void sendDataFound(FcpListener listener, FcpConnection connection) {
+ listener.receivedDataFound(connection, new DataFound(
+ new FcpMessage("DataFound").put("Identifier", "get1").put("Metadata.ContentType", "application/test").put("DataLength", "12345"))
+ );
+ }
+
private void sendRequests(FcpListener listener, FcpConnection connection) {
listener.receivedPersistentGet(connection, new PersistentGet(new FcpMessage("PersistentGet").put("Identifier", "get1").put("Global", "false")));
listener.receivedPersistentPut(connection, new PersistentPut(new FcpMessage("PersistentPut").put("Identifier", "put1").put("Global", "false")));
listener.receivedEndListPersistentRequests(connection, new EndListPersistentRequests(new FcpMessage("EndListPersistentRequests")));
}
+ @SafeVarargs
+ private final BiConsumer<FcpListener, FcpConnection> sendRequests(BiConsumer<FcpListener, FcpConnection>... messageConsumers) {
+ return (listener, connection) -> {
+ stream(messageConsumers).forEach(consumer -> consumer.accept(listener, connection));
+ };
+ }
+
private static void doNothing(FcpListener listener, FcpConnection connection) {
// do nothing.
}
package net.pterodactylus.fcp.test;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
-
import net.pterodactylus.fcp.FcpMessage;
-
+import net.pterodactylus.fcp.highlevel.Request;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeDiagnosingMatcher;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.function.Predicate;
+
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
};
}
+ public static <R extends Request> Matcher<R> matches(String name, Predicate<R> predicate) {
+ return new TypeSafeDiagnosingMatcher<R>() {
+ @Override
+ protected boolean matchesSafely(R item, Description mismatchDescription) {
+ if (!predicate.test(item)) {
+ mismatchDescription.appendText("not ").appendValue(name);
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public void describeTo(Description description) {
+ description.appendText(name);
+ }
+ };
+ }
+
}
return isGetRequest(anything());
}
- public static Matcher<Request> isGetRequest(Matcher<? super String> identifier) {
- return isRequest(instanceOf(GetRequest.class), identifier);
+ @SafeVarargs
+ public static Matcher<Request> isGetRequest(Matcher<? super String> identifier, Matcher<? super Request>... requestMatchers) {
+ return isRequest(instanceOf(GetRequest.class), identifier, requestMatchers);
}
public static Matcher<Request> isPutRequest() {
return isRequest(instanceOf(PutRequest.class), identifier);
}
- private static TypeSafeDiagnosingMatcher<Request> isRequest(Matcher<? super Class<?>> requestClass, Matcher<? super String> identifier) {
+ @SafeVarargs
+ private static TypeSafeDiagnosingMatcher<Request> isRequest(Matcher<? super Class<?>> requestClass, Matcher<? super String> identifier, Matcher<? super Request>... requestMatchers) {
return new TypeSafeDiagnosingMatcher<Request>() {
@Override
protected boolean matchesSafely(Request item, Description mismatchDescription) {
mismatchDescription.appendText("identifier is ").appendValue(item.getIdentifier());
return false;
}
+ for (Matcher<? super Request> requestMatcher : requestMatchers) {
+ if (!requestMatcher.matches(item)) {
+ requestMatcher.describeMismatch(item, mismatchDescription);
+ return false;
+ }
+ }
return true;
}