Use USK key in tests (even though it doesn’t matter)
[jFCPlib.git] / src / test / java / net / pterodactylus / fcp / quelaton / DefaultFcpClientTest.java
index 557350b..4e5eb84 100644 (file)
@@ -20,10 +20,13 @@ import java.util.Collection;
 import java.util.List;
 import java.util.Optional;
 import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
 import java.util.function.Supplier;
 import java.util.stream.Collectors;
 
@@ -1993,20 +1996,20 @@ public class DefaultFcpClientTest {
                assertThat(newConfigData.get().getCurrent("foo.bar"), is("baz"));
        }
 
-       public class PluginCommands {
+       private List<String> lines;
+       private String identifier;
 
-               private static final String CLASS_NAME = "foo.plugin.Plugin";
+       private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
+       throws InterruptedException, ExecutionException, IOException {
+               connectNode();
+               lines = fcpServer.collectUntil(is("EndMessage"));
+               identifier = extractIdentifier(lines);
+               assertThat(lines, requestMatcher.get());
+       }
 
-               private List<String> lines;
-               private String identifier;
+       public class PluginCommands {
 
-               private void connectAndAssert(Supplier<Matcher<List<String>>> requestMatcher)
-               throws InterruptedException, ExecutionException, IOException {
-                       connectNode();
-                       lines = fcpServer.collectUntil(is("EndMessage"));
-                       identifier = extractIdentifier(lines);
-                       assertThat(lines, requestMatcher.get());
-               }
+               private static final String CLASS_NAME = "foo.plugin.Plugin";
 
                private void replyWithPluginInfo() throws IOException {
                        fcpServer.writeLine(
@@ -2285,4 +2288,46 @@ public class DefaultFcpClientTest {
 
        }
 
+       public class UskSubscriptionCommands {
+
+               private static final String URI = "USK@some,uri/file.txt";
+
+               @Test
+               public void subscriptionWorks() throws InterruptedException, ExecutionException, IOException {
+                       Future<Optional<UskSubscription>> uskSubscription = fcpClient.subscribeUsk().uri(URI).execute();
+                       connectAndAssert(() -> matchesFcpMessage("SubscribeUSK", "URI=" + URI, "EndMessage"));
+                       fcpServer.writeLine(
+                               "SubscribedUSK",
+                               "Identifier=" + identifier,
+                               "URI=" + URI,
+                               "DontPoll=false",
+                               "EndMessage"
+                       );
+                       assertThat(uskSubscription.get().get().getUri(), is(URI));
+                       AtomicInteger edition = new AtomicInteger();
+                       CountDownLatch updated = new CountDownLatch(2);
+                       uskSubscription.get().get().onUpdate(e -> {
+                               edition.set(e);
+                               updated.countDown();
+                       });
+                       fcpServer.writeLine(
+                               "SubscribedUSKUpdate",
+                               "Identifier=" + identifier,
+                               "URI=" + URI,
+                               "Edition=23",
+                               "EndMessage"
+                       );
+                       fcpServer.writeLine(
+                               "SubscribedUSKUpdate",
+                               "Identifier=" + identifier,
+                               "URI=" + URI,
+                               "Edition=24",
+                               "EndMessage"
+                       );
+                       assertThat("updated in time", updated.await(5, TimeUnit.SECONDS), is(true));
+                       assertThat(edition.get(), is(24));
+               }
+
+       }
+
 }