package net.pterodactylus.fcp.highlevel;
-import com.google.common.base.Predicate;
import net.pterodactylus.fcp.AddPeer;
import net.pterodactylus.fcp.AddPeer.Trust;
import net.pterodactylus.fcp.AddPeer.Visibility;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
-import static com.google.common.collect.FluentIterable.from;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
* if an FCP error occurs
*/
public Collection<Request> getPutRequests(final boolean global) throws IOException, FcpException {
- return from(getRequests(global)).filter(new Predicate<Request>() {
- @Override
- public boolean apply(Request request) {
- return request instanceof PutRequest;
- }
- }).toList();
+ return getRequests(global).stream().filter(request -> request instanceof PutRequest).collect(toList());
}
/**
import static net.pterodactylus.fcp.test.PeerMatchers.peerWithIdentity;
import static net.pterodactylus.fcp.test.Peers.createPeer;
import static net.pterodactylus.fcp.test.Requests.isGetRequest;
+import static net.pterodactylus.fcp.test.Requests.isPutRequest;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
@Test
public void getGetRequestsReturnsGetRequests() throws IOException, FcpException {
- 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")));
- });
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), this::sendRequests);
+ try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
+ Collection<Request> requests = fcpClient.getGetRequests(false);
+ assertThat(requests, contains(isGetRequest(equalTo("get1"))));
+ }
+ }
+
+ @Test
+ public void getPutRequestsReturnsPutRequests() throws IOException, FcpException {
+ FcpConnection fcpConnection = createFcpConnectionReactingToSingleMessage(named("ListPersistentRequests"), this::sendRequests);
try (FcpClient fcpClient = new FcpClient(fcpConnection)) {
- Collection<Request> getRequests = fcpClient.getGetRequests(false);
- assertThat(getRequests, contains(isGetRequest(equalTo("get1"))));
+ Collection<Request> requests = fcpClient.getPutRequests(false);
+ assertThat(requests, contains(isPutRequest(equalTo("put1")), isPutRequest(equalTo("put2"))));
}
}
+ private void sendRequests(FcpListener listener, FcpConnection 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")));
+ }
+
private static void doNothing(FcpListener listener, FcpConnection connection) {
// do nothing.
}
package net.pterodactylus.fcp.test;
import net.pterodactylus.fcp.highlevel.GetRequest;
+import net.pterodactylus.fcp.highlevel.PutRequest;
import net.pterodactylus.fcp.highlevel.Request;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
-import org.hamcrest.Matchers;
import org.hamcrest.TypeSafeDiagnosingMatcher;
+import static org.hamcrest.Matchers.anything;
+import static org.hamcrest.Matchers.instanceOf;
+
public class Requests {
public static Matcher<Request> isGetRequest() {
- return isGetRequest(Matchers.anything());
+ return isGetRequest(anything());
}
public static Matcher<Request> isGetRequest(Matcher<? super String> identifier) {
+ return isRequest(instanceOf(GetRequest.class), identifier);
+ }
+
+ public static Matcher<Request> isPutRequest() {
+ return isPutRequest(anything());
+ }
+
+ public static Matcher<Request> isPutRequest(Matcher<? super String> identifier) {
+ return isRequest(instanceOf(PutRequest.class), identifier);
+ }
+
+ private static TypeSafeDiagnosingMatcher<Request> isRequest(Matcher<? super Class<?>> requestClass, Matcher<? super String> identifier) {
return new TypeSafeDiagnosingMatcher<Request>() {
@Override
protected boolean matchesSafely(Request item, Description mismatchDescription) {
- if (!(item instanceof GetRequest)) {
+ if (!requestClass.matches(item)) {
mismatchDescription.appendText("is a " + item.getClass().getSimpleName());
return false;
}
@Override
public void describeTo(Description description) {
- description.appendText("get request");
+ description.appendDescriptionOf(requestClass);
}
};
}