Skip to content

Commit

Permalink
test: FileIO upload-file
Browse files Browse the repository at this point in the history
  • Loading branch information
Woynert committed Sep 26, 2023
1 parent afcf793 commit 4754de7
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
70 changes: 70 additions & 0 deletions app/src/test/java/gateway/ITFileIO.java
Original file line number Diff line number Diff line change
@@ -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");
}
}
13 changes: 13 additions & 0 deletions app/src/test/java/gateway/testutils/TestUtilGenerator.java
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 4754de7

Please sign in to comment.