Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.databind.JsonNode;
import jakarta.ws.rs.core.Response;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.apache.http.client.HttpResponseException;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -49,6 +54,16 @@ private Folder createFolder(RestClient rest, CreateFolder request) throws HttpRe
return rest.create(FOLDER_PATH, request, Folder.class);
}

private List<String> listFileIds(RestClient rest, String path) {
try (Response response = rest.rawGet(path)) {
assertEquals(200, response.getStatus());
JsonNode root = JsonUtils.readTree(response.readEntity(String.class));
List<String> ids = new ArrayList<>();
root.get("data").forEach(node -> ids.add(node.get("id").asText()));
return ids;
}
}

// --- CRUD ---

@Test
Expand Down Expand Up @@ -149,6 +164,65 @@ void testRestoreSoftDeletedFile(TestNamespace ns) throws HttpResponseException {
assertEquals(file.getId(), getFile(rest, file.getId(), "").getId());
}

@Test
void testListFilesOrderByUpdatedAtDesc(TestNamespace ns) throws Exception {
RestClient rest = RestClient.admin();

ContextFile older =
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("ordered-older"))
.withProcessingStatus(ProcessingStatus.Uploaded));
Thread.sleep(5);
ContextFile newer =
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("ordered-newer"))
.withProcessingStatus(ProcessingStatus.Uploaded));

List<String> ids = listFileIds(rest, FILE_PATH + "?limit=1000&orderBy=DESC");

int olderIndex = ids.indexOf(older.getId().toString());
int newerIndex = ids.indexOf(newer.getId().toString());
assertTrue(olderIndex >= 0, "Expected ordered older file in list response");
assertTrue(newerIndex >= 0, "Expected ordered newer file in list response");
assertTrue(newerIndex < olderIndex, "Newer file should be listed before older file");
}

@Test
void testListFilesOrderByRejectsDefaultCursor(TestNamespace ns) throws Exception {
RestClient rest = RestClient.admin();

createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("default-cursor-first"))
.withProcessingStatus(ProcessingStatus.Uploaded));
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("default-cursor-second"))
.withProcessingStatus(ProcessingStatus.Uploaded));

try (Response response = rest.rawGet(FILE_PATH + "?limit=1")) {
assertEquals(200, response.getStatus());
JsonNode root = JsonUtils.readTree(response.readEntity(String.class));
JsonNode after = root.get("paging").get("after");
assertNotNull(after, "Default list response should include an after cursor");

String encodedCursor = URLEncoder.encode(after.asText(), StandardCharsets.UTF_8);
try (Response orderByResponse =
rest.rawGet(FILE_PATH + "?limit=1&orderBy=DESC&after=" + encodedCursor)) {
String body = orderByResponse.readEntity(String.class);
assertEquals(
Response.Status.BAD_REQUEST.getStatusCode(), orderByResponse.getStatus(), body);
assertTrue(body.contains("Invalid cursor for orderBy pagination"));
}
}
}

@Test
void testHardDeleteFileIsAsync(TestNamespace ns) throws HttpResponseException {
RestClient rest = RestClient.admin();
Expand Down Expand Up @@ -471,6 +545,107 @@ void testMoveFilePermissions(TestNamespace ns) throws HttpResponseException {
"Expected 403/401, got " + ex.getStatusCode());
}

@Test
void testBulkMoveAndDeleteFiles(TestNamespace ns) throws HttpResponseException {
RestClient rest = RestClient.admin();
Folder target = createFolder(rest, new CreateFolder().withName(ns.prefix("bulk-target")));
ContextFile first =
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("bulk-first"))
.withDisplayName("Bulk First")
.withProcessingStatus(ProcessingStatus.Uploaded));
ContextFile second =
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("bulk-second"))
.withDisplayName("Bulk Second")
.withProcessingStatus(ProcessingStatus.Uploaded));
List<String> ids = List.of(first.getId().toString(), second.getId().toString());

try (Response response =
rest.rawPut(
FILE_PATH + "/bulk/move", Map.of("ids", ids, "folder", target.getEntityReference()))) {
String body = response.readEntity(String.class);
assertEquals(200, response.getStatus(), body);
JsonNode result = JsonUtils.readTree(body);
assertEquals("success", result.get("status").asText());
assertEquals(2, result.get("numberOfRowsPassed").asInt());
}

assertEquals(target.getId(), getFile(rest, first.getId(), "folder").getFolder().getId());
assertEquals(target.getId(), getFile(rest, second.getId(), "folder").getFolder().getId());

try (Response response =
rest.rawPost(FILE_PATH + "/bulk/delete", Map.of("ids", ids, "hardDelete", false))) {
String body = response.readEntity(String.class);
assertEquals(200, response.getStatus(), body);
JsonNode result = JsonUtils.readTree(body);
assertEquals("success", result.get("status").asText());
assertEquals(2, result.get("numberOfRowsPassed").asInt());
}

HttpResponseException firstEx =
assertThrows(HttpResponseException.class, () -> getFile(rest, first.getId(), ""));
HttpResponseException secondEx =
assertThrows(HttpResponseException.class, () -> getFile(rest, second.getId(), ""));
assertEquals(404, firstEx.getStatusCode());
assertEquals(404, secondEx.getStatusCode());

rest.delete(FOLDER_PATH, target.getId());

HttpResponseException folderEx =
assertThrows(
HttpResponseException.class,
() -> rest.getById(FOLDER_PATH, target.getId(), "", Folder.class));
assertEquals(404, folderEx.getStatusCode());
}

@Test
void testDeleteFolderCascadesMovedFiles(TestNamespace ns) throws HttpResponseException {
RestClient rest = RestClient.admin();
Folder target =
createFolder(rest, new CreateFolder().withName(ns.prefix("delete-cascade-target")));
ContextFile first =
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("cascade-first"))
.withDisplayName("Cascade First")
.withProcessingStatus(ProcessingStatus.Uploaded));
ContextFile second =
createFile(
rest,
new CreateContextFile()
.withName(ns.prefix("cascade-second"))
.withDisplayName("Cascade Second")
.withProcessingStatus(ProcessingStatus.Uploaded));
List<String> ids = List.of(first.getId().toString(), second.getId().toString());

try (Response response =
rest.rawPut(
FILE_PATH + "/bulk/move", Map.of("ids", ids, "folder", target.getEntityReference()))) {
String body = response.readEntity(String.class);
assertEquals(200, response.getStatus(), body);
}

rest.delete(FOLDER_PATH, target.getId());

HttpResponseException folderEx =
assertThrows(
HttpResponseException.class,
() -> rest.getById(FOLDER_PATH, target.getId(), "", Folder.class));
HttpResponseException firstEx =
assertThrows(HttpResponseException.class, () -> getFile(rest, first.getId(), ""));
HttpResponseException secondEx =
assertThrows(HttpResponseException.class, () -> getFile(rest, second.getId(), ""));
assertEquals(404, folderEx.getStatusCode());
assertEquals(404, firstEx.getStatusCode());
assertEquals(404, secondEx.getStatusCode());
}

@Test
void testFileAppearsInSearch(TestNamespace ns) throws Exception {
RestClient rest = RestClient.admin();
Expand Down
Loading
Loading