Skip to content
Snippets Groups Projects
Commit a5988102 authored by vkovtun's avatar vkovtun
Browse files

SSDM-13579: Made the AfsClient.resumeRead() method work correctly.

parent 75fd8ee0
No related branches found
No related tags found
1 merge request!40SSDM-13578 : 2PT : Database and V3 Implementation - include the new AFS "free"...
......@@ -17,6 +17,7 @@ import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
......@@ -240,34 +241,36 @@ public final class AfsClient implements PublicAPI, ClientAPI
public void resumeRead(@NonNull String owner, @NonNull String source, @NonNull Path destination,
@NonNull Long offset) throws Exception
{
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(destination, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
List<File> infos = list(owner, source, false);
if (infos.isEmpty())
{
throw ClientExceptions.API_ERROR.getInstance("File not found '" + source + "'");
}
File file = null;
for (File info : infos)
try (final FileChannel fileChannel = FileChannel.open(destination, StandardOpenOption.CREATE, StandardOpenOption.WRITE))
{
if (info.getName().equals(getName(source)))
List<File> infos = list(owner, source, false);
if (infos.isEmpty())
{
file = info;
break;
throw ClientExceptions.API_ERROR.getInstance("File not found '" + source + "'");
}
File file = null;
for (File info : infos)
{
if (info.getName().equals(getName(source)))
{
file = info;
break;
}
}
if (file == null)
{
throw ClientExceptions.API_ERROR.getInstance("File not found '" + source + "'");
}
}
if (file == null)
{
throw ClientExceptions.API_ERROR.getInstance("File not found '" + source + "'");
}
while (offset < file.getSize())
{
byte[] read = read(owner, source, offset, DEFAULT_PACKAGE_SIZE_IN_BYTES);
fileChannel.write(ByteBuffer.wrap(read), offset);
offset += read.length;
while (offset < file.getSize())
{
byte[] read = read(owner, source, offset, DEFAULT_PACKAGE_SIZE_IN_BYTES);
System.out.printf("Writing %s", Arrays.toString(read));
fileChannel.write(ByteBuffer.wrap(read), offset);
offset += read.length;
}
}
fileChannel.close();
}
@Override
......
......@@ -137,8 +137,10 @@ public class AfsClientTest
{
login();
final String fileName = "afs-test.txt";
final Path filePath = Path.of(fileName);
final String sourceFileName = "afs-test-src.txt";
final Path sourceFilePath = Path.of(sourceFileName);
final String destinationFileName = "afs-test-dst.txt";
final Path destinationFilePath = Path.of(destinationFileName);
final String fileNameJson = String.format("{\n"
+ " \"id\" : \"1\",\n"
+ " \"result\" : [ \"java.util.ArrayList\", [ [ \"ch.ethz.sis.afsapi.dto.File\", {\n"
......@@ -151,19 +153,37 @@ public class AfsClientTest
+ " \"lastAccessTime\" : \"2023-06-27T17:18:08.900154283+02:00\"\n"
+ " } ] ] ],\n"
+ " \"error\" : null\n"
+ "}", fileName, fileName);
+ "}", sourceFileName, sourceFileName);
byte[] fileData = "ABCD".getBytes();
Files.write(filePath, fileData);
Files.write(sourceFilePath, fileData);
httpServer.setNextResponses(new byte[][] {fileNameJson.getBytes(), fileData}, new String[] {"application/json", "application/octet-stream"});
afsClient.resumeRead("", fileName, filePath, 1000L);
afsClient.resumeRead("", sourceFileName, destinationFilePath, 0L);
assertEquals("GET", httpServer.getHttpExchange().getRequestMethod());
assertArrayEquals(fileData, Files.readAllBytes(filePath));
assertArrayEquals(httpServer.getLastRequestBody(), new byte[0]);
assertArrayEquals(fileData, Files.readAllBytes(destinationFilePath));
assertEquals(0, httpServer.getLastRequestBody().length);
sourceFilePath.toFile().delete();
destinationFilePath.toFile().delete();
}
filePath.toFile().delete();
@Test
public void resumeWrite_methodIsPost() throws Exception
{
// login();
//
// final String fileName = "afs-test.txt";
//
// httpServer.setNextResponse("{\"result\": true}");
// final Boolean result = afsClient.resumeWrite("", fileName, , 0L);
//
// assertEquals("POST", httpServer.getHttpExchange().getRequestMethod());
// assertTrue(result);
// assertTrue(httpServer.getLastRequestBody().length > 0);
//
// filePath.toFile().delete();
}
@Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment