Skip to content

Commit

Permalink
fix: file API tests (#49) (#48)
Browse files Browse the repository at this point in the history
Co-authored-by: Maksim_Hadalau <[email protected]>
  • Loading branch information
Maxim-Gadalov and Maksim_Hadalau authored Nov 22, 2023
1 parent c513091 commit f30c89b
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public InputStreamReader(Vertx vertx, InputStream in, int bufferSize) {
}
});
queue.drainHandler(v -> readDataFromStream());
queue.pause();
readDataFromStream();
}

Expand Down
275 changes: 155 additions & 120 deletions src/test/java/com/epam/aidial/core/FileApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import com.epam.aidial.core.config.Storage;
import com.epam.aidial.core.data.FileMetadata;
import com.epam.aidial.core.storage.BlobStorage;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.Vertx;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.json.JsonArray;
Expand All @@ -28,6 +30,9 @@
import java.nio.file.Path;
import java.util.Properties;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

@ExtendWith(VertxExtension.class)
@Slf4j
public class FileApiTest {
Expand Down Expand Up @@ -76,8 +81,8 @@ public void testEmptyFilesList(Vertx vertx, VertxTestContext context) {
.as(BodyCodec.jsonArray())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(JsonArray.of(), response.body());
assertEquals(200, response.statusCode());
assertEquals(JsonArray.of(), response.body());
context.completeNow();
});
}));
Expand All @@ -92,8 +97,8 @@ public void testFileNotFound(Vertx vertx, VertxTestContext context) {
.as(BodyCodec.buffer())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(404, response.statusCode());
Assertions.assertNull(response.body());
assertEquals(404, response.statusCode());
assertNull(response.body());
context.completeNow();
});
}));
Expand All @@ -104,50 +109,60 @@ public void testFileUpload(Vertx vertx, VertxTestContext context) {
Checkpoint checkpoint = context.checkpoint(3);
WebClient client = WebClient.create(vertx);

// verify no files
client.get(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.addQueryParam("purpose", "metadata")
.as(BodyCodec.jsonArray())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(JsonArray.of(), response.body());
checkpoint.flag();
});
}));

FileMetadata expectedFileMetadata = new FileMetadata("file.txt", "Users/User1/files", 17, "text/plain");

// upload test file
client.post(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("file.txt", TEST_FILE_CONTENT),
context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(expectedFileMetadata, response.body());
checkpoint.flag();
});
})
);
Future.succeededFuture().compose((mapper) -> {
Promise<Void> promise = Promise.promise();
// verify no files
client.get(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.addQueryParam("purpose", "metadata")
.as(BodyCodec.jsonArray())
.send(context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
assertEquals(JsonArray.of(), response.body());
checkpoint.flag();
promise.complete();
});
}));

// verify uploaded file can be listed
client.get(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.addQueryParam("purpose", "metadata")
.as(BodyCodec.jsonArray())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertIterableEquals(JsonArray.of(JsonObject.mapFrom(expectedFileMetadata)), response.body());
checkpoint.flag();
});
}));
return promise.future();
}).compose((mapper) -> {
Promise<Void> promise = Promise.promise();
// upload test file
client.post(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("file.txt", TEST_FILE_CONTENT),
context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
assertEquals(expectedFileMetadata, response.body());
checkpoint.flag();
promise.complete();
});
})
);

return promise.future();
}).andThen((result) -> {
// verify uploaded file can be listed
client.get(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.addQueryParam("purpose", "metadata")
.as(BodyCodec.jsonArray())
.send(context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
Assertions.assertIterableEquals(JsonArray.of(JsonObject.mapFrom(expectedFileMetadata)), response.body());
checkpoint.flag();
});
}));
});
}

@Test
Expand All @@ -157,47 +172,57 @@ public void testFileDownload(Vertx vertx, VertxTestContext context) {

FileMetadata expectedFileMetadata = new FileMetadata("file.txt", "Users/User1/files/folder1", 17, "text/plain");

// upload test file
client.post(serverPort, "localhost", "/v1/files/folder1")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("file.txt", TEST_FILE_CONTENT),
context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(expectedFileMetadata, response.body());
checkpoint.flag();
});
})
);
Future.succeededFuture().compose((mapper) -> {
Promise<Void> promise = Promise.promise();
// upload test file
client.post(serverPort, "localhost", "/v1/files/folder1")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("file.txt", TEST_FILE_CONTENT),
context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
assertEquals(expectedFileMetadata, response.body());
checkpoint.flag();
promise.complete();
});
})
);

// download by relative path
client.get(serverPort, "localhost", "/v1/files/folder1/file.txt")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(TEST_FILE_CONTENT, response.body());
checkpoint.flag();
});
}));
return promise.future();
}).compose((mapper) -> {
Promise<Void> promise = Promise.promise();
// download by relative path
client.get(serverPort, "localhost", "/v1/files/folder1/file.txt")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
assertEquals(TEST_FILE_CONTENT, response.body());
checkpoint.flag();
promise.complete();
});
}));

// download by absolute path
client.get(serverPort, "localhost", "/v1/files/Users/User1/files/folder1/file.txt")
.addQueryParam("path", "absolute")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User2"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(TEST_FILE_CONTENT, response.body());
checkpoint.flag();
});
}));
return promise.future();
}).andThen((result) -> {
// download by absolute path
client.get(serverPort, "localhost", "/v1/files/Users/User1/files/folder1/file.txt")
.addQueryParam("path", "absolute")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User2"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
assertEquals(TEST_FILE_CONTENT, response.body());
checkpoint.flag();
});
}));
});
}

@Test
Expand All @@ -207,44 +232,54 @@ public void testFileDelete(Vertx vertx, VertxTestContext context) {

FileMetadata expectedFileMetadata = new FileMetadata("test_file.txt", "Users/User1/files", 17, "text/plain");

// upload test file
client.post(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("test_file.txt", TEST_FILE_CONTENT),
context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
Assertions.assertEquals(expectedFileMetadata, response.body());
checkpoint.flag();
});
})
);
Future.succeededFuture().compose((mapper) -> {
Promise<Void> promise = Promise.promise();
// upload test file
client.post(serverPort, "localhost", "/v1/files")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.json(FileMetadata.class))
.sendMultipartForm(generateMultipartForm("test_file.txt", TEST_FILE_CONTENT),
context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
assertEquals(expectedFileMetadata, response.body());
checkpoint.flag();
promise.complete();
});
})
);

// delete file
client.delete(serverPort, "localhost", "/v1/files/test_file.txt")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(200, response.statusCode());
checkpoint.flag();
});
}));
return promise.future();
}).compose((mapper) -> {
Promise<Void> promise = Promise.promise();
// delete file
client.delete(serverPort, "localhost", "/v1/files/test_file.txt")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
assertEquals(200, response.statusCode());
checkpoint.flag();
promise.complete();
});
}));

// try to download deleted file
client.get(serverPort, "localhost", "/v1/files/test_file.txt")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
Assertions.assertEquals(404, response.statusCode());
checkpoint.flag();
});
}));
return promise.future();
}).andThen((mapper) -> {
// try to download deleted file
client.get(serverPort, "localhost", "/v1/files/test_file.txt")
.putHeader("Api-key", "proxyKey2")
.bearerTokenAuthentication(generateJwtToken("User1"))
.as(BodyCodec.string())
.send(context.succeeding(response -> {
context.verify(() -> {
assertEquals(404, response.statusCode());
checkpoint.flag();
});
}));
});
}

private static BlobStorage buildFsBlobStorage(Path baseDir) {
Expand Down

0 comments on commit f30c89b

Please sign in to comment.