From 4754de79b1c814e04e230c38a04f6c9fbe16eb30 Mon Sep 17 00:00:00 2001 From: woynert Date: Tue, 26 Sep 2023 08:27:19 -0500 Subject: [PATCH] test: FileIO upload-file --- app/src/test/java/gateway/ITFileIO.java | 70 +++++++++++++++++++ .../gateway/testutils/TestUtilGenerator.java | 13 ++++ 2 files changed, 83 insertions(+) create mode 100644 app/src/test/java/gateway/ITFileIO.java create mode 100644 app/src/test/java/gateway/testutils/TestUtilGenerator.java diff --git a/app/src/test/java/gateway/ITFileIO.java b/app/src/test/java/gateway/ITFileIO.java new file mode 100644 index 0000000..145e059 --- /dev/null +++ b/app/src/test/java/gateway/ITFileIO.java @@ -0,0 +1,70 @@ +package gateway; + +import static org.junit.jupiter.api.Assertions.*; + +import gateway.config.Config; +import gateway.controller.CtrlAccountRegister; +import gateway.controller.CtrlFileUpload; +import gateway.soap.request.Credentials; +import gateway.soap.request.ReqFileUpload; +import gateway.soap.response.ResSession; +import gateway.testutils.TestUtilConfig; +import gateway.testutils.TestUtilGenerator; +import java.util.UUID; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class ITFileIO +{ + @BeforeEach void setup () { Config.initializeFromEnv (); } + + @Test void uploadFile () + { + + // register + + ResSession res = CtrlAccountRegister.account_register ( + new Credentials (UUID.randomUUID ().toString (), "pass")); + String token = res.auth.token; + assertEquals (201, res.code, "Login successfully"); + + // 400 field validation + + ReqFileUpload args = new ReqFileUpload (); + assertEquals (400, CtrlFileUpload.file_upload (args).code, "Field validation failed"); + + // 401 auth failed + + args.fileName = UUID.randomUUID ().toString (); + args.fileContent = TestUtilGenerator.randomBytes (1); + args.location = null; + args.token = "invalid token"; + assertEquals (401, CtrlFileUpload.file_upload (args).code, "Authorization failed"); + + // 400 empty file + + args.token = token; + args.fileContent = TestUtilGenerator.randomBytes (0); + assertEquals (400, CtrlFileUpload.file_upload (args).code, "Empty file is rejected"); + + // 413 file too big + + args.fileContent = TestUtilGenerator.randomBytes (100000001); + assertEquals (413, CtrlFileUpload.file_upload (args).code, "Big file is rejected"); + + // 201 file uploaded + + args.fileContent = TestUtilGenerator.randomBytes (1); + assertEquals (201, CtrlFileUpload.file_upload (args).code, "File upload success"); + + args.fileName = UUID.randomUUID ().toString (); + TestUtilConfig.makeInvalidWorker (); + assertEquals (500, CtrlFileUpload.file_upload (args).code, "Can't reach Worker"); + + TestUtilConfig.makeInvalidMetadata (); + assertEquals (500, CtrlFileUpload.file_upload (args).code, "Can't reach Metadata server"); + + TestUtilConfig.makeInvalidAll (); + assertEquals (500, CtrlFileUpload.file_upload (args).code, "Can't reach Auth"); + } +} diff --git a/app/src/test/java/gateway/testutils/TestUtilGenerator.java b/app/src/test/java/gateway/testutils/TestUtilGenerator.java new file mode 100644 index 0000000..32d6286 --- /dev/null +++ b/app/src/test/java/gateway/testutils/TestUtilGenerator.java @@ -0,0 +1,13 @@ +package gateway.testutils; + +import java.util.Random; + +public class TestUtilGenerator +{ + public static byte[] randomBytes (int size) + { + byte[] buff = new byte[size]; + (new Random ()).nextBytes (buff); + return buff; + } +}