+ public class ClientGet {
+
+ @Test
+ public void works() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
+ replyWithAllData("not-test", "Hello World", "text/plain;charset=latin-9");
+ replyWithAllData(identifier, "Hello", "text/plain;charset=utf-8");
+ Optional<Data> data = dataFuture.get();
+ verifyData(data);
+ }
+
+ @Test
+ public void getFailedIsRecognized() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
+ replyWithGetFailed(identifier);
+ Optional<Data> data = dataFuture.get();
+ assertThat(data.isPresent(), is(false));
+ }
+
+ @Test
+ public void getFailedForDifferentIdentifierIsIgnored()
+ throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
+ replyWithGetFailed("not-test");
+ replyWithAllData(identifier, "Hello", "text/plain;charset=utf-8");
+ Optional<Data> data = dataFuture.get();
+ verifyData(data);
+ }
+
+ @Test(expected = ExecutionException.class)
+ public void connectionClosedIsRecognized() throws InterruptedException, ExecutionException, IOException {
+ Future<Optional<Data>> dataFuture = fcpClient.clientGet().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt"));
+ fcpServer.close();
+ dataFuture.get();
+ }
+
+ @Test
+ public void withIgnoreData() throws InterruptedException, ExecutionException, IOException {
+ fcpClient.clientGet().ignoreDataStore().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "IgnoreDS=true"));
+ }
+
+ @Test
+ public void withDataStoreOnly() throws InterruptedException, ExecutionException, IOException {
+ fcpClient.clientGet().dataStoreOnly().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "DSonly=true"));
+ }
+
+ @Test
+ public void clientGetWithMaxSizeSettingSendsCorrectCommands()
+ throws InterruptedException, ExecutionException, IOException {
+ fcpClient.clientGet().maxSize(1048576).uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "MaxSize=1048576"));
+ }
+
+ @Test
+ public void clientGetWithPrioritySettingSendsCorrectCommands()
+ throws InterruptedException, ExecutionException, IOException {
+ fcpClient.clientGet().priority(Priority.interactive).uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "PriorityClass=1"));
+ }
+
+ @Test
+ public void clientGetWithRealTimeSettingSendsCorrectCommands()
+ throws InterruptedException, ExecutionException, IOException {
+ fcpClient.clientGet().realTime().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "RealTimeFlag=true"));
+ }
+
+ @Test
+ public void clientGetWithGlobalSettingSendsCorrectCommands()
+ throws InterruptedException, ExecutionException, IOException {
+ fcpClient.clientGet().global().uri("KSK@foo.txt").execute();
+ connectAndAssert(() -> matchesFcpMessage("ClientGet", "URI=KSK@foo.txt", "Global=true"));
+ }
+
+ private void replyWithGetFailed(String identifier) throws IOException {
+ fcpServer.writeLine(
+ "GetFailed",
+ "Identifier=" + identifier,
+ "Code=3",
+ "EndMessage"
+ );
+ }
+
+ private void replyWithAllData(String identifier, String text, String contentType) throws IOException {
+ fcpServer.writeLine(
+ "AllData",
+ "Identifier=" + identifier,
+ "DataLength=" + (text.length() + 1),
+ "StartupTime=1435610539000",
+ "CompletionTime=1435610540000",
+ "Metadata.ContentType=" + contentType,
+ "Data",
+ text
+ );
+ }
+
+ private void verifyData(Optional<Data> data) throws IOException {
+ assertThat(data.get().getMimeType(), is("text/plain;charset=utf-8"));
+ assertThat(data.get().size(), is(6L));
+ assertThat(ByteStreams.toByteArray(data.get().getInputStream()),
+ is("Hello\n".getBytes(StandardCharsets.UTF_8)));
+ }
+
+ }
+